Difftime code

Clive D.W. Feather clive at demon.net
Tue Aug 10 05:59:15 UTC 2004


Paul Eggert said:
> Thanks for the tour of some of the darker corners of C; I didn't
> realize all the ins and outs of hosts with padding bits.

You're welcome.

> /*
> ** Algorithm courtesy Paul Eggert (eggert at cs.ucla.edu).

No credit?

> #ifndef HAVE_STDINT_H
> #define HAVE_STDINT_H		(199901 <= __STDC_VERSION__)

19910L (can you say "16 bits"?)

> #if HAVE_STDINT_H
> #include <stdint.h>
> #define uintmax uintmax_t

I always feel typedef is safer for this sort of thing.

> 	/*
> 	** Use floating point if there should be no double-rounding error.
> 	** However, avoid long double if it must be wider than needed,
> 	** as it's sometimes much more expensive in these cases
> 	** (e.g., 64-bit sparc).
> 	*/
> 	if (TYPE_BIT(time_t) <= DBL_MANT_DIG
> 	    || (TYPE_FLOATING(time_t)
> 		&& sizeof(time_t) < sizeof(long_double))) {

Using != allows for certain perverse implementations. Not important,
though.

> 	if ((TYPE_BIT(time_t) <= LDBL_MANT_DIG
> 	     && (TYPE_BIT(time_t) == LDBL_MANT_DIG
> 		 || (TYPE_SIGNED(time_t) && UINTMAX_MAX / 2 < INTMAX_MAX)))

Why write this last test like this?
UINTMAX_MAX and INTMAX_MAX are both going to be numbers of the form 2**N-1.
Furthermore, UINTMAX_MAX >= INTMAX_MAX. So that last one can be done as
UINTMAX_MAX == INTMAX_MAX.

It happens again later.

-- 
Clive D.W. Feather  | Work:  <clive at demon.net>   | Tel:    +44 20 8495 6138
Internet Expert     | Home:  <clive at davros.org>  | Fax:    +44 870 051 9937
Demon Internet      | WWW: http://www.davros.org | Mobile: +44 7973 377646
Thus plc            |                            |



More information about the tz mailing list