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