1 /*
 2  * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
 3  * Copyright (c) 2022, Alibaba Group Holding Limited. All rights reserved.
 4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 5  *
 6  * This code is free software; you can redistribute it and/or modify it
 7  * under the terms of the GNU General Public License version 2 only, as
 8  * published by the Free Software Foundation.
 9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * @test
27  * @summary Test libm intrinsics
28  * @library /test/lib /
29  *
30  * @build jdk.test.whitebox.WhiteBox
31  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
33  *                   -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
34  *                   compiler.floatingpoint.TestLibmIntrinsics
35  */
36 
37 package compiler.floatingpoint;
38 
39 import compiler.whitebox.CompilerWhiteBoxTest;
40 import jdk.test.whitebox.WhiteBox;
41 
42 import java.lang.reflect.Method;
43 
44 public class TestLibmIntrinsics {
45 
46     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
47 
48     private static final double pi = 3.1415926;
49 
50     private static final double expected = 2.5355263553695413;
51 
52     static double m() {
53         return Math.pow(pi, Math.sin(Math.cos(Math.tan(Math.log(Math.log10(Math.exp(pi)))))));
54     }
55 
56     static public void main(String[] args) throws NoSuchMethodException {
57         Method test_method = compiler.floatingpoint.TestLibmIntrinsics.class.getDeclaredMethod("m");
58 
59         double interpreter_result = m();
60 
61         // Compile with C1 if possible
62         WHITE_BOX.enqueueMethodForCompilation(test_method, CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);
63 
64         double c1_result = m();
65 
66         WHITE_BOX.deoptimizeMethod(test_method);
67 
68         // Compile it with C2 if possible
69         WHITE_BOX.enqueueMethodForCompilation(test_method, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
70 
71         double c2_result = m();
72 
73         if (interpreter_result != c1_result ||
74             interpreter_result != c2_result ||
75             c1_result != c2_result) {
76             System.out.println("interpreter = " + interpreter_result + " c1 = " + c1_result + " c2 = " + c2_result);
77             throw new RuntimeException("Test Failed");
78         }
79     }
80 }