What initial value should I pass to the Aarch64 crc32 instructions?

Viewed 212

The AArch64 crc32{b,h,w,x} instructions take as input a CRC-32 value and a data value (8, 16, 32, or 64 bits, respectively), and output a new CRC-32 value which, presumably, should be passed in as the input to the next crc32 instruction.

To get the same value at the end that the crc32 program produces for a given set of bytes, what does the initial input value have to be? Is there anything else I have to do?

The algorithm is well-described elsewhere, but I can't find examples of using the instructions anywhere.

1 Answers

Through the wonders of trial and error, it seems the initial value for the crc32 accumulator is 0xffffffff (or -1), and to get the standard crc32 value you invert the value returned, so ~crc32.

e.g. (dwords array must be 8-byte aligned).

uint32_t crc32 = 0xffffffff;
for (int i = 0; i < number_of_dwords; i++) {
  asm volatile ( "crc32x %w[crc], %w[crcin], %x[data]" : [crc] "=r" (crc32) : [crcin] "r" (crc32), [data] "r" (dwords[i]) );
}
return ~crc32;
Related