strtof.c revision 1.6
1/* $NetBSD: strtof.c,v 1.6 2013/04/18 21:54:11 joerg Exp $ */ 2 3/**************************************************************** 4 5The author of this software is David M. Gay. 6 7Copyright (C) 1998, 2000 by Lucent Technologies 8All Rights Reserved 9 10Permission to use, copy, modify, and distribute this software and 11its documentation for any purpose and without fee is hereby 12granted, provided that the above copyright notice appear in all 13copies and that both that the copyright notice and this 14permission notice and warranty disclaimer appear in supporting 15documentation, and that the name of Lucent or any of its entities 16not be used in advertising or publicity pertaining to 17distribution of the software without specific, written prior 18permission. 19 20LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 21INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 22IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 23SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 24WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 25IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 26ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 27THIS 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 41__weak_alias(strtof, _strtof) 42__weak_alias(strtof_l, _strtof_l) 43#endif 44 45static 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 96float 97strtof(CONST char *s, char **sp) 98{ 99 return _int_strtof_l(s, sp, *_current_locale()); 100} 101 102float 103strtof_l(CONST char *s, char **sp, locale_t loc) 104{ 105 if (loc == NULL) 106 loc = _C_locale; 107 return _int_strtof_l(s, sp, loc); 108} 109