View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.nabla.automatic.functions;
18  
19  import org.apache.commons.nabla.automatic.analysis.MethodDifferentiator;
20  import org.objectweb.asm.Opcodes;
21  import org.objectweb.asm.tree.InsnList;
22  import org.objectweb.asm.tree.InsnNode;
23  import org.objectweb.asm.tree.MethodInsnNode;
24  
25  /** Differentiation transformer for the acosh function invocation instructions.
26   * <p>As of java 6, the JVM does not supply an inverse hyperbolic
27   * cosine function (see <a
28   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4919337">bug
29   * 4919337</a>), so this generator will not be triggered for java 6 and below.</p>
30   */
31  public class AcoshTransformer implements MathInvocationTransformer {
32  
33      /** {@inheritDoc} */
34      public InsnList getReplacementList(final String owner, final MethodDifferentiator methodDifferentiator) {
35  
36          // generate differential code
37          // ... u0, u1  --> ...  acosh(u0), u1 / sqrt(u0 * u0 - 1)
38          final InsnList list = new InsnList();
39          list.add(new InsnNode(Opcodes.DUP2_X2));
40          list.add(new InsnNode(Opcodes.POP2));
41          list.add(new InsnNode(Opcodes.DUP2_X2));
42          list.add(new InsnNode(Opcodes.DUP2));
43          list.add(new InsnNode(Opcodes.DMUL));
44          list.add(new InsnNode(Opcodes.DCONST_1));
45          list.add(new InsnNode(Opcodes.DSUB));
46          list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner, "sqrt", D_RETURN_D_DESCRIPTOR));
47          list.add(new InsnNode(Opcodes.DDIV));
48          list.add(new InsnNode(Opcodes.DUP2_X2));
49          list.add(new InsnNode(Opcodes.POP2));
50          list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner, "acosh", D_RETURN_D_DESCRIPTOR));
51          list.add(new InsnNode(Opcodes.DUP2_X2));
52          list.add(new InsnNode(Opcodes.POP2));
53          return list;
54  
55      }
56  }