2024-01-01 18:12:26 +00:00
|
|
|
/* Copyright (C) 1991-2024 Free Software Foundation, Inc.
|
1997-01-01 15:28:18 +00:00
|
|
|
This file is part of the GNU C Library.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-01-01 15:28:18 +00:00
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-01-01 15:28:18 +00:00
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2004-11-11 22:31:17 +00:00
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2004-11-11 22:31:17 +00:00
|
|
|
#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
|
2018-12-20 21:14:08 +00:00
|
|
|
subtract (__time64_t time1, __time64_t time0)
|
2004-11-11 22:31:17 +00:00
|
|
|
{
|
2018-12-20 21:14:08 +00:00
|
|
|
if (! TYPE_SIGNED (__time64_t))
|
2004-11-11 22:31:17 +00:00
|
|
|
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;
|
2018-12-20 21:14:08 +00:00
|
|
|
__time64_t ht1 = time1 / 2;
|
|
|
|
__time64_t ht0 = time0 / 2;
|
|
|
|
__time64_t dht = ht1 - ht0;
|
2004-11-11 22:31:17 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
/* Return the difference between TIME1 and TIME0. */
|
|
|
|
double
|
2018-12-20 21:14:08 +00:00
|
|
|
__difftime64 (__time64_t time1, __time64_t time0)
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
2004-11-11 22:31:17 +00:00
|
|
|
/* Convert to double and then subtract if no double-rounding error could
|
|
|
|
result. */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2018-12-20 21:14:08 +00:00
|
|
|
if (TYPE_BITS (__time64_t) <= DBL_MANT_DIG
|
|
|
|
|| (TYPE_FLOATING (__time64_t) && sizeof (__time64_t) < sizeof (long double)))
|
1995-02-18 01:27:10 +00:00
|
|
|
return (double) time1 - (double) time0;
|
2004-11-11 22:31:17 +00:00
|
|
|
|
|
|
|
/* Likewise for long double. */
|
|
|
|
|
2018-12-20 21:14:08 +00:00
|
|
|
if (TYPE_BITS (__time64_t) <= LDBL_MANT_DIG || TYPE_FLOATING (__time64_t))
|
1997-01-01 15:28:18 +00:00
|
|
|
return (long double) time1 - (long double) time0;
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2004-11-11 22:31:17 +00:00
|
|
|
/* 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);
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
2018-12-20 21:14:08 +00:00
|
|
|
|
|
|
|
/* Provide a 32-bit wrapper if needed. */
|
|
|
|
|
|
|
|
#if __TIMESIZE != 64
|
|
|
|
|
|
|
|
libc_hidden_def (__difftime64)
|
|
|
|
|
|
|
|
double
|
|
|
|
__difftime (time_t time1, time_t time0)
|
|
|
|
{
|
|
|
|
return __difftime64 (time1, time0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-04-01 21:56:34 +00:00
|
|
|
strong_alias (__difftime, difftime)
|