]>
git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/difftime.c
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
6 #pragma clang diagnostic push
7 #pragma clang diagnostic ignored "-Wunreachable-code"
12 static char elsieid
[] __unused
= "@(#)difftime.c 8.1";
13 #endif /* !defined NOID */
14 #endif /* !defined lint */
15 __FBSDID("$FreeBSD: src/lib/libc/stdtime/difftime.c,v 1.9 2009/05/23 06:31:50 edwin Exp $");
19 #include "namespace.h"
20 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
21 #include "un-namespace.h"
24 difftime(time1
, time0
)
29 ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
30 ** (assuming that the larger type has more precision).
31 ** This is the common real-world case circa 2004.
33 if (sizeof (double) > sizeof (time_t))
34 return (double) time1
- (double) time0
;
35 if (!TYPE_INTEGRAL(time_t)) {
37 ** time_t is floating.
41 if (!TYPE_SIGNED(time_t)) {
43 ** time_t is integral and unsigned.
44 ** The difference of two unsigned values can't overflow
45 ** if the minuend is greater than or equal to the subtrahend.
49 else return -((double) (time0
- time1
));
52 ** time_t is integral and signed.
53 ** Handle cases where both time1 and time0 have the same sign
54 ** (meaning that their difference cannot overflow).
56 if ((time1
< 0) == (time0
< 0))
59 ** time1 and time0 have opposite signs.
60 ** Punt if unsigned long is too narrow.
62 if (sizeof (unsigned long) < sizeof (time_t))
63 return (double) time1
- (double) time0
;
65 ** Stay calm...decent optimizers will eliminate the complexity below.
67 if (time1
>= 0 /* && time0 < 0 */)
68 return (unsigned long) time1
+
69 (unsigned long) (-(time0
+ 1)) + 1;
70 return -(double) ((unsigned long) time0
+
71 (unsigned long) (-(time1
+ 1)) + 1);
73 #pragma clang diagnostic pop