4 ms·
> It’s the “load 5 bytes” and “store 5 bytes” that I don’t have a good solution for Strings in C are null terminated. You can avoiding loading exactly 5 bytes.
by Const-me 4y ago
> It’s the “load 5 bytes” and “store 5 bytes” that I don’t have a good solution for
Strings in C are null terminated. You can avoiding loading exactly 5 bytes. For a string of length 4 load 4 bytes, for a string of length 5 load 6 bytes, you’ll read 5 characters of the payload + the terminating '\0'.
If you code C++, the standard library was fortunately designed with C interop in mind, strings form the standard library have c_str() method.
About how to do that efficiently, a good way is a jump table. Not all compilers are smart enough to compile switch into a table, but many of them are. Modern GCC usually does the right thing, take a look: https://godbolt.org/z/sK7jcoooa https://godbolt.org/z/sK7jcoooa (untested)
Partial stores are pretty similar. Compare the vector of bytes for b == `\0`, use _mm_movemask_epi8(), then _tzcnt_u32() or _BitScanForward() to find the length, then in the switch use various extract instructions: _mm_cvtsi128_si32, _mm_cvtsi128_si64, _mm_extract_epi16, and/or _mm_extract_epi32.