https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121682

--- Comment #1 from Kang-Che Sung <Explorer09 at gmail dot com> ---
I think this information can be helpful for developers who try to implement
this optimization.

For unsigned integers `x` and `y`...
1. The three bitwise comparisons `(x & y) == y`, `(x | y) == x`, and `(~x & y)
== 0` are all equivalent. This represents when the 1-bits in `y` are a subset
of the 1-bits in `x`. (Clang can detect and optimize this already.)
2. `(x xor y) == (x & ~y)` if and only if `(~x & y) == 0`. So sometimes the
`(~x & y) == 0` conditional can be replaced with the former if sometimes the
client code needs the results of both `(x xor y)` and `(x & ~y)` (although I
assume such case would be rare).
3. If `(~x & y) == 0`, then `(x - y) == (x xor y)` but the conditional doesn't
work in the opposite direction. There is one exception case that `(x - y) == (x
xor y)` but `(x & y) != y` when `x` is `0` and `y` is `((~0 >> 1) + 1)` (i.e.
the MSB is 1 and all other bits 0).

Points 1 and 2 can be proven with truth tables. I'm not certain with point 3
yet, so I disclaim the accuracy on that one, but I've done my best.

Reply via email to