Created
June 13, 2017 15:13
-
-
Save TheServer201/a89ecc785d42ed254bad80bc2b2e6fe7 to your computer and use it in GitHub Desktop.
Use of SSE4.2 to compute crc32c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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