1 /*
2  * Copyright (c) 2013, 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 7088419
27  * @summary Use x86 Hardware CRC32 Instruction with java.util.zip.CRC32 and java.util.zip.Adler32
28  *
29  * @run main compiler.codegen.CRCTest
30  */
31 
32 package compiler.codegen;
33 
34 import java.nio.ByteBuffer;
35 import java.util.zip.CRC32;
36 import java.util.zip.Checksum;
37 
38 public class CRCTest {
39 
main(String[] args)40     public static void main(String[] args) throws Exception {
41 
42         byte[] b = initializedBytes(4096 * 4096);
43 
44         {
45             CRC32 crc1 = new CRC32();
46             CRC32 crc2 = new CRC32();
47             CRC32 crc3 = new CRC32();
48             CRC32 crc4 = new CRC32();
49 
50             crc1.update(b, 0, b.length);
51             updateSerial(crc2, b, 0, b.length);
52             updateDirect(crc3, b, 0, b.length);
53             updateSerialSlow(crc4, b, 0, b.length);
54 
55             check(crc1, crc2);
56             check(crc3, crc4);
57             check(crc1, crc3);
58 
59             crc1.update(17);
60             crc2.update(17);
61             crc3.update(17);
62             crc4.update(17);
63 
64             crc1.update(b, 1, b.length-2);
65             updateSerial(crc2, b, 1, b.length-2);
66             updateDirect(crc3, b, 1, b.length-2);
67             updateSerialSlow(crc4, b, 1, b.length-2);
68 
69             check(crc1, crc2);
70             check(crc3, crc4);
71             check(crc1, crc3);
72 
73             report("finished huge crc", crc1, crc2, crc3, crc4);
74 
75             for (int i = 0; i < 256; i++) {
76                 for (int j = 0; j < 256; j += 1) {
77                     crc1.update(b, i, j);
78                     updateSerial(crc2, b, i, j);
79                     updateDirect(crc3, b, i, j);
80                     updateSerialSlow(crc4, b, i, j);
81 
82                     check(crc1, crc2);
83                     check(crc3, crc4);
84                     check(crc1, crc3);
85 
86                 }
87             }
88 
89             report("finished small survey crc", crc1, crc2, crc3, crc4);
90         }
91 
92     }
93 
report(String s, Checksum crc1, Checksum crc2, Checksum crc3, Checksum crc4)94     private static void report(String s, Checksum crc1, Checksum crc2,
95             Checksum crc3, Checksum crc4) {
96         System.out.println(s + ", crc1 = " + crc1.getValue() +
97                 ", crc2 = " + crc2.getValue()+
98                 ", crc3 = " + crc3.getValue()+
99                 ", crc4 = " + crc4.getValue());
100     }
101 
check(Checksum crc1, Checksum crc2)102     private static void check(Checksum crc1, Checksum crc2) throws Exception {
103         if (crc1.getValue() != crc2.getValue()) {
104             String s = "value 1 = " + crc1.getValue() + ", value 2 = " + crc2.getValue();
105             System.err.println(s);
106             throw new Exception(s);
107         }
108     }
109 
initializedBytes(int M)110     private static byte[] initializedBytes(int M) {
111         byte[] bytes = new byte[M];
112         for (int i = 0; i < bytes.length; i++) {
113             bytes[i] = (byte) i;
114         }
115         return bytes;
116     }
117 
updateSerial(Checksum crc, byte[] b, int start, int length)118     private static void updateSerial(Checksum crc, byte[] b, int start, int length) {
119         for (int i = 0; i < length; i++)
120             crc.update(b[i+start]);
121     }
122 
updateSerialSlow(Checksum crc, byte[] b, int start, int length)123     private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) {
124         for (int i = 0; i < length; i++)
125             crc.update(b[i+start]);
126         crc.getValue();
127     }
128 
updateDirect(CRC32 crc3, byte[] b, int start, int length)129     private static void updateDirect(CRC32 crc3, byte[] b, int start, int length) {
130         ByteBuffer buf = ByteBuffer.allocateDirect(length);
131         buf.put(b, start, length);
132         buf.flip();
133         crc3.update(buf);
134     }
135 }
136