annotate graal/com.oracle.graal.replacements.amd64/src/com/oracle/graal/replacements/amd64/AMD64MathSubstitutions.java @ 20865:9c8743f5ff53

InvocationPlugins can only be applied for direct invokes
author Doug Simon <doug.simon@oracle.com>
date Thu, 09 Apr 2015 10:26:28 +0200
parents 3081a57f95fd
children 66d45b977e44
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
18298
ba5c9055c53a Make abs and sqrt subclasses of UnaryArithmeticNode.
Roland Schatz <roland.schatz@oracle.com>
parents: 17117
diff changeset
2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
23 package com.oracle.graal.replacements.amd64;
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
24
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
25 import static com.oracle.graal.compiler.target.Backend.*;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
26
9736
951d5ebf3c49 refactored ForeignCallDescriptor into a top level class and moved it to the api.meta project
Doug Simon <doug.simon@oracle.com>
parents: 9735
diff changeset
27 import com.oracle.graal.api.meta.*;
6521
2a0c9f20baa1 consolidated framework for runtime and stub calls and moved declaration of descriptors for such calls to the source file in which they are used
Doug Simon <doug.simon@oracle.com>
parents: 5579
diff changeset
28 import com.oracle.graal.graph.Node.ConstantNodeParameter;
2a0c9f20baa1 consolidated framework for runtime and stub calls and moved declaration of descriptors for such calls to the source file in which they are used
Doug Simon <doug.simon@oracle.com>
parents: 5579
diff changeset
29 import com.oracle.graal.graph.Node.NodeIntrinsic;
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
30 import com.oracle.graal.nodes.extended.*;
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
31 import com.oracle.graal.replacements.amd64.AMD64MathIntrinsicNode.Operation;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
32
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
33 /**
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
34 * Substitutions for some {@link java.lang.Math} methods that leverage AMD64 instructions for
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
35 * selected input values.
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
36 */
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
37 public class AMD64MathSubstitutions {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
38
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
39 private static final double PI_4 = Math.PI / 4;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
40
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
41 /**
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
42 * Special cases from {@link Math#pow} and __ieee754_pow (in sharedRuntimeTrans.cpp).
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
43 */
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
44 public static double pow(double x, double y) {
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
45 // If the second argument is positive or negative zero, then the result is 1.0.
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
46 if (y == 0.0D) {
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
47 return 1;
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
48 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
49
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
50 // If the second argument is 1.0, then the result is the same as the first argument.
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
51 if (y == 1.0D) {
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
52 return x;
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
53 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
54
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
55 // If the second argument is NaN, then the result is NaN.
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
56 if (Double.isNaN(y)) {
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
57 return Double.NaN;
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
58 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
59
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
60 // If the first argument is NaN and the second argument is nonzero, then the result is NaN.
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
61 if (Double.isNaN(x) && y != 0.0D) {
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
62 return Double.NaN;
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
63 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
64
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
65 // x**-1 = 1/x
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
66 if (y == -1.0D) {
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
67 return 1 / x;
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
68 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
69
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
70 // x**2 = x*x
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
71 if (y == 2.0D) {
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
72 return x * x;
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
73 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
74
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
75 // x**0.5 = sqrt(x)
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
76 if (y == 0.5D && x >= 0.0D) {
19791
14e703edb2ab use GraphBuilderPlugins for method substitutions, intrinsics and snippets (GRAAL-982)
Doug Simon <doug.simon@oracle.com>
parents: 18960
diff changeset
77 return Math.sqrt(x);
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
78 }
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
79 return callDouble2(ARITHMETIC_POW, x, y);
13250
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
80 }
ca061aaeddaf added Math.pow method substitution with code for handling some special cases
twisti
parents: 9738
diff changeset
81
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
82 // NOTE on snippets below:
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7356
diff changeset
83 // Math.sin(), .cos() and .tan() guarantee a value within 1 ULP of the
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7356
diff changeset
84 // exact result, but x87 trigonometric FPU instructions are only that
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7356
diff changeset
85 // accurate within [-pi/4, pi/4]. Examine the passed value and provide
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7356
diff changeset
86 // a slow path for inputs outside of that interval.
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
87
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
88 public static double sin(double x) {
19791
14e703edb2ab use GraphBuilderPlugins for method substitutions, intrinsics and snippets (GRAAL-982)
Doug Simon <doug.simon@oracle.com>
parents: 18960
diff changeset
89 if (Math.abs(x) < PI_4) {
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
90 return AMD64MathIntrinsicNode.compute(x, Operation.SIN);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
91 } else {
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
92 return callDouble1(ARITHMETIC_SIN, x);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
93 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
94 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
95
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
96 public static double cos(double x) {
19791
14e703edb2ab use GraphBuilderPlugins for method substitutions, intrinsics and snippets (GRAAL-982)
Doug Simon <doug.simon@oracle.com>
parents: 18960
diff changeset
97 if (Math.abs(x) < PI_4) {
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
98 return AMD64MathIntrinsicNode.compute(x, Operation.COS);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
99 } else {
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
100 return callDouble1(ARITHMETIC_COS, x);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
101 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
102 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
103
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
104 public static double tan(double x) {
19791
14e703edb2ab use GraphBuilderPlugins for method substitutions, intrinsics and snippets (GRAAL-982)
Doug Simon <doug.simon@oracle.com>
parents: 18960
diff changeset
105 if (Math.abs(x) < PI_4) {
20854
3081a57f95fd converted @MethodSubstitutions for java.lang.Math to MethodSubstitutionPlugins
Doug Simon <doug.simon@oracle.com>
parents: 20045
diff changeset
106 return AMD64MathIntrinsicNode.compute(x, Operation.TAN);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
107 } else {
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
108 return callDouble1(ARITHMETIC_TAN, x);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
109 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
110 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
111
9738
857632c8086a renamed: RuntimeCallNode -> ForeignCallNode, RuntimeCallStateSplitNode -> ForeignCallStateSplitNode
Doug Simon <doug.simon@oracle.com>
parents: 9737
diff changeset
112 @NodeIntrinsic(value = ForeignCallNode.class, setStampFromReturnType = true)
19813
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
113 private static native double callDouble1(@ConstantNodeParameter ForeignCallDescriptor descriptor, double value);
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
114
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
115 @NodeIntrinsic(value = ForeignCallNode.class, setStampFromReturnType = true)
853f84c7cc6f fixed Math.pow intrinsic to make a runtime call instead of calling the original method
Doug Simon <doug.simon@oracle.com>
parents: 19791
diff changeset
116 private static native double callDouble2(@ConstantNodeParameter ForeignCallDescriptor descriptor, double a, double b);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
117 }