Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/compiler/6603011/Test.java
32285 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 660301126* @summary long/int division by constant27*28* @run main/othervm -Xcomp -Xbatch -XX:-Inline Test29*/3031//32// -XX:-Inline is essential to this test so that verification functions33// divi, modi, divl and modl generate "plain" divides.34// -Xcomp -Xbatch are also useful to ensure the full range of35// dividend and divisor combinations are tested36//3738import java.net.*;3940class s {41static int divi(int dividend, int divisor) { return dividend / divisor; }42static int modi(int dividend, int divisor) { return dividend % divisor; }43static long divl(long dividend, long divisor) { return dividend / divisor; }44static long modl(long dividend, long divisor) { return dividend % divisor; }45}4647public class Test implements Runnable {48// Report verbose messages on failure; turn off to suppress49// too much output with gross numbers of failures.50static final boolean VERBOSE = true;5152// Initailize DIVISOR so that it is final in this class.53static final int DIVISOR;54static {55int value = 0;56try {57value = Integer.decode(System.getProperty("divisor"));58} catch (Throwable e) {59}60DIVISOR = value;61}6263// The methods of interest. We want the JIT to compile these64// and convert the divide into a multiply.65public int divbyI (int dividend) { return dividend / DIVISOR; }66public int modbyI (int dividend) { return dividend % DIVISOR; }67public long divbyL (long dividend) { return dividend / DIVISOR; }68public long modbyL (long dividend) { return dividend % DIVISOR; }6970public int divisor() { return DIVISOR; }7172public boolean checkI (int dividend) {73int quo = divbyI(dividend);74int rem = modbyI(dividend);75int quo0 = s.divi(dividend, divisor());76int rem0 = s.modi(dividend, divisor());7778if (quo != quo0 || rem != rem0) {79if (VERBOSE) {80System.out.println("Computed: " + dividend + " / " + divisor() + " = " +81quo + ", " + dividend + " % " + divisor() + " = " + rem );82System.out.println("expected: " + dividend + " / " + divisor() + " = " +83quo0 + ", " + dividend + " % " + divisor() + " = " + rem0);84// Report sign of rem failure85if (rem != 0 && (rem ^ dividend) < 0) {86System.out.println(" rem & dividend have different signs");87}88// Report range of rem failure89if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) {90System.out.println(" remainder out of range");91}92// Report quo/rem identity relationship failure93if ((quo * divisor()) + rem != dividend) {94System.out.println(" quotien/remainder invariant broken");95}96}97return false;98}99return true;100}101102public boolean checkL (long dividend) {103long quo = divbyL(dividend);104long rem = modbyL(dividend);105long quo0 = s.divl(dividend, divisor());106long rem0 = s.modl(dividend, divisor());107108if (quo != quo0 || rem != rem0) {109if (VERBOSE) {110System.out.println("Computed: " + dividend + " / " + divisor() + " = " +111quo + ", " + dividend + " % " + divisor() + " = " + rem );112System.out.println("expected: " + dividend + " / " + divisor() + " = " +113quo0 + ", " + dividend + " % " + divisor() + " = " + rem0);114// Report sign of rem failure115if (rem != 0 && (rem ^ dividend) < 0) {116System.out.println(" rem & dividend have different signs");117}118// Report range of rem failure119if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) {120System.out.println(" remainder out of range");121}122// Report quo/rem identity relationship failure123if ((quo * divisor()) + rem != dividend) {124System.out.println(" (" + quo + " * " + divisor() + ") + " + rem + " != "125+ dividend);126}127}128return false;129}130return true;131}132133public void run() {134// Don't try to divide by zero135if (divisor() == 0) return;136137// Range of dividends to check. Try dividends from start to end138// inclusive, as well as variations on those values as shifted139// left.140int start = -1024;141int end = 1024;142143// Test int division using a variety of dividends.144int wrong = 0;145int total = 0;146147outerloop:148for (int i = start; i <= end; i++) {149for (int s = 0; s < 32; s += 4) {150total++;151int dividend = i << s;152if (!checkI(dividend)) {153wrong++;154// Stop on the first failure155// break outerloop;156}157}158}159if (wrong > 0) {160System.out.println("divisor " + divisor() + ": " +161wrong + "/" + total + " wrong int divisions");162}163164// Test long division using a variety of dividends.165wrong = 0;166total = 0;167168outerloop:169for (int i = start; i <= end; i++) {170for (int s = 0; s < 64; s += 4) {171total++;172long dividend = ((long)i) << s;173if (!checkL(dividend)) {174wrong++;175// Stop on the first failure176// break outerloop;177}178}179}180if (wrong > 0) {181System.out.println("divisor " + divisor() + ": " +182wrong + "/" + total + " wrong long divisions");183}184185}186187// Reload this class with the "divisor" property set to the input parameter.188// This allows the JIT to see q.DIVISOR as a final constant, and change189// any divisions or mod operations into multiplies.190public static void test_divisor(int divisor,191URLClassLoader apploader) throws Exception {192System.setProperty("divisor", "" + divisor);193ClassLoader loader = new URLClassLoader(apploader.getURLs(),194apploader.getParent());195Class c = loader.loadClass("Test");196Runnable r = (Runnable)c.newInstance();197r.run();198}199200public static void main(String[] args) throws Exception {201Class cl = Class.forName("Test");202URLClassLoader apploader = (URLClassLoader)cl.getClassLoader();203204205// Test every divisor between -100 and 100.206for (int i = -100; i <= 100; i++) {207test_divisor(i, apploader);208}209210// Try a few divisors outside the typical range.211// The values below have been observed in rt.jar.212test_divisor(101, apploader);213test_divisor(400, apploader);214test_divisor(1000, apploader);215test_divisor(3600, apploader);216test_divisor(9973, apploader);217test_divisor(86400, apploader);218test_divisor(1000000, apploader);219}220221}222223224