Updated to fedora-glibc-20080524T2218
[glibc.git] / time / difftime.c
blobad896e207d40c2d745c381c80e3fcac14b544da8
1 /* Copyright (C) 1991, 1994, 1996, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 /* Written by Paul Eggert <eggert@cs.ucla.edu>. */
21 #include <time.h>
23 #include <limits.h>
24 #include <float.h>
25 #include <stdint.h>
27 #define TYPE_BITS(type) (sizeof (type) * CHAR_BIT)
28 #define TYPE_FLOATING(type) ((type) 0.5 == 0.5)
29 #define TYPE_SIGNED(type) ((type) -1 < 0)
31 /* Return the difference between TIME1 and TIME0, where TIME0 <= TIME1.
32 time_t is known to be an integer type. */
34 static double
35 subtract (time_t time1, time_t time0)
37 if (! TYPE_SIGNED (time_t))
38 return time1 - time0;
39 else
41 /* Optimize the common special cases where time_t
42 can be converted to uintmax_t without losing information. */
43 uintmax_t dt = (uintmax_t) time1 - (uintmax_t) time0;
44 double delta = dt;
46 if (UINTMAX_MAX / 2 < INTMAX_MAX)
48 /* This is a rare host where uintmax_t has padding bits, and possibly
49 information was lost when converting time_t to uintmax_t.
50 Check for overflow by comparing dt/2 to (time1/2 - time0/2).
51 Overflow occurred if they differ by more than a small slop.
52 Thanks to Clive D.W. Feather for detailed technical advice about
53 hosts with padding bits.
55 In the following code the "h" prefix means half. By range
56 analysis, we have:
58 -0.5 <= ht1 - 0.5*time1 <= 0.5
59 -0.5 <= ht0 - 0.5*time0 <= 0.5
60 -1.0 <= dht - 0.5*(time1 - time0) <= 1.0
62 If overflow has not occurred, we also have:
64 -0.5 <= hdt - 0.5*(time1 - time0) <= 0
65 -1.0 <= dht - hdt <= 1.5
67 and since dht - hdt is an integer, we also have:
69 -1 <= dht - hdt <= 1
71 or equivalently:
73 0 <= dht - hdt + 1 <= 2
75 In the above analysis, all the operators have their exact
76 mathematical semantics, not C semantics. However, dht - hdt +
77 1 is unsigned in C, so it need not be compared to zero. */
79 uintmax_t hdt = dt / 2;
80 time_t ht1 = time1 / 2;
81 time_t ht0 = time0 / 2;
82 time_t dht = ht1 - ht0;
84 if (2 < dht - hdt + 1)
86 /* Repair delta overflow.
88 The following expression contains a second rounding,
89 so the result may not be the closest to the true answer.
90 This problem occurs only with very large differences.
91 It's too painful to fix this portably. */
93 delta = dt + 2.0L * (UINTMAX_MAX - UINTMAX_MAX / 2);
97 return delta;
101 /* Return the difference between TIME1 and TIME0. */
102 double
103 __difftime (time_t time1, time_t time0)
105 /* Convert to double and then subtract if no double-rounding error could
106 result. */
108 if (TYPE_BITS (time_t) <= DBL_MANT_DIG
109 || (TYPE_FLOATING (time_t) && sizeof (time_t) < sizeof (long double)))
110 return (double) time1 - (double) time0;
112 /* Likewise for long double. */
114 if (TYPE_BITS (time_t) <= LDBL_MANT_DIG || TYPE_FLOATING (time_t))
115 return (long double) time1 - (long double) time0;
117 /* Subtract the smaller integer from the larger, convert the difference to
118 double, and then negate if needed. */
120 return time1 < time0 ? - subtract (time0, time1) : subtract (time1, time0);
122 strong_alias (__difftime, difftime)