1 /****************************************************************
2 
3 The author of this software is David M. Gay.
4 
5 Copyright (C) 2000 by Lucent Technologies
6 All Rights Reserved
7 
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17 
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26 
27 ****************************************************************/
28 
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to ".").	*/
31 
32 #include "gdtoaimp.h"
33 
L_shift(ULong * x,ULong * x1,int i)34 static void L_shift (ULong *x, ULong *x1, int i)
35 {
36 	int j;
37 
38 	i = 8 - i;
39 	i <<= 2;
40 	j = ULbits - i;
41 	do {
42 		*x |= x[1] << j;
43 		x[1] >>= i;
44 	} while(++x < x1);
45 }
46 
hexnan(const char ** sp,FPI * fpi,ULong * x0)47 int hexnan (const char **sp, FPI *fpi, ULong *x0)
48 {
49 	ULong c, h, *x, *x1, *xe;
50 	const char *s;
51 	int havedig, hd0, i, nbits;
52 
53 	if (!hexdig['0'])
54 		hexdig_init_D2A();
55 	nbits = fpi->nbits;
56 	x = x0 + (nbits >> kshift);
57 	if (nbits & kmask)
58 		x++;
59 	*--x = 0;
60 	x1 = xe = x;
61 	havedig = hd0 = i = 0;
62 	s = *sp;
63 	/* allow optional initial 0x or 0X */
64 	while((c = *(const unsigned char*)(s+1)) && c <= ' ')
65 		++s;
66 	if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
67 	 && *(const unsigned char*)(s+3) > ' ')
68 		s += 2;
69 	while((c = *(const unsigned char*)++s)) {
70 		if (!(h = hexdig[c])) {
71 			if (c <= ' ') {
72 				if (hd0 < havedig) {
73 					if (x < x1 && i < 8)
74 						L_shift(x, x1, i);
75 					if (x <= x0) {
76 						i = 8;
77 						continue;
78 					}
79 					hd0 = havedig;
80 					*--x = 0;
81 					x1 = x;
82 					i = 0;
83 				}
84 				while(*(const unsigned char*)(s+1) <= ' ')
85 					++s;
86 				if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
87 				 && *(const unsigned char*)(s+3) > ' ')
88 					s += 2;
89 				continue;
90 			}
91 			if (/*(*/ c == ')' && havedig) {
92 				*sp = s + 1;
93 				break;
94 			}
95 #ifndef GDTOA_NON_PEDANTIC_NANCHECK
96 			do {
97 				if (/*(*/ c == ')') {
98 					*sp = s + 1;
99 					break;
100 				}
101 			} while((c = *++s));
102 #endif
103 			return STRTOG_NaN;
104 		}
105 		havedig++;
106 		if (++i > 8) {
107 			if (x <= x0)
108 				continue;
109 			i = 1;
110 			*--x = 0;
111 		}
112 		*x = (*x << 4) | (h & 0xf);
113 	}
114 	if (!havedig)
115 		return STRTOG_NaN;
116 	if (x < x1 && i < 8)
117 		L_shift(x, x1, i);
118 	if (x > x0) {
119 		x1 = x0;
120 		do *x1++ = *x++;
121 			while(x <= xe);
122 		do *x1++ = 0;
123 			while(x1 <= xe);
124 	}
125 	else {
126 		/* truncate high-order word if necessary */
127 		if ( (i = nbits & (ULbits-1)) !=0)
128 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
129 	}
130 	for(x1 = xe;; --x1) {
131 		if (*x1 != 0)
132 			break;
133 		if (x1 == x0) {
134 			*x1 = 1;
135 			break;
136 		}
137 	}
138 	return STRTOG_NaNbits;
139 }
140