News for hackers. Stuff that matters.
Re:Undefined behavior in C is a reading error by tom_mellior on Thursday May 20, 2021 @02:11PM (#27224866)
The world doesn't end, but in the "int" case you get nice vector code and in the "unsigned int" case you get much less nice scalar code: https://gcc.godbolt.org/z/cje6naYP4
Re:Undefined behavior in C is a reading error by adwn on Thursday May 20, 2021 @02:55PM (#27225500)
Yes, that is true. The proper way for a compiler to handle this, would be to add a single overflow check before the loop, which branches to a scalar translation of the loop. Most realistic code will need a scalar version anyway, to deal with the prolog/epilog of the unrolled loop for iteration counts that aren't multiples of the unrolling factor.

Surely you agree that treating unsigned overflow differently from signed does not make any sense semantically? Why is signed overflow UB, but unsigned wrapping, and not the other way around? The terms 'signed' and 'unsigned' denote the value range, not "operations on this type might overflow/will never overflow".

Re:Undefined behavior in C is a reading error by rocqua on Thursday May 20, 2021 @04:20PM (#27226647)
To a mathematician, wrapping 2^n+1 back to 0 is a lot more intuitive than wrapping 2^n to -2^n. Mathematically the two systems are largely equivalent. They are equivalent when considering addition and multiplication. Both implement arithmetic modulo 2^n+1.

However, the canonical representation of this system runs from 0 to 2^n+1. Hence, if you were going to make one kind of integer overflow, and not the other, C made the correct choice.

That leaves out the question of whether the difference between the two cases is significant enough to have a difference in how overflow works.

Re:Undefined behavior in C is a reading error by tom_mellior on Thursday May 20, 2021 @06:28PM (#27228228)
> The proper way for a compiler to handle this, would be to add a single overflow check before the loop, which branches to a scalar translation of the loop. Most realistic code will need a scalar version anyway, to deal with the prolog/epilog of the unrolled loop for iteration counts that aren't multiples of the unrolling factor.

That's true, I agree that that would be a clever way to handle this particular case. It would still happily invoke undefined behavior if the indices don't match the array's length, of course. Many assumptions about the programmer knowing what they are doing goes into the optimization of C code.

> Surely you agree that treating unsigned overflow differently from signed does not make any sense semantically?

Yes. Silently wrapping unsigned overflow is also very often semantically meaningless.

Re:Undefined behavior in C is a reading error by vyodaiken on Thursday May 20, 2021 @10:12PM (#27229783)
Clang uses vectors for both. https://gcc.godbolt.org/z/G997Ge9KT
Re:Undefined behavior in C is a reading error by tom_mellior on Friday May 21, 2021 @04:22PM (#27239417)
Yes, with lots of extra ceremony around it. (More than is needed, since it doesn't seem to realize that it will always process exactly 16 loop iterations.)

Since you've posted a lot along the lines of "these optimizations don't even make a difference", you might want to see if Clang's safer-looking version is as fast as GCC's.

Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @04:57PM (#27239873)
It's not an interesting optimization. Micro-benchmarks are of limited utility. The extra complication is to protect the code from flying off and writing on random memory. Well worth it.