diff options
author | 2015-02-09 08:36:53 +0000 | |
---|---|---|
committer | 2015-02-09 08:36:53 +0000 | |
commit | 83b46c29fb9194c83e11468b7fefa9e478901e67 (patch) | |
tree | 336cac64c3eb41c48899ae9635a28b72cb7436f2 /lib/libc/time | |
parent | this removes just a few ifdef checks for things that should always be enabled, (diff) | |
download | wireguard-openbsd-83b46c29fb9194c83e11468b7fefa9e478901e67.tar.xz wireguard-openbsd-83b46c29fb9194c83e11468b7fefa9e478901e67.zip |
greatly simplfy and correct difftime for large time_t. ok guenther
Diffstat (limited to 'lib/libc/time')
-rw-r--r-- | lib/libc/time/difftime.c | 60 |
1 files changed, 5 insertions, 55 deletions
diff --git a/lib/libc/time/difftime.c b/lib/libc/time/difftime.c index 8521a60110c..1cb246b34c7 100644 --- a/lib/libc/time/difftime.c +++ b/lib/libc/time/difftime.c @@ -1,60 +1,10 @@ -/* $OpenBSD: difftime.c,v 1.9 2005/08/08 08:05:38 espie Exp $ */ -/* -** This file is in the public domain, so clarified as of -** 1996-06-05 by Arthur David Olson. -*/ +/* $OpenBSD: difftime.c,v 1.10 2015/02/09 08:36:53 tedu Exp $ */ +/* This file is placed in the public domain by Ted Unangst. */ -/*LINTLIBRARY*/ - -#include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */ +#include "private.h" double -difftime(time1, time0) -const time_t time1; -const time_t time0; +difftime(time_t time1, time_t time0) { - /* - ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract - ** (assuming that the larger type has more precision). - ** This is the common real-world case circa 2004. - */ - if (sizeof (double) > sizeof (time_t)) - return (double) time1 - (double) time0; - if (!TYPE_INTEGRAL(time_t)) { - /* - ** time_t is floating. - */ - return time1 - time0; - } - if (!TYPE_SIGNED(time_t)) { - /* - ** time_t is integral and unsigned. - ** The difference of two unsigned values can't overflow - ** if the minuend is greater than or equal to the subtrahend. - */ - if (time1 >= time0) - return time1 - time0; - else return -((double) (time0 - time1)); - } - /* - ** time_t is integral and signed. - ** Handle cases where both time1 and time0 have the same sign - ** (meaning that their difference cannot overflow). - */ - if ((time1 < 0) == (time0 < 0)) - return time1 - time0; - /* - ** time1 and time0 have opposite signs. - ** Punt if unsigned long is too narrow. - */ - if (sizeof (unsigned long) < sizeof (time_t)) - return (double) time1 - (double) time0; - /* - ** Stay calm...decent optimizers will eliminate the complexity below. - */ - if (time1 >= 0 /* && time0 < 0 */) - return (unsigned long) time1 + - (unsigned long) (-(time0 + 1)) + 1; - return -(double) ((unsigned long) time0 + - (unsigned long) (-(time1 + 1)) + 1); + return time1 - time0; } |