3 ms·
> Some advice I've read is to only use unsigned integers if you want to explicitly opt-in to having overflow be defined behavior. I read somewhere that the tru
by cesarb 7y ago
> Some advice I've read is to only use unsigned integers if you want to explicitly opt-in to having overflow be defined behavior.
I read somewhere that the true reason for that advice is that it allows the compiler to silently store an "int" loop counter in a 64-bit register, without having to care about 32-bit overflow. If you use size_t for the loop counter, that's no longer an issue.
- jdsully 7y agoAt least on X86 that truncation behavior is free. 32-bit EAX is just first 32-bits of RAX.
- nullc 7y agoI haven't seen an example where that's the case. It commonly factors in loop analysis around unrolling and vectorization, e.g. the loop will run exactly 16*n times OR an integer will overflow and it'll run some other number of times. Use of signed precludes the overflow and the exact bound enables efficient vectorization.