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