diff options
author | Jakub Jelinek <jakub@redhat.com> | 2004-11-12 16:56:15 +0000 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2004-11-12 16:56:15 +0000 |
commit | 8ae4ba1c6d925bdd110d6bbc04f6338065dd56ac (patch) | |
tree | 48c51adddf5da14da5cf924e6c6e6e946eaea57f /time | |
parent | 9abf55c24c31a53d987ebf53e46cbd64eab417bc (diff) | |
download | glibc-8ae4ba1c6d925bdd110d6bbc04f6338065dd56ac.tar.gz |
Updated to fedora-glibc-20041112T1640
Diffstat (limited to 'time')
-rw-r--r-- | time/difftime.c | 129 | ||||
-rw-r--r-- | time/mktime.c | 46 |
2 files changed, 123 insertions, 52 deletions
diff --git a/time/difftime.c b/time/difftime.c index 228090c057..ad896e207d 100644 --- a/time/difftime.c +++ b/time/difftime.c @@ -16,52 +16,107 @@ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ +/* Written by Paul Eggert <eggert@cs.ucla.edu>. */ + #include <time.h> -#include <values.h> +#include <limits.h> +#include <float.h> +#include <stdint.h> + +#define TYPE_BITS(type) (sizeof (type) * CHAR_BIT) +#define TYPE_FLOATING(type) ((type) 0.5 == 0.5) +#define TYPE_SIGNED(type) ((type) -1 < 0) + +/* Return the difference between TIME1 and TIME0, where TIME0 <= TIME1. + time_t is known to be an integer type. */ + +static double +subtract (time_t time1, time_t time0) +{ + if (! TYPE_SIGNED (time_t)) + return time1 - time0; + else + { + /* Optimize the common special cases where time_t + can be converted to uintmax_t without losing information. */ + uintmax_t dt = (uintmax_t) time1 - (uintmax_t) time0; + double delta = dt; + + if (UINTMAX_MAX / 2 < INTMAX_MAX) + { + /* This is a rare host where uintmax_t has padding bits, and possibly + information was lost when converting time_t to uintmax_t. + Check for overflow by comparing dt/2 to (time1/2 - time0/2). + Overflow occurred if they differ by more than a small slop. + Thanks to Clive D.W. Feather for detailed technical advice about + hosts with padding bits. + + In the following code the "h" prefix means half. By range + analysis, we have: + + -0.5 <= ht1 - 0.5*time1 <= 0.5 + -0.5 <= ht0 - 0.5*time0 <= 0.5 + -1.0 <= dht - 0.5*(time1 - time0) <= 1.0 + + If overflow has not occurred, we also have: + + -0.5 <= hdt - 0.5*(time1 - time0) <= 0 + -1.0 <= dht - hdt <= 1.5 + + and since dht - hdt is an integer, we also have: + + -1 <= dht - hdt <= 1 + + or equivalently: + + 0 <= dht - hdt + 1 <= 2 + + In the above analysis, all the operators have their exact + mathematical semantics, not C semantics. However, dht - hdt + + 1 is unsigned in C, so it need not be compared to zero. */ + + uintmax_t hdt = dt / 2; + time_t ht1 = time1 / 2; + time_t ht0 = time0 / 2; + time_t dht = ht1 - ht0; + + if (2 < dht - hdt + 1) + { + /* Repair delta overflow. + + The following expression contains a second rounding, + so the result may not be the closest to the true answer. + This problem occurs only with very large differences. + It's too painful to fix this portably. */ + + delta = dt + 2.0L * (UINTMAX_MAX - UINTMAX_MAX / 2); + } + } + + return delta; + } +} /* Return the difference between TIME1 and TIME0. */ double -__difftime (time1, time0) - time_t time1; - time_t time0; +__difftime (time_t time1, time_t time0) { - /* Algorithm courtesy Paul Eggert (eggert@twinsun.com). */ - - time_t delta, hibit; + /* Convert to double and then subtract if no double-rounding error could + result. */ - if (sizeof (time_t) < sizeof (double)) + if (TYPE_BITS (time_t) <= DBL_MANT_DIG + || (TYPE_FLOATING (time_t) && sizeof (time_t) < sizeof (long double))) return (double) time1 - (double) time0; - if (sizeof (time_t) < sizeof (long double)) + + /* Likewise for long double. */ + + if (TYPE_BITS (time_t) <= LDBL_MANT_DIG || TYPE_FLOATING (time_t)) return (long double) time1 - (long double) time0; - if (time1 < time0) - return - __difftime (time0, time1); - - /* As much as possible, avoid loss of precision by computing the - difference before converting to double. */ - delta = time1 - time0; - if (delta >= 0) - return delta; - - /* Repair delta overflow. */ - hibit = (~ (time_t) 0) << (_TYPEBITS (time_t) - 1); - - /* The following expression rounds twice, which means the result may not - be the closest to the true answer. For example, suppose time_t is - 64-bit signed int, long_double is IEEE 754 double with default - rounding, time1 = 9223372036854775807 and time0 = -1536. Then the - true difference is 9223372036854777343, which rounds to - 9223372036854777856 with a total error of 513. But delta overflows to - -9223372036854774273, which rounds to -9223372036854774784, and - correcting this by subtracting 2 * (long_double) hibit (i.e. by adding - 2**64 = 18446744073709551616) yields 9223372036854776832, which rounds - to 9223372036854775808 with a total error of 1535 instead. This - problem occurs only with very large differences. It's too painful to - fix this portably. We are not alone in this problem; many C compilers - round twice when converting large unsigned types to small floating - types, so if time_t is unsigned the "return delta" above has the same - double-rounding problem. */ - return delta - 2 * (long double) hibit; + /* Subtract the smaller integer from the larger, convert the difference to + double, and then negate if needed. */ + + return time1 < time0 ? - subtract (time0, time1) : subtract (time1, time0); } strong_alias (__difftime, difftime) diff --git a/time/mktime.c b/time/mktime.c index 72b20128a3..280f5f47d5 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -46,6 +46,21 @@ # define mktime my_mktime #endif /* DEBUG */ +/* Shift A right by B bits portably, by dividing A by 2**B and + truncating towards minus infinity. A and B should be free of side + effects, and B should be in the range 0 <= B <= INT_BITS - 2, where + INT_BITS is the number of useful bits in an int. GNU code can + assume that INT_BITS is at least 32. + + ISO C99 says that A >> B is implementation-defined if A < 0. Some + implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift + right in the usual way when A < 0, so SHR falls back on division if + ordinary A >> B doesn't seem to be the usual signed shift. */ +#define SHR(a, b) \ + (-1 >> 1 == -1 \ + ? (a) >> (b) \ + : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0)) + /* The extra casts work around common compiler bugs. */ #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) /* The outer cast is needed to work around a bug in Cray C 5.0.3.0. @@ -60,14 +75,13 @@ #ifndef TIME_T_MAX # define TIME_T_MAX TYPE_MAXIMUM (time_t) #endif -#define TIME_T_MIDPOINT (((TIME_T_MIN + TIME_T_MAX) >> 1) + 1) +#define TIME_T_MIDPOINT (SHR (TIME_T_MIN + TIME_T_MAX, 1) + 1) /* Verify a requirement at compile-time (unlike assert, which is runtime). */ #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } verify (time_t_is_integer, (time_t) 0.5 == 0); verify (twos_complement_arithmetic, -1 == ~1 + 1); -verify (right_shift_propagates_sign, -1 >> 1 == -1); /* The code also assumes that signed integer overflow silently wraps around, but this assumption can't be stated without causing a diagnostic on some hosts. */ @@ -133,12 +147,12 @@ ydhms_diff (long int year1, long int yday1, int hour1, int min1, int sec1, /* Compute intervening leap days correctly even if year is negative. Take care to avoid integer overflow here. */ - int a4 = (year1 >> 2) + (TM_YEAR_BASE >> 2) - ! (year1 & 3); - int b4 = (year0 >> 2) + (TM_YEAR_BASE >> 2) - ! (year0 & 3); + int a4 = SHR (year1, 2) + SHR (TM_YEAR_BASE, 2) - ! (year1 & 3); + int b4 = SHR (year0, 2) + SHR (TM_YEAR_BASE, 2) - ! (year0 & 3); int a100 = a4 / 25 - (a4 % 25 < 0); int b100 = b4 / 25 - (b4 % 25 < 0); - int a400 = a100 >> 2; - int b400 = b100 >> 2; + int a400 = SHR (a100, 2); + int b400 = SHR (b100, 2); int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); /* Compute the desired time in time_t precision. Overflow might @@ -322,14 +336,16 @@ __mktime_internal (struct tm *tp, int LOG2_YEARS_PER_BIENNIUM = 1; int approx_requested_biennia = - ((year_requested >> LOG2_YEARS_PER_BIENNIUM) - - ((EPOCH_YEAR - TM_YEAR_BASE) >> LOG2_YEARS_PER_BIENNIUM) - + (mday >> ALOG2_DAYS_PER_BIENNIUM) - + (hour >> ALOG2_HOURS_PER_BIENNIUM) - + (min >> ALOG2_MINUTES_PER_BIENNIUM) - + (LEAP_SECONDS_POSSIBLE ? 0 : sec >> ALOG2_SECONDS_PER_BIENNIUM)); - - int approx_biennia = t0 >> ALOG2_SECONDS_PER_BIENNIUM; + (SHR (year_requested, LOG2_YEARS_PER_BIENNIUM) + - SHR (EPOCH_YEAR - TM_YEAR_BASE, LOG2_YEARS_PER_BIENNIUM) + + SHR (mday, ALOG2_DAYS_PER_BIENNIUM) + + SHR (hour, ALOG2_HOURS_PER_BIENNIUM) + + SHR (min, ALOG2_MINUTES_PER_BIENNIUM) + + (LEAP_SECONDS_POSSIBLE + ? 0 + : SHR (sec, ALOG2_SECONDS_PER_BIENNIUM))); + + int approx_biennia = SHR (t0, ALOG2_SECONDS_PER_BIENNIUM); int diff = approx_biennia - approx_requested_biennia; int abs_diff = diff < 0 ? - diff : diff; @@ -347,7 +363,7 @@ __mktime_internal (struct tm *tp, /* Overflow occurred. Try repairing it; this might work if the time zone offset is enough to undo the overflow. */ time_t repaired_t0 = -1 - t0; - approx_biennia = repaired_t0 >> ALOG2_SECONDS_PER_BIENNIUM; + approx_biennia = SHR (repaired_t0, ALOG2_SECONDS_PER_BIENNIUM); diff = approx_biennia - approx_requested_biennia; abs_diff = diff < 0 ? - diff : diff; if (overflow_threshold < abs_diff) |