1 /*
2  * Copyright (c) 2013, 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 import java.lang.reflect.Executable;
25 import java.util.concurrent.Callable;
26 
27 public class MathIntrinsic {
28 
29     enum IntIntrinsic implements CompilerWhiteBoxTest.TestCase {
30         Add {
31             @Override
execMathMethod()32             Object execMathMethod() {
33                 return intR = Math.addExact(int1, int2);
34             }
35         },
36         Subtract {
37             @Override
execMathMethod()38             Object execMathMethod() {
39                 return intR = Math.subtractExact(int1, int2);
40             }
41         },
42         Multiply {
43             @Override
execMathMethod()44             Object execMathMethod() {
45                 return intR = Math.multiplyExact(int1, int2);
46             }
47         },
48         Increment {
49             @Override
execMathMethod()50             Object execMathMethod() {
51                 return intR = Math.incrementExact(int1);
52             }
53         },
54         Decrement {
55             @Override
execMathMethod()56             Object execMathMethod() {
57                 return intR = Math.decrementExact(int1);
58             }
59         },
60         Negate {
61             @Override
execMathMethod()62             Object execMathMethod() {
63                 return intR = Math.negateExact(int1);
64             }
65         };
66         protected int int1;
67         protected int int2;
68         protected int intR;
69 
execMathMethod()70         abstract Object execMathMethod();
71 
72         @Override
getExecutable()73         public Executable getExecutable() {
74             try {
75                 return getClass().getDeclaredMethod("execMathMethod");
76             } catch (NoSuchMethodException e) {
77                 throw new RuntimeException("Test bug, no such method: " + e);
78             }
79         }
80 
81         @Override
getCallable()82         public Callable<Integer> getCallable() {
83             return null;
84         }
85 
86         @Override
isOsr()87         public boolean isOsr() {
88             return false;
89         }
90 
91     }
92 
93     enum LongIntrinsic implements CompilerWhiteBoxTest.TestCase {
94         Add {
95             @Override
execMathMethod()96             Object execMathMethod() {
97                 return longR = Math.addExact(long1, long2);
98             }
99         },
100         Subtract {
101             @Override
execMathMethod()102             Object execMathMethod() {
103                 return longR = Math.subtractExact(long1, long2);
104             }
105         },
106         Multiply {
107             @Override
execMathMethod()108             Object execMathMethod() {
109                 return longR = Math.multiplyExact(long1, long2);
110             }
111         },
112         Increment {
113             @Override
execMathMethod()114             Object execMathMethod() {
115                 return longR = Math.incrementExact(long1);
116             }
117         },
118         Decrement {
119             @Override
execMathMethod()120             Object execMathMethod() {
121                 return longR = Math.decrementExact(long1);
122             }
123         },
124         Negate {
125             @Override
execMathMethod()126             Object execMathMethod() {
127                 return longR = Math.negateExact(long1);
128             }
129         };
130         protected long long1;
131         protected long long2;
132         protected long longR;
133 
execMathMethod()134         abstract Object execMathMethod();
135 
136         @Override
getExecutable()137         public Executable getExecutable() {
138             try {
139                 return getClass().getDeclaredMethod("execMathMethod");
140             } catch (NoSuchMethodException e) {
141                 throw new RuntimeException("Test bug, no such method: " + e);
142             }
143         }
144 
145         @Override
getCallable()146         public Callable<Integer> getCallable() {
147             return null;
148         }
149 
150         @Override
isOsr()151         public boolean isOsr() {
152             return false;
153         }
154     }
155 }
156