4 ms·
Fun fact: the fastest way to load an XMM register (that is, SSE) to all bits 1 is to compare that register to itself, essentially: int x; x = (x == x);
by ridiculous_fish 10y ago
Fun fact: the fastest way to load an XMM register (that is, SSE) to all bits 1 is to compare that register to itself, essentially:
int x;
x = (x == x);
This is indeed what clang and gcc emit for e.g. _mm_set1_epi8(-1). All values are equal to themselves - well, except NaNs!
- poikniok 10y agoCould you explain further, so x = 1 now, what exactly has all bits set to 1?
- gsg 10y agox86-64 vector compare instructions (mostly) produce masks, which are vector-sized values containing 1 for each bit of the input for which the comparison is true, and 0 where it is false. Since a vector analogue of x == x would be true everywhere, it would result in 0b1111.... It's true that the given C code doesn't have those semantics, but you can see what the GP was driving at if you squint a bit. Uh, and if you know how these instructions work already.
- heinrich5991 10y agoThis code has undefined behavior.
- ridiculous_fish 10y agoYep! The compiler gets to emit it, but you don't!