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