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