3 ms·
The "party trick" is more general than just xor, for example: void swap(unsigned* a, unsigned* b) { *a = *a + *b; *b = *a - *b;
by jcalvinowens 6mo ago
The "party trick" is more general than just xor, for example:
void swap(unsigned* a, unsigned* b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
GCC still sees through it with restrict: https://godbolt.org/z/8n3bGha3e https://godbolt.org/z/8n3bGha3e
- sifar 6mo agoThis has overflow issues unlike xor.
- deathanatos 6mo agoDepending on what you think the "issue" is, one of: * wrapping is well-defined behavior for unsigned integers; signed integer wrapping is UB, but is not used here. * the equations (that a & b cancel each other out, resulting in the swap) hold even when done in a mod N ring.
- warmwaffles 6mo agoYea this is operating on the pointer to the values, but if you use just an `unsigned a` it uses xor clang: https://godbolt.org/z/5PEMTrxba https://godbolt.org/z/5PEMTrxba gcc: https://godbolt.org/z/7j8h4zo4v https://godbolt.org/z/7j8h4zo4v
- zahlman 6mo agoThis is covered in TFA.