src/share/classes/java/util/zip/CRC32.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Cdiff src/share/classes/java/util/zip/CRC32.java

src/share/classes/java/util/zip/CRC32.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 23,32 **** --- 23,33 ---- * questions. */ package java.util.zip; + import java.lang.reflect.Field; import java.nio.ByteBuffer; import sun.nio.ch.DirectBuffer; /** * A class that can be used to compute the CRC-32 of a data stream.
*** 53,63 **** * eight bits of the argument b). * * @param b the byte to update the checksum with */ public void update(int b) { ! crc = update(crc, b); } /** * Updates the CRC-32 checksum with the specified array of bytes. */ --- 54,67 ---- * eight bits of the argument b). * * @param b the byte to update the checksum with */ public void update(int b) { ! int c = ~ crc; ! b = timesXtoThe32[(b ^ c) & 0xFF]; ! b = b ^ (c >>> 8); ! crc = ~b; } /** * Updates the CRC-32 checksum with the specified array of bytes. */
*** 66,77 **** --- 70,86 ---- throw new NullPointerException(); } if (off < 0 || len < 0 || off > b.length - len) { throw new ArrayIndexOutOfBoundsException(); } + + if (len < javaCRCIfSmallerThan) { + crc = updateBytesSimple(crc, b, off, len); + } else { crc = updateBytes(crc, b, off, len); } + } /** * Updates the CRC-32 checksum with the specified array of bytes. * * @param b the array of bytes to update the checksum with
*** 129,134 **** --- 138,183 ---- private native static int update(int crc, int b); private native static int updateBytes(int crc, byte[] b, int off, int len); private native static int updateByteBuffer(int adler, long addr, int off, int len); + + /** + * Simple byte-at-a-time CRC for avoiding the overhead of native call. + * Breakeven ranges between 60 and 100 bytes. + */ + private static int updateBytesSimple(int crc, byte[] b, int off, int len) { + int[] a = timesXtoThe32; + if (a.length < 256) + throw new ArrayIndexOutOfBoundsException(); + int c = ~crc; + for (int i = 0; i < len; i++ ) { + int x0 = b[i + off]; + x0 = a[(x0 ^ c) & 0xFF]; + c = x0 ^ (c >>> 8); + } + return ~c; + } + + /** + * Returns true if CLMUL is both requested and supported. + */ + private native static boolean init(int[] timesXtoThe32, boolean try_use_clmul); + + /** + * timesXtoThe32[a] = rep(poly(a)*x**32) + */ + static int[] timesXtoThe32; + + /** + * Estimated CRC size below which the JNI overhead is too large. + * May be modified depending on platform properties. + */ + static int javaCRCIfSmallerThan = 80; + + static { + timesXtoThe32 = new int[256]; + boolean try_use_clmul = + "true".equals(sun.misc.VM.getSavedProperty("sun.zip.clmulSupported")); + init(timesXtoThe32, try_use_clmul); + } }
src/share/classes/java/util/zip/CRC32.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File