// File: /workspace/github_cpp_makefile_star2_O2/BU-DiSC[P]BF-Shared-Hashing/hash/Crc32.cpp
// Function: crc32_16bytes_prefetch(void const*, unsigned long, unsigned int, unsigned long)
uint32_t crc32_16bytes_prefetch(const void* data, size_t length, uint32_t previousCrc32, size_t prefetchAhead)
{
  // CRC code is identical to crc32_16bytes (including unrolling), only added prefetching
  // 256 bytes look-ahead seems to be the sweet spot on Core i7 CPUs

  uint32_t crc = ~previousCrc32; // same as previousCrc32 ^ 0xFFFFFFFF
  const uint32_t* current = (const uint32_t*) data;

  // enabling optimization (at least -O2) automatically unrolls the for-loop
  const size_t Unroll = 4;
  const size_t BytesAtOnce = 16 * Unroll;

  while (length >= BytesAtOnce + prefetchAhead)
  {
    PREFETCH(((const char*) current) + prefetchAhead);

    for (size_t unrolling = 0; unrolling < Unroll; unrolling++)
    {
#if __BYTE_ORDER == __BIG_ENDIAN
    uint32_t one   = *current++ ^ swap(crc);
    uint32_t two   = *current++;
    uint32_t three = *current++;
    uint32_t four  = *current++;
    crc  = Crc32Lookup[ 0][ four         & 0xFF] ^
           Crc32Lookup[ 1][(four  >>  8) & 0xFF] ^
           Crc32Lookup[ 2][(four  >> 16) & 0xFF] ^
           Crc32Lookup[ 3][(four  >> 24) & 0xFF] ^
           Crc32Lookup[ 4][ three        & 0xFF] ^
           Crc32Lookup[ 5][(three >>  8) & 0xFF] ^
           Crc32Lookup[ 6][(three >> 16) & 0xFF] ^
           Crc32Lookup[ 7][(three >> 24) & 0xFF] ^
           Crc32Lookup[ 8][ two          & 0xFF] ^
           Crc32Lookup[ 9][(two   >>  8) & 0xFF] ^
           Crc32Lookup[10][(two   >> 16) & 0xFF] ^
           Crc32Lookup[11][(two   >> 24) & 0xFF] ^
           Crc32Lookup[12][ one          & 0xFF] ^
           Crc32Lookup[13][(one   >>  8) & 0xFF] ^
           Crc32Lookup[14][(one   >> 16) & 0xFF] ^
           Crc32Lookup[15][(one   >> 24) & 0xFF];
#else
    uint32_t one   = *current++ ^ crc;
    uint32_t two   = *current++;
    uint32_t three = *current++;
    uint32_t four  = *current++;
    crc  = Crc32Lookup[ 0][(four  >> 24) & 0xFF] ^
           Crc32Lookup[ 1][(four  >> 16) & 0xFF] ^
           Crc32Lookup[ 2][(four  >>  8) & 0xFF] ^
           Crc32Lookup[ 3][ four         & 0xFF] ^
           Crc32Lookup[ 4][(three >> 24) & 0xFF] ^
           Crc32Lookup[ 5][(three >> 16) & 0xFF] ^
           Crc32Lookup[ 6][(three >>  8) & 0xFF] ^
           Crc32Lookup[ 7][ three        & 0xFF] ^
           Crc32Lookup[ 8][(two   >> 24) & 0xFF] ^
           Crc32Lookup[ 9][(two   >> 16) & 0xFF] ^
           Crc32Lookup[10][(two   >>  8) & 0xFF] ^
           Crc32Lookup[11][ two          & 0xFF] ^
           Crc32Lookup[12][(one   >> 24) & 0xFF] ^
           Crc32Lookup[13][(one   >> 16) & 0xFF] ^
           Crc32Lookup[14][(one   >>  8) & 0xFF] ^
           Crc32Lookup[15][ one          & 0xFF];
#endif
    }

    length -= BytesAtOnce;
  }

  const uint8_t* currentChar = (const uint8_t*) current;
  // remaining 1 to 63 bytes (standard algorithm)
  while (length-- != 0)
    crc = (crc >> 8) ^ Crc32Lookup[0][(crc & 0xFF) ^ *currentChar++];

  return ~crc; // same as crc ^ 0xFFFFFFFF
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edx, %eax
notl %eax
leaq 0x40(%rcx), %r8
leaq 0x25e6(%rip), %rdx      # 0x23700
movl $0x3fc, %r9d            # imm = 0x3FC
movl $0xff, %r10d
cmpq %r8, %rsi
jb 0x2125c
prefetcht0 (%rdi,%rcx)
xorl %r11d, %r11d
cmpq $0x40, %r11
je 0x2124f
xorl (%rdi,%r11), %eax
movl 0xc(%rdi,%r11), %r14d
movl %r14d, %r15d
shrl $0x18, %r15d
movl %r14d, %ebx
shrl $0xe, %ebx
andl %r9d, %ebx
movl 0x400(%rbx,%rdx), %ebx
xorl (%rdx,%r15,4), %ebx
movl %r14d, %r15d
shrl $0x8, %r15d
andl %r10d, %r15d
xorl 0x800(%rdx,%r15,4), %ebx
movl 0x8(%rdi,%r11), %r15d
andl %r10d, %r14d
xorl 0xc00(%rdx,%r14,4), %ebx
movl %r15d, %r14d
shrl $0x18, %r14d
xorl 0x1000(%rdx,%r14,4), %ebx
movl 0x4(%rdi,%r11), %r14d
movl %r15d, %r12d
shrl $0xe, %r12d
andl %r9d, %r12d
xorl 0x1400(%r12,%rdx), %ebx
movl %r15d, %r12d
shrl $0x8, %r12d
andl %r10d, %r12d
xorl 0x1800(%rdx,%r12,4), %ebx
andl %r10d, %r15d
xorl 0x1c00(%rdx,%r15,4), %ebx
movl %r14d, %r15d
shrl $0x18, %r15d
xorl 0x2000(%rdx,%r15,4), %ebx
movl %r14d, %r15d
shrl $0xe, %r15d
andl %r9d, %r15d
xorl 0x2400(%r15,%rdx), %ebx
movl %r14d, %r15d
shrl $0x8, %r15d
andl %r10d, %r15d
xorl 0x2800(%rdx,%r15,4), %ebx
andl %r10d, %r14d
xorl 0x2c00(%rdx,%r14,4), %ebx
movl %eax, %r14d
shrl $0x18, %r14d
xorl 0x3000(%rdx,%r14,4), %ebx
movl %eax, %r14d
shrl $0xe, %r14d
andl %r9d, %r14d
xorl 0x3400(%r14,%rdx), %ebx
movl %eax, %r14d
shrl $0x8, %r14d
andl %r10d, %r14d
xorl 0x3800(%rdx,%r14,4), %ebx
andl %r10d, %eax
xorl 0x3c00(%rdx,%rax,4), %ebx
addq $0x10, %r11
movl %ebx, %eax
jmp 0x21136
addq $0x40, %rdi
addq $-0x40, %rsi
jmp 0x21126
xorl %ecx, %ecx
cmpq %rcx, %rsi
je 0x2127b
movzbl %al, %r8d
shrl $0x8, %eax
movzbl (%rdi,%rcx), %r9d
xorl %r8d, %r9d
xorl (%rdx,%r9,4), %eax
incq %rcx
jmp 0x2125e
notl %eax
popq %rbx
popq %r12
popq %r14
popq %r15
retq