1 /* 2 * Copyright (c) 2011, 2015, 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 package org.graalvm.compiler.replacements.amd64; 24 25 import org.graalvm.compiler.api.replacements.ClassSubstitution; 26 import org.graalvm.compiler.api.replacements.MethodSubstitution; 27 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 28 import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 29 import org.graalvm.compiler.graph.Node.NodeIntrinsic; 30 import org.graalvm.compiler.nodes.extended.ForeignCallNode; 31 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode; 32 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation; 33 34 // JaCoCo Exclude 35 36 /** 37 * Substitutions for some {@link java.lang.Math} methods that leverage AMD64 instructions for 38 * selected input values. 39 */ 40 @ClassSubstitution(Math.class) 41 public class AMD64MathSubstitutions { 42 43 private static final double PI_4 = Math.PI / 4; 44 45 // NOTE on snippets below: 46 // Math.sin(), .cos() and .tan() guarantee a value within 1 ULP of the 47 // exact result, but x87 trigonometric FPU instructions are only that 48 // accurate within [-pi/4, pi/4]. Examine the passed value and provide 49 // a slow path for inputs outside of that interval. 50 51 @MethodSubstitution 52 public static double sin(double x) { 53 if (Math.abs(x) < PI_4) { 54 return UnaryMathIntrinsicNode.compute(x, UnaryOperation.SIN); 55 } else { 56 return callDouble1(UnaryOperation.SIN.foreignCallDescriptor, x); 57 } 58 } 59 60 @MethodSubstitution 61 public static double cos(double x) { 62 if (Math.abs(x) < PI_4) { 63 return UnaryMathIntrinsicNode.compute(x, UnaryOperation.COS); 64 } else { 65 return callDouble1(UnaryOperation.COS.foreignCallDescriptor, x); 66 } 67 } 68 69 @MethodSubstitution 70 public static double tan(double x) { 71 if (Math.abs(x) < PI_4) { 72 return UnaryMathIntrinsicNode.compute(x, UnaryOperation.TAN); 73 } else { 74 return callDouble1(UnaryOperation.TAN.foreignCallDescriptor, x); 75 } 76 } 77 78 @NodeIntrinsic(value = ForeignCallNode.class) 79 private static native double callDouble1(@ConstantNodeParameter ForeignCallDescriptor descriptor, double value); 80 81 @NodeIntrinsic(value = ForeignCallNode.class) 82 private static native double callDouble2(@ConstantNodeParameter ForeignCallDescriptor descriptor, double a, double b); 83 }