1 | /* Copyright (C) 1991-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
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. |
8 | |
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. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | /* Written by Paul Eggert <eggert@cs.ucla.edu>. */ |
19 | |
20 | #include <time.h> |
21 | |
22 | #include <limits.h> |
23 | #include <float.h> |
24 | #include <stdint.h> |
25 | |
26 | #define TYPE_BITS(type) (sizeof (type) * CHAR_BIT) |
27 | #define TYPE_FLOATING(type) ((type) 0.5 == 0.5) |
28 | #define TYPE_SIGNED(type) ((type) -1 < 0) |
29 | |
30 | /* Return the difference between TIME1 and TIME0, where TIME0 <= TIME1. |
31 | time_t is known to be an integer type. */ |
32 | |
33 | static double |
34 | subtract (__time64_t time1, __time64_t time0) |
35 | { |
36 | if (! TYPE_SIGNED (__time64_t)) |
37 | return time1 - time0; |
38 | else |
39 | { |
40 | /* Optimize the common special cases where time_t |
41 | can be converted to uintmax_t without losing information. */ |
42 | uintmax_t dt = (uintmax_t) time1 - (uintmax_t) time0; |
43 | double delta = dt; |
44 | |
45 | if (UINTMAX_MAX / 2 < INTMAX_MAX) |
46 | { |
47 | /* This is a rare host where uintmax_t has padding bits, and possibly |
48 | information was lost when converting time_t to uintmax_t. |
49 | Check for overflow by comparing dt/2 to (time1/2 - time0/2). |
50 | Overflow occurred if they differ by more than a small slop. |
51 | Thanks to Clive D.W. Feather for detailed technical advice about |
52 | hosts with padding bits. |
53 | |
54 | In the following code the "h" prefix means half. By range |
55 | analysis, we have: |
56 | |
57 | -0.5 <= ht1 - 0.5*time1 <= 0.5 |
58 | -0.5 <= ht0 - 0.5*time0 <= 0.5 |
59 | -1.0 <= dht - 0.5*(time1 - time0) <= 1.0 |
60 | |
61 | If overflow has not occurred, we also have: |
62 | |
63 | -0.5 <= hdt - 0.5*(time1 - time0) <= 0 |
64 | -1.0 <= dht - hdt <= 1.5 |
65 | |
66 | and since dht - hdt is an integer, we also have: |
67 | |
68 | -1 <= dht - hdt <= 1 |
69 | |
70 | or equivalently: |
71 | |
72 | 0 <= dht - hdt + 1 <= 2 |
73 | |
74 | In the above analysis, all the operators have their exact |
75 | mathematical semantics, not C semantics. However, dht - hdt + |
76 | 1 is unsigned in C, so it need not be compared to zero. */ |
77 | |
78 | uintmax_t hdt = dt / 2; |
79 | __time64_t ht1 = time1 / 2; |
80 | __time64_t ht0 = time0 / 2; |
81 | __time64_t dht = ht1 - ht0; |
82 | |
83 | if (2 < dht - hdt + 1) |
84 | { |
85 | /* Repair delta overflow. |
86 | |
87 | The following expression contains a second rounding, |
88 | so the result may not be the closest to the true answer. |
89 | This problem occurs only with very large differences. |
90 | It's too painful to fix this portably. */ |
91 | |
92 | delta = dt + 2.0L * (UINTMAX_MAX - UINTMAX_MAX / 2); |
93 | } |
94 | } |
95 | |
96 | return delta; |
97 | } |
98 | } |
99 | |
100 | /* Return the difference between TIME1 and TIME0. */ |
101 | double |
102 | __difftime64 (__time64_t time1, __time64_t time0) |
103 | { |
104 | /* Convert to double and then subtract if no double-rounding error could |
105 | result. */ |
106 | |
107 | if (TYPE_BITS (__time64_t) <= DBL_MANT_DIG |
108 | || (TYPE_FLOATING (__time64_t) && sizeof (__time64_t) < sizeof (long double))) |
109 | return (double) time1 - (double) time0; |
110 | |
111 | /* Likewise for long double. */ |
112 | |
113 | if (TYPE_BITS (__time64_t) <= LDBL_MANT_DIG || TYPE_FLOATING (__time64_t)) |
114 | return (long double) time1 - (long double) time0; |
115 | |
116 | /* Subtract the smaller integer from the larger, convert the difference to |
117 | double, and then negate if needed. */ |
118 | |
119 | return time1 < time0 ? - subtract (time0, time1) : subtract (time1, time0); |
120 | } |
121 | |
122 | /* Provide a 32-bit wrapper if needed. */ |
123 | |
124 | #if __TIMESIZE != 64 |
125 | |
126 | libc_hidden_def (__difftime64) |
127 | |
128 | double |
129 | __difftime (time_t time1, time_t time0) |
130 | { |
131 | return __difftime64 (time1, time0); |
132 | } |
133 | |
134 | #endif |
135 | |
136 | strong_alias (__difftime, difftime) |
137 | |