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