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