xref: /netbsd-src/lib/libc/gdtoa/strtopQ.c (revision c99aac45e540bc210cc660619a6b5323cbb5c17f)
1*c99aac45Sjoerg /* $NetBSD: strtopQ.c,v 1.6 2013/04/18 21:54:11 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 
347684d5e0Skleink #include "gdtoaimp.h"
357684d5e0Skleink 
367684d5e0Skleink #undef _0
377684d5e0Skleink #undef _1
387684d5e0Skleink 
39ac898a26Skleink /* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */
407684d5e0Skleink 
41ac898a26Skleink #ifdef IEEE_BIG_ENDIAN
427684d5e0Skleink #define _0 0
437684d5e0Skleink #define _1 1
447684d5e0Skleink #define _2 2
457684d5e0Skleink #define _3 3
467684d5e0Skleink #endif
47ac898a26Skleink #ifdef IEEE_LITTLE_ENDIAN
487684d5e0Skleink #define _0 3
497684d5e0Skleink #define _1 2
507684d5e0Skleink #define _2 1
517684d5e0Skleink #define _3 0
527684d5e0Skleink #endif
537684d5e0Skleink 
547684d5e0Skleink  int
strtopQ(CONST char * s,char ** sp,void * V,locale_t loc)55*c99aac45Sjoerg strtopQ(CONST char *s, char **sp, void *V, locale_t loc)
567684d5e0Skleink {
5761e56760Schristos 	static CONST FPI fpi0 = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, SI };
587684d5e0Skleink 	ULong bits[4];
59bc89c06cSkleink 	Long expt;
607684d5e0Skleink 	int k;
617684d5e0Skleink 	ULong *L = (ULong*)V;
6261e56760Schristos #ifdef Honor_FLT_ROUNDS
6361e56760Schristos #include "gdtoa_fltrnds.h"
6461e56760Schristos #else
6561e56760Schristos #define fpi &fpi0
6661e56760Schristos #endif
677684d5e0Skleink 
68*c99aac45Sjoerg 	k = strtodg(s, sp, fpi, &expt, bits, loc);
69ab625449Schristos 	if (k == STRTOG_NoMemory)
70ab625449Schristos 		return k;
717684d5e0Skleink 	switch(k & STRTOG_Retmask) {
727684d5e0Skleink 	  case STRTOG_NoNumber:
737684d5e0Skleink 	  case STRTOG_Zero:
747684d5e0Skleink 		L[0] = L[1] = L[2] = L[3] = 0;
757684d5e0Skleink 		break;
767684d5e0Skleink 
777684d5e0Skleink 	  case STRTOG_Normal:
787684d5e0Skleink 	  case STRTOG_NaNbits:
797684d5e0Skleink 		L[_3] = bits[0];
807684d5e0Skleink 		L[_2] = bits[1];
817684d5e0Skleink 		L[_1] = bits[2];
82bc89c06cSkleink 		L[_0] = (bits[3] & ~0x10000) | ((expt + 0x3fff + 112) << 16);
837684d5e0Skleink 		break;
847684d5e0Skleink 
857684d5e0Skleink 	  case STRTOG_Denormal:
867684d5e0Skleink 		L[_3] = bits[0];
877684d5e0Skleink 		L[_2] = bits[1];
887684d5e0Skleink 		L[_1] = bits[2];
897684d5e0Skleink 		L[_0] = bits[3];
907684d5e0Skleink 		break;
917684d5e0Skleink 
927684d5e0Skleink 	  case STRTOG_Infinite:
937684d5e0Skleink 		L[_0] = 0x7fff0000;
947684d5e0Skleink 		L[_1] = L[_2] = L[_3] = 0;
957684d5e0Skleink 		break;
967684d5e0Skleink 
977684d5e0Skleink 	  case STRTOG_NaN:
987684d5e0Skleink 		L[0] = ld_QNAN0;
997684d5e0Skleink 		L[1] = ld_QNAN1;
1007684d5e0Skleink 		L[2] = ld_QNAN2;
1017684d5e0Skleink 		L[3] = ld_QNAN3;
1027684d5e0Skleink 	  }
1037684d5e0Skleink 	if (k & STRTOG_Neg)
1047684d5e0Skleink 		L[_0] |= 0x80000000L;
1057684d5e0Skleink 	return k;
1067684d5e0Skleink 	}
107