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