Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / pc532 / stdlib / nsatof.c
blob03371b56641488825a0852b57a1cc4b0a4cd8b50
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * the Systems Programming Group of the University of Utah Computer
7 * Science Department.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
35 * SUCH DAMAGE.
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>
48 #include <stdlib.h>
49 #include <math.h>
50 #include <ctype.h>
52 static double twoemax = 9007199254740992.; /*2^53*/
54 /* attempt to be as exact as possible */
55 static struct {
56 long low_word;
57 long high_word;
58 } exp5[] = {
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 */
79 #endif
82 double
83 atof(p)
84 register const char *p;
86 register int c;
87 register int exp = 0;
88 register int eexp = 0;
89 double fl = 0;
90 double flexp = 1.0;
91 int bexp;
92 int neg = 1;
93 int negexp = 1;
95 while (isspace(*p))
96 ++p;
98 if ((c = *p++) == '-')
99 neg = -1;
100 else if (c == '+')
101 /* skip it */;
102 else
103 --p;
105 while ((c = *p++) && isdigit(c))
106 if (fl < twoemax)
107 fl = 10 * fl + (c-'0');
108 else
109 ++exp;
111 if (c == '.')
112 while ((c = *p++) && isdigit(c))
113 if (fl < twoemax) {
114 fl = 10 * fl + (c-'0');
115 --exp;
118 if (c == 'E' || c == 'e') {
119 if ((c = *p++) == '-')
120 negexp = -1;
121 else if (c == '+')
122 /* skip it */;
123 else
124 --p;
125 while ((c = *p++) && isdigit(c))
126 eexp = 10 * eexp + (c-'0');
127 if (negexp < 0)
128 eexp = -eexp;
129 exp += eexp;
132 bexp = exp;
133 if (exp < 0)
134 exp = -exp;
136 for (c = 0; exp && c < sizeof exp5 / sizeof exp5[0]; ++c) {
137 if (exp & 1)
138 flexp *= *(double *)&exp5[c];
139 exp >>= 1;
142 if (bexp < 0)
143 fl /= flexp;
144 else
145 fl *= flexp;
147 fl = ldexp(fl, bexp);
149 return neg < 0 ? -fl : fl;