2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * the Systems Programming Group of the University of Utah Computer
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
39 /* static char sccsid[] = "@(#)atof.c 5.2 (Berkeley) 4/12/91"; */
40 static char rcsid
[] = "";
41 #endif /* LIBC_SCCS and not lint */
44 * simple atof() for IEEE 754 architectures
47 #include <machine/endian.h>
52 static double twoemax
= 9007199254740992.; /*2^53*/
54 /* attempt to be as exact as possible */
59 #if BYTE_ORDER == BIG_ENDIAN
60 { 0x40140000, 0x00000000 }, /* 5 */
61 { 0x40390000, 0x00000000 }, /* 25 */
62 { 0x40838800, 0x00000000 }, /* 625 */
63 { 0x4117d784, 0x00000000 }, /* 390625 */
64 { 0x4241c379, 0x37e08000 }, /* 152587890625 */
65 { 0x4493b8b5, 0xb5056e17 }, /* 2.3283064365387e+022 */
66 { 0x49384f03, 0xe93ff9f6 }, /* 5.42101086242753e+044 */
67 { 0x52827748, 0xf9301d33 }, /* 2.93873587705572e+089 */
68 { 0x65154fdd, 0x7f73bf3f } /* 8.63616855509445e+178 */
69 #else /* BYTE_ORDER == LITTLE_ENDIAN */
70 { 0x00000000, 0x40140000 }, /* 5 */
71 { 0x00000000, 0x40390000 }, /* 25 */
72 { 0x00000000, 0x40838800 }, /* 625 */
73 { 0x00000000, 0x4117d784 }, /* 390625 */
74 { 0x37e08000, 0x4241c379 }, /* 152587890625 */
75 { 0xb5056e17, 0x4493b8b5 }, /* 2.3283064365387e+022 */
76 { 0xe93ff9f6, 0x49384f03 }, /* 5.42101086242753e+044 */
77 { 0xf9301d33, 0x52827748 }, /* 2.93873587705572e+089 */
78 { 0x7f73bf3f, 0x65154fdd } /* 8.63616855509445e+178 */
84 register const char *p
;
88 register int eexp
= 0;
98 if ((c
= *p
++) == '-')
105 while ((c
= *p
++) && isdigit(c
))
107 fl
= 10 * fl
+ (c
-'0');
112 while ((c
= *p
++) && isdigit(c
))
114 fl
= 10 * fl
+ (c
-'0');
118 if (c
== 'E' || c
== 'e') {
119 if ((c
= *p
++) == '-')
125 while ((c
= *p
++) && isdigit(c
))
126 eexp
= 10 * eexp
+ (c
-'0');
136 for (c
= 0; exp
&& c
< sizeof exp5
/ sizeof exp5
[0]; ++c
) {
138 flexp
*= *(double *)&exp5
[c
];
147 fl
= ldexp(fl
, bexp
);
149 return neg
< 0 ? -fl
: fl
;