xref: /dflybsd-src/contrib/gdtoa/strtof.c (revision 0d5acd7467c4e95f792ef49fceb3ab8e917ce86b)
11181b21fSPeter Avalos /****************************************************************
21181b21fSPeter Avalos 
31181b21fSPeter Avalos The author of this software is David M. Gay.
41181b21fSPeter Avalos 
51181b21fSPeter Avalos Copyright (C) 1998, 2000 by Lucent Technologies
61181b21fSPeter Avalos All Rights Reserved
71181b21fSPeter Avalos 
81181b21fSPeter Avalos Permission to use, copy, modify, and distribute this software and
91181b21fSPeter Avalos its documentation for any purpose and without fee is hereby
101181b21fSPeter Avalos granted, provided that the above copyright notice appear in all
111181b21fSPeter Avalos copies and that both that the copyright notice and this
121181b21fSPeter Avalos permission notice and warranty disclaimer appear in supporting
131181b21fSPeter Avalos documentation, and that the name of Lucent or any of its entities
141181b21fSPeter Avalos not be used in advertising or publicity pertaining to
151181b21fSPeter Avalos distribution of the software without specific, written prior
161181b21fSPeter Avalos permission.
171181b21fSPeter Avalos 
181181b21fSPeter Avalos LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
191181b21fSPeter Avalos INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
201181b21fSPeter Avalos IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
211181b21fSPeter Avalos SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
221181b21fSPeter Avalos WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
231181b21fSPeter Avalos IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
241181b21fSPeter Avalos ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
251181b21fSPeter Avalos THIS SOFTWARE.
261181b21fSPeter Avalos 
271181b21fSPeter Avalos ****************************************************************/
281181b21fSPeter Avalos 
291181b21fSPeter Avalos /* Please send bug reports to David M. Gay (dmg at acm dot org,
301181b21fSPeter Avalos  * with " at " changed at "@" and " dot " changed to ".").	*/
311181b21fSPeter Avalos 
32*0d5acd74SJohn Marino /* $FreeBSD: head/contrib/gdtoa/strtof.c 227753 2011-11-20 14:45:42Z theraven $ */
33*0d5acd74SJohn Marino 
341181b21fSPeter Avalos #include "gdtoaimp.h"
351181b21fSPeter Avalos 
361181b21fSPeter Avalos  float
371181b21fSPeter Avalos #ifdef KR_headers
strtof_l(s,sp,loc)38*0d5acd74SJohn Marino strtof_l(s, sp, loc) CONST char *s; char **sp; locale_t loc;
391181b21fSPeter Avalos #else
40*0d5acd74SJohn Marino strtof_l(CONST char *s, char **sp, locale_t loc)
411181b21fSPeter Avalos #endif
421181b21fSPeter Avalos {
431181b21fSPeter Avalos 	static FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, SI };
441181b21fSPeter Avalos 	ULong bits[1];
451181b21fSPeter Avalos 	Long exp;
461181b21fSPeter Avalos 	int k;
471181b21fSPeter Avalos 	union { ULong L[1]; float f; } u;
481181b21fSPeter Avalos #ifdef Honor_FLT_ROUNDS
491181b21fSPeter Avalos #include "gdtoa_fltrnds.h"
501181b21fSPeter Avalos #else
511181b21fSPeter Avalos #define fpi &fpi0
521181b21fSPeter Avalos #endif
531181b21fSPeter Avalos 
54*0d5acd74SJohn Marino 	k = strtodg_l(s, sp, fpi, &exp, bits, loc);
551181b21fSPeter Avalos 	switch(k & STRTOG_Retmask) {
561181b21fSPeter Avalos 	  case STRTOG_NoNumber:
571181b21fSPeter Avalos 	  case STRTOG_Zero:
581181b21fSPeter Avalos 		u.L[0] = 0;
591181b21fSPeter Avalos 		break;
601181b21fSPeter Avalos 
611181b21fSPeter Avalos 	  case STRTOG_Normal:
622a5b511eSPeter Avalos 		u.L[0] = (bits[0] & 0x7fffff) | ((exp + 0x7f + 23) << 23);
631181b21fSPeter Avalos 		break;
641181b21fSPeter Avalos 
65*0d5acd74SJohn Marino 	  case STRTOG_NaNbits:
66*0d5acd74SJohn Marino 		/* FreeBSD local: always return a quiet NaN */
67*0d5acd74SJohn Marino 		u.L[0] = bits[0] | 0x7fc00000;
68*0d5acd74SJohn Marino 		break;
69*0d5acd74SJohn Marino 
701181b21fSPeter Avalos 	  case STRTOG_Denormal:
711181b21fSPeter Avalos 		u.L[0] = bits[0];
721181b21fSPeter Avalos 		break;
731181b21fSPeter Avalos 
741181b21fSPeter Avalos 	  case STRTOG_Infinite:
751181b21fSPeter Avalos 		u.L[0] = 0x7f800000;
761181b21fSPeter Avalos 		break;
771181b21fSPeter Avalos 
781181b21fSPeter Avalos 	  case STRTOG_NaN:
791181b21fSPeter Avalos 		u.L[0] = f_QNAN;
801181b21fSPeter Avalos 	  }
811181b21fSPeter Avalos 	if (k & STRTOG_Neg)
821181b21fSPeter Avalos 		u.L[0] |= 0x80000000L;
831181b21fSPeter Avalos 	return u.f;
841181b21fSPeter Avalos 	}
85*0d5acd74SJohn Marino  float
86*0d5acd74SJohn Marino #ifdef KR_headers
strtof(s,sp)87*0d5acd74SJohn Marino strtof(s, sp) CONST char *s; char **sp;
88*0d5acd74SJohn Marino #else
89*0d5acd74SJohn Marino strtof(CONST char *s, char **sp)
90*0d5acd74SJohn Marino #endif
91*0d5acd74SJohn Marino {
92*0d5acd74SJohn Marino 	return strtof_l(s, sp, __get_locale());
93*0d5acd74SJohn Marino }
94*0d5acd74SJohn Marino 
95