xref: /freebsd/contrib/gdtoa/strtorx.c (revision 8fa0b743820f61c661ba5f3ea0e3be0dc137910e)
1cc36ccd1SDavid Schultz /****************************************************************
2cc36ccd1SDavid Schultz 
3cc36ccd1SDavid Schultz The author of this software is David M. Gay.
4cc36ccd1SDavid Schultz 
5cc36ccd1SDavid Schultz Copyright (C) 1998, 2000 by Lucent Technologies
6cc36ccd1SDavid Schultz All Rights Reserved
7cc36ccd1SDavid Schultz 
8cc36ccd1SDavid Schultz Permission to use, copy, modify, and distribute this software and
9cc36ccd1SDavid Schultz its documentation for any purpose and without fee is hereby
10cc36ccd1SDavid Schultz granted, provided that the above copyright notice appear in all
11cc36ccd1SDavid Schultz copies and that both that the copyright notice and this
12cc36ccd1SDavid Schultz permission notice and warranty disclaimer appear in supporting
13cc36ccd1SDavid Schultz documentation, and that the name of Lucent or any of its entities
14cc36ccd1SDavid Schultz not be used in advertising or publicity pertaining to
15cc36ccd1SDavid Schultz distribution of the software without specific, written prior
16cc36ccd1SDavid Schultz permission.
17cc36ccd1SDavid Schultz 
18cc36ccd1SDavid Schultz LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19cc36ccd1SDavid Schultz INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20cc36ccd1SDavid Schultz IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21cc36ccd1SDavid Schultz SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22cc36ccd1SDavid Schultz WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23cc36ccd1SDavid Schultz IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24cc36ccd1SDavid Schultz ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25cc36ccd1SDavid Schultz THIS SOFTWARE.
26cc36ccd1SDavid Schultz 
27cc36ccd1SDavid Schultz ****************************************************************/
28cc36ccd1SDavid Schultz 
29c88250a5SDavid Schultz /* Please send bug reports to David M. Gay (dmg at acm dot org,
30c88250a5SDavid Schultz  * with " at " changed at "@" and " dot " changed to ".").	*/
31cc36ccd1SDavid Schultz 
3210a381e5SDavid Schultz /* $FreeBSD$ */
3310a381e5SDavid Schultz 
34cc36ccd1SDavid Schultz #include "gdtoaimp.h"
35cc36ccd1SDavid Schultz 
36cc36ccd1SDavid Schultz #undef _0
37cc36ccd1SDavid Schultz #undef _1
38cc36ccd1SDavid Schultz 
39cc36ccd1SDavid Schultz /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
40cc36ccd1SDavid Schultz 
41cc36ccd1SDavid Schultz #ifdef IEEE_MC68k
42cc36ccd1SDavid Schultz #define _0 0
43cc36ccd1SDavid Schultz #define _1 1
44cc36ccd1SDavid Schultz #define _2 2
45cc36ccd1SDavid Schultz #define _3 3
46cc36ccd1SDavid Schultz #define _4 4
47cc36ccd1SDavid Schultz #endif
48cc36ccd1SDavid Schultz #ifdef IEEE_8087
49cc36ccd1SDavid Schultz #define _0 4
50cc36ccd1SDavid Schultz #define _1 3
51cc36ccd1SDavid Schultz #define _2 2
52cc36ccd1SDavid Schultz #define _3 1
53cc36ccd1SDavid Schultz #define _4 0
54cc36ccd1SDavid Schultz #endif
55cc36ccd1SDavid Schultz 
56cc36ccd1SDavid Schultz  void
57cc36ccd1SDavid Schultz #ifdef KR_headers
ULtox(L,bits,exp,k)58cc36ccd1SDavid Schultz ULtox(L, bits, exp, k) UShort *L; ULong *bits; Long exp; int k;
59cc36ccd1SDavid Schultz #else
60cc36ccd1SDavid Schultz ULtox(UShort *L, ULong *bits, Long exp, int k)
61cc36ccd1SDavid Schultz #endif
62cc36ccd1SDavid Schultz {
63cc36ccd1SDavid Schultz 	switch(k & STRTOG_Retmask) {
64cc36ccd1SDavid Schultz 	  case STRTOG_NoNumber:
65cc36ccd1SDavid Schultz 	  case STRTOG_Zero:
66cc36ccd1SDavid Schultz 		L[0] = L[1] = L[2] = L[3] = L[4] = 0;
67cc36ccd1SDavid Schultz 		break;
68cc36ccd1SDavid Schultz 
69cc36ccd1SDavid Schultz 	  case STRTOG_Denormal:
70c88250a5SDavid Schultz 		L[_0] = 0;
71c88250a5SDavid Schultz 		goto normal_bits;
72c88250a5SDavid Schultz 
73c88250a5SDavid Schultz 	  case STRTOG_Normal:
74c88250a5SDavid Schultz 		L[_0] = exp + 0x3fff + 63;
75c88250a5SDavid Schultz  normal_bits:
76cc36ccd1SDavid Schultz 		L[_4] = (UShort)bits[0];
77cc36ccd1SDavid Schultz 		L[_3] = (UShort)(bits[0] >> 16);
78cc36ccd1SDavid Schultz 		L[_2] = (UShort)bits[1];
79fb048ccaSDavid Schultz 		L[_1] = (UShort)(bits[1] >> 16);
80fb048ccaSDavid Schultz 		break;
81fb048ccaSDavid Schultz 
82fb048ccaSDavid Schultz 	  case STRTOG_NaNbits:
83fb048ccaSDavid Schultz 		L[_0] = exp + 0x3fff + 63;
84fb048ccaSDavid Schultz 		L[_4] = (UShort)bits[0];
85fb048ccaSDavid Schultz 		L[_3] = (UShort)(bits[0] >> 16);
86fb048ccaSDavid Schultz 		L[_2] = (UShort)bits[1];
8710a381e5SDavid Schultz 		L[_1] = (UShort)((bits[1] >> 16) | (3 << 14));
88cc36ccd1SDavid Schultz 		break;
89cc36ccd1SDavid Schultz 
90cc36ccd1SDavid Schultz 	  case STRTOG_Infinite:
91cc36ccd1SDavid Schultz 		L[_0] = 0x7fff;
9250dad48bSDavid Schultz 		L[_1] = 0x8000;
9350dad48bSDavid Schultz 		L[_2] = L[_3] = L[_4] = 0;
94cc36ccd1SDavid Schultz 		break;
95cc36ccd1SDavid Schultz 
96cc36ccd1SDavid Schultz 	  case STRTOG_NaN:
97c88250a5SDavid Schultz 		L[0] = ldus_QNAN0;
98c88250a5SDavid Schultz 		L[1] = ldus_QNAN1;
99c88250a5SDavid Schultz 		L[2] = ldus_QNAN2;
100c88250a5SDavid Schultz 		L[3] = ldus_QNAN3;
101c88250a5SDavid Schultz 		L[4] = ldus_QNAN4;
102cc36ccd1SDavid Schultz 	  }
103cc36ccd1SDavid Schultz 	if (k & STRTOG_Neg)
104cc36ccd1SDavid Schultz 		L[_0] |= 0x8000;
105cc36ccd1SDavid Schultz 	}
106cc36ccd1SDavid Schultz 
107cc36ccd1SDavid Schultz  int
108cc36ccd1SDavid Schultz #ifdef KR_headers
strtorx_l(s,sp,rounding,L,locale)109*3c87aa1dSDavid Chisnall strtorx_l(s, sp, rounding, L, locale) CONST char *s; char **sp; int rounding;
110*3c87aa1dSDavid Chisnall void *L; locale_t locale;
111cc36ccd1SDavid Schultz #else
112*3c87aa1dSDavid Chisnall strtorx_l(CONST char *s, char **sp, int rounding, void *L, locale_t locale)
113cc36ccd1SDavid Schultz #endif
114cc36ccd1SDavid Schultz {
115cc36ccd1SDavid Schultz 	static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI };
116cc36ccd1SDavid Schultz 	FPI *fpi, fpi1;
117cc36ccd1SDavid Schultz 	ULong bits[2];
118cc36ccd1SDavid Schultz 	Long exp;
119cc36ccd1SDavid Schultz 	int k;
120cc36ccd1SDavid Schultz 
121cc36ccd1SDavid Schultz 	fpi = &fpi0;
122cc36ccd1SDavid Schultz 	if (rounding != FPI_Round_near) {
123cc36ccd1SDavid Schultz 		fpi1 = fpi0;
124cc36ccd1SDavid Schultz 		fpi1.rounding = rounding;
125cc36ccd1SDavid Schultz 		fpi = &fpi1;
126cc36ccd1SDavid Schultz 		}
127*3c87aa1dSDavid Chisnall 	k = strtodg_l(s, sp, fpi, &exp, bits, locale);
128cc36ccd1SDavid Schultz 	ULtox((UShort*)L, bits, exp, k);
129cc36ccd1SDavid Schultz 	return k;
130cc36ccd1SDavid Schultz 	}
131