12 ms·
I am sort of uneasy with what the optimizing doing in here. But I would not ever write something like this: int32_t i = x * 0x1ff / 0xffff; Not because I a
by FpUser 4y ago
I am sort of uneasy with what the optimizing doing in here.
But I would not ever write something like this:
int32_t i = x * 0x1ff / 0xffff;
Not because I am supersmart and will / can predict how optimizer will fuck it up. It is just that I am paranoid when coding and this type of code just simply hurts my perception for some reason.
- saagarjha 4y agoIt seems like a perfectly cromulent line of code to me, assuming you have overflow checks somewhere.
- Karliss 4y agoThe trap in C is that you can't have overflow checks after the calculations testing if overflow happened by making assumptions about what the UB does. Once the UB has happened it's already too late. What you actually need is input range checks to ensure that following code can perform it's calculations correctly without hitting UB or you need to use helper intrinsics which perform checked math operations.
- FpUser 4y agoAs already said it just ruffles my feathers the wrong way. I would not analyze why as I usually have better things to do with my programming time. The best answer I can come up with without thinking is something like this: overflow is an error. I would prefer the code not to create errors unless absolutely needed.
- eru 4y agoThis is the minimal example. For all we know, the original code might have made more sense in context?