Path: blob/master/test/hotspot/jtreg/compiler/codegen/CRCTest.java
41149 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 708841926* @summary Use x86 Hardware CRC32 Instruction with java.util.zip.CRC32 and java.util.zip.Adler3227*28* @run main compiler.codegen.CRCTest29*/3031package compiler.codegen;3233import java.nio.ByteBuffer;34import java.util.zip.CRC32;35import java.util.zip.Checksum;3637public class CRCTest {3839public static void main(String[] args) throws Exception {4041byte[] b = initializedBytes(4096 * 4096);4243{44CRC32 crc1 = new CRC32();45CRC32 crc2 = new CRC32();46CRC32 crc3 = new CRC32();47CRC32 crc4 = new CRC32();4849crc1.update(b, 0, b.length);50updateSerial(crc2, b, 0, b.length);51updateDirect(crc3, b, 0, b.length);52updateSerialSlow(crc4, b, 0, b.length);5354check(crc1, crc2);55check(crc3, crc4);56check(crc1, crc3);5758crc1.update(17);59crc2.update(17);60crc3.update(17);61crc4.update(17);6263crc1.update(b, 1, b.length-2);64updateSerial(crc2, b, 1, b.length-2);65updateDirect(crc3, b, 1, b.length-2);66updateSerialSlow(crc4, b, 1, b.length-2);6768check(crc1, crc2);69check(crc3, crc4);70check(crc1, crc3);7172report("finished huge crc", crc1, crc2, crc3, crc4);7374for (int i = 0; i < 256; i++) {75for (int j = 0; j < 256; j += 1) {76crc1.update(b, i, j);77updateSerial(crc2, b, i, j);78updateDirect(crc3, b, i, j);79updateSerialSlow(crc4, b, i, j);8081check(crc1, crc2);82check(crc3, crc4);83check(crc1, crc3);8485}86}8788report("finished small survey crc", crc1, crc2, crc3, crc4);89}9091}9293private static void report(String s, Checksum crc1, Checksum crc2,94Checksum crc3, Checksum crc4) {95System.out.println(s + ", crc1 = " + crc1.getValue() +96", crc2 = " + crc2.getValue()+97", crc3 = " + crc3.getValue()+98", crc4 = " + crc4.getValue());99}100101private static void check(Checksum crc1, Checksum crc2) throws Exception {102if (crc1.getValue() != crc2.getValue()) {103String s = "value 1 = " + crc1.getValue() + ", value 2 = " + crc2.getValue();104System.err.println(s);105throw new Exception(s);106}107}108109private static byte[] initializedBytes(int M) {110byte[] bytes = new byte[M];111for (int i = 0; i < bytes.length; i++) {112bytes[i] = (byte) i;113}114return bytes;115}116117private static void updateSerial(Checksum crc, byte[] b, int start, int length) {118for (int i = 0; i < length; i++)119crc.update(b[i+start]);120}121122private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) {123for (int i = 0; i < length; i++)124crc.update(b[i+start]);125crc.getValue();126}127128private static void updateDirect(CRC32 crc3, byte[] b, int start, int length) {129ByteBuffer buf = ByteBuffer.allocateDirect(length);130buf.put(b, start, length);131buf.flip();132crc3.update(buf);133}134}135136137