Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/zip/TestCRC32C.java
41153 views
/*1* Copyright (c) 2015, 2017, 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 807358326* @summary C2 support for CRC32C on SPARC27*28* @run main/othervm/timeout=600 -Xbatch compiler.intrinsics.zip.TestCRC32C -m29*/3031package compiler.intrinsics.zip;3233import java.nio.ByteBuffer;34import java.util.zip.CRC32C;35import java.util.zip.Checksum;3637public class TestCRC32C {38// CRC32C (Castagnoli) polynomial39// coefficients in different forms40// normal: polyBits = 0x1edc6f41 = 0b0001 1110 1101 1100 0110 1111 0100 000141// reversed: polybits = 0x82f63b78 = 0b1000 0010 1111 0110 0011 1011 0111 100042// reversed reciprocal polybits = 0x8f6e37a0 = 0b1000 1111 0110 1110 0011 0111 1010 000043//44// 0 5 9 13 17 21 25 2945// | | | | | | | |46// reversed shiftL 1 polyBits = 0x105ec76f1L = 0b1 0000 0101 1110 1100 0111 0110 1111 000147final static long polyBits = (1L<<(32-32)) + (1L<<(32-28)) + (1L<<(32-27))48+ (1L<<(32-26)) + (1L<<(32-25)) + (1L<<(32-23)) + (1L<<(32-22))49+ (1L<<(32-20)) + (1L<<(32-19)) + (1L<<(32-18)) + (1L<<(32-14))50+ (1L<<(32-13)) + (1L<<(32-11)) + (1L<<(32-10)) + (1L<<(32-9))51+ (1L<<(32-8)) + (1L<<(32-6)) + (1L<<(32-0));52final static long polyBitsShifted = polyBits>>1;5354public static void main(String[] args) throws Exception {55int offset = Integer.getInteger("offset", 0);56int msgSize = Integer.getInteger("msgSize", 512);57boolean multi = false;58int iters = 20000;59int warmupIters = 20000;6061if (args.length > 0) {62if (args[0].equals("-m")) {63multi = true;64} else {65iters = Integer.valueOf(args[0]);66}67if (args.length > 1) {68warmupIters = Integer.valueOf(args[1]);69}70}7172if (multi) {73test_multi(warmupIters);74return;75}7677System.out.println(" offset = " + offset);78System.out.println("msgSize = " + msgSize + " bytes");79System.out.println(" iters = " + iters);8081byte[] b = initializedBytes(msgSize, offset);8283final long crcReference = update_byteLoop(0, b, offset);8485CRC32C crc0 = new CRC32C();86CRC32C crc1 = new CRC32C();87CRC32C crc2 = new CRC32C();8889crc0.update(b, offset, msgSize);90check(crc0, crcReference);9192System.out.println("-------------------------------------------------------");9394/* warm up */95for (int i = 0; i < warmupIters; i++) {96crc1.reset();97crc1.update(b, offset, msgSize);98check(crc1, crcReference);99}100101/* check correctness102* Do that before measuring performance103* to even better heat up involved methods.104*/105for (int i = 0; i < iters; i++) {106crc1.reset();107crc1.update(b, offset, msgSize);108check(crc1, crcReference);109}110report("CRCs", crc1, crcReference);111112/* measure performance113* Don't spoil times with error checking.114*/115long start = System.nanoTime();116for (int i = 0; i < iters; i++) {117crc1.reset();118crc1.update(b, offset, msgSize);119}120long end = System.nanoTime();121122double total = (double)(end - start)/1e9; // in seconds123double thruput = (double)msgSize*iters/1e6/total; // in MB/s124System.out.println("CRC32C.update(byte[]) runtime = " + total + " seconds");125System.out.println("CRC32C.update(byte[]) throughput = " + thruput + " MB/s");126report("CRCs", crc1, crcReference);127128System.out.println("-------------------------------------------------------");129130ByteBuffer buf = ByteBuffer.allocateDirect(msgSize);131buf.put(b, offset, msgSize);132buf.flip();133134/* warm up */135for (int i = 0; i < warmupIters; i++) {136crc2.reset();137crc2.update(buf);138buf.rewind();139check(crc2, crcReference);140}141142/* check correctness143* Do that before measuring performance144* to even better heat up involved methods.145*/146for (int i = 0; i < iters; i++) {147crc2.reset();148crc2.update(buf);149buf.rewind();150check(crc2, crcReference);151}152report("CRCs", crc2, crcReference);153154/* measure performance155* Don't spoil times with error checking.156*/157start = System.nanoTime();158for (int i = 0; i < iters; i++) {159crc2.reset();160crc2.update(buf);161buf.rewind();162}163end = System.nanoTime();164total = (double)(end - start)/1e9; // in seconds165thruput = (double)msgSize*iters/1e6/total; // in MB/s166System.out.println("CRC32C.update(ByteBuffer) runtime = " + total + " seconds");167System.out.println("CRC32C.update(ByteBuffer) throughput = " + thruput + " MB/s");168report("CRCs", crc2, crcReference);169170System.out.println("-------------------------------------------------------");171}172173// Just a loop over a byte array, updating the CRC byte by byte.174public static long update_byteLoop(long crc, byte[] buf, int offset) {175return update_byteLoop(crc, buf, offset, buf.length-offset);176}177178// Just a loop over a byte array, with given length, updating the CRC byte by byte.179public static long update_byteLoop(long crc, byte[] buf, int offset, int length) {180int end = length+offset;181for (int i = offset; i < end; i++) {182crc = update_singlebyte(crc, polyBitsShifted, buf[i]);183}184return crc;185}186187// Straight-forward implementation of CRC update by one byte.188// We use this very basic implementation to calculate reference189// results. It is necessary to have full control over how the190// reference results are calculated. It is not sufficient to rely191// on the interpreter (or c1, or c2) to do the right thing.192public static long update_singlebyte(long crc, long polynomial, int val) {193crc = (crc ^ -1L) & 0x00000000ffffffffL; // use 1's complement of crc194crc = crc ^ (val&0xff); // XOR in next byte from stream195for (int i = 0; i < 8; i++) {196boolean bitset = (crc & 0x01L) != 0;197198crc = crc>>1;199if (bitset) {200crc = crc ^ polynomial;201crc = crc & 0x00000000ffffffffL;202}203}204crc = (crc ^ -1L) & 0x00000000ffffffffL; // revert taking 1's complement205return crc;206}207208private static void report(String s, Checksum crc, long crcReference) {209System.out.printf("%s: crc = %08x, crcReference = %08x\n",210s, crc.getValue(), crcReference);211}212213private static void check(Checksum crc, long crcReference) throws Exception {214if (crc.getValue() != crcReference) {215System.err.printf("ERROR: crc = %08x, crcReference = %08x\n",216crc.getValue(), crcReference);217throw new Exception("TestCRC32C Error");218}219}220221private static byte[] initializedBytes(int M, int offset) {222byte[] bytes = new byte[M + offset];223for (int i = 0; i < offset; i++) {224bytes[i] = (byte) i;225}226for (int i = offset; i < bytes.length; i++) {227bytes[i] = (byte) (i - offset);228}229return bytes;230}231232private static void test_multi(int iters) throws Exception {233int len1 = 8; // the 8B/iteration loop234int len2 = 32; // the 32B/iteration loop235int len3 = 4096; // the 4KB/iteration loop236237byte[] b = initializedBytes(len3*16, 0);238int[] offsets = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 64, 128, 256, 512 };239int[] sizes = { 0, 1, 2, 3, 4, 5, 6, 7,240len1, len1+1, len1+2, len1+3, len1+4, len1+5, len1+6, len1+7,241len1*2, len1*2+1, len1*2+3, len1*2+5, len1*2+7,242len2, len2+1, len2+3, len2+5, len2+7,243len2*2, len2*4, len2*8, len2*16, len2*32, len2*64,244len3, len3+1, len3+3, len3+5, len3+7,245len3*2, len3*4, len3*8,246len1+len2, len1+len2+1, len1+len2+3, len1+len2+5, len1+len2+7,247len1+len3, len1+len3+1, len1+len3+3, len1+len3+5, len1+len3+7,248len2+len3, len2+len3+1, len2+len3+3, len2+len3+5, len2+len3+7,249len1+len2+len3, len1+len2+len3+1, len1+len2+len3+3,250len1+len2+len3+5, len1+len2+len3+7,251(len1+len2+len3)*2, (len1+len2+len3)*2+1, (len1+len2+len3)*2+3,252(len1+len2+len3)*2+5, (len1+len2+len3)*2+7,253(len1+len2+len3)*3, (len1+len2+len3)*3-1, (len1+len2+len3)*3-3,254(len1+len2+len3)*3-5, (len1+len2+len3)*3-7 };255CRC32C[] crc1 = new CRC32C[offsets.length*sizes.length];256long[] crcReference = new long[offsets.length*sizes.length];257int i, j, k;258259System.out.printf("testing %d cases ...\n", offsets.length*sizes.length);260261// Initialize CRC32C result arrays, CRC32C reference array.262// Reference is calculated using a very basic Java implementation.263for (i = 0; i < offsets.length; i++) {264for (j = 0; j < sizes.length; j++) {265crc1[i*sizes.length + j] = new CRC32C();266crcReference[i*sizes.length + j] = update_byteLoop(0, b, offsets[i], sizes[j]);267}268}269270// Warm up the JIT compiler. Over time, all methods involved will271// be executed by the interpreter, then get compiled by c1 and272// finally by c2. Each calculated CRC value must, in each iteration,273// be equal to the precalculated reference value for the test to pass.274for (k = 0; k < iters; k++) {275for (i = 0; i < offsets.length; i++) {276for (j = 0; j < sizes.length; j++) {277crc1[i*sizes.length + j].reset();278crc1[i*sizes.length + j].update(b, offsets[i], sizes[j]);279check(crc1[i*sizes.length + j], crcReference[i*sizes.length + j]);280}281}282}283}284}285286287