hotspot/test/compiler/intrinsics/mathexact/Verify.java
author chegar
Mon, 21 Oct 2013 15:00:56 +0100
changeset 22332 fba4c66089bb
parent 20289 35d78de0c547
child 21105 47618ee96ed5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20289
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     1
/*
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     4
 *
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     8
 *
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    13
 * accompanied this code).
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    14
 *
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    18
 *
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    21
 * questions.
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    22
 */
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    23
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    24
public class Verify {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    25
  public static String throwWord(boolean threw) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    26
    return (threw ? "threw" : "didn't throw");
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    27
  }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    28
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    29
  public static void verify(int a, int b) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    30
    boolean exception1 = false, exception2 = false;
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    31
    int result1 = 0, result2 = 0;
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    32
    try {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    33
      result1 = testIntrinsic(a, b);
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    34
    } catch (ArithmeticException e) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    35
      exception1 = true;
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    36
    }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    37
    try {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    38
      result2 = testNonIntrinsic(a, b);
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    39
    } catch (ArithmeticException e) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    40
      exception2 = true;
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    41
    }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    42
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    43
    if (exception1 != exception2) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    44
      throw new RuntimeException("Intrinsic version " + throwWord(exception1) + " exception, NonIntrinsic version " + throwWord(exception2) + " for: " + a + " + " + b);
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    45
    }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    46
    if (result1 != result2) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    47
      throw new RuntimeException("Intrinsic version returned: " + a + " while NonIntrinsic version returned: " + b);
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    48
    }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    49
  }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    50
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    51
  public static int testIntrinsic(int a, int b) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    52
    return java.lang.Math.addExact(a, b);
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    53
  }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    54
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    55
  public static int testNonIntrinsic(int a, int b) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    56
    return safeAddExact(a, b);
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    57
  }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    58
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    59
  // Copied java.lang.Math.addExact to avoid intrinsification
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    60
  public static int safeAddExact(int x, int y) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    61
    int r = x + y;
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    62
    // HD 2-12 Overflow iff both arguments have the opposite sign of the result
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    63
    if (((x ^ r) & (y ^ r)) < 0) {
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    64
      throw new ArithmeticException("integer overflow");
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    65
    }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    66
    return r;
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    67
  }
35d78de0c547 8024924: Intrinsify java.lang.Math.addExact
rbackman
parents:
diff changeset
    68
}