I see reports that ((int64_t)x >> 63) is undefined behaviour in C. Ok. Fine. Is there an equivalent that is somehow “defined”? I expect my hardware to use 2-complement. // cc @johnregehr
Hmm... I would *guess* that 63 isn't a problem at all, only the fact that the lhs is signed is an issue. You could do something like ~(((uint64_t)x >> 63) - 1) and hope the compiler can optimize it. (Untested code as I am on my phone right now. So no guarantees. :)
-
-
Best answer so far, and it works under many compilers, but not all… https://godbolt.org/z/xBG_OG
-
gcc is generally smart enough to know how to do what you want if you just say it explicitly: x < 0 ? -1 : 0 generates exactly the same code: https://godbolt.org/z/pwlFR_
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.