On 2006-12-29 00:55:18 -0800, Paul Eggert wrote:
[...]
> Obviously this code is buggy, at least in theory, due to the signed
> integer overflows.  But rewriting it is not so easy, since we have no
> INT_MAX to rescue us as we did in the bigtime_test loop.  Here's what
> I eventually came up with:
> 
>   for (;;)
>     {
>       time_t t = (time_t_max << 1) + 1;
>       if (t <= time_t_max)
>       break;
>       time_t_max = t;
>     }
>   time_t_min = - ((time_t) ~ (time_t) 0 == (time_t) -1) - time_t_max;
> 
> This isn't guaranteed to be portable by C99 either, of course; among
> other things, left-shift has undefined behavior on signed integer
> overflow.  I am relying on your heuristic advice to use left shift
> rather than multiplication by 2, so that GCC won't mess up here.  But
> it's a weak heuristic and I'm afraid it doesn't inspire a whole lot of
> confidence.

I don't understand why "GCC won't mess up".

You should probably use the upper bound obtained from sizeof(time_t)
and CHAR_BIT. As this bound is the actual maximum value when there
are no padding bits, this means that the code will be portable on all
C99 implementations where time_t has no padding bits.

Shouldn't GCC provide an extension to obtain the maximum and minimum
values of integer types?

-- 
Vincent Lefèvre <[EMAIL PROTECTED]> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)

Reply via email to