xref: /dflybsd-src/contrib/gdtoa/strtorx.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/strtorx.c 227753 2011-11-20 14:45:42Z theraven $ */
33*0d5acd74SJohn Marino 
341181b21fSPeter Avalos #include "gdtoaimp.h"
351181b21fSPeter Avalos 
361181b21fSPeter Avalos #undef _0
371181b21fSPeter Avalos #undef _1
381181b21fSPeter Avalos 
391181b21fSPeter Avalos /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
401181b21fSPeter Avalos 
411181b21fSPeter Avalos #ifdef IEEE_MC68k
421181b21fSPeter Avalos #define _0 0
431181b21fSPeter Avalos #define _1 1
441181b21fSPeter Avalos #define _2 2
451181b21fSPeter Avalos #define _3 3
461181b21fSPeter Avalos #define _4 4
471181b21fSPeter Avalos #endif
481181b21fSPeter Avalos #ifdef IEEE_8087
491181b21fSPeter Avalos #define _0 4
501181b21fSPeter Avalos #define _1 3
511181b21fSPeter Avalos #define _2 2
521181b21fSPeter Avalos #define _3 1
531181b21fSPeter Avalos #define _4 0
541181b21fSPeter Avalos #endif
551181b21fSPeter Avalos 
561181b21fSPeter Avalos  void
571181b21fSPeter Avalos #ifdef KR_headers
ULtox(L,bits,exp,k)581181b21fSPeter Avalos ULtox(L, bits, exp, k) UShort *L; ULong *bits; Long exp; int k;
591181b21fSPeter Avalos #else
601181b21fSPeter Avalos ULtox(UShort *L, ULong *bits, Long exp, int k)
611181b21fSPeter Avalos #endif
621181b21fSPeter Avalos {
631181b21fSPeter Avalos 	switch(k & STRTOG_Retmask) {
641181b21fSPeter Avalos 	  case STRTOG_NoNumber:
651181b21fSPeter Avalos 	  case STRTOG_Zero:
661181b21fSPeter Avalos 		L[0] = L[1] = L[2] = L[3] = L[4] = 0;
671181b21fSPeter Avalos 		break;
681181b21fSPeter Avalos 
691181b21fSPeter Avalos 	  case STRTOG_Denormal:
701181b21fSPeter Avalos 		L[_0] = 0;
711181b21fSPeter Avalos 		goto normal_bits;
721181b21fSPeter Avalos 
731181b21fSPeter Avalos 	  case STRTOG_Normal:
741181b21fSPeter Avalos 		L[_0] = exp + 0x3fff + 63;
751181b21fSPeter Avalos  normal_bits:
761181b21fSPeter Avalos 		L[_4] = (UShort)bits[0];
771181b21fSPeter Avalos 		L[_3] = (UShort)(bits[0] >> 16);
781181b21fSPeter Avalos 		L[_2] = (UShort)bits[1];
791181b21fSPeter Avalos 		L[_1] = (UShort)(bits[1] >> 16);
801181b21fSPeter Avalos 		break;
811181b21fSPeter Avalos 
82*0d5acd74SJohn Marino 	  case STRTOG_NaNbits:
83*0d5acd74SJohn Marino 		L[_0] = exp + 0x3fff + 63;
84*0d5acd74SJohn Marino 		L[_4] = (UShort)bits[0];
85*0d5acd74SJohn Marino 		L[_3] = (UShort)(bits[0] >> 16);
86*0d5acd74SJohn Marino 		L[_2] = (UShort)bits[1];
87*0d5acd74SJohn Marino 		L[_1] = (UShort)((bits[1] >> 16) | (3 << 14));
88*0d5acd74SJohn Marino 		break;
89*0d5acd74SJohn Marino 
901181b21fSPeter Avalos 	  case STRTOG_Infinite:
911181b21fSPeter Avalos 		L[_0] = 0x7fff;
922a5b511eSPeter Avalos 		L[_1] = 0x8000;
932a5b511eSPeter Avalos 		L[_2] = L[_3] = L[_4] = 0;
941181b21fSPeter Avalos 		break;
951181b21fSPeter Avalos 
961181b21fSPeter Avalos 	  case STRTOG_NaN:
971181b21fSPeter Avalos 		L[0] = ldus_QNAN0;
981181b21fSPeter Avalos 		L[1] = ldus_QNAN1;
991181b21fSPeter Avalos 		L[2] = ldus_QNAN2;
1001181b21fSPeter Avalos 		L[3] = ldus_QNAN3;
1011181b21fSPeter Avalos 		L[4] = ldus_QNAN4;
1021181b21fSPeter Avalos 	  }
1031181b21fSPeter Avalos 	if (k & STRTOG_Neg)
1041181b21fSPeter Avalos 		L[_0] |= 0x8000;
1051181b21fSPeter Avalos 	}
1061181b21fSPeter Avalos 
1071181b21fSPeter Avalos  int
1081181b21fSPeter Avalos #ifdef KR_headers
strtorx_l(s,sp,rounding,L,locale)109*0d5acd74SJohn Marino strtorx_l(s, sp, rounding, L, locale) CONST char *s; char **sp; int rounding;
110*0d5acd74SJohn Marino void *L; locale_t locale;
1111181b21fSPeter Avalos #else
112*0d5acd74SJohn Marino strtorx_l(CONST char *s, char **sp, int rounding, void *L, locale_t locale)
1131181b21fSPeter Avalos #endif
1141181b21fSPeter Avalos {
1151181b21fSPeter Avalos 	static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI };
1161181b21fSPeter Avalos 	FPI *fpi, fpi1;
1171181b21fSPeter Avalos 	ULong bits[2];
1181181b21fSPeter Avalos 	Long exp;
1191181b21fSPeter Avalos 	int k;
1201181b21fSPeter Avalos 
1211181b21fSPeter Avalos 	fpi = &fpi0;
1221181b21fSPeter Avalos 	if (rounding != FPI_Round_near) {
1231181b21fSPeter Avalos 		fpi1 = fpi0;
1241181b21fSPeter Avalos 		fpi1.rounding = rounding;
1251181b21fSPeter Avalos 		fpi = &fpi1;
1261181b21fSPeter Avalos 		}
127*0d5acd74SJohn Marino 	k = strtodg_l(s, sp, fpi, &exp, bits, locale);
1281181b21fSPeter Avalos 	ULtox((UShort*)L, bits, exp, k);
1291181b21fSPeter Avalos 	return k;
1301181b21fSPeter Avalos 	}
131