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  * @run main/othervm/timeout=720 -Xint                         -Doffset=1 compiler.intrinsics.zip.TestCRC32
  31  * @run main/othervm/timeout=720 -Xcomp -XX:+TieredCompilation -Doffset=1 compiler.intrinsics.zip.TestCRC32
  32  * @run main/othervm/timeout=720 -Xcomp -XX:-TieredCompilation -Doffset=1 compiler.intrinsics.zip.TestCRC32
  33  */
  34 
  35 package compiler.intrinsics.zip;
  36 
  37 import java.nio.ByteBuffer;
  38 import java.util.zip.CRC32;
  39 import java.util.zip.Checksum;
  40 
  41 public class TestCRC32 {
  42     // standard CRC32 polynomial
  43     // coefficients in different forms
  44     // normal:              polyBits = 0x04c11db7   = 0b0000 0100 1100 0001 0001 1101 1011 0111
  45     // reversed:            polybits = 0xedb88320   = 0b1110 1101 1011 1000 1000 0011 0010 0000
  46     // reversed reciprocal  polybits = 0x82608edb   = 0b1000 0010 0110 0000 1000 1110 1101 1011
  47     //
  48     //                                                  0      5    9    13   17   21   25   29
  49     //                                                  |      |    |    |    |    |    |    |
  50     // reversed shiftL 1    polyBits = 0x1db710641L = 0b1 1101 1011 0111 0001 0000 0110 0100 0001
  51     final static long polyBits = (1L<<(32-32)) + (1L<<(32-26)) + (1L<<(32-23)) + (1L<<(32-22))
  52                                + (1L<<(32-16)) + (1L<<(32-12)) + (1L<<(32-11)) + (1L<<(32-10))
  53                                + (1L<<(32-8))  + (1L<<(32-7))  + (1L<<(32-5))  + (1L<<(32-4))
  54                                + (1L<<(32-2))  + (1L<<(32-1))  + (1L<<(32-0));
  55     final static long polyBitsShifted = polyBits>>1;
  56 
  57     public static void main(String[] args) {
  58         int offset = Integer.getInteger("offset", 0);
  59         int msgSize = Integer.getInteger("msgSize", 512);
  60         boolean multi = false;
  61         int iters = 20000;
  62         int warmupIters = 20000;
  63 
  64         if (args.length > 0) {
  65             if (args[0].equals("-m")) {
  66                 multi = true;
  67             } else {
  68                 iters = Integer.valueOf(args[0]);
  69             }
  70             if (args.length > 1) {
  71                 warmupIters = Integer.valueOf(args[1]);
  72             }
  73         }
  74 
  75         if (multi) {
  76             test_multi(warmupIters);
  77             return;
  78         }
  79 
  80         System.out.println(" offset = " + offset);
  81         System.out.println("msgSize = " + msgSize + " bytes");
  82         System.out.println("  iters = " + iters);
  83 
  84         byte[] b = initializedBytes(msgSize, offset);
  85 
  86         final long crc_reference = update_byteloop(0, b, offset);
  87 
  88         CRC32 crc0 = new CRC32();
  89         CRC32 crc1 = new CRC32();
  90         CRC32 crc2 = new CRC32();
  91 
  92         crc0.update(b, offset, msgSize);
  93         if (!check(crc0, crc_reference)) {
  94             System.out.println("CRC32: crc mismatch during initialization.");
  95             return;
  96         }
  97 
  98         System.out.println("-------------------------------------------------------");
  99 
 100         /* warm up */
 101         for (int i = 0; i < warmupIters; i++) {
 102             crc1.reset();
 103             crc1.update(b, offset, msgSize);
 104             if (!check(crc1, crc_reference)) {
 105                 System.out.println("CRC32: crc mismatch during warmup iteration " + i);
 106                 break;
 107             }
 108         }
 109 
 110         /* measure performance */
 111         long start = System.nanoTime();
 112         for (int i = 0; i < iters; i++) {
 113             crc1.reset();
 114             crc1.update(b, offset, msgSize);
 115         }
 116         long end = System.nanoTime();
 117         double total = (double)(end - start)/1e9;         // in seconds
 118         double thruput = (double)msgSize*iters/1e6/total; // in MB/s
 119         System.out.println("CRC32.update(byte[]) runtime = " + total + " seconds");
 120         System.out.println("CRC32.update(byte[]) throughput = " + thruput + " MB/s");
 121 
 122         /* check correctness */
 123         for (int i = 0; i < iters; i++) {
 124             crc1.reset();
 125             crc1.update(b, offset, msgSize);
 126             if (!check(crc0, crc1)) break;
 127         }
 128         report("CRCs", crc0, crc1);
 129 
 130         System.out.println("-------------------------------------------------------");
 131 
 132         ByteBuffer buf = ByteBuffer.allocateDirect(msgSize);
 133         buf.put(b, offset, msgSize);
 134         buf.flip();
 135 
 136         /* warm up */
 137         for (int i = 0; i < warmupIters; i++) {
 138             crc2.reset();
 139             crc2.update(buf);
 140             buf.rewind();
 141         }
 142 
 143         /* measure performance */
 144         start = System.nanoTime();
 145         for (int i = 0; i < iters; i++) {
 146             crc2.reset();
 147             crc2.update(buf);
 148             buf.rewind();
 149         }
 150         end = System.nanoTime();
 151         total = (double)(end - start)/1e9;         // in seconds
 152         thruput = (double)msgSize*iters/1e6/total; // in MB/s
 153         System.out.println("CRC32.update(ByteBuffer) runtime = " + total + " seconds");
 154         System.out.println("CRC32.update(ByteBuffer) throughput = " + thruput + " MB/s");
 155 
 156         /* check correctness */
 157         for (int i = 0; i < iters; i++) {
 158             crc2.reset();
 159             crc2.update(buf);
 160             buf.rewind();
 161             if (!check(crc0, crc2)) break;
 162         }
 163         report("CRCs", crc0, crc2);
 164 
 165         System.out.println("-------------------------------------------------------");
 166     }
 167 
 168     public static long update_byteloop(long crc, byte[] buf, int offset) {
 169         for (int i = offset; i < buf.length; i++) {
 170             crc = update_singlebyte(crc, polyBitsShifted, buf[i]);
 171         }
 172         return crc;
 173     }
 174 
 175     // Straight-forward implementation of CRC update by one byte.
 176     public static long update_singlebyte(long crc, long polynomial, int val) {
 177         crc = (crc ^ -1L) & 0x00000000ffffffffL;  // use 1's complement of crc
 178         crc =  crc ^ (val&0xff);                  // XOR in next byte from stream
 179         for (int i = 0; i <  8; i++) {
 180             boolean bitset = (crc & 0x01L) != 0;
 181 
 182             crc = crc>>1;
 183             if (bitset) {
 184                 crc = crc ^ polynomial;
 185                 crc = crc & 0x00000000ffffffffL;
 186             }
 187         }
 188         crc = (crc ^ -1L) & 0x00000000ffffffffL;  // revert taking 1's complement
 189         return crc;
 190     }
 191 
 192     private static void report(String s, Checksum crc0, Checksum crc1) {
 193         System.out.printf("%s: crc0 = %08x, crc1 = %08x\n",
 194                           s, crc0.getValue(), crc1.getValue());
 195     }
 196 
 197     private static boolean check(Checksum crc0, Checksum crc1) {
 198         if (crc0.getValue() != crc1.getValue()) {
 199             System.err.printf("ERROR: crc0 = %08x, crc1 = %08x\n",
 200                               crc0.getValue(), crc1.getValue());
 201             return false;
 202         }
 203         return true;
 204     }
 205 
 206     private static boolean check(Checksum crc0, long crc_reference) {
 207         if (crc0.getValue() != crc_reference) {
 208             System.err.printf("ERROR: crc0 = %08x, crc_reference = %08x\n",
 209                               crc0.getValue(), crc_reference);
 210             return false;
 211         }
 212         return true;
 213     }
 214 
 215     private static byte[] initializedBytes(int M, int offset) {
 216         byte[] bytes = new byte[M + offset];
 217         for (int i = 0; i < offset; i++) {
 218             bytes[i] = (byte) i;
 219         }
 220         for (int i = offset; i < bytes.length; i++) {
 221             bytes[i] = (byte) (i - offset);
 222         }
 223         return bytes;
 224     }
 225 
 226     private static void test_multi(int iters) {
 227         int len1 = 8;    // the  8B/iteration loop
 228         int len2 = 32;   // the 32B/iteration loop
 229         int len3 = 4096; // the 4KB/iteration loop
 230 
 231         byte[] b = initializedBytes(len3*16, 0);
 232         int[] offsets = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 64, 128, 256, 512 };
 233         int[] sizes = { 0, 1, 2, 3, 4, 5, 6, 7,
 234                         len1, len1+1, len1+2, len1+3, len1+4, len1+5, len1+6, len1+7,
 235                         len1*2, len1*2+1, len1*2+3, len1*2+5, len1*2+7,
 236                         len2, len2+1, len2+3, len2+5, len2+7,
 237                         len2*2, len2*4, len2*8, len2*16, len2*32, len2*64,
 238                         len3, len3+1, len3+3, len3+5, len3+7,
 239                         len3*2, len3*4, len3*8,
 240                         len1+len2, len1+len2+1, len1+len2+3, len1+len2+5, len1+len2+7,
 241                         len1+len3, len1+len3+1, len1+len3+3, len1+len3+5, len1+len3+7,
 242                         len2+len3, len2+len3+1, len2+len3+3, len2+len3+5, len2+len3+7,
 243                         len1+len2+len3, len1+len2+len3+1, len1+len2+len3+3,
 244                         len1+len2+len3+5, len1+len2+len3+7,
 245                         (len1+len2+len3)*2, (len1+len2+len3)*2+1, (len1+len2+len3)*2+3,
 246                         (len1+len2+len3)*2+5, (len1+len2+len3)*2+7,
 247                         (len1+len2+len3)*3, (len1+len2+len3)*3-1, (len1+len2+len3)*3-3,
 248                         (len1+len2+len3)*3-5, (len1+len2+len3)*3-7 };
 249         CRC32[] crc0 = new CRC32[offsets.length*sizes.length];
 250         CRC32[] crc1 = new CRC32[offsets.length*sizes.length];
 251         int i, j, k;
 252 
 253         System.out.printf("testing %d cases ...\n", offsets.length*sizes.length);
 254 
 255         /* set the result from interpreter as reference */
 256         for (i = 0; i < offsets.length; i++) {
 257             for (j = 0; j < sizes.length; j++) {
 258                 crc0[i*sizes.length + j] = new CRC32();
 259                 crc1[i*sizes.length + j] = new CRC32();
 260                 crc0[i*sizes.length + j].update(b, offsets[i], sizes[j]);
 261             }
 262         }
 263 
 264         /* warm up the JIT compiler and get result */
 265         for (k = 0; k < iters; k++) {
 266             for (i = 0; i < offsets.length; i++) {
 267                 for (j = 0; j < sizes.length; j++) {
 268                     crc1[i*sizes.length + j].reset();
 269                     crc1[i*sizes.length + j].update(b, offsets[i], sizes[j]);
 270                 }
 271             }
 272         }
 273 
 274         /* check correctness */
 275         for (i = 0; i < offsets.length; i++) {
 276             for (j = 0; j < sizes.length; j++) {
 277                 if (!check(crc0[i*sizes.length + j], crc1[i*sizes.length + j])) {
 278                     System.out.printf("offsets[%d] = %d", i, offsets[i]);
 279                     System.out.printf("\tsizes[%d] = %d\n", j, sizes[j]);
 280                 }
 281             }
 282         }
 283     }
 284 }