author | William Roberts <williamr@symbian.org> |
Thu, 22 Jul 2010 16:48:56 +0100 | |
branch | GCC_SURGE |
changeset 45 | 4b03adbd26ca |
parent 18 | 47c74d1534e1 |
parent 34 | 5fae379060a7 |
permissions | -rw-r--r-- |
31 | 1 |
/* |
34
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
2 |
** This file is in the public domain, so clarified as of |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
3 |
** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov). |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
4 |
** © Portions copyright (c) 2005-2006 Nokia Corporation. All rights reserved. |
31 | 5 |
*/ |
6 |
||
7 |
#include <sys/cdefs.h> |
|
8 |
#ifndef lint |
|
9 |
#ifndef NOID |
|
10 |
static char elsieid[] __unused = "@(#)difftime.c 7.9"; |
|
11 |
#endif /* !defined NOID */ |
|
12 |
#endif /* !defined lint */ |
|
13 |
__FBSDID("$FreeBSD: src/lib/libc/stdtime/difftime.c,v 1.8 2004/06/14 10:31:52 stefanf Exp $"); |
|
14 |
||
15 |
/*LINTLIBRARY*/ |
|
16 |
||
17 |
#include "namespace.h" |
|
18 |
#include "private.h" |
|
19 |
#include "un-namespace.h" |
|
20 |
||
21 |
/* |
|
22 |
** Algorithm courtesy Paul Eggert (eggert@twinsun.com). |
|
23 |
*/ |
|
24 |
||
25 |
#ifdef HAVE_LONG_DOUBLE |
|
26 |
#define long_double long double |
|
27 |
#endif /* defined HAVE_LONG_DOUBLE */ |
|
28 |
#ifndef HAVE_LONG_DOUBLE |
|
29 |
#define long_double double |
|
30 |
#endif /* !defined HAVE_LONG_DOUBLE */ |
|
31 |
||
32 |
EXPORT_C |
|
33 |
double |
|
34 |
difftime(time1, time0) |
|
35 |
const time_t time1; |
|
36 |
const time_t time0; |
|
37 |
{ |
|
38 |
time_t delta; |
|
39 |
time_t hibit; |
|
40 |
||
41 |
{ |
|
42 |
time_t tt; |
|
43 |
double d; |
|
44 |
long_double ld; |
|
45 |
||
46 |
if (sizeof tt < sizeof d) |
|
47 |
return (double) time1 - (double) time0; |
|
48 |
if (sizeof tt < sizeof ld) |
|
49 |
return (long_double) time1 - (long_double) time0; |
|
50 |
} |
|
51 |
if (time1 < time0) |
|
52 |
return -difftime(time0, time1); |
|
53 |
/* |
|
54 |
** As much as possible, avoid loss of precision |
|
55 |
** by computing the difference before converting to double. |
|
56 |
*/ |
|
57 |
delta = time1 - time0; |
|
58 |
if (delta >= 0) |
|
59 |
return delta; |
|
60 |
/* |
|
61 |
** Repair delta overflow. |
|
62 |
*/ |
|
63 |
hibit = (~ (time_t) 0) << (TYPE_BIT(time_t) - 1); |
|
64 |
/* |
|
65 |
** The following expression rounds twice, which means |
|
66 |
** the result may not be the closest to the true answer. |
|
67 |
** For example, suppose time_t is 64-bit signed int, |
|
68 |
** long_double is IEEE 754 double with default rounding, |
|
69 |
** time1 = 9223372036854775807 and time0 = -1536. |
|
70 |
** Then the true difference is 9223372036854777343, |
|
71 |
** which rounds to 9223372036854777856 |
|
72 |
** with a total error of 513. |
|
73 |
** But delta overflows to -9223372036854774273, |
|
74 |
** which rounds to -9223372036854774784, and correcting |
|
75 |
** this by subtracting 2 * (long_double) hibit |
|
76 |
** (i.e. by adding 2**64 = 18446744073709551616) |
|
77 |
** yields 9223372036854776832, which |
|
78 |
** rounds to 9223372036854775808 |
|
79 |
** with a total error of 1535 instead. |
|
80 |
** This problem occurs only with very large differences. |
|
81 |
** It's too painful to fix this portably. |
|
82 |
** We are not alone in this problem; |
|
83 |
** some C compilers round twice when converting |
|
84 |
** large unsigned types to small floating types, |
|
85 |
** so if time_t is unsigned the "return delta" above |
|
86 |
** has the same double-rounding problem with those compilers. |
|
87 |
*/ |
|
88 |
return delta - 2 * (long_double) hibit; |
|
89 |
} |