test/compiler/6603011/Test.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 2275
2fe998383789
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * @test
aoqi@0 26 * @bug 6603011
aoqi@0 27 * @summary long/int division by constant
aoqi@0 28 *
aoqi@0 29 * @run main/othervm -Xcomp -Xbatch -XX:-Inline Test
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 //
aoqi@0 33 // -XX:-Inline is essential to this test so that verification functions
aoqi@0 34 // divi, modi, divl and modl generate "plain" divides.
aoqi@0 35 // -Xcomp -Xbatch are also useful to ensure the full range of
aoqi@0 36 // dividend and divisor combinations are tested
aoqi@0 37 //
aoqi@0 38
aoqi@0 39 import java.net.*;
aoqi@0 40
aoqi@0 41 class s {
aoqi@0 42 static int divi(int dividend, int divisor) { return dividend / divisor; }
aoqi@0 43 static int modi(int dividend, int divisor) { return dividend % divisor; }
aoqi@0 44 static long divl(long dividend, long divisor) { return dividend / divisor; }
aoqi@0 45 static long modl(long dividend, long divisor) { return dividend % divisor; }
aoqi@0 46 }
aoqi@0 47
aoqi@0 48 public class Test implements Runnable {
aoqi@0 49 // Report verbose messages on failure; turn off to suppress
aoqi@0 50 // too much output with gross numbers of failures.
aoqi@0 51 static final boolean VERBOSE = true;
aoqi@0 52
aoqi@0 53 // Initailize DIVISOR so that it is final in this class.
aoqi@0 54 static final int DIVISOR;
aoqi@0 55 static {
aoqi@0 56 int value = 0;
aoqi@0 57 try {
aoqi@0 58 value = Integer.decode(System.getProperty("divisor"));
aoqi@0 59 } catch (Throwable e) {
aoqi@0 60 }
aoqi@0 61 DIVISOR = value;
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 // The methods of interest. We want the JIT to compile these
aoqi@0 65 // and convert the divide into a multiply.
aoqi@0 66 public int divbyI (int dividend) { return dividend / DIVISOR; }
aoqi@0 67 public int modbyI (int dividend) { return dividend % DIVISOR; }
aoqi@0 68 public long divbyL (long dividend) { return dividend / DIVISOR; }
aoqi@0 69 public long modbyL (long dividend) { return dividend % DIVISOR; }
aoqi@0 70
aoqi@0 71 public int divisor() { return DIVISOR; }
aoqi@0 72
aoqi@0 73 public boolean checkI (int dividend) {
aoqi@0 74 int quo = divbyI(dividend);
aoqi@0 75 int rem = modbyI(dividend);
aoqi@0 76 int quo0 = s.divi(dividend, divisor());
aoqi@0 77 int rem0 = s.modi(dividend, divisor());
aoqi@0 78
aoqi@0 79 if (quo != quo0 || rem != rem0) {
aoqi@0 80 if (VERBOSE) {
aoqi@0 81 System.out.println("Computed: " + dividend + " / " + divisor() + " = " +
aoqi@0 82 quo + ", " + dividend + " % " + divisor() + " = " + rem );
aoqi@0 83 System.out.println("expected: " + dividend + " / " + divisor() + " = " +
aoqi@0 84 quo0 + ", " + dividend + " % " + divisor() + " = " + rem0);
aoqi@0 85 // Report sign of rem failure
aoqi@0 86 if (rem != 0 && (rem ^ dividend) < 0) {
aoqi@0 87 System.out.println(" rem & dividend have different signs");
aoqi@0 88 }
aoqi@0 89 // Report range of rem failure
aoqi@0 90 if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) {
aoqi@0 91 System.out.println(" remainder out of range");
aoqi@0 92 }
aoqi@0 93 // Report quo/rem identity relationship failure
aoqi@0 94 if ((quo * divisor()) + rem != dividend) {
aoqi@0 95 System.out.println(" quotien/remainder invariant broken");
aoqi@0 96 }
aoqi@0 97 }
aoqi@0 98 return false;
aoqi@0 99 }
aoqi@0 100 return true;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public boolean checkL (long dividend) {
aoqi@0 104 long quo = divbyL(dividend);
aoqi@0 105 long rem = modbyL(dividend);
aoqi@0 106 long quo0 = s.divl(dividend, divisor());
aoqi@0 107 long rem0 = s.modl(dividend, divisor());
aoqi@0 108
aoqi@0 109 if (quo != quo0 || rem != rem0) {
aoqi@0 110 if (VERBOSE) {
aoqi@0 111 System.out.println("Computed: " + dividend + " / " + divisor() + " = " +
aoqi@0 112 quo + ", " + dividend + " % " + divisor() + " = " + rem );
aoqi@0 113 System.out.println("expected: " + dividend + " / " + divisor() + " = " +
aoqi@0 114 quo0 + ", " + dividend + " % " + divisor() + " = " + rem0);
aoqi@0 115 // Report sign of rem failure
aoqi@0 116 if (rem != 0 && (rem ^ dividend) < 0) {
aoqi@0 117 System.out.println(" rem & dividend have different signs");
aoqi@0 118 }
aoqi@0 119 // Report range of rem failure
aoqi@0 120 if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) {
aoqi@0 121 System.out.println(" remainder out of range");
aoqi@0 122 }
aoqi@0 123 // Report quo/rem identity relationship failure
aoqi@0 124 if ((quo * divisor()) + rem != dividend) {
aoqi@0 125 System.out.println(" (" + quo + " * " + divisor() + ") + " + rem + " != "
aoqi@0 126 + dividend);
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129 return false;
aoqi@0 130 }
aoqi@0 131 return true;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public void run() {
aoqi@0 135 // Don't try to divide by zero
aoqi@0 136 if (divisor() == 0) return;
aoqi@0 137
aoqi@0 138 // Range of dividends to check. Try dividends from start to end
aoqi@0 139 // inclusive, as well as variations on those values as shifted
aoqi@0 140 // left.
aoqi@0 141 int start = -1024;
aoqi@0 142 int end = 1024;
aoqi@0 143
aoqi@0 144 // Test int division using a variety of dividends.
aoqi@0 145 int wrong = 0;
aoqi@0 146 int total = 0;
aoqi@0 147
aoqi@0 148 outerloop:
aoqi@0 149 for (int i = start; i <= end; i++) {
aoqi@0 150 for (int s = 0; s < 32; s += 4) {
aoqi@0 151 total++;
aoqi@0 152 int dividend = i << s;
aoqi@0 153 if (!checkI(dividend)) {
aoqi@0 154 wrong++;
aoqi@0 155 // Stop on the first failure
aoqi@0 156 // break outerloop;
aoqi@0 157 }
aoqi@0 158 }
aoqi@0 159 }
aoqi@0 160 if (wrong > 0) {
aoqi@0 161 System.out.println("divisor " + divisor() + ": " +
aoqi@0 162 wrong + "/" + total + " wrong int divisions");
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 // Test long division using a variety of dividends.
aoqi@0 166 wrong = 0;
aoqi@0 167 total = 0;
aoqi@0 168
aoqi@0 169 outerloop:
aoqi@0 170 for (int i = start; i <= end; i++) {
aoqi@0 171 for (int s = 0; s < 64; s += 4) {
aoqi@0 172 total++;
aoqi@0 173 long dividend = ((long)i) << s;
aoqi@0 174 if (!checkL(dividend)) {
aoqi@0 175 wrong++;
aoqi@0 176 // Stop on the first failure
aoqi@0 177 // break outerloop;
aoqi@0 178 }
aoqi@0 179 }
aoqi@0 180 }
aoqi@0 181 if (wrong > 0) {
aoqi@0 182 System.out.println("divisor " + divisor() + ": " +
aoqi@0 183 wrong + "/" + total + " wrong long divisions");
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 // Reload this class with the "divisor" property set to the input parameter.
aoqi@0 189 // This allows the JIT to see q.DIVISOR as a final constant, and change
aoqi@0 190 // any divisions or mod operations into multiplies.
aoqi@0 191 public static void test_divisor(int divisor,
aoqi@0 192 URLClassLoader apploader) throws Exception {
aoqi@0 193 System.setProperty("divisor", "" + divisor);
aoqi@0 194 ClassLoader loader = new URLClassLoader(apploader.getURLs(),
aoqi@0 195 apploader.getParent());
aoqi@0 196 Class c = loader.loadClass("Test");
aoqi@0 197 Runnable r = (Runnable)c.newInstance();
aoqi@0 198 r.run();
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 public static void main(String[] args) throws Exception {
aoqi@0 202 Class cl = Class.forName("Test");
aoqi@0 203 URLClassLoader apploader = (URLClassLoader)cl.getClassLoader();
aoqi@0 204
aoqi@0 205
aoqi@0 206 // Test every divisor between -100 and 100.
aoqi@0 207 for (int i = -100; i <= 100; i++) {
aoqi@0 208 test_divisor(i, apploader);
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 // Try a few divisors outside the typical range.
aoqi@0 212 // The values below have been observed in rt.jar.
aoqi@0 213 test_divisor(101, apploader);
aoqi@0 214 test_divisor(400, apploader);
aoqi@0 215 test_divisor(1000, apploader);
aoqi@0 216 test_divisor(3600, apploader);
aoqi@0 217 test_divisor(9973, apploader);
aoqi@0 218 test_divisor(86400, apploader);
aoqi@0 219 test_divisor(1000000, apploader);
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 }

mercurial