Home | History | Annotate | Download | only in C
      1 /* XzCrc64Opt.c -- CRC64 calculation
      2 2017-06-30 : Igor Pavlov : Public domain */
      3 
      4 #include "Precomp.h"
      5 
      6 #include "CpuArch.h"
      7 
      8 #ifndef MY_CPU_BE
      9 
     10 #define CRC64_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
     11 
     12 UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table)
     13 {
     14   const Byte *p = (const Byte *)data;
     15   for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
     16     v = CRC64_UPDATE_BYTE_2(v, *p);
     17   for (; size >= 4; size -= 4, p += 4)
     18   {
     19     UInt32 d = (UInt32)v ^ *(const UInt32 *)p;
     20     v = (v >> 32)
     21         ^ (table + 0x300)[((d      ) & 0xFF)]
     22         ^ (table + 0x200)[((d >>  8) & 0xFF)]
     23         ^ (table + 0x100)[((d >> 16) & 0xFF)]
     24         ^ (table + 0x000)[((d >> 24))];
     25   }
     26   for (; size > 0; size--, p++)
     27     v = CRC64_UPDATE_BYTE_2(v, *p);
     28   return v;
     29 }
     30 
     31 #endif
     32 
     33 
     34 #ifndef MY_CPU_LE
     35 
     36 #define CRC_UINT64_SWAP(v) \
     37       ((v >> 56) \
     38     | ((v >> 40) & ((UInt64)0xFF <<  8)) \
     39     | ((v >> 24) & ((UInt64)0xFF << 16)) \
     40     | ((v >>  8) & ((UInt64)0xFF << 24)) \
     41     | ((v <<  8) & ((UInt64)0xFF << 32)) \
     42     | ((v << 24) & ((UInt64)0xFF << 40)) \
     43     | ((v << 40) & ((UInt64)0xFF << 48)) \
     44     | ((v << 56)))
     45 
     46 #define CRC64_UPDATE_BYTE_2_BE(crc, b) (table[(Byte)((crc) >> 56) ^ (b)] ^ ((crc) << 8))
     47 
     48 UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table)
     49 {
     50   const Byte *p = (const Byte *)data;
     51   table += 0x100;
     52   v = CRC_UINT64_SWAP(v);
     53   for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
     54     v = CRC64_UPDATE_BYTE_2_BE(v, *p);
     55   for (; size >= 4; size -= 4, p += 4)
     56   {
     57     UInt32 d = (UInt32)(v >> 32) ^ *(const UInt32 *)p;
     58     v = (v << 32)
     59         ^ (table + 0x000)[((d      ) & 0xFF)]
     60         ^ (table + 0x100)[((d >>  8) & 0xFF)]
     61         ^ (table + 0x200)[((d >> 16) & 0xFF)]
     62         ^ (table + 0x300)[((d >> 24))];
     63   }
     64   for (; size > 0; size--, p++)
     65     v = CRC64_UPDATE_BYTE_2_BE(v, *p);
     66   return CRC_UINT64_SWAP(v);
     67 }
     68 
     69 #endif
     70