test/compiler/intrinsics/squaretolen/TestSquareToLen.java

changeset 8307
daaf806995b3
child 8319
0cd040567d60
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/intrinsics/squaretolen/TestSquareToLen.java	Wed Feb 17 13:40:12 2016 +0300
     1.3 @@ -0,0 +1,114 @@
     1.4 +/*
     1.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/**
    1.29 + * @test
    1.30 + * @bug 8081778
    1.31 + * @summary Add C2 x86 intrinsic for BigInteger::squareToLen() method
    1.32 + *
    1.33 + * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch
    1.34 + *      -XX:CompileCommand=exclude,TestSquareToLen::main
    1.35 + *      -XX:CompileCommand=option,TestSquareToLen::base_multiply,ccstr,DisableIntrinsic,_squareToLen
    1.36 + *      -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_squareToLen
    1.37 + *      -XX:CompileCommand=option,java.math.BigInteger::square,ccstr,DisableIntrinsic,_squareToLen
    1.38 + *      -XX:CompileCommand=option,java.math.BigInteger::squareToLen,ccstr,DisableIntrinsic,_squareToLen
    1.39 + *      -XX:CompileCommand=inline,java.math.BigInteger::multiply
    1.40 + *      -XX:CompileCommand=inline,java.math.BigInteger::square
    1.41 + *      -XX:CompileCommand=inline,java.math.BigInteger::squareToLen TestSquareToLen
    1.42 + */
    1.43 +
    1.44 +import java.util.Random;
    1.45 +import java.math.*;
    1.46 +
    1.47 +public class TestSquareToLen {
    1.48 +
    1.49 +    // Avoid intrinsic by preventing inlining multiply() and squareToLen().
    1.50 +    public static BigInteger base_multiply(BigInteger op1) {
    1.51 +      return op1.multiply(op1);
    1.52 +    }
    1.53 +
    1.54 +    // Generate squareToLen() intrinsic by inlining multiply().
    1.55 +    public static BigInteger new_multiply(BigInteger op1) {
    1.56 +      return op1.multiply(op1);
    1.57 +    }
    1.58 +
    1.59 +    public static boolean bytecompare(BigInteger b1, BigInteger b2) {
    1.60 +      byte[] data1 = b1.toByteArray();
    1.61 +      byte[] data2 = b2.toByteArray();
    1.62 +      if (data1.length != data2.length)
    1.63 +        return false;
    1.64 +      for (int i = 0; i < data1.length; i++) {
    1.65 +        if (data1[i] != data2[i])
    1.66 +          return false;
    1.67 +      }
    1.68 +      return true;
    1.69 +    }
    1.70 +
    1.71 +    public static String stringify(BigInteger b) {
    1.72 +      String strout= "";
    1.73 +      byte [] data = b.toByteArray();
    1.74 +      for (int i = 0; i < data.length; i++) {
    1.75 +        strout += (String.format("%02x",data[i]) + " ");
    1.76 +      }
    1.77 +      return strout;
    1.78 +    }
    1.79 +
    1.80 +    public static void main(String args[]) throws Exception {
    1.81 +
    1.82 +      BigInteger oldsum = new BigInteger("0");
    1.83 +      BigInteger newsum = new BigInteger("0");
    1.84 +
    1.85 +      BigInteger b1, b2, oldres, newres;
    1.86 +
    1.87 +      Random rand = new Random();
    1.88 +      long seed = System.nanoTime();
    1.89 +      Random rand1 = new Random();
    1.90 +      long seed1 = System.nanoTime();
    1.91 +      rand.setSeed(seed);
    1.92 +      rand1.setSeed(seed1);
    1.93 +
    1.94 +      for (int j = 0; j < 100000; j++) {
    1.95 +        int rand_int = rand1.nextInt(3136)+32;
    1.96 +        b1 = new BigInteger(rand_int, rand);
    1.97 +
    1.98 +        oldres = base_multiply(b1);
    1.99 +        newres = new_multiply(b1);
   1.100 +
   1.101 +        oldsum = oldsum.add(oldres);
   1.102 +        newsum = newsum.add(newres);
   1.103 +
   1.104 +        if (!bytecompare(oldres,newres)) {
   1.105 +          System.out.print("mismatch for:b1:" + stringify(b1) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
   1.106 +          System.out.println(b1);
   1.107 +          throw new Exception("Failed");
   1.108 +        }
   1.109 +      }
   1.110 +      if (!bytecompare(oldsum,newsum))  {
   1.111 +        System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));
   1.112 +        throw new Exception("Failed");
   1.113 +      } else {
   1.114 +        System.out.println("Success");
   1.115 +      }
   1.116 +   }
   1.117 +}

mercurial