4 ms·
I agree with this almost entierly and i use almost exclsivly unsigned ints in C. However, its not true that unsigned is faster because the compiler can optimiz
by quelsolaar 5y ago
I agree with this almost entierly and i use almost exclsivly unsigned ints in C.
However, its not true that unsigned is faster because the compiler can optimize. Consider:
x = (x * 2) / 2;
If x is unsigned, and overflows, x will not be the same after this operation so the compiler can not optimize away this line. If x is signed and overflow is undefined, the compiler can assume it wont overflow and optimize away the line. UB affords the compiler a lot of optimizations.
- MaxBarraclough 5y agoThe article acknowledges this: > There are some optimizations compilers can make assuming signed integers cannot underflow or overflow that unsigned does not get to participate in.
- Const-me 5y ago> its not true that unsigned is faster because the compiler can optimize. It’s not false either. For instance, casting int32_t into int64_t requires an instruction like movsxd which does sign extend. Casting uint32_t into uint64_t can often be merged into the instruction who computed the uint32_t value, because the instruction which write 4 bytes registers like eax zero out old data in the higher 4 bytes of the corresponding 8 bytes rax register.
- quelsolaar 5y agoThanks! I didn't know that!