kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

crc32.c (559B)


      1 #include "core/crc32.h"
      2 
      3 /* Bit-at-a-time reflected CRC-32. Branch-free inner loop (mask trick) keeps it
      4  * constant-time per bit and free of a 1 KiB lookup table — the throughput is
      5  * ample for hashing and the gzip trailer, the two callers. */
      6 uint32_t kit_crc32(uint32_t crc, const uint8_t* data, size_t len) {
      7   size_t i;
      8   unsigned k;
      9   crc = ~crc;
     10   for (i = 0; i < len; ++i) {
     11     crc ^= data[i];
     12     for (k = 0; k < 8u; ++k) {
     13       uint32_t mask = (uint32_t)0 - (crc & 1u);
     14       crc = (crc >> 1) ^ (0xedb88320u & mask);
     15     }
     16   }
     17   return ~crc;
     18 }