1 /*
2  * Copyright (c) 2015, 2017, 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 
main(String[] args)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         check(crc0, crcReference);
92 
93         System.out.println("-------------------------------------------------------");
94 
95         /* warm up */
96         for (int i = 0; i < warmupIters; i++) {
97             crc1.reset();
98             crc1.update(b, offset, msgSize);
99             check(crc1, crcReference);
100         }
101 
102         /* check correctness
103          * Do that before measuring performance
104          * to even better heat up involved methods.
105          */
106         for (int i = 0; i < iters; i++) {
107             crc1.reset();
108             crc1.update(b, offset, msgSize);
109             check(crc1, crcReference);
110         }
111         report("CRCs", crc1, crcReference);
112 
113         /* measure performance
114          * Don't spoil times with error checking.
115          */
116         long start = System.nanoTime();
117         for (int i = 0; i < iters; i++) {
118             crc1.reset();
119             crc1.update(b, offset, msgSize);
120         }
121         long end = System.nanoTime();
122 
123         double total = (double)(end - start)/1e9;         // in seconds
124         double thruput = (double)msgSize*iters/1e6/total; // in MB/s
125         System.out.println("CRC32C.update(byte[]) runtime = " + total + " seconds");
126         System.out.println("CRC32C.update(byte[]) throughput = " + thruput + " MB/s");
127         report("CRCs", crc1, crcReference);
128 
129         System.out.println("-------------------------------------------------------");
130 
131         ByteBuffer buf = ByteBuffer.allocateDirect(msgSize);
132         buf.put(b, offset, msgSize);
133         buf.flip();
134 
135         /* warm up */
136         for (int i = 0; i < warmupIters; i++) {
137             crc2.reset();
138             crc2.update(buf);
139             buf.rewind();
140             check(crc2, crcReference);
141         }
142 
143         /* check correctness
144          * Do that before measuring performance
145          * to even better heat up involved methods.
146          */
147         for (int i = 0; i < iters; i++) {
148             crc2.reset();
149             crc2.update(buf);
150             buf.rewind();
151             check(crc2, crcReference);
152         }
153         report("CRCs", crc2, crcReference);
154 
155         /* measure performance
156          * Don't spoil times with error checking.
157          */
158         start = System.nanoTime();
159         for (int i = 0; i < iters; i++) {
160             crc2.reset();
161             crc2.update(buf);
162             buf.rewind();
163         }
164         end = System.nanoTime();
165         total = (double)(end - start)/1e9;         // in seconds
166         thruput = (double)msgSize*iters/1e6/total; // in MB/s
167         System.out.println("CRC32C.update(ByteBuffer) runtime = " + total + " seconds");
168         System.out.println("CRC32C.update(ByteBuffer) throughput = " + thruput + " MB/s");
169         report("CRCs", crc2, crcReference);
170 
171         System.out.println("-------------------------------------------------------");
172     }
173 
174     // Just a loop over a byte array, updating the CRC byte by byte.
update_byteLoop(long crc, byte[] buf, int offset)175     public static long update_byteLoop(long crc, byte[] buf, int offset) {
176         return update_byteLoop(crc, buf, offset, buf.length-offset);
177     }
178 
179     // Just a loop over a byte array, with given length, updating the CRC byte by byte.
update_byteLoop(long crc, byte[] buf, int offset, int length)180     public static long update_byteLoop(long crc, byte[] buf, int offset, int length) {
181         int end = length+offset;
182         for (int i = offset; i < end; i++) {
183             crc = update_singlebyte(crc, polyBitsShifted, buf[i]);
184         }
185         return crc;
186     }
187 
188     // Straight-forward implementation of CRC update by one byte.
189     // We use this very basic implementation to calculate reference
190     // results. It is necessary to have full control over how the
191     // reference results are calculated. It is not sufficient to rely
192     // on the interpreter (or c1, or c2) to do the right thing.
update_singlebyte(long crc, long polynomial, int val)193     public static long update_singlebyte(long crc, long polynomial, int val) {
194         crc = (crc ^ -1L) & 0x00000000ffffffffL;  // use 1's complement of crc
195         crc =  crc ^ (val&0xff);                  // XOR in next byte from stream
196         for (int i = 0; i <  8; i++) {
197             boolean bitset = (crc & 0x01L) != 0;
198 
199             crc = crc>>1;
200             if (bitset) {
201                 crc = crc ^ polynomial;
202                 crc = crc & 0x00000000ffffffffL;
203             }
204         }
205         crc = (crc ^ -1L) & 0x00000000ffffffffL;  // revert taking 1's complement
206         return crc;
207     }
208 
report(String s, Checksum crc, long crcReference)209     private static void report(String s, Checksum crc, long crcReference) {
210         System.out.printf("%s: crc = %08x, crcReference = %08x\n",
211                           s, crc.getValue(), crcReference);
212     }
213 
check(Checksum crc, long crcReference)214     private static void check(Checksum crc, long crcReference) throws Exception {
215         if (crc.getValue() != crcReference) {
216             System.err.printf("ERROR: crc = %08x, crcReference = %08x\n",
217                               crc.getValue(), crcReference);
218             throw new Exception("TestCRC32C Error");
219         }
220     }
221 
initializedBytes(int M, int offset)222     private static byte[] initializedBytes(int M, int offset) {
223         byte[] bytes = new byte[M + offset];
224         for (int i = 0; i < offset; i++) {
225             bytes[i] = (byte) i;
226         }
227         for (int i = offset; i < bytes.length; i++) {
228             bytes[i] = (byte) (i - offset);
229         }
230         return bytes;
231     }
232 
test_multi(int iters)233     private static void test_multi(int iters) throws Exception {
234         int len1 = 8;    // the  8B/iteration loop
235         int len2 = 32;   // the 32B/iteration loop
236         int len3 = 4096; // the 4KB/iteration loop
237 
238         byte[] b = initializedBytes(len3*16, 0);
239         int[] offsets = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 64, 128, 256, 512 };
240         int[] sizes = { 0, 1, 2, 3, 4, 5, 6, 7,
241                         len1, len1+1, len1+2, len1+3, len1+4, len1+5, len1+6, len1+7,
242                         len1*2, len1*2+1, len1*2+3, len1*2+5, len1*2+7,
243                         len2, len2+1, len2+3, len2+5, len2+7,
244                         len2*2, len2*4, len2*8, len2*16, len2*32, len2*64,
245                         len3, len3+1, len3+3, len3+5, len3+7,
246                         len3*2, len3*4, len3*8,
247                         len1+len2, len1+len2+1, len1+len2+3, len1+len2+5, len1+len2+7,
248                         len1+len3, len1+len3+1, len1+len3+3, len1+len3+5, len1+len3+7,
249                         len2+len3, len2+len3+1, len2+len3+3, len2+len3+5, len2+len3+7,
250                         len1+len2+len3, len1+len2+len3+1, len1+len2+len3+3,
251                         len1+len2+len3+5, len1+len2+len3+7,
252                         (len1+len2+len3)*2, (len1+len2+len3)*2+1, (len1+len2+len3)*2+3,
253                         (len1+len2+len3)*2+5, (len1+len2+len3)*2+7,
254                         (len1+len2+len3)*3, (len1+len2+len3)*3-1, (len1+len2+len3)*3-3,
255                         (len1+len2+len3)*3-5, (len1+len2+len3)*3-7 };
256         CRC32C[] crc1 = new CRC32C[offsets.length*sizes.length];
257         long[] crcReference = new long[offsets.length*sizes.length];
258         int i, j, k;
259 
260         System.out.printf("testing %d cases ...\n", offsets.length*sizes.length);
261 
262         // Initialize CRC32C result arrays, CRC32C reference array.
263         // Reference is calculated using a very basic Java implementation.
264         for (i = 0; i < offsets.length; i++) {
265             for (j = 0; j < sizes.length; j++) {
266                 crc1[i*sizes.length + j] = new CRC32C();
267                 crcReference[i*sizes.length + j] = update_byteLoop(0, b, offsets[i], sizes[j]);
268             }
269         }
270 
271         // Warm up the JIT compiler. Over time, all methods involved will
272         // be executed by the interpreter, then get compiled by c1 and
273         // finally by c2. Each calculated CRC value must, in each iteration,
274         // be equal to the precalculated reference value for the test to pass.
275         for (k = 0; k < iters; k++) {
276             for (i = 0; i < offsets.length; i++) {
277                 for (j = 0; j < sizes.length; j++) {
278                     crc1[i*sizes.length + j].reset();
279                     crc1[i*sizes.length + j].update(b, offsets[i], sizes[j]);
280                     check(crc1[i*sizes.length + j], crcReference[i*sizes.length + j]);
281                 }
282             }
283         }
284     }
285 }
286