Remix.run Logo
forrestthewoods 4 days ago

What is SWAR?

ncruces 4 days ago | parent [-]

SIMD within a register: https://en.wikipedia.org/wiki/SWAR

It's generally used for techniques that apply SIMD principles within general-purpose registers and instructions.

Assume you've loaded a 64-bit register (a uint64_t) with 8 bytes (unsigned char) of data. Can you answer the question “is any of these 8 bytes zero (the NUL terminator)?”

If you find a cheap way to do it, you can make strlen go faster by consuming 8 bytes at a time.

Et voilà:

   #define ONES ((uint64_t)-1/UCHAR_MAX)
   #define HIGHS (ONES \* (UCHAR_MAX/2+1))
   #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
forrestthewoods 2 days ago | parent [-]

TIL, thanks!