dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libast / common / sfio / sfstrtof.h
blob2bde892365036aa37602579730540c2a0114f8ea
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
23 * AT&T Research
24 * Glenn Fowler & Phong Vo
26 * common header and implementation for
28 * strtof strtod strtold _sfdscan
29 * strntof strntod strntold
31 * define these macros to instantiate an implementation:
33 * S2F_function the function name
34 * S2F_static <0:export =0:extern >0:static
35 * S2F_type 0:float 1:double 2:long.double
36 * S2F_qualifier 1 for optional [fFlL] qualifier suffix
37 * S2F_size 1 for interface with size_t second arg
38 * S2F_scan 1 for alternate interface with these arguments:
39 * void* handle
40 * int (*getchar)(void* handle, int flag)
41 * exactly one extra (*getchar)() is done, i.e.,
42 * the caller must do the pushback
43 * flag==0 get next char
44 * flag==1 no number seen
45 * return 0 on error or EOF
48 #include "sfhdr.h"
49 #include "FEATURE/float"
52 * the default is _sfdscan for standalone sfio compatibility
55 #if !defined(S2F_function)
56 #define S2F_function _sfdscan
57 #define S2F_static 1
58 #define S2F_type 2
59 #define S2F_scan 1
60 #ifndef elementsof
61 #define elementsof(a) (sizeof(a)/sizeof(a[0]))
62 #endif
63 #endif
65 #if S2F_type == 2 && _ast_fltmax_double
66 #undef S2F_type
67 #define S2F_type 1
68 #endif
70 #if S2F_type == 0
71 #define S2F_number float
72 #define S2F_ldexp ldexp
73 #define S2F_pow10 _Sffpow10
74 #define S2F_inf _Sffinf
75 #define S2F_nan _Sffnan
76 #define S2F_min (FLT_MIN)
77 #define S2F_max (FLT_MAX)
78 #define S2F_exp_10_min (FLT_MIN_10_EXP)
79 #define S2F_exp_10_max (FLT_MAX_10_EXP)
80 #define S2F_exp_2_min (FLT_MIN_EXP)
81 #define S2F_exp_2_max (FLT_MAX_EXP)
82 #endif
83 #if S2F_type == 1
84 #define S2F_number double
85 #define S2F_ldexp ldexp
86 #define S2F_pow10 _Sfdpow10
87 #define S2F_inf _Sfdinf
88 #define S2F_nan _Sfdnan
89 #define S2F_min (DBL_MIN)
90 #define S2F_max (DBL_MAX)
91 #define S2F_exp_10_min (DBL_MIN_10_EXP)
92 #define S2F_exp_10_max (DBL_MAX_10_EXP)
93 #define S2F_exp_2_min (DBL_MIN_EXP)
94 #define S2F_exp_2_max (DBL_MAX_EXP)
95 #endif
96 #if S2F_type == 2
97 #define S2F_number long double
98 #define S2F_ldexp ldexpl
99 #define S2F_pow10 _Sflpow10
100 #define S2F_inf _Sflinf
101 #define S2F_nan _Sflnan
102 #define S2F_min (LDBL_MIN)
103 #define S2F_max (LDBL_MAX)
104 #define S2F_exp_10_min (LDBL_MIN_10_EXP)
105 #define S2F_exp_10_max (LDBL_MAX_10_EXP)
106 #define S2F_exp_2_min (LDBL_MIN_EXP)
107 #define S2F_exp_2_max (LDBL_MAX_EXP)
108 #endif
110 #if -S2F_exp_10_min < S2F_exp_10_max
111 #define S2F_exp_10_abs (-S2F_exp_10_min)
112 #else
113 #define S2F_exp_10_abs S2F_exp_10_max
114 #endif
116 #define S2F_batch _ast_flt_unsigned_max_t
118 #undef ERR /* who co-opted this namespace? */
120 #if S2F_scan
122 typedef int (*S2F_get_f)_ARG_((void*, int));
124 #define ERR(e)
125 #define GET(p) (*get)(p,0)
126 #define NON(p) (*get)(p,1)
127 #define PUT(p)
128 #define REV(p,t,b)
129 #define SET(p,t,b)
131 #else
133 #define ERR(e) (errno=(e))
134 #define NON(p)
136 #if S2F_size
137 #define GET(p) (((p)<(z))?(*p++):(back=0))
138 #define PUT(p) (end?(*end=(char*)p-back):(char*)0)
139 #define REV(p,t,b) (p=t,back=b)
140 #define SET(p,t,b) (t=p,b=back)
141 #else
142 #define GET(p) (*p++)
143 #define PUT(p) (end?(*end=(char*)p-1):(char*)0)
144 #define REV(p,t,b) (p=t)
145 #define SET(p,t,b) (t=p)
146 #endif
148 #endif
150 typedef struct S2F_part_s
152 S2F_batch batch;
153 int digits;
154 } S2F_part_t;
156 #if !defined(ERANGE)
157 #define ERANGE EINVAL
158 #endif
160 #if S2F_static > 0
161 static
162 #else
163 #if S2F_static < 0 || !defined(S2F_static)
164 #if defined(__EXPORT__)
165 #define extern __EXPORT__
166 #endif
167 extern
168 #undef extern
169 #endif
170 #endif
171 S2F_number
172 #if S2F_scan
173 #if __STD_C
174 S2F_function(void* s, S2F_get_f get)
175 #else
176 S2F_function(s, get) void* s; S2F_get_f get;
177 #endif
178 #else
179 #if S2F_size
180 #if __STD_C
181 S2F_function(const char* str, size_t size, char** end)
182 #else
183 S2F_function(str, size, end) char* str; size_t size; char** end;
184 #endif
185 #else
186 #if __STD_C
187 S2F_function(const char* str, char** end)
188 #else
189 S2F_function(str, end) char* str; char** end;
190 #endif
191 #endif
192 #endif
194 #if !S2F_scan
195 register unsigned char* s = (unsigned char*)str;
196 #if S2F_size
197 register unsigned char* z = s + size;
198 int back = 1;
199 int b;
200 #endif
201 unsigned char* t;
202 #endif
203 register S2F_batch n;
204 register int c;
205 register int digits;
206 register int m;
207 register unsigned char* cv;
208 int negative;
209 int enegative;
210 int fraction;
211 int decimal = 0;
212 int thousand = 0;
213 int part = 0;
214 S2F_number v;
215 S2F_number p;
216 S2F_part_t parts[16];
219 * radix char and thousands separator are locale specific
222 SFSETLOCALE(&decimal, &thousand);
223 SFCVINIT();
226 * skip initial blanks
229 do c = GET(s); while (isspace(c));
230 SET(s, t, b);
233 * get the sign
236 if ((negative = (c == '-')) || c == '+')
237 c = GET(s);
240 * drop leading 0's
243 digits = 0;
244 fraction = -1;
245 if (c == '0')
247 c = GET(s);
248 if (c == 'x' || c == 'X')
251 * hex floating point -- easy
254 cv = _Sfcv36;
255 v = 0;
256 for (;;)
258 c = GET(s);
259 if ((part = cv[c]) < 16)
261 digits++;
262 v *= 16;
263 v += part;
265 else if (c == decimal)
267 decimal = -1;
268 fraction = digits;
270 else
271 break;
273 m = 0;
274 if (c == 'p' || c == 'P')
276 c = GET(s);
277 if ((enegative = c == '-') || c == '+')
278 c = GET(s);
279 while (c >= '0' && c <= '9')
281 m = (m << 3) + (m << 1) + (c - '0');
282 c = GET(s);
284 if (enegative)
285 m = -m;
288 #if S2F_qualifier
291 * consume the optional suffix
294 switch (c)
296 case 'f':
297 case 'F':
298 case 'l':
299 case 'L':
300 c = GET(s);
301 break;
303 #endif
304 PUT(s);
305 if (v == 0)
306 return negative ? -v : v;
307 if (fraction >= 0)
308 m -= 4 * (digits - fraction);
309 if (m < S2F_exp_2_min)
311 if ((m -= S2F_exp_2_min) < S2F_exp_2_min)
313 ERR(ERANGE);
314 return 0;
316 v = S2F_ldexp(v, S2F_exp_2_min);
318 else if (m > S2F_exp_2_max)
320 ERR(ERANGE);
321 return negative ? -S2F_inf : S2F_inf;
323 v = S2F_ldexp(v, m);
324 goto check;
326 while (c == '0')
327 c = GET(s);
329 else if (c == decimal)
331 decimal = -1;
332 fraction = 0;
333 for (;;)
335 c = GET(s);
336 if (c != '0')
337 break;
338 digits++;
341 else if (c == 'i' || c == 'I')
343 if ((c = GET(s)) != 'n' && c != 'N' ||
344 (c = GET(s)) != 'f' && c != 'F')
346 REV(s, t, b);
347 PUT(s);
348 return 0;
350 c = GET(s);
351 SET(s, t, b);
352 if (((c) == 'i' || c == 'I') &&
353 ((c = GET(s)) == 'n' || c == 'N') &&
354 ((c = GET(s)) == 'i' || c == 'I') &&
355 ((c = GET(s)) == 't' || c == 'T') &&
356 ((c = GET(s)) == 'y' || c == 'Y'))
358 c = GET(s);
359 SET(s, t, b);
361 REV(s, t, b);
362 PUT(s);
363 return negative ? -S2F_inf : S2F_inf;
365 else if (c == 'n' || c == 'N')
367 if ((c = GET(s)) != 'a' && c != 'A' ||
368 (c = GET(s)) != 'n' && c != 'N')
370 REV(s, t, b);
371 PUT(s);
372 return 0;
374 do c = GET(s); while (c && !isspace(c));
375 PUT(s);
376 return negative ? -S2F_nan : S2F_nan;
378 else if (c < '1' || c > '9')
380 REV(s, t, b);
381 PUT(s);
382 NON(s);
383 return 0;
387 * consume the integral and fractional parts
390 n = 0;
391 m = 0;
392 for (;;)
394 if (c >= '0' && c <= '9')
396 digits++;
397 n = (n << 3) + (n << 1) + (c - '0');
398 if (n >= ((~((S2F_batch)0)) / 10) && part < elementsof(parts))
400 parts[part].batch = n;
401 n = 0;
402 parts[part].digits = digits;
403 part++;
406 else if (m && (digits - m) != 3)
407 break;
408 else if (c == decimal)
410 decimal = -1;
411 thousand = -1;
412 m = 0;
413 fraction = digits;
415 else if (c != thousand)
416 break;
417 else if (!(m = digits))
418 break;
419 c = GET(s);
423 * don't forget the last part
426 if (n && part < elementsof(parts))
428 parts[part].batch = n;
429 parts[part].digits = digits;
430 part++;
434 * consume the exponent
437 if (fraction >= 0)
438 digits = fraction;
439 if (c == 'e' || c == 'E')
441 c = GET(s);
442 if ((enegative = (c == '-')) || c == '+')
443 c = GET(s);
444 n = 0;
445 while (c >= '0' && c <= '9')
447 n = (n << 3) + (n << 1) + (c - '0');
448 c = GET(s);
450 if (enegative)
451 digits -= n;
452 else
453 digits += n;
456 #if S2F_qualifier
459 * consume the optional suffix
462 switch (c)
464 case 'f':
465 case 'F':
466 case 'l':
467 case 'L':
468 c = GET(s);
469 break;
471 #endif
472 PUT(s);
475 * adjust for at most one multiply per part
476 * and at most one divide overall
479 v = 0;
480 if (!part)
481 return negative ? -v : v;
482 else if ((m = parts[part-1].digits - digits) > 0)
483 digits += m;
484 else
485 m = 0;
488 * combine the parts
491 while (part--)
493 p = parts[part].batch;
494 c = digits - parts[part].digits;
495 if (c > S2F_exp_10_max)
497 ERR(ERANGE);
498 return negative ? -S2F_inf : S2F_inf;
500 if (c > 0)
502 #if _ast_mpy_overflow_fpe
503 if ((S2F_max / p) < S2F_pow10[c])
505 ERR(ERANGE);
506 return negative ? -S2F_inf : S2F_inf;
508 #endif
509 p *= S2F_pow10[c];
511 v += p;
513 if (m)
515 while (m > S2F_exp_10_max)
517 m -= S2F_exp_10_max;
518 v /= S2F_pow10[S2F_exp_10_max];
520 #if _ast_div_underflow_fpe
521 if ((S2F_min * p) > S2F_pow10[c])
523 ERR(ERANGE);
524 return negative ? -S2F_inf : S2F_inf;
526 #endif
527 v /= S2F_pow10[m];
531 * check the range
534 check:
535 if (v < S2F_min)
537 ERR(ERANGE);
538 v = 0;
540 else if (v > S2F_max)
542 ERR(ERANGE);
543 v = S2F_inf;
547 * done
550 return negative ? -v : v;