Implicit assumptions (was: Re: Some fun with -O2)
Walter von Entferndt
walter.von.entferndt at posteo.net
Fri Jan 15 22:26:39 UTC 2021
At Freitag, 15. Januar 2021, 21:57:14 CET, Mark Millard wrote:
> The rationale vintage that I have a copy of is available at:
>
> http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf
>
Thank you very much. Now I found that "The result of shifting by a bit count
greater than or equal to the word's size is undefined behavior in C and C++"
(https://en.wikipedia.org/wiki/Bitwise_operation#C-family ; likewise http://
www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). So we'll have to go back
to the loop-solution with the multiply-by-2:
--- check_mktime.c.patch ---
--- check_mktime.c.orig 2021-01-15 03:19:33.962253000 +0100
+++ check_mktime.c 2021-01-15 23:22:01.951385000 +0100
@@ -3,6 +3,10 @@
# include <sys/time.h>
# include <time.h>
# include <unistd.h>
+# include <stdlib.h>
+# include <stdio.h> /* printf() */
+# include <inttypes.h> /* format spec PRIX64: ll/l + X on 32/64-bit arch */
+# include <limits.h> /* CHAR_BIT */
/* Work around redefinition to rpl_putenv by other config tests. */
#undef putenv
@@ -106,9 +110,15 @@
time_t t, delta;
int i, j;
- for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
- continue;
- time_t_max--;
+ /* portably compute the maximum of a signed type:
+ * NOTE: << is undefined if the shift width >= word length
+ * i.e. shifting a 64-bit type by 62 on a 32-bit machine: undef
+ */
+ for (i = t = 1; i < CHAR_BIT*sizeof t - 1; i++)
+ t *= 2;
+ time_t_max = t|(t - 1);
+ printf ("time_t_max = 0x%"PRIX64"\n", time_t_max);
+
delta = time_t_max / 997; /* a suitable prime number */
for (i = 0; i < N_STRINGS; i++)
{
--
=|o) "Stell' Dir vor es geht und keiner kriegt's hin." (Wolfgang Neuss)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: check_mktime.c.patch
Type: text/x-patch
Size: 1039 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20210115/fe0e6550/attachment.bin>
More information about the freebsd-hackers
mailing list