Re: [math-fun] math-fun Digest, Vol 146, Issue 19
On Wed, 8 Apr 2015 12:53:47 +0200, Joerg Arndt <arndt@jjj.de> wrote:
Don't essentially all existing platforms(*) just treat signed "int"s as what they are: fixed length registers? I know only of Itanium where overflow may trap (not even sure if it's the default there, at least I got no warnings and no traps the single time I used Itanium).
All *contemporary* existing platforms likely do. There used to be platforms where integers were essentially denormalized floating point numbers. A non-trapping integer overflow (yielding a normalized floating point number) would not be equal to any integer, and an attempt to perform bitwise Boolean operations on it would result in garbage. My understanding is that the proximate rationale for making integer overflow undefined behavior is DSP-like CPUs which might have saturating integer overflow by default; thus int a = 1; while (a != 0) a = a+1; is indeed an infinite loop. Teaching people not to use signed integers when they need modulo 2^w operations or bitmask manipulation is a good thing. Leo
* Leo Broukhis <leob@mailcom.com> [Apr 09. 2015 14:00]:
On Wed, 8 Apr 2015 12:53:47 +0200, Joerg Arndt <arndt@jjj.de> wrote:
Don't essentially all existing platforms(*) just treat signed "int"s as what they are: fixed length registers? I know only of Itanium where overflow may trap (not even sure if it's the default there, at least I got no warnings and no traps the single time I used Itanium).
All *contemporary* existing platforms likely do. There used to be platforms where integers were essentially denormalized floating point numbers.
Eek, I hope to never see such a machine.
A non-trapping integer overflow (yielding a normalized floating point number) would not be equal to any integer, and an attempt to perform bitwise Boolean operations on it would result in garbage.
Thanks for clarifying.
My understanding is that the proximate rationale for making integer overflow undefined behavior is DSP-like CPUs which might have saturating integer overflow by default; thus
int a = 1; while (a != 0) a = a+1;
is indeed an infinite loop.
Yes, I forgot DSP for a moment. Someone once told me that there you need to analyze algorithms (for all possible inputs!) for overflow beforehand anyway and assured me this is nothing anyone likes to do.
Teaching people not to use signed integers when they need modulo 2^w operations or bitmask manipulation is a good thing.
I call that particular discipline "unsigned correctness". It often also leads to better performance and readability, e.g., because a for-loop has to be replaced by a do-while.
Leo
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com https://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
participants (2)
-
Joerg Arndt -
Leo Broukhis