4 ms·
I thought you could make this clearer and possibly faster by using a switch, but initialising the switch constants is beyond me. You need a constexpr version of
by twic 4y ago
I thought you could make this clearer and possibly faster by using a switch, but initialising the switch constants is beyond me. You need a constexpr version of memcpy, and i couldn't find one that i could use. std::copy is constexpr, but i don't think you can whip up a char* pointing to the result uint64_t in a way that's constexpr. I could write a manual loop to do the copying, but surely that wouldn't reliably be as fast as memcpy.
Anyway, this whole thing reminded me of FourCC codes, where you identify things with four-character ASCII strings, and freely reinterpret those as 32-bit integers whenever it's convenient:
https://en.wikipedia.org/wiki/FourCC https://en.wikipedia.org/wiki/FourCC
- morelisp 4y agonginx uses switch on a combination of length + specific known characters as part of a similar optimization. https://github.com/nginx/nginx/blob/9c7a2c7ce4ad02a36df1bb0ee5e40a84610fffb9/src/http/ngx_http_parse.c#L164 https://github.com/nginx/nginx/blob/9c7a2c7ce4ad02a36df1bb0e...
- gjye 4y agostd::bit_cast is constexpr. I'd defer the switch case generation to the compiler though: https://godbolt.org/z/oaWvGzWcn https://godbolt.org/z/oaWvGzWcn Seems to do the trick with the bunch of cmp/je in the generated asm.