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