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
  32             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
  33                 return Class.forName("java.lang.Math").getDeclaredMethod("addExact", int.class, int.class);
  34             }
  35 
  36             @Override
  37             Object execMathMethod() {
  38                 return intR = Math.addExact(int1, int2);
  39             }
  40         },
  41        Subtract {
  42             @Override
  43             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
  44                 return Class.forName("java.lang.Math").getDeclaredMethod("subtractExact", int.class, int.class);
  45             }
  46 
  47             @Override
  48             Object execMathMethod() {
  49                 return intR = Math.subtractExact(int1, int2);
  50             }
  51         },
  52         Multiply {
  53             @Override
  54             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
  55                 return Class.forName("java.lang.Math").getDeclaredMethod("multiplyExact", int.class, int.class);
  56             }
  57 
  58             @Override
  59             Object execMathMethod() {
  60                 return intR = Math.multiplyExact(int1, int2);
  61             }
  62         },
  63         Increment {
  64             @Override
  65             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
  66                 return Class.forName("java.lang.Math").getDeclaredMethod("incrementExact", int.class);
  67             }
  68 
  69             @Override
  70             Object execMathMethod() {
  71                 return intR = Math.incrementExact(int1);
  72             }
  73         },
  74         Decrement {
  75             @Override
  76             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
  77                 return Class.forName("java.lang.Math").getDeclaredMethod("decrementExact", int.class);
  78             }
  79 
  80             @Override
  81             Object execMathMethod() {
  82                 return intR = Math.decrementExact(int1);
  83             }
  84         },
  85         Negate {
  86             @Override
  87             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
  88                 return Class.forName("java.lang.Math").getDeclaredMethod("negateExact", int.class);
  89             }
  90 
  91             @Override
  92             Object execMathMethod() {
  93                 return intR = Math.negateExact(int1);
  94             }
  95         };
  96 
  97         protected int int1;
  98         protected int int2;
  99         protected int intR;
 100 
 101         abstract Executable testMethod() throws NoSuchMethodException, ClassNotFoundException;
 102         abstract Object execMathMethod();
 103 
 104         public Executable getTestMethod() {
 105             try {
 106                 return testMethod();
 107             } catch (NoSuchMethodException e) {
 108                 throw new RuntimeException("Test bug, no such method: " + e);
 109             } catch (ClassNotFoundException e) {
 110                 throw new RuntimeException("Test bug, no such class: " + e);
 111             }
 112         }
 113 
 114         @Override
 115         public Executable getExecutable() {
 116             try {
 117                 return getClass().getDeclaredMethod("execMathMethod");
 118             } catch (NoSuchMethodException e) {
 119                 throw new RuntimeException("Test bug, no such method: " + e);
 120             }
 121         }
 122 
 123         @Override
 124         public Callable<Integer> getCallable() {
 125             return null;
 126         }
 127 
 128         @Override
 129         public boolean isOsr() {
 130             return false;
 131         }
 132 
 133     }
 134 
 135     enum LongIntrinsic implements CompilerWhiteBoxTest.TestCase {
 136         Add {
 137             @Override
 138             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
 139                 return Class.forName("java.lang.Math").getDeclaredMethod("addExact", long.class, long.class);
 140             }
 141 
 142             @Override
 143             Object execMathMethod() {
 144                 return longR = Math.addExact(long1, long2);
 145             }
 146         },
 147         Subtract {
 148             @Override
 149             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
 150                 return Class.forName("java.lang.Math").getDeclaredMethod("subtractExact", long.class, long.class);
 151             }
 152 
 153             @Override
 154             Object execMathMethod() {
 155                 return longR = Math.subtractExact(long1, long2);
 156             }
 157         },
 158         Multiply {
 159             @Override
 160             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
 161                 return Class.forName("java.lang.Math").getDeclaredMethod("multiplyExact", long.class, long.class);
 162             }
 163 
 164             @Override
 165             Object execMathMethod() {
 166                 return longR = Math.multiplyExact(long1, long2);
 167             }
 168         },
 169         Increment {
 170             @Override
 171             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
 172                 return Class.forName("java.lang.Math").getDeclaredMethod("incrementExact", long.class);
 173             }
 174 
 175             @Override
 176             Object execMathMethod() {
 177                 return longR = Math.incrementExact(long1);
 178             }
 179         },
 180         Decrement {
 181             @Override
 182             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
 183                 return Class.forName("java.lang.Math").getDeclaredMethod("decrementExact", long.class);
 184             }
 185 
 186             @Override
 187             Object execMathMethod() {
 188                 return longR = Math.decrementExact(long1);
 189             }
 190         },
 191         Negate {
 192             @Override
 193             Executable testMethod() throws NoSuchMethodException, ClassNotFoundException {
 194                 return Class.forName("java.lang.Math").getDeclaredMethod("negateExact", long.class);
 195             }
 196 
 197             @Override
 198             Object execMathMethod() {
 199                 return longR = Math.negateExact(long1);
 200             }
 201         };
 202         protected long long1;
 203         protected long long2;
 204         protected long longR;
 205 
 206         abstract Executable testMethod() throws NoSuchMethodException, ClassNotFoundException;
 207         abstract Object execMathMethod();
 208 
 209         public Executable getTestMethod() {
 210             try {
 211                 return testMethod();
 212             } catch (NoSuchMethodException e) {
 213                 throw new RuntimeException("Test bug, no such method: " + e);
 214             } catch (ClassNotFoundException e) {
 215                 throw new RuntimeException("Test bug, no such class: " + e);
 216             }
 217         }
 218 
 219         @Override
 220         public Executable getExecutable() {
 221             try {
 222                 return getClass().getDeclaredMethod("execMathMethod");
 223             } catch (NoSuchMethodException e) {
 224                 throw new RuntimeException("Test bug, no such method: " + e);
 225             }
 226         }
 227 
 228         @Override
 229         public Callable<Integer> getCallable() {
 230             return null;
 231         }
 232 
 233         @Override
 234         public boolean isOsr() {
 235             return false;
 236         }
 237     }
 238 }