Re:GCC undefined behaviors are getting wild
by Asooka on Monday November 28, 2022 @05:33AM
(#33771905)
It's not to protect from UB, it's to protect from the optimiser deleting your bounds checks.
Re:GCC undefined behaviors are getting wild
by lilllly on Monday November 28, 2022 @06:33AM
(#33772339)
I can easily test if n+1 is < n with fwrapv.
Without you have to do convoluted things like rearranging the expression to unnatural forms (move the addition to the right but invert to subtraction, etc), special case INT_MAX/INT_MIN, and so on - which you then have to hope the compiler is smart enough to optimize, which it often isn't (oh how ironic).
It is sad that we don't get hardware assistance for that trap on any widespread cpu, at least that I know of.