xref: /freebsd/contrib/gdtoa/hexnan.c (revision 50dad48bb740a8e56d185d9e8c165e0758f46e25)
1cc36ccd1SDavid Schultz /****************************************************************
2cc36ccd1SDavid Schultz 
3cc36ccd1SDavid Schultz The author of this software is David M. Gay.
4cc36ccd1SDavid Schultz 
5cc36ccd1SDavid Schultz Copyright (C) 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 
32cc36ccd1SDavid Schultz #include "gdtoaimp.h"
33cc36ccd1SDavid Schultz 
34cc36ccd1SDavid Schultz  static void
35cc36ccd1SDavid Schultz #ifdef KR_headers
L_shift(x,x1,i)36cc36ccd1SDavid Schultz L_shift(x, x1, i) ULong *x; ULong *x1; int i;
37cc36ccd1SDavid Schultz #else
38cc36ccd1SDavid Schultz L_shift(ULong *x, ULong *x1, int i)
39cc36ccd1SDavid Schultz #endif
40cc36ccd1SDavid Schultz {
41cc36ccd1SDavid Schultz 	int j;
42cc36ccd1SDavid Schultz 
43cc36ccd1SDavid Schultz 	i = 8 - i;
44cc36ccd1SDavid Schultz 	i <<= 2;
45cc36ccd1SDavid Schultz 	j = ULbits - i;
46cc36ccd1SDavid Schultz 	do {
47cc36ccd1SDavid Schultz 		*x |= x[1] << j;
48cc36ccd1SDavid Schultz 		x[1] >>= i;
49cc36ccd1SDavid Schultz 		} while(++x < x1);
50cc36ccd1SDavid Schultz 	}
51cc36ccd1SDavid Schultz 
52cc36ccd1SDavid Schultz  int
53cc36ccd1SDavid Schultz #ifdef KR_headers
hexnan(sp,fpi,x0)54cc36ccd1SDavid Schultz hexnan(sp, fpi, x0)
55cc36ccd1SDavid Schultz 	CONST char **sp; FPI *fpi; ULong *x0;
56cc36ccd1SDavid Schultz #else
57cc36ccd1SDavid Schultz hexnan( CONST char **sp, FPI *fpi, ULong *x0)
58cc36ccd1SDavid Schultz #endif
59cc36ccd1SDavid Schultz {
60cc36ccd1SDavid Schultz 	ULong c, h, *x, *x1, *xe;
61cc36ccd1SDavid Schultz 	CONST char *s;
62cc36ccd1SDavid Schultz 	int havedig, hd0, i, nbits;
63cc36ccd1SDavid Schultz 
64cc36ccd1SDavid Schultz 	if (!hexdig['0'])
65cc36ccd1SDavid Schultz 		hexdig_init_D2A();
66cc36ccd1SDavid Schultz 	nbits = fpi->nbits;
67cc36ccd1SDavid Schultz 	x = x0 + (nbits >> kshift);
68cc36ccd1SDavid Schultz 	if (nbits & kmask)
69cc36ccd1SDavid Schultz 		x++;
70cc36ccd1SDavid Schultz 	*--x = 0;
71cc36ccd1SDavid Schultz 	x1 = xe = x;
72cc36ccd1SDavid Schultz 	havedig = hd0 = i = 0;
73cc36ccd1SDavid Schultz 	s = *sp;
74*50dad48bSDavid Schultz 	/* allow optional initial 0x or 0X */
75*50dad48bSDavid Schultz 	while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
76*50dad48bSDavid Schultz 		++s;
77*50dad48bSDavid Schultz 	if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
78*50dad48bSDavid Schultz 	 && *(CONST unsigned char*)(s+3) > ' ')
792ca7a12aSDavid Schultz 		s += 2;
80*50dad48bSDavid Schultz 	while((c = *(CONST unsigned char*)++s)) {
81cc36ccd1SDavid Schultz 		if (!(h = hexdig[c])) {
82cc36ccd1SDavid Schultz 			if (c <= ' ') {
83cc36ccd1SDavid Schultz 				if (hd0 < havedig) {
84cc36ccd1SDavid Schultz 					if (x < x1 && i < 8)
85cc36ccd1SDavid Schultz 						L_shift(x, x1, i);
86cc36ccd1SDavid Schultz 					if (x <= x0) {
87cc36ccd1SDavid Schultz 						i = 8;
88cc36ccd1SDavid Schultz 						continue;
89cc36ccd1SDavid Schultz 						}
90cc36ccd1SDavid Schultz 					hd0 = havedig;
91cc36ccd1SDavid Schultz 					*--x = 0;
92cc36ccd1SDavid Schultz 					x1 = x;
93cc36ccd1SDavid Schultz 					i = 0;
94cc36ccd1SDavid Schultz 					}
95*50dad48bSDavid Schultz 				while(*(CONST unsigned char*)(s+1) <= ' ')
96*50dad48bSDavid Schultz 					++s;
97*50dad48bSDavid Schultz 				if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
98*50dad48bSDavid Schultz 				 && *(CONST unsigned char*)(s+3) > ' ')
99*50dad48bSDavid Schultz 					s += 2;
100cc36ccd1SDavid Schultz 				continue;
101cc36ccd1SDavid Schultz 				}
102cc36ccd1SDavid Schultz 			if (/*(*/ c == ')' && havedig) {
103cc36ccd1SDavid Schultz 				*sp = s + 1;
104cc36ccd1SDavid Schultz 				break;
105cc36ccd1SDavid Schultz 				}
106*50dad48bSDavid Schultz #ifndef GDTOA_NON_PEDANTIC_NANCHECK
107*50dad48bSDavid Schultz 			do {
108*50dad48bSDavid Schultz 				if (/*(*/ c == ')') {
109*50dad48bSDavid Schultz 					*sp = s + 1;
1102ca7a12aSDavid Schultz 					break;
111cc36ccd1SDavid Schultz 					}
112*50dad48bSDavid Schultz 				} while((c = *++s));
113*50dad48bSDavid Schultz #endif
114*50dad48bSDavid Schultz 			return STRTOG_NaN;
115*50dad48bSDavid Schultz 			}
116cc36ccd1SDavid Schultz 		havedig++;
117cc36ccd1SDavid Schultz 		if (++i > 8) {
118cc36ccd1SDavid Schultz 			if (x <= x0)
119cc36ccd1SDavid Schultz 				continue;
120cc36ccd1SDavid Schultz 			i = 1;
121cc36ccd1SDavid Schultz 			*--x = 0;
122cc36ccd1SDavid Schultz 			}
123*50dad48bSDavid Schultz 		*x = (*x << 4) | (h & 0xf);
124cc36ccd1SDavid Schultz 		}
125*50dad48bSDavid Schultz 	if (!havedig)
126*50dad48bSDavid Schultz 		return STRTOG_NaN;
127*50dad48bSDavid Schultz 	if (x < x1 && i < 8)
128cc36ccd1SDavid Schultz 		L_shift(x, x1, i);
129cc36ccd1SDavid Schultz 	if (x > x0) {
130cc36ccd1SDavid Schultz 		x1 = x0;
131cc36ccd1SDavid Schultz 		do *x1++ = *x++;
132cc36ccd1SDavid Schultz 			while(x <= xe);
133cc36ccd1SDavid Schultz 		do *x1++ = 0;
134cc36ccd1SDavid Schultz 			while(x1 <= xe);
135cc36ccd1SDavid Schultz 		}
136cc36ccd1SDavid Schultz 	else {
137cc36ccd1SDavid Schultz 		/* truncate high-order word if necessary */
138cc36ccd1SDavid Schultz 		if ( (i = nbits & (ULbits-1)) !=0)
139cc36ccd1SDavid Schultz 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
140cc36ccd1SDavid Schultz 		}
141cc36ccd1SDavid Schultz 	for(x1 = xe;; --x1) {
142cc36ccd1SDavid Schultz 		if (*x1 != 0)
143cc36ccd1SDavid Schultz 			break;
144cc36ccd1SDavid Schultz 		if (x1 == x0) {
145cc36ccd1SDavid Schultz 			*x1 = 1;
146cc36ccd1SDavid Schultz 			break;
147cc36ccd1SDavid Schultz 			}
148cc36ccd1SDavid Schultz 		}
149cc36ccd1SDavid Schultz 	return STRTOG_NaNbits;
150cc36ccd1SDavid Schultz 	}
151