xref: /netbsd-src/lib/libc/gdtoa/strtof.c (revision e0ac190e1e4142d8158f3a99bc575fef0a0a0a5c)
1*e0ac190eSjoerg /* $NetBSD: strtof.c,v 1.7 2013/05/17 12:55:57 joerg Exp $ */
27684d5e0Skleink 
37684d5e0Skleink /****************************************************************
47684d5e0Skleink 
57684d5e0Skleink The author of this software is David M. Gay.
67684d5e0Skleink 
77684d5e0Skleink Copyright (C) 1998, 2000 by Lucent Technologies
87684d5e0Skleink All Rights Reserved
97684d5e0Skleink 
107684d5e0Skleink Permission to use, copy, modify, and distribute this software and
117684d5e0Skleink its documentation for any purpose and without fee is hereby
127684d5e0Skleink granted, provided that the above copyright notice appear in all
137684d5e0Skleink copies and that both that the copyright notice and this
147684d5e0Skleink permission notice and warranty disclaimer appear in supporting
157684d5e0Skleink documentation, and that the name of Lucent or any of its entities
167684d5e0Skleink not be used in advertising or publicity pertaining to
177684d5e0Skleink distribution of the software without specific, written prior
187684d5e0Skleink permission.
197684d5e0Skleink 
207684d5e0Skleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
217684d5e0Skleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
227684d5e0Skleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
237684d5e0Skleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
247684d5e0Skleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
257684d5e0Skleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
267684d5e0Skleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
277684d5e0Skleink THIS SOFTWARE.
287684d5e0Skleink 
297684d5e0Skleink ****************************************************************/
307684d5e0Skleink 
317684d5e0Skleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
327684d5e0Skleink  * with " at " changed at "@" and " dot " changed to ".").	*/
337684d5e0Skleink 
34bc89c06cSkleink #include "namespace.h"
357684d5e0Skleink #include "gdtoaimp.h"
367684d5e0Skleink 
37c99aac45Sjoerg #include <locale.h>
38c99aac45Sjoerg #include "setlocale_local.h"
39c99aac45Sjoerg 
40bc89c06cSkleink #ifdef __weak_alias
__weak_alias(strtof,_strtof)41bc89c06cSkleink __weak_alias(strtof, _strtof)
42c99aac45Sjoerg __weak_alias(strtof_l, _strtof_l)
43bc89c06cSkleink #endif
44bc89c06cSkleink 
45c99aac45Sjoerg static float
46c99aac45Sjoerg _int_strtof_l(CONST char *s, char **sp, locale_t loc)
477684d5e0Skleink {
4861e56760Schristos 	static CONST FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, SI };
497684d5e0Skleink 	ULong bits[1];
50bc89c06cSkleink 	Long expt;
517684d5e0Skleink 	int k;
527684d5e0Skleink 	union { ULong L[1]; float f; } u;
5361e56760Schristos #ifdef Honor_FLT_ROUNDS
5461e56760Schristos #include "gdtoa_fltrnds.h"
5561e56760Schristos #else
5661e56760Schristos #define fpi &fpi0
5761e56760Schristos #endif
587684d5e0Skleink 
59c99aac45Sjoerg 	k = strtodg(s, sp, fpi, &expt, bits, loc);
60ab625449Schristos 	if (k == STRTOG_NoMemory) {
61ab625449Schristos 		errno = ERANGE;
62ab625449Schristos 		return HUGE_VALF;
63ab625449Schristos 	}
647684d5e0Skleink 	switch(k & STRTOG_Retmask) {
657684d5e0Skleink 	  case STRTOG_NoNumber:
667684d5e0Skleink 	  case STRTOG_Zero:
677684d5e0Skleink 		u.L[0] = 0;
687684d5e0Skleink 		break;
697684d5e0Skleink 
707684d5e0Skleink 	  case STRTOG_Normal:
717684d5e0Skleink 	  case STRTOG_NaNbits:
72bc89c06cSkleink 		u.L[0] = (bits[0] & 0x7fffff) | ((expt + 0x7f + 23) << 23);
737684d5e0Skleink 		break;
747684d5e0Skleink 
757684d5e0Skleink 	  case STRTOG_Denormal:
767684d5e0Skleink 		u.L[0] = bits[0];
777684d5e0Skleink 		break;
787684d5e0Skleink 
797684d5e0Skleink 	  case STRTOG_Infinite:
807684d5e0Skleink 		u.L[0] = 0x7f800000;
817684d5e0Skleink 		break;
827684d5e0Skleink 
837684d5e0Skleink 	  case STRTOG_NaN:
847684d5e0Skleink 		u.L[0] = f_QNAN;
85493d3410Smrg 		break;
86493d3410Smrg 
87493d3410Smrg 	  default:
88493d3410Smrg 		u.L[0] = 0; /* for gcc warning */
89493d3410Smrg 		break;
907684d5e0Skleink 	  }
917684d5e0Skleink 	if (k & STRTOG_Neg)
927684d5e0Skleink 		u.L[0] |= 0x80000000L;
937684d5e0Skleink 	return u.f;
947684d5e0Skleink 	}
95c99aac45Sjoerg 
96c99aac45Sjoerg float
strtof(CONST char * s,char ** sp)97c99aac45Sjoerg strtof(CONST char *s, char **sp)
98c99aac45Sjoerg {
99*e0ac190eSjoerg 	return _int_strtof_l(s, sp, _current_locale());
100c99aac45Sjoerg }
101c99aac45Sjoerg 
102c99aac45Sjoerg float
strtof_l(CONST char * s,char ** sp,locale_t loc)103c99aac45Sjoerg strtof_l(CONST char *s, char **sp, locale_t loc)
104c99aac45Sjoerg {
105c99aac45Sjoerg 	return _int_strtof_l(s, sp, loc);
106c99aac45Sjoerg }
107