1 /*
   2  * Copyright (c) 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 8143012
  27  * @summary CRC32 Intrinsics support on SPARC
  28  *
  29  * @run main/othervm/timeout=720 -Xbatch compiler.intrinsics.zip.TestCRC32 -m
  30  */
  31 
  32 package compiler.intrinsics.zip;
  33 
  34 import java.nio.ByteBuffer;
  35 import java.util.zip.CRC32;
  36 import java.util.zip.Checksum;
  37 
  38 public class TestCRC32 {
  39     // standard CRC32 polynomial
  40     // coefficients in different forms
  41     // normal:              polyBits = 0x04c11db7   = 0b0000 0100 1100 0001 0001 1101 1011 0111
  42     // reversed:            polybits = 0xedb88320   = 0b1110 1101 1011 1000 1000 0011 0010 0000
  43     // reversed reciprocal  polybits = 0x82608edb   = 0b1000 0010 0110 0000 1000 1110 1101 1011
  44     //
  45     //                                                  0      5    9    13   17   21   25   29
  46     //                                                  |      |    |    |    |    |    |    |
  47     // reversed shiftL 1    polyBits = 0x1db710641L = 0b1 1101 1011 0111 0001 0000 0110 0100 0001
  48     final static long polyBits = (1L<<(32-32)) + (1L<<(32-26)) + (1L<<(32-23)) + (1L<<(32-22))
  49                                + (1L<<(32-16)) + (1L<<(32-12)) + (1L<<(32-11)) + (1L<<(32-10))
  50                                + (1L<<(32-8))  + (1L<<(32-7))  + (1L<<(32-5))  + (1L<<(32-4))
  51                                + (1L<<(32-2))  + (1L<<(32-1))  + (1L<<(32-0));
  52     final static long polyBitsShifted = polyBits>>1;
  53 
  54     public static void main(String[] args) throws Exception {
  55         int offset = Integer.getInteger("offset", 0);
  56         int msgSize = Integer.getInteger("msgSize", 512);
  57         boolean multi = false;
  58         int iters = 20000;
  59         int warmupIters = 20000;
  60 
  61         if (args.length > 0) {
  62             if (args[0].equals("-m")) {
  63                 multi = true;
  64             } else {
  65                 iters = Integer.valueOf(args[0]);
  66             }
  67             if (args.length > 1) {
  68                 warmupIters = Integer.valueOf(args[1]);
  69             }
  70         }
  71 
  72         if (multi) {
  73             test_multi(warmupIters);
  74             return;
  75         }
  76 
  77         System.out.println(" offset = " + offset);
  78         System.out.println("msgSize = " + msgSize + " bytes");
  79         System.out.println("  iters = " + iters);
  80 
  81         byte[] b = initializedBytes(msgSize, offset);
  82 
  83         final long crcReference = update_byteLoop(0, b, offset);
  84 
  85         CRC32 crc0 = new CRC32();
  86         CRC32 crc1 = new CRC32();
  87         CRC32 crc2 = new CRC32();
  88 
  89         crc0.update(b, offset, msgSize);
  90         if (!check(crc0, crcReference)) {
  91             System.out.println("CRC32: crc mismatch during initialization.");
  92             throw new Exception("TestCRC32 Error");
  93         }
  94 
  95         System.out.println("-------------------------------------------------------");
  96 
  97         /* warm up */
  98         for (int i = 0; i < warmupIters; i++) {
  99             crc1.reset();
 100             crc1.update(b, offset, msgSize);
 101             if (!check(crc1, crcReference)) {
 102                 System.out.println("CRC32: crc mismatch during warmup iteration " + i);
 103                 throw new Exception("TestCRC32 Error");
 104             }
 105         }
 106 
 107         /* check correctness
 108          * Do that before measuring performance
 109          * to even better heat up involved methods.
 110          */
 111         for (int i = 0; i < iters; i++) {
 112             crc1.reset();
 113             crc1.update(b, offset, msgSize);
 114             if (!check(crc1, crcReference)) {
 115                 System.out.println("CRC32: crc mismatch during check iteration " + i);
 116                 throw new Exception("TestCRC32 Error");
 117             }
 118         }
 119         report("CRCs", crc1, crcReference);
 120 
 121         /* measure performance
 122          * Don't spoil times with error checking.
 123          */
 124         long start = System.nanoTime();
 125         for (int i = 0; i < iters; i++) {
 126             crc1.reset();
 127             crc1.update(b, offset, msgSize);
 128         }
 129         long end = System.nanoTime();
 130 
 131         double total = (double)(end - start)/1e9;         // in seconds
 132         double thruput = (double)msgSize*iters/1e6/total; // in MB/s
 133         System.out.println("CRC32.update(byte[]) runtime = " + total + " seconds");
 134         System.out.println("CRC32.update(byte[]) throughput = " + thruput + " MB/s");
 135         report("CRCs", crc1, crcReference);
 136 
 137         System.out.println("-------------------------------------------------------");
 138 
 139         ByteBuffer buf = ByteBuffer.allocateDirect(msgSize);
 140         buf.put(b, offset, msgSize);
 141         buf.flip();
 142 
 143         /* warm up */
 144         for (int i = 0; i < warmupIters; i++) {
 145             crc2.reset();
 146             crc2.update(buf);
 147             buf.rewind();
 148             if (!check(crc2, crcReference)) {
 149                 System.out.println("CRC32: crc2 mismatch during warmup iteration " + i);
 150                 throw new Exception("TestCRC32 Error");
 151             }
 152         }
 153 
 154         /* check correctness
 155          * Do that before measuring performance
 156          * to even better heat up involved methods.
 157          */
 158         for (int i = 0; i < iters; i++) {
 159             crc2.reset();
 160             crc2.update(buf);
 161             buf.rewind();
 162             if (!check(crc2, crcReference)) {
 163                 System.out.println("CRC32: crc2 mismatch during check iteration " + i);
 164                 throw new Exception("TestCRC32 Error");
 165             }
 166         }
 167         report("CRCs", crc2, crcReference);
 168 
 169         /* measure performance
 170          * Don't spoil times with error checking.
 171          */
 172         start = System.nanoTime();
 173         for (int i = 0; i < iters; i++) {
 174             crc2.reset();
 175             crc2.update(buf);
 176             buf.rewind();
 177         }
 178         end = System.nanoTime();
 179         total = (double)(end - start)/1e9;         // in seconds
 180         thruput = (double)msgSize*iters/1e6/total; // in MB/s
 181         System.out.println("CRC32.update(ByteBuffer) runtime = " + total + " seconds");
 182         System.out.println("CRC32.update(ByteBuffer) throughput = " + thruput + " MB/s");
 183         report("CRCs", crc2, crcReference);
 184 
 185         System.out.println("-------------------------------------------------------");
 186     }
 187 
 188     // Just a loop over a byte array, updating the CRC byte by byte.
 189     public static long update_byteLoop(long crc, byte[] buf, int offset) {
 190         return update_byteLoop(crc, buf, offset, buf.length-offset);
 191     }
 192 
 193     // Just a loop over a byte array, with given length, updating the CRC byte by byte.
 194     public static long update_byteLoop(long crc, byte[] buf, int offset, int length) {
 195         int end = length+offset;
 196         for (int i = offset; i < end; i++) {
 197             crc = update_singlebyte(crc, polyBitsShifted, buf[i]);
 198         }
 199         return crc;
 200     }
 201 
 202     // Straight-forward implementation of CRC update by one byte.
 203     // We use this very basic implementation to calculate reference
 204     // results. It is necessary to have full control over how the
 205     // reference results are calculated. It is not sufficient to rely
 206     // on the interpreter (or c1, or c2) to do the right thing.
 207     public static long update_singlebyte(long crc, long polynomial, int val) {
 208         crc = (crc ^ -1L) & 0x00000000ffffffffL;  // use 1's complement of crc
 209         crc =  crc ^ (val&0xff);                  // XOR in next byte from stream
 210         for (int i = 0; i <  8; i++) {
 211             boolean bitset = (crc & 0x01L) != 0;
 212 
 213             crc = crc>>1;
 214             if (bitset) {
 215                 crc = crc ^ polynomial;
 216                 crc = crc & 0x00000000ffffffffL;
 217             }
 218         }
 219         crc = (crc ^ -1L) & 0x00000000ffffffffL;  // revert taking 1's complement
 220         return crc;
 221     }
 222 
 223     private static void report(String s, Checksum crc, long crcReference) {
 224         System.out.printf("%s: crc = %08x, crcReference = %08x\n",
 225                           s, crc.getValue(), crcReference);
 226     }
 227 
 228     private static boolean check(Checksum crc, long crcReference) {
 229         if (crc.getValue() != crcReference) {
 230             System.err.printf("ERROR: crc = %08x, crcReference = %08x\n",
 231                               crc.getValue(), crcReference);
 232             return false;
 233         }
 234         return true;
 235     }
 236 
 237     private static byte[] initializedBytes(int M, int offset) {
 238         byte[] bytes = new byte[M + offset];
 239         for (int i = 0; i < offset; i++) {
 240             bytes[i] = (byte) i;
 241         }
 242         for (int i = offset; i < bytes.length; i++) {
 243             bytes[i] = (byte) (i - offset);
 244         }
 245         return bytes;
 246     }
 247 
 248     private static void test_multi(int iters) throws Exception {
 249         int len1 = 8;    // the  8B/iteration loop
 250         int len2 = 32;   // the 32B/iteration loop
 251         int len3 = 4096; // the 4KB/iteration loop
 252 
 253         byte[] b = initializedBytes(len3*16, 0);
 254         int[] offsets = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 64, 128, 256, 512 };
 255         int[] sizes = { 0, 1, 2, 3, 4, 5, 6, 7,
 256                         len1, len1+1, len1+2, len1+3, len1+4, len1+5, len1+6, len1+7,
 257                         len1*2, len1*2+1, len1*2+3, len1*2+5, len1*2+7,
 258                         len2, len2+1, len2+3, len2+5, len2+7,
 259                         len2*2, len2*4, len2*8, len2*16, len2*32, len2*64,
 260                         len3, len3+1, len3+3, len3+5, len3+7,
 261                         len3*2, len3*4, len3*8,
 262                         len1+len2, len1+len2+1, len1+len2+3, len1+len2+5, len1+len2+7,
 263                         len1+len3, len1+len3+1, len1+len3+3, len1+len3+5, len1+len3+7,
 264                         len2+len3, len2+len3+1, len2+len3+3, len2+len3+5, len2+len3+7,
 265                         len1+len2+len3, len1+len2+len3+1, len1+len2+len3+3,
 266                         len1+len2+len3+5, len1+len2+len3+7,
 267                         (len1+len2+len3)*2, (len1+len2+len3)*2+1, (len1+len2+len3)*2+3,
 268                         (len1+len2+len3)*2+5, (len1+len2+len3)*2+7,
 269                         (len1+len2+len3)*3, (len1+len2+len3)*3-1, (len1+len2+len3)*3-3,
 270                         (len1+len2+len3)*3-5, (len1+len2+len3)*3-7 };
 271         CRC32[] crc1 = new CRC32[offsets.length*sizes.length];
 272         long[] crcReference = new long[offsets.length*sizes.length];
 273         int i, j, k;
 274 
 275         System.out.printf("testing %d cases ...\n", offsets.length*sizes.length);
 276 
 277         try {
 278             // Initialize CRC32 result arrays, CRC32 reference array.
 279             // Reference is calculated using a very basic Java implementation.
 280             for (i = 0; i < offsets.length; i++) {
 281                 for (j = 0; j < sizes.length; j++) {
 282                     crc1[i*sizes.length + j] = new CRC32();
 283                     crcReference[i*sizes.length + j] = update_byteLoop(0, b, offsets[i], sizes[j]);
 284                 }
 285             }
 286 
 287             // Warm up the JIT compiler. Over time, all methods involved will
 288             // be executed by the interpreter, then get compiled by c1 and
 289             // finally by c2. Each calculated CRC value must, in each iteration,
 290             // be equal to the precalculated reference value for the test to pass.
 291             for (k = 0; k < iters; k++) {
 292                 for (i = 0; i < offsets.length; i++) {
 293                     for (j = 0; j < sizes.length; j++) {
 294                         crc1[i*sizes.length + j].reset();
 295                         crc1[i*sizes.length + j].update(b, offsets[i], sizes[j]);
 296 
 297                         if (!check(crc1[i*sizes.length + j], crcReference[i*sizes.length + j])) {
 298                             System.out.printf("iteration %d:", k);
 299                             System.out.printf("\toffsets[%d] = %d", i, offsets[i]);
 300                             System.out.printf("\tsizes[%d]   = %d\n", j, sizes[j]);
 301                             throw new Exception("TestCRC32 Error");
 302                         }
 303                     }
 304                 }
 305             }
 306         } catch (Exception e) {
 307             System.out.println("Exception: " + e);
 308             //System.exit(1);
 309             throw new Exception(e);
 310         }
 311     }
 312 }