Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Jan 2021 23:25:25 +0100
From:      Walter von Entferndt <walter.von.entferndt@posteo.net>
To:        Mark Millard <marklmi@yahoo.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Implicit assumptions (was: Re: Some fun with -O2)
Message-ID:  <5358091.mMMZhaHaU6@t450s.local.lan>
In-Reply-To: <4E90FC92-D255-4082-9F89-2BEE4D4C4E92@yahoo.com>
References:  <mailman.29.1610625600.45116.freebsd-hackers@freebsd.org> <8830694.EFs4ROYVHJ@t450s.local.lan> <4E90FC92-D255-4082-9F89-2BEE4D4C4E92@yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.

--nextPart5430162.XOh7uYVVfo
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="us-ascii"

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)
--nextPart5430162.XOh7uYVVfo
Content-Disposition: attachment; filename="check_mktime.c.patch"
Content-Transfer-Encoding: 7Bit
Content-Type: text/x-patch; charset="UTF-8"; name="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++)
     {

--nextPart5430162.XOh7uYVVfo--






Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5358091.mMMZhaHaU6>