Problem
I am trying to implement a hash function for a simple hash table using the crc32c instruction in sse4.2 x86 extension. However I am not that comfortable with these kinds of problems yet so I have some issues.
I have looked up that there is a function unsigned int _mm_crc32_u8 (unsigned int crc, unsigned char v)
(source: https://software.intel.com/sites/landingpage/IntrinsicsGuide/#techs=SSE4_2&expand=1283 ) which takes unsigned char and returns the hash. According to my understanding the crc
variable is there for error-checking purposes for leading bits which does not concern me (I can either set it to 0 or 0xffffffff and don't care about it).
However I have a string char *s
and want to compute the hash of it.
Questions
(in order of importance)
- How to use
_mm_crc32_u8
to hash the whole string? Should I hash all of the chars ins
separately and bitwise XOR the results? I really have no clue. - The function
_mm_crc32_u8
takes unsigned chars. Is it OK to just cast unsigned char on char to convert it like(unsigned char)s[0]
in this context? As far as I know it is because ASCII chars only have values 0 to 127 so it does not overflow in the casting process. - There are also functions on multiple bytes (
_mm_crc32_u{16,32,64}
), should I perhaps use these for better performance since they can hash up to 4 bytes at once?
Details
#include <nmmintrin.h>
provides the functions above. I am compiling it with clang
flag -msse4.2