Path: blob/master/test/hotspot/jtreg/compiler/c2/Test6603011.java
41149 views
/*1* Copyright (c) 2010, 2016, 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* @modules java.base/jdk.internal.misc28* @library /test/lib29*30* @run main/othervm/timeout=480 -Xcomp -Xbatch -XX:-Inline compiler.c2.Test660301131*/3233//34// -XX:-Inline is essential to this test so that verification functions35// divi, modi, divl and modl generate "plain" divides.36// -Xcomp -Xbatch are also useful to ensure the full range of37// dividend and divisor combinations are tested38//3940package compiler.c2;4142import jdk.test.lib.Utils;4344public class Test6603011 implements Runnable {45static class s {46static int divi(int dividend, int divisor) { return dividend / divisor; }47static int modi(int dividend, int divisor) { return dividend % divisor; }48static long divl(long dividend, long divisor) { return dividend / divisor; }49static long modl(long dividend, long divisor) { return dividend % divisor; }50}51// Report verbose messages on failure; turn off to suppress52// too much output with gross numbers of failures.53static final boolean VERBOSE = true;5455// Initailize DIVISOR so that it is final in this class.56static final int DIVISOR;57static {58int value = 0;59try {60value = Integer.decode(System.getProperty("divisor"));61} catch (Throwable e) {62}63DIVISOR = value;64}6566// The methods of interest. We want the JIT to compile these67// and convert the divide into a multiply.68public int divbyI (int dividend) { return dividend / DIVISOR; }69public int modbyI (int dividend) { return dividend % DIVISOR; }70public long divbyL (long dividend) { return dividend / DIVISOR; }71public long modbyL (long dividend) { return dividend % DIVISOR; }7273public int divisor() { return DIVISOR; }7475public boolean checkI (int dividend) {76int quo = divbyI(dividend);77int rem = modbyI(dividend);78int quo0 = s.divi(dividend, divisor());79int rem0 = s.modi(dividend, divisor());8081if (quo != quo0 || rem != rem0) {82if (VERBOSE) {83System.out.println("Computed: " + dividend + " / " + divisor() + " = " +84quo + ", " + dividend + " % " + divisor() + " = " + rem );85System.out.println("expected: " + dividend + " / " + divisor() + " = " +86quo0 + ", " + dividend + " % " + divisor() + " = " + rem0);87// Report sign of rem failure88if (rem != 0 && (rem ^ dividend) < 0) {89System.out.println(" rem & dividend have different signs");90}91// Report range of rem failure92if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) {93System.out.println(" remainder out of range");94}95// Report quo/rem identity relationship failure96if ((quo * divisor()) + rem != dividend) {97System.out.println(" quotien/remainder invariant broken");98}99}100return false;101}102return true;103}104105public boolean checkL (long dividend) {106long quo = divbyL(dividend);107long rem = modbyL(dividend);108long quo0 = s.divl(dividend, divisor());109long rem0 = s.modl(dividend, divisor());110111if (quo != quo0 || rem != rem0) {112if (VERBOSE) {113System.out.println("Computed: " + dividend + " / " + divisor() + " = " +114quo + ", " + dividend + " % " + divisor() + " = " + rem );115System.out.println("expected: " + dividend + " / " + divisor() + " = " +116quo0 + ", " + dividend + " % " + divisor() + " = " + rem0);117// Report sign of rem failure118if (rem != 0 && (rem ^ dividend) < 0) {119System.out.println(" rem & dividend have different signs");120}121// Report range of rem failure122if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) {123System.out.println(" remainder out of range");124}125// Report quo/rem identity relationship failure126if ((quo * divisor()) + rem != dividend) {127System.out.println(" (" + quo + " * " + divisor() + ") + " + rem + " != "128+ dividend);129}130}131return false;132}133return true;134}135136public void run() {137// Don't try to divide by zero138if (divisor() == 0) return;139140// Range of dividends to check. Try dividends from start to end141// inclusive, as well as variations on those values as shifted142// left.143int start = -1024;144int end = 1024;145146// Test int division using a variety of dividends.147int wrong = 0;148int total = 0;149150outerloop:151for (int i = start; i <= end; i++) {152for (int s = 0; s < 32; s += 4) {153total++;154int dividend = i << s;155if (!checkI(dividend)) {156wrong++;157// Stop on the first failure158// break outerloop;159}160}161}162if (wrong > 0) {163System.out.println("divisor " + divisor() + ": " +164wrong + "/" + total + " wrong int divisions");165}166167// Test long division using a variety of dividends.168wrong = 0;169total = 0;170171outerloop:172for (int i = start; i <= end; i++) {173for (int s = 0; s < 64; s += 4) {174total++;175long dividend = ((long)i) << s;176if (!checkL(dividend)) {177wrong++;178// Stop on the first failure179// break outerloop;180}181}182}183if (wrong > 0) {184System.out.println("divisor " + divisor() + ": " +185wrong + "/" + total + " wrong long divisions");186}187188}189190// Reload this class with the "divisor" property set to the input parameter.191// This allows the JIT to see q.DIVISOR as a final constant, and change192// any divisions or mod operations into multiplies.193public static void test_divisor(int divisor,194ClassLoader apploader) throws Exception {195System.setProperty("divisor", "" + divisor);196ClassLoader loader197= Utils.getTestClassPathURLClassLoader(apploader.getParent());198Class c = loader.loadClass(Test6603011.class.getName());199Runnable r = (Runnable)c.newInstance();200r.run();201}202203public static void main(String[] args) throws Exception {204Class cl = Test6603011.class;205ClassLoader apploader = cl.getClassLoader();206207208// Test every divisor between -100 and 100.209for (int i = -100; i <= 100; i++) {210test_divisor(i, apploader);211}212213// Try a few divisors outside the typical range.214// The values below have been observed in rt.jar.215test_divisor(101, apploader);216test_divisor(400, apploader);217test_divisor(1000, apploader);218test_divisor(3600, apploader);219test_divisor(9973, apploader);220test_divisor(86400, apploader);221test_divisor(1000000, apploader);222}223224}225226227