xref: /minix/lib/libc/gdtoa/strtof.c (revision 84d9c625)
1 /* $NetBSD: strtof.c,v 1.7 2013/05/17 12:55:57 joerg Exp $ */
2 
3 /****************************************************************
4 
5 The author of this software is David M. Gay.
6 
7 Copyright (C) 1998, 2000 by Lucent Technologies
8 All Rights Reserved
9 
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
18 permission.
19 
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 THIS SOFTWARE.
28 
29 ****************************************************************/
30 
31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
32  * with " at " changed at "@" and " dot " changed to ".").	*/
33 
34 #include "namespace.h"
35 #include "gdtoaimp.h"
36 
37 #include <locale.h>
38 #include "setlocale_local.h"
39 
40 #ifdef __weak_alias
__weak_alias(strtof,_strtof)41 __weak_alias(strtof, _strtof)
42 __weak_alias(strtof_l, _strtof_l)
43 #endif
44 
45 static float
46 _int_strtof_l(CONST char *s, char **sp, locale_t loc)
47 {
48 	static CONST FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, SI };
49 	ULong bits[1];
50 	Long expt;
51 	int k;
52 	union { ULong L[1]; float f; } u;
53 #ifdef Honor_FLT_ROUNDS
54 #include "gdtoa_fltrnds.h"
55 #else
56 #define fpi &fpi0
57 #endif
58 
59 	k = strtodg(s, sp, fpi, &expt, bits, loc);
60 	if (k == STRTOG_NoMemory) {
61 		errno = ERANGE;
62 		return HUGE_VALF;
63 	}
64 	switch(k & STRTOG_Retmask) {
65 	  case STRTOG_NoNumber:
66 	  case STRTOG_Zero:
67 		u.L[0] = 0;
68 		break;
69 
70 	  case STRTOG_Normal:
71 	  case STRTOG_NaNbits:
72 		u.L[0] = (bits[0] & 0x7fffff) | ((expt + 0x7f + 23) << 23);
73 		break;
74 
75 	  case STRTOG_Denormal:
76 		u.L[0] = bits[0];
77 		break;
78 
79 	  case STRTOG_Infinite:
80 		u.L[0] = 0x7f800000;
81 		break;
82 
83 	  case STRTOG_NaN:
84 		u.L[0] = f_QNAN;
85 		break;
86 
87 	  default:
88 		u.L[0] = 0; /* for gcc warning */
89 		break;
90 	  }
91 	if (k & STRTOG_Neg)
92 		u.L[0] |= 0x80000000L;
93 	return u.f;
94 	}
95 
96 float
strtof(CONST char * s,char ** sp)97 strtof(CONST char *s, char **sp)
98 {
99 	return _int_strtof_l(s, sp, _current_locale());
100 }
101 
102 float
strtof_l(CONST char * s,char ** sp,locale_t loc)103 strtof_l(CONST char *s, char **sp, locale_t loc)
104 {
105 	return _int_strtof_l(s, sp, loc);
106 }
107