Here are some compiler warnings that it would probably be good to be rid of. The compiler is optimizing away intended overflow tests in some cases. Results of integer operations are officially undefined when overflow occurs, so the compiler is within its rights in making this kind of assumption. $ gcc -Wall -O3 -c *.c localtime.c: In function ‘timesub’: localtime.c:1541: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false localtime.c:1545: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false localtime.c: In function ‘T.144’: localtime.c:1727: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false localtime.c:1738: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false localtime.c:1748: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false localtime.c:1752: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false localtime.c:1768: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false zdump.c: In function ‘main’: zdump.c:442: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false $ gcc --version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 -- Andy Heninger
Here’s an "increment_overflow" replacement that doesn't get any complaints from gcc. How can it be portably simplified? --ado static int increment_overflow(ip, j) register int * const ip; register int j; { register int i, halfsum, halfmod, bothneg; i = *ip; *ip += j; if (i == 0 || j == 0) { return 0; } if ((i > 0) != (j > 0)) { return 0; } bothneg = i < 0; if (bothneg) { if (i == INT_MIN || j == INT_MIN) return 1; i = -i; j = -j; } halfsum = (i / 2) + (j / 2); halfmod = (i % 2) + (j % 2); if (halfmod == 2) { ++halfsum; halfmod = 0; } if (bothneg) { if (halfsum < -(INT_MIN / 2)) return 0; if (halfsum > -(INT_MIN / 2)) return 1; return ((INT_MIN % 2) == 0) ? halfmod : 0; } else { if (halfsum < (INT_MAX / 2)) return 0; if (halfsum > (INT_MAX / 2)) return 1; return ((INT_MAX % 2) == 0) ? halfmod : 0; } }
On Apr 13, 9:38am, olsona@dc37a.nci.nih.gov ("Olson, Arthur David (NIH/NCI) [E]") wrote: -- Subject: RE: Compiler Warnings | Here’s an "increment_overflow" replacement that doesn't get any complaints from gcc. | How can it be portably simplified? I think that Paul Eggert posted a simpler one: static int increment_overflow(int *number, int delta) { int number0; number0 = *number; if (delta < 0 ? number0 < INT_MIN - delta : INT_MAX - delta < number0) return 1; *number += delta; return 0; } Any updates putting in the per timezone functionality in? I've committed it to the NetBSD codebase and seems to work fine... http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/?only_with_tag=MAIN christos
participants (3)
-
Andy Heninger -
christos@zoulas.com -
Olson, Arthur David (NIH/NCI) [E]