]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/difftime.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / stdtime / FreeBSD / difftime.c
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
4 */
5
6 #pragma clang diagnostic push
7 #pragma clang diagnostic ignored "-Wunreachable-code"
8
9 #include <sys/cdefs.h>
10 #ifndef lint
11 #ifndef NOID
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 $");
16
17 /*LINTLIBRARY*/
18
19 #include "namespace.h"
20 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
21 #include "un-namespace.h"
22
23 double
24 difftime(time1, time0)
25 const time_t time1;
26 const time_t time0;
27 {
28 /*
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.
32 */
33 if (sizeof (double) > sizeof (time_t))
34 return (double) time1 - (double) time0;
35 if (!TYPE_INTEGRAL(time_t)) {
36 /*
37 ** time_t is floating.
38 */
39 return time1 - time0;
40 }
41 if (!TYPE_SIGNED(time_t)) {
42 /*
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.
46 */
47 if (time1 >= time0)
48 return time1 - time0;
49 else return -((double) (time0 - time1));
50 }
51 /*
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).
55 */
56 if ((time1 < 0) == (time0 < 0))
57 return time1 - time0;
58 /*
59 ** time1 and time0 have opposite signs.
60 ** Punt if unsigned long is too narrow.
61 */
62 if (sizeof (unsigned long) < sizeof (time_t))
63 return (double) time1 - (double) time0;
64 /*
65 ** Stay calm...decent optimizers will eliminate the complexity below.
66 */
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);
72 }
73 #pragma clang diagnostic pop