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 DMUL instructions.
029 * <p>This transformer is used when both arguments of DMUL are
030 * expanded differential pairs. It implements the classical
031 * differentiation rules for multiplication.</p>
032 * @see DMulTransformer1
033 * @see DMulTransformer2
034 */
035 public class DMulTransformer12 implements InstructionsTransformer {
036
037 // CHECKSTYLE: stop HideUtilityClassConstructor
038 /** Holder for the singleton instance.*/
039 private static class LazyHolder {
040 /** The singleton instance. */
041 private static final InstructionsTransformer INSTANCE = new DMulTransformer12();
042 }
043 // CHECKSTYLE: resume HideUtilityClassConstructor
044
045 /** Hidden constructor.
046 */
047 private DMulTransformer12() {
048 }
049
050 /** Get the singleton instance.
051 * <p>We use here the Initialization on Demand Holder idiom.</p>
052 * @return the singleton instance
053 */
054 public static InstructionsTransformer getInstance() {
055 return LazyHolder.INSTANCE;
056 }
057
058 /** {@inheritDoc} */
059 public InsnList getReplacement(final AbstractInsnNode insn,
060 final MethodDifferentiator methodDifferentiator)
061 throws DifferentiationException {
062
063 final int tmp1 = methodDifferentiator.getTmp(1);
064 final int tmp2 = methodDifferentiator.getTmp(2);
065 final int tmp3 = methodDifferentiator.getTmp(3);
066 final InsnList list = new InsnList();
067
068 // operand stack initial state: a0, a1, b0, b1
069 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0, a1, b0
070 list.add(new InsnNode(Opcodes.DUP2)); // => a0, a1, b0, b0
071 list.add(new VarInsnNode(Opcodes.DSTORE, tmp2)); // => a0, a1, b0
072 list.add(new InsnNode(Opcodes.DMUL)); // => a0, a1*b0
073 list.add(new VarInsnNode(Opcodes.DSTORE, tmp3)); // => a0
074 list.add(new InsnNode(Opcodes.DUP2)); // => a0, a0
075 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0, a0, b1
076 list.add(new InsnNode(Opcodes.DMUL)); // => a0, a0*b1
077 list.add(new VarInsnNode(Opcodes.DLOAD, tmp3)); // => a0, a0*b1, a1*b0
078 list.add(new InsnNode(Opcodes.DADD)); // => a0, a0*b1+a1*b0
079 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0
080 list.add(new VarInsnNode(Opcodes.DLOAD, tmp2)); // => a0, b0
081 list.add(new InsnNode(Opcodes.DMUL)); // => a0*b0
082 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0*b0, a0*b1+a1*b0
083
084 return list;
085
086 }
087
088 }