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 8143012 27 * @summary CRC32 Intrinsics support on SPARC 28 * 29 * @run main/othervm/timeout=720 -Xbatch compiler.intrinsics.zip.TestCRC32 -m 30 */ 31 32 package compiler.intrinsics.zip; 33 34 import java.nio.ByteBuffer; 35 import java.util.zip.CRC32; 36 import java.util.zip.Checksum; 37 38 public class TestCRC32 { 39 public static void main(String[] args) { 40 int offset = Integer.getInteger("offset", 0); 41 int msgSize = Integer.getInteger("msgSize", 512); 42 boolean multi = false; 43 int iters = 20000; 44 int warmupIters = 20000; 45 46 if (args.length > 0) { 47 if (args[0].equals("-m")) { 48 multi = true; 49 } else { 50 iters = Integer.valueOf(args[0]); 51 } 52 if (args.length > 1) { 53 warmupIters = Integer.valueOf(args[1]); 54 } 55 } 56 57 if (multi) { 58 test_multi(warmupIters); 59 return; 60 } 61 62 System.out.println(" offset = " + offset); 63 System.out.println("msgSize = " + msgSize + " bytes"); 64 System.out.println(" iters = " + iters); 65 66 byte[] b = initializedBytes(msgSize, offset); 67 68 CRC32 crc0 = new CRC32(); 69 CRC32 crc1 = new CRC32(); 70 CRC32 crc2 = new CRC32(); 71 72 crc0.update(b, offset, msgSize); 73 74 System.out.println("-------------------------------------------------------"); 75 76 /* warm up */ 77 for (int i = 0; i < warmupIters; i++) { 78 crc1.reset(); 79 crc1.update(b, offset, msgSize); 80 } 81 82 /* measure performance */ 83 long start = System.nanoTime(); 84 for (int i = 0; i < iters; i++) { 85 crc1.reset(); 86 crc1.update(b, offset, msgSize); 87 } 88 long end = System.nanoTime(); 89 double total = (double)(end - start)/1e9; // in seconds 90 double thruput = (double)msgSize*iters/1e6/total; // in MB/s 91 System.out.println("CRC32.update(byte[]) runtime = " + total + " seconds"); 92 System.out.println("CRC32.update(byte[]) throughput = " + thruput + " MB/s"); 93 94 /* check correctness */ 95 for (int i = 0; i < iters; i++) { 96 crc1.reset(); 97 crc1.update(b, offset, msgSize); 98 if (!check(crc0, crc1)) break; 99 } 120 buf.rewind(); 121 } 122 end = System.nanoTime(); 123 total = (double)(end - start)/1e9; // in seconds 124 thruput = (double)msgSize*iters/1e6/total; // in MB/s 125 System.out.println("CRC32.update(ByteBuffer) runtime = " + total + " seconds"); 126 System.out.println("CRC32.update(ByteBuffer) throughput = " + thruput + " MB/s"); 127 128 /* check correctness */ 129 for (int i = 0; i < iters; i++) { 130 crc2.reset(); 131 crc2.update(buf); 132 buf.rewind(); 133 if (!check(crc0, crc2)) break; 134 } 135 report("CRCs", crc0, crc2); 136 137 System.out.println("-------------------------------------------------------"); 138 } 139 140 private static void report(String s, Checksum crc0, Checksum crc1) { 141 System.out.printf("%s: crc0 = %08x, crc1 = %08x\n", 142 s, crc0.getValue(), crc1.getValue()); 143 } 144 145 private static boolean check(Checksum crc0, Checksum crc1) { 146 if (crc0.getValue() != crc1.getValue()) { 147 System.err.printf("ERROR: crc0 = %08x, crc1 = %08x\n", 148 crc0.getValue(), crc1.getValue()); 149 return false; 150 } 151 return true; 152 } 153 154 private static byte[] initializedBytes(int M, int offset) { 155 byte[] bytes = new byte[M + offset]; 156 for (int i = 0; i < offset; i++) { 157 bytes[i] = (byte) i; 158 } 159 for (int i = offset; i < bytes.length; i++) { 160 bytes[i] = (byte) (i - offset); 161 } 162 return bytes; 163 } 164 165 private static void test_multi(int iters) { 166 int len1 = 8; // the 8B/iteration loop 167 int len2 = 32; // the 32B/iteration loop 168 int len3 = 4096; // the 4KB/iteration loop | 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 8143012 27 * @summary CRC32 Intrinsics support on SPARC 28 * 29 * @run main/othervm/timeout=720 -Xbatch compiler.intrinsics.zip.TestCRC32 -m 30 * @run main/othervm/timeout=720 -Xint -Doffset=1 compiler.intrinsics.zip.TestCRC32 31 * @run main/othervm/timeout=720 -Xcomp -XX:+TieredCompilation -Doffset=1 compiler.intrinsics.zip.TestCRC32 32 * @run main/othervm/timeout=720 -Xcomp -XX:-TieredCompilation -Doffset=1 compiler.intrinsics.zip.TestCRC32 33 */ 34 35 package compiler.intrinsics.zip; 36 37 import java.nio.ByteBuffer; 38 import java.util.zip.CRC32; 39 import java.util.zip.Checksum; 40 41 public class TestCRC32 { 42 // standard CRC32 polynomial 43 // coefficients in different forms 44 // normal: polyBits = 0x04c11db7 = 0b0000 0100 1100 0001 0001 1101 1011 0111 45 // reversed: polybits = 0xedb88320 = 0b1110 1101 1011 1000 1000 0011 0010 0000 46 // reversed reciprocal polybits = 0x82608edb = 0b1000 0010 0110 0000 1000 1110 1101 1011 47 // 48 // 0 5 9 13 17 21 25 29 49 // | | | | | | | | 50 // reversed shiftL 1 polyBits = 0x1db710641L = 0b1 1101 1011 0111 0001 0000 0110 0100 0001 51 final static long polyBits = (1L<<(32-32)) + (1L<<(32-26)) + (1L<<(32-23)) + (1L<<(32-22)) 52 + (1L<<(32-16)) + (1L<<(32-12)) + (1L<<(32-11)) + (1L<<(32-10)) 53 + (1L<<(32-8)) + (1L<<(32-7)) + (1L<<(32-5)) + (1L<<(32-4)) 54 + (1L<<(32-2)) + (1L<<(32-1)) + (1L<<(32-0)); 55 final static long polyBitsShifted = polyBits>>1; 56 57 public static void main(String[] args) { 58 int offset = Integer.getInteger("offset", 0); 59 int msgSize = Integer.getInteger("msgSize", 512); 60 boolean multi = false; 61 int iters = 20000; 62 int warmupIters = 20000; 63 64 if (args.length > 0) { 65 if (args[0].equals("-m")) { 66 multi = true; 67 } else { 68 iters = Integer.valueOf(args[0]); 69 } 70 if (args.length > 1) { 71 warmupIters = Integer.valueOf(args[1]); 72 } 73 } 74 75 if (multi) { 76 test_multi(warmupIters); 77 return; 78 } 79 80 System.out.println(" offset = " + offset); 81 System.out.println("msgSize = " + msgSize + " bytes"); 82 System.out.println(" iters = " + iters); 83 84 byte[] b = initializedBytes(msgSize, offset); 85 86 final long crc_reference = update_byteloop(0, b, offset); 87 88 CRC32 crc0 = new CRC32(); 89 CRC32 crc1 = new CRC32(); 90 CRC32 crc2 = new CRC32(); 91 92 crc0.update(b, offset, msgSize); 93 if (!check(crc0, crc_reference)) { 94 System.out.println("CRC32: crc mismatch during initialization."); 95 return; 96 } 97 98 System.out.println("-------------------------------------------------------"); 99 100 /* warm up */ 101 for (int i = 0; i < warmupIters; i++) { 102 crc1.reset(); 103 crc1.update(b, offset, msgSize); 104 if (!check(crc1, crc_reference)) { 105 System.out.println("CRC32: crc mismatch during warmup iteration " + i); 106 break; 107 } 108 } 109 110 /* measure performance */ 111 long start = System.nanoTime(); 112 for (int i = 0; i < iters; i++) { 113 crc1.reset(); 114 crc1.update(b, offset, msgSize); 115 } 116 long end = System.nanoTime(); 117 double total = (double)(end - start)/1e9; // in seconds 118 double thruput = (double)msgSize*iters/1e6/total; // in MB/s 119 System.out.println("CRC32.update(byte[]) runtime = " + total + " seconds"); 120 System.out.println("CRC32.update(byte[]) throughput = " + thruput + " MB/s"); 121 122 /* check correctness */ 123 for (int i = 0; i < iters; i++) { 124 crc1.reset(); 125 crc1.update(b, offset, msgSize); 126 if (!check(crc0, crc1)) break; 127 } 148 buf.rewind(); 149 } 150 end = System.nanoTime(); 151 total = (double)(end - start)/1e9; // in seconds 152 thruput = (double)msgSize*iters/1e6/total; // in MB/s 153 System.out.println("CRC32.update(ByteBuffer) runtime = " + total + " seconds"); 154 System.out.println("CRC32.update(ByteBuffer) throughput = " + thruput + " MB/s"); 155 156 /* check correctness */ 157 for (int i = 0; i < iters; i++) { 158 crc2.reset(); 159 crc2.update(buf); 160 buf.rewind(); 161 if (!check(crc0, crc2)) break; 162 } 163 report("CRCs", crc0, crc2); 164 165 System.out.println("-------------------------------------------------------"); 166 } 167 168 public static long update_byteloop(long crc, byte[] buf, int offset) { 169 for (int i = offset; i < buf.length; i++) { 170 crc = update_singlebyte(crc, polyBitsShifted, buf[i]); 171 } 172 return crc; 173 } 174 175 // Straight-forward implementation of CRC update by one byte. 176 public static long update_singlebyte(long crc, long polynomial, int val) { 177 crc = (crc ^ -1L) & 0x00000000ffffffffL; // use 1's complement of crc 178 crc = crc ^ (val&0xff); // XOR in next byte from stream 179 for (int i = 0; i < 8; i++) { 180 boolean bitset = (crc & 0x01L) != 0; 181 182 crc = crc>>1; 183 if (bitset) { 184 crc = crc ^ polynomial; 185 crc = crc & 0x00000000ffffffffL; 186 } 187 } 188 crc = (crc ^ -1L) & 0x00000000ffffffffL; // revert taking 1's complement 189 return crc; 190 } 191 192 private static void report(String s, Checksum crc0, Checksum crc1) { 193 System.out.printf("%s: crc0 = %08x, crc1 = %08x\n", 194 s, crc0.getValue(), crc1.getValue()); 195 } 196 197 private static boolean check(Checksum crc0, Checksum crc1) { 198 if (crc0.getValue() != crc1.getValue()) { 199 System.err.printf("ERROR: crc0 = %08x, crc1 = %08x\n", 200 crc0.getValue(), crc1.getValue()); 201 return false; 202 } 203 return true; 204 } 205 206 private static boolean check(Checksum crc0, long crc_reference) { 207 if (crc0.getValue() != crc_reference) { 208 System.err.printf("ERROR: crc0 = %08x, crc_reference = %08x\n", 209 crc0.getValue(), crc_reference); 210 return false; 211 } 212 return true; 213 } 214 215 private static byte[] initializedBytes(int M, int offset) { 216 byte[] bytes = new byte[M + offset]; 217 for (int i = 0; i < offset; i++) { 218 bytes[i] = (byte) i; 219 } 220 for (int i = offset; i < bytes.length; i++) { 221 bytes[i] = (byte) (i - offset); 222 } 223 return bytes; 224 } 225 226 private static void test_multi(int iters) { 227 int len1 = 8; // the 8B/iteration loop 228 int len2 = 32; // the 32B/iteration loop 229 int len3 = 4096; // the 4KB/iteration loop |