--- old/src/share/classes/java/util/zip/CRC32.java 2013-05-30 12:42:14.000000000 -0400 +++ new/src/share/classes/java/util/zip/CRC32.java 2013-05-30 12:42:14.000000000 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -55,7 +55,10 @@ * @param b the byte to update the checksum with */ public void update(int b) { - crc = update(crc, b); + int c = ~ crc; + b = timesXtoThe32[(b ^ c) & 0xFF]; + b = b ^ (c >>> 8); + crc = ~b; } /** @@ -68,7 +71,12 @@ if (off < 0 || len < 0 || off > b.length - len) { throw new ArrayIndexOutOfBoundsException(); } - crc = updateBytes(crc, b, off, len); + + if (len < javaCRCIfSmallerThan) { + crc = updateBytesSimple(crc, b, off, len); + } else { + crc = updateBytes(crc, b, off, len); + } } /** @@ -131,4 +139,44 @@ 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); + } }