Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libc / time / difftime.c
blob4ec16ef86604fe731a6f2281e5136160df0c8fca
1 /* $NetBSD: difftime.c,v 1.9 2002/01/29 12:58:32 kleink Exp $ */
3 /*
4 ** This file is in the public domain, so clarified as of
5 ** 1996-06-05 by Arthur David Olson.
6 */
8 #include <sys/cdefs.h>
9 #if defined(LIBC_SCCS) && !defined(lint)
10 #if 0
11 static char elsieid[] = "@(#)difftime.c 8.1";
12 #else
13 __RCSID("$NetBSD: difftime.c,v 1.9 2002/01/29 12:58:32 kleink Exp $");
14 #endif
15 #endif /* LIBC_SCCS and not lint */
17 /*LINTLIBRARY*/
19 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
21 double
22 difftime(time1, time0)
23 const time_t time1;
24 const time_t time0;
27 ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
28 ** (assuming that the larger type has more precision).
29 ** This is the common real-world case circa 2004.
31 /* LINTED constant */
32 if (sizeof (double) > sizeof (time_t))
33 return (double) time1 - (double) time0;
34 /* LINTED constant */
35 if (!TYPE_INTEGRAL(time_t)) {
37 ** time_t is floating.
39 return time1 - time0;
41 /* LINTED constant */
42 if (!TYPE_SIGNED(time_t)) {
44 ** time_t is integral and unsigned.
45 ** The difference of two unsigned values can't overflow
46 ** if the minuend is greater than or equal to the subtrahend.
48 if (time1 >= time0)
49 return time1 - time0;
50 else return -((double) (time0 - time1));
53 ** time_t is integral and signed.
54 ** Handle cases where both time1 and time0 have the same sign
55 ** (meaning that their difference cannot overflow).
57 if ((time1 < 0) == (time0 < 0))
58 return time1 - time0;
60 ** time1 and time0 have opposite signs.
61 ** Punt if unsigned long is too narrow.
63 /* CONSTCOND */
64 if (sizeof (unsigned long) < sizeof (time_t))
65 return (double) time1 - (double) time0;
67 ** Stay calm...decent optimizers will eliminate the complexity below.
69 if (time1 >= 0 /* && time0 < 0 */)
70 return (unsigned long) time1 +
71 (unsigned long) (-(time0 + 1)) + 1;
72 return -(double) ((unsigned long) time0 +
73 (unsigned long) (-(time1 + 1)) + 1);