< prev index next >

src/java.base/share/classes/java/util/zip/CRC32C.java

Print this page




 205 
 206     /**
 207      * Updates the CRC-32C checksum with the specified array of bytes.
 208      */
 209     @HotSpotIntrinsicCandidate
 210     private static int updateBytes(int crc, byte[] b, int off, int end) {
 211 
 212         // Do only byte reads for arrays so short they can't be aligned
 213         // or if bytes are stored with a larger witdh than one byte.,%
 214         if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {
 215 
 216             // align on 8 bytes
 217             int alignLength
 218                     = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
 219             for (int alignEnd = off + alignLength; off < alignEnd; off++) {
 220                 crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
 221             }
 222 
 223             if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
 224                 crc = Integer.reverseBytes(crc);
 225             }
 226 
 227             // slicing-by-8
 228             for (; off < (end - Long.BYTES); off += Long.BYTES) {
 229                 int firstHalf;
 230                 int secondHalf;
 231                 if (Unsafe.ADDRESS_SIZE == 4) {
 232                     // On 32 bit platforms read two ints instead of a single 64bit long
 233                     firstHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
 234                     secondHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off
 235                                                + Integer.BYTES);
 236                 } else {
 237                     long value = UNSAFE.getLong(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
 238                     if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
 239                         firstHalf = (int) value;
 240                         secondHalf = (int) (value >>> 32);
 241                     } else { // ByteOrder.BIG_ENDIAN
 242                         firstHalf = (int) (value >>> 32);
 243                         secondHalf = (int) value;
 244                     }
 245                 }
 246                 crc ^= firstHalf;
 247                 if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
 248                     crc = byteTable7[crc & 0xFF]
 249                             ^ byteTable6[(crc >>> 8) & 0xFF]
 250                             ^ byteTable5[(crc >>> 16) & 0xFF]
 251                             ^ byteTable4[crc >>> 24]
 252                             ^ byteTable3[secondHalf & 0xFF]
 253                             ^ byteTable2[(secondHalf >>> 8) & 0xFF]
 254                             ^ byteTable1[(secondHalf >>> 16) & 0xFF]
 255                             ^ byteTable0[secondHalf >>> 24];
 256                 } else { // ByteOrder.BIG_ENDIAN
 257                     crc = byteTable0[secondHalf & 0xFF]
 258                             ^ byteTable1[(secondHalf >>> 8) & 0xFF]
 259                             ^ byteTable2[(secondHalf >>> 16) & 0xFF]
 260                             ^ byteTable3[secondHalf >>> 24]
 261                             ^ byteTable4[crc & 0xFF]
 262                             ^ byteTable5[(crc >>> 8) & 0xFF]
 263                             ^ byteTable6[(crc >>> 16) & 0xFF]
 264                             ^ byteTable7[crc >>> 24];
 265                 }
 266             }
 267 
 268             if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
 269                 crc = Integer.reverseBytes(crc);

























 270             }
 271         }
 272 
 273         // Tail
 274         for (; off < end; off++) {
 275             crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
 276         }
 277 
 278         return crc;
 279     }
 280 
 281     /**
 282      * Updates the CRC-32C checksum reading from the specified address.
 283      */
 284     @HotSpotIntrinsicCandidate
 285     private static int updateDirectByteBuffer(int crc, long address,
 286                                               int off, int end) {
 287 
 288         // Do only byte reads for arrays so short they can't be aligned
 289         if (end - off >= 8) {
 290 
 291             // align on 8 bytes
 292             int alignLength = (8 - (int) ((address + off) & 0x7)) & 0x7;
 293             for (int alignEnd = off + alignLength; off < alignEnd; off++) {
 294                 crc = (crc >>> 8)
 295                         ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
 296             }
 297 
 298             if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
 299                 crc = Integer.reverseBytes(crc);
 300             }
 301 
 302             // slicing-by-8
 303             for (; off <= (end - Long.BYTES); off += Long.BYTES) {
 304                 // Always reading two ints as reading a long followed by
 305                 // shifting and casting was slower.
 306                 int firstHalf = UNSAFE.getInt(address + off);
 307                 int secondHalf = UNSAFE.getInt(address + off + Integer.BYTES);
 308                 crc ^= firstHalf;
 309                 if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
 310                     crc = byteTable7[crc & 0xFF]
 311                             ^ byteTable6[(crc >>> 8) & 0xFF]
 312                             ^ byteTable5[(crc >>> 16) & 0xFF]
 313                             ^ byteTable4[crc >>> 24]
 314                             ^ byteTable3[secondHalf & 0xFF]
 315                             ^ byteTable2[(secondHalf >>> 8) & 0xFF]
 316                             ^ byteTable1[(secondHalf >>> 16) & 0xFF]
 317                             ^ byteTable0[secondHalf >>> 24];
 318                 } else { // ByteOrder.BIG_ENDIAN
 319                     crc = byteTable0[secondHalf & 0xFF]
 320                             ^ byteTable1[(secondHalf >>> 8) & 0xFF]
 321                             ^ byteTable2[(secondHalf >>> 16) & 0xFF]
 322                             ^ byteTable3[secondHalf >>> 24]
 323                             ^ byteTable4[crc & 0xFF]
 324                             ^ byteTable5[(crc >>> 8) & 0xFF]
 325                             ^ byteTable6[(crc >>> 16) & 0xFF]
 326                             ^ byteTable7[crc >>> 24];
 327                 }
 328             }
 329 
 330             if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
 331                 crc = Integer.reverseBytes(crc);
















 332             }


 333         }
 334 
 335         // Tail
 336         for (; off < end; off++) {
 337             crc = (crc >>> 8)
 338                     ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
 339         }
 340 
 341         return crc;
 342     }
 343 }


 205 
 206     /**
 207      * Updates the CRC-32C checksum with the specified array of bytes.
 208      */
 209     @HotSpotIntrinsicCandidate
 210     private static int updateBytes(int crc, byte[] b, int off, int end) {
 211 
 212         // Do only byte reads for arrays so short they can't be aligned
 213         // or if bytes are stored with a larger witdh than one byte.,%
 214         if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {
 215 
 216             // align on 8 bytes
 217             int alignLength
 218                     = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
 219             for (int alignEnd = off + alignLength; off < alignEnd; off++) {
 220                 crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
 221             }
 222 
 223             if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
 224                 crc = Integer.reverseBytes(crc);

 225 
 226                 // slicing-by-8
 227                 for (; off < (end - Long.BYTES); off += Long.BYTES) {
 228                     int firstHalf;
 229                     int secondHalf;
 230                     if (Unsafe.ADDRESS_SIZE == 4) {
 231                         // On 32 bit platforms read two ints instead of a single 64bit long
 232                         firstHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
 233                         secondHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off
 234                                                    + Integer.BYTES);
 235                     } else {
 236                         long value = UNSAFE.getLong(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);




 237                         firstHalf = (int) (value >>> 32);
 238                         secondHalf = (int) value;
 239                     }

 240                     crc ^= firstHalf;
 241                     crc = (byteTable0[secondHalf & 0xFF]
 242                             ^ byteTable1[(secondHalf >>> 8) & 0xFF])
 243                         ^ (byteTable2[(secondHalf >>> 16) & 0xFF]
 244                             ^ byteTable3[secondHalf >>> 24])
 245                         ^ (byteTable4[crc & 0xFF]
 246                             ^ byteTable5[(crc >>> 8) & 0xFF])
 247                         ^ (byteTable6[(crc >>> 16) & 0xFF]
 248                             ^ byteTable7[crc >>> 24]);











 249                 }
 250 

 251                 crc = Integer.reverseBytes(crc);
 252             } else { // ByteOrder.LITTLE_ENDIAN
 253                 // slicing-by-8
 254                 for (; off < (end - Long.BYTES); off += Long.BYTES) {
 255                     int firstHalf;
 256                     int secondHalf;
 257                     if (Unsafe.ADDRESS_SIZE == 4) {
 258                         // On 32 bit platforms read two ints instead of a single 64bit long
 259                         firstHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
 260                         secondHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off
 261                                                    + Integer.BYTES);
 262                     } else {
 263                         long value = UNSAFE.getLong(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
 264                         firstHalf = (int) value;
 265                         secondHalf = (int) (value >>> 32);
 266                     }
 267                     crc ^= firstHalf;
 268                     crc = (byteTable7[crc & 0xFF]
 269                             ^ byteTable6[(crc >>> 8) & 0xFF])
 270                         ^ (byteTable5[(crc >>> 16) & 0xFF]
 271                             ^ byteTable4[crc >>> 24])
 272                         ^ (byteTable3[secondHalf & 0xFF]
 273                             ^ byteTable2[(secondHalf >>> 8) & 0xFF])
 274                         ^ (byteTable1[(secondHalf >>> 16) & 0xFF]
 275                             ^ byteTable0[secondHalf >>> 24]);
 276                 }
 277             }
 278         }
 279 
 280         // Tail
 281         for (; off < end; off++) {
 282             crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
 283         }
 284 
 285         return crc;
 286     }
 287 
 288     /**
 289      * Updates the CRC-32C checksum reading from the specified address.
 290      */
 291     @HotSpotIntrinsicCandidate
 292     private static int updateDirectByteBuffer(int crc, long address,
 293                                               int off, int end) {
 294 
 295         // Do only byte reads for arrays so short they can't be aligned
 296         if (end - off >= 8) {
 297 
 298             // align on 8 bytes
 299             int alignLength = (8 - (int) ((address + off) & 0x7)) & 0x7;
 300             for (int alignEnd = off + alignLength; off < alignEnd; off++) {
 301                 crc = (crc >>> 8)
 302                         ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
 303             }
 304 
 305             if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
 306                 crc = Integer.reverseBytes(crc);

 307 
 308                 // slicing-by-8
 309                 for (; off <= (end - Long.BYTES); off += Long.BYTES) {
 310                     // Always reading two ints as reading a long followed by
 311                     // shifting and casting was slower.
 312                     int firstHalf = UNSAFE.getInt(address + off);
 313                     int secondHalf = UNSAFE.getInt(address + off + Integer.BYTES);
 314                     crc ^= firstHalf;
 315                     crc = (byteTable0[secondHalf & 0xFF]
 316                             ^ byteTable1[(secondHalf >>> 8) & 0xFF])
 317                         ^ (byteTable2[(secondHalf >>> 16) & 0xFF]
 318                             ^ byteTable3[secondHalf >>> 24])
 319                         ^ (byteTable4[crc & 0xFF]
 320                             ^ byteTable5[(crc >>> 8) & 0xFF])
 321                         ^ (byteTable6[(crc >>> 16) & 0xFF]
 322                             ^ byteTable7[crc >>> 24]);










 323                 }



 324                 crc = Integer.reverseBytes(crc);
 325             } else { // ByteOrder.LITTLE_ENDIAN
 326                 // slicing-by-8
 327                 for (; off <= (end - Long.BYTES); off += Long.BYTES) {
 328                     // Always reading two ints as reading a long followed by
 329                     // shifting and casting was slower.
 330                     int firstHalf = UNSAFE.getInt(address + off);
 331                     int secondHalf = UNSAFE.getInt(address + off + Integer.BYTES);
 332                     crc ^= firstHalf;
 333                     crc = (byteTable7[crc & 0xFF]
 334                             ^ byteTable6[(crc >>> 8) & 0xFF])
 335                         ^ (byteTable5[(crc >>> 16) & 0xFF]
 336                             ^ byteTable4[crc >>> 24])
 337                         ^ (byteTable3[secondHalf & 0xFF]
 338                             ^ byteTable2[(secondHalf >>> 8) & 0xFF])
 339                         ^ (byteTable1[(secondHalf >>> 16) & 0xFF]
 340                             ^ byteTable0[secondHalf >>> 24]);
 341                 }
 342             }
 343 
 344         }
 345 
 346         // Tail
 347         for (; off < end; off++) {
 348             crc = (crc >>> 8)
 349                     ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
 350         }
 351 
 352         return crc;
 353     }
 354 }
< prev index next >