001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla.algorithmic.forward.arithmetic;
018    
019    import org.apache.commons.nabla.algorithmic.forward.analysis.InstructionsTransformer;
020    import org.apache.commons.nabla.algorithmic.forward.analysis.MethodDifferentiator;
021    import org.apache.commons.nabla.core.DifferentiationException;
022    import org.objectweb.asm.Opcodes;
023    import org.objectweb.asm.tree.AbstractInsnNode;
024    import org.objectweb.asm.tree.InsnList;
025    import org.objectweb.asm.tree.InsnNode;
026    import org.objectweb.asm.tree.VarInsnNode;
027    
028    /** Differentiation transformer for DREM instructions.
029     * <p>This transformer is used when only the first argument
030     * of DREM is an expanded differential pair. It is based on
031     * the identity: <code>a%b = a - q * b</code> where q is an
032     * integer.</p>
033     * @see DRemTransformer12
034     * @see DRemTransformer2
035     */
036    public class DRemTransformer1 implements InstructionsTransformer {
037    
038        // CHECKSTYLE: stop HideUtilityClassConstructor
039        /** Holder for the singleton instance.*/
040        private static class LazyHolder  {
041            /** The singleton instance. */
042            private static final InstructionsTransformer INSTANCE = new DRemTransformer1();
043        }
044        // CHECKSTYLE: resume HideUtilityClassConstructor
045    
046        /** Hidden constructor.
047         */
048        private DRemTransformer1() {
049        }
050    
051        /** Get the singleton instance.
052         * <p>We use here the Initialization on Demand Holder idiom.</p>
053         * @return the singleton instance
054         */
055        public static InstructionsTransformer getInstance() {
056            return LazyHolder.INSTANCE;
057        }
058    
059        /** {@inheritDoc} */
060        public InsnList getReplacement(final AbstractInsnNode insn,
061                                       final MethodDifferentiator methodDifferentiator)
062            throws DifferentiationException {
063    
064            final int tmp = methodDifferentiator.getTmp(1);
065            final InsnList list = new InsnList();
066    
067            // operand stack initial state: a0, a1, b
068            list.add(new InsnNode(Opcodes.DUP2_X2));        // => a0, b, a1, b
069            list.add(new InsnNode(Opcodes.POP2));           // => a0, b, a1
070            list.add(new VarInsnNode(Opcodes.DSTORE, tmp)); // => a0, b
071            list.add(new InsnNode(Opcodes.DREM));           // => a0%b
072            list.add(new VarInsnNode(Opcodes.DLOAD,  tmp)); // => a0%b, a1
073    
074            return list;
075    
076        }
077    
078    }