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