src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements.amd64/src/org/graalvm/compiler/replacements/amd64/AMD64MathSubstitutions.java
branchJDK-8200758-branch
changeset 57262 e2b18d61132f
parent 57261 13b6672477df
parent 54095 8b4a1177202d
child 57263 86e75b8f99c4
equal deleted inserted replaced
57261:13b6672477df 57262:e2b18d61132f
     1 /*
       
     2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 
       
    25 package org.graalvm.compiler.replacements.amd64;
       
    26 
       
    27 import org.graalvm.compiler.api.replacements.ClassSubstitution;
       
    28 import org.graalvm.compiler.api.replacements.MethodSubstitution;
       
    29 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
       
    30 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
       
    31 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
       
    32 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
       
    33 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
       
    34 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
       
    35 
       
    36 // JaCoCo Exclude
       
    37 
       
    38 /**
       
    39  * Substitutions for some {@link java.lang.Math} methods that leverage AMD64 instructions for
       
    40  * selected input values.
       
    41  */
       
    42 @ClassSubstitution(Math.class)
       
    43 public class AMD64MathSubstitutions {
       
    44 
       
    45     private static final double PI_4 = Math.PI / 4;
       
    46 
       
    47     // NOTE on snippets below:
       
    48     // Math.sin(), .cos() and .tan() guarantee a value within 1 ULP of the
       
    49     // exact result, but x87 trigonometric FPU instructions are only that
       
    50     // accurate within [-pi/4, pi/4]. Examine the passed value and provide
       
    51     // a slow path for inputs outside of that interval.
       
    52 
       
    53     @MethodSubstitution
       
    54     public static double sin(double x) {
       
    55         if (Math.abs(x) < PI_4) {
       
    56             return UnaryMathIntrinsicNode.compute(x, UnaryOperation.SIN);
       
    57         } else {
       
    58             return callDouble1(UnaryOperation.SIN.foreignCallDescriptor, x);
       
    59         }
       
    60     }
       
    61 
       
    62     @MethodSubstitution
       
    63     public static double cos(double x) {
       
    64         if (Math.abs(x) < PI_4) {
       
    65             return UnaryMathIntrinsicNode.compute(x, UnaryOperation.COS);
       
    66         } else {
       
    67             return callDouble1(UnaryOperation.COS.foreignCallDescriptor, x);
       
    68         }
       
    69     }
       
    70 
       
    71     @MethodSubstitution
       
    72     public static double tan(double x) {
       
    73         if (Math.abs(x) < PI_4) {
       
    74             return UnaryMathIntrinsicNode.compute(x, UnaryOperation.TAN);
       
    75         } else {
       
    76             return callDouble1(UnaryOperation.TAN.foreignCallDescriptor, x);
       
    77         }
       
    78     }
       
    79 
       
    80     @NodeIntrinsic(value = ForeignCallNode.class)
       
    81     private static native double callDouble1(@ConstantNodeParameter ForeignCallDescriptor descriptor, double value);
       
    82 
       
    83     @NodeIntrinsic(value = ForeignCallNode.class)
       
    84     private static native double callDouble2(@ConstantNodeParameter ForeignCallDescriptor descriptor, double a, double b);
       
    85 }