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.functions;
018
019 import org.apache.commons.nabla.algorithmic.forward.analysis.MethodDifferentiator;
020 import org.apache.commons.nabla.core.DifferentiationException;
021 import org.objectweb.asm.Opcodes;
022 import org.objectweb.asm.tree.InsnList;
023 import org.objectweb.asm.tree.InsnNode;
024 import org.objectweb.asm.tree.MethodInsnNode;
025 import org.objectweb.asm.tree.VarInsnNode;
026
027 /** Differentiation transformer for the atan2 function invocation instructions.
028 */
029 public class Atan2Transformer12 implements MathInvocationTransformer {
030
031 /** {@inheritDoc} */
032 public InsnList getReplacementList(final String owner, final MethodDifferentiator methodDifferentiator)
033 throws DifferentiationException {
034
035 final int tmp1 = methodDifferentiator.getTmp(1);
036 final int tmp2 = methodDifferentiator.getTmp(2);
037 final int tmp3 = methodDifferentiator.getTmp(3);
038 final int tmp4 = methodDifferentiator.getTmp(4);
039
040 // generate differential code
041 // ... y0, y1, x0, x1 --> ... atan2(y0, x0), (x0*y1x0*y1-x1*y00)/(x0^2+y0^2)
042 final InsnList list = new InsnList();
043 list.add(new VarInsnNode(Opcodes.DSTORE, tmp4)); // => y0, y1, x0
044 list.add(new VarInsnNode(Opcodes.DSTORE, tmp3)); // => y0, y1
045 list.add(new VarInsnNode(Opcodes.DSTORE, tmp2)); // => y0
046 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // =>
047 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => y0
048 list.add(new VarInsnNode(Opcodes.DLOAD, tmp3)); // => y0, x0
049 list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner, "atan2", DD_RETURN_D_DESCRIPTOR)); // => atan2(y0,x0)
050 list.add(new VarInsnNode(Opcodes.DLOAD, tmp3)); // => atan2(y0,x0), x0
051 list.add(new VarInsnNode(Opcodes.DLOAD, tmp2)); // => atan2(y0,x0), x0, y1
052 list.add(new InsnNode(Opcodes.DMUL)); // => atan2(y0,x0), x0*y1
053 list.add(new VarInsnNode(Opcodes.DLOAD, tmp4)); // => atan2(y0,x0), x0*y1, x1
054 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => atan2(y0,x0), x0*y1, x1, y0
055 list.add(new InsnNode(Opcodes.DMUL)); // => atan2(y0,x0), x0*y1, x1*y0
056 list.add(new InsnNode(Opcodes.DSUB)); // => atan2(y0,x0), x0*y1-x1*y0
057 list.add(new VarInsnNode(Opcodes.DLOAD, tmp3)); // => atan2(y0,x0), x0*y1-x1*y0, x0
058 list.add(new InsnNode(Opcodes.DUP2)); // => atan2(y0,x0), x0*y1-x1*y0, x0, x0
059 list.add(new InsnNode(Opcodes.DMUL)); // => atan2(y0,x0), x0*y1-x1*y0, x0^2
060 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => atan2(y0,x0), x0*y1-x1*y0, x0^2, y0
061 list.add(new InsnNode(Opcodes.DUP2)); // => atan2(y0,x0), x0*y1-x1*y0, x0^2, y0, y0
062 list.add(new InsnNode(Opcodes.DMUL)); // => atan2(y0,x0), x0*y1-x1*y0, x0^2, y0^2
063 list.add(new InsnNode(Opcodes.DADD)); // => atan2(y0,x0), x0*y1-x1*y0, x0^2+y0^2
064 list.add(new InsnNode(Opcodes.DDIV)); // => atan2(y0,x0), (x0*y1-x1*y0)/(x0^2+y0^2)
065 return list;
066
067 }
068
069 }