Skip to content

Instantly share code, notes, and snippets.

@TheServer201
Created June 13, 2017 15:13
Show Gist options
  • Save TheServer201/a89ecc785d42ed254bad80bc2b2e6fe7 to your computer and use it in GitHub Desktop.
Save TheServer201/a89ecc785d42ed254bad80bc2b2e6fe7 to your computer and use it in GitHub Desktop.
Use of SSE4.2 to compute crc32c
#include <nmmintrin.h>
#include <stdint.h>
#include <string.h>
uint32_t crc32(uint32_t len, uint8_t *msg) {
uint32_t pos = 0, hsh = -1;
while (1) {
uint32_t tmp = len - pos;
if (tmp >= 4) {
uint32_t val;
memcpy(&val, msg + pos, sizeof(val));
hsh = _mm_crc32_u32(hsh, val);
pos += 4;
} else if (tmp >= 2) {
uint16_t val;
memcpy(&val, msg + pos, sizeof(val));
hsh = _mm_crc32_u16(hsh, val);
pos += 2;
} else {
uint8_t val;
memcpy(&val, msg + pos, sizeof(val));
hsh = _mm_crc32_u8(hsh, val);
break;
}
}
return ~hsh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment