xref: /freebsd/contrib/gdtoa/hexnan.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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 /* $FreeBSD$ */
33 
34 #include "gdtoaimp.h"
35 
36  static void
37 #ifdef KR_headers
38 L_shift(x, x1, i) ULong *x; ULong *x1; int i;
39 #else
40 L_shift(ULong *x, ULong *x1, int i)
41 #endif
42 {
43 	int j;
44 
45 	i = 8 - i;
46 	i <<= 2;
47 	j = ULbits - i;
48 	do {
49 		*x |= x[1] << j;
50 		x[1] >>= i;
51 		} while(++x < x1);
52 	}
53 
54  int
55 #ifdef KR_headers
56 hexnan(sp, fpi, x0)
57 	CONST char **sp; FPI *fpi; ULong *x0;
58 #else
59 hexnan( CONST char **sp, FPI *fpi, ULong *x0)
60 #endif
61 {
62 	ULong c, h, *x, *x1, *xe;
63 	CONST char *s;
64 	int havedig, hd0, i, nbits;
65 
66 	if (!hexdig['0'])
67 		hexdig_init_D2A();
68 	nbits = fpi->nbits;
69 	x = x0 + (nbits >> kshift);
70 	if (nbits & kmask)
71 		x++;
72 	*--x = 0;
73 	x1 = xe = x;
74 	havedig = hd0 = i = 0;
75 	s = *sp;
76 
77 	/* FreeBSD local: Accept (but ignore) the '0x' prefix. */
78 	if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
79 		s += 2;
80 
81 	while(c = *(CONST unsigned char*)++s) {
82 		if (!(h = hexdig[c])) {
83 #if 0
84 			if (c <= ' ') {
85 				if (hd0 < havedig) {
86 					if (x < x1 && i < 8)
87 						L_shift(x, x1, i);
88 					if (x <= x0) {
89 						i = 8;
90 						continue;
91 						}
92 					hd0 = havedig;
93 					*--x = 0;
94 					x1 = x;
95 					i = 0;
96 					}
97 				continue;
98 				}
99 			if (/*(*/ c == ')' && havedig) {
100 				*sp = s + 1;
101 				break;
102 				}
103 #endif
104 			break;
105 			}
106 		havedig++;
107 		if (++i > 8) {
108 			if (x <= x0)
109 				continue;
110 			i = 1;
111 			*--x = 0;
112 			}
113 		*x = (*x << 4) | h & 0xf;
114 		}
115 	if (havedig && x < x1 && i < 8)
116 		L_shift(x, x1, i);
117 	if (x > x0) {
118 		x1 = x0;
119 		do *x1++ = *x++;
120 			while(x <= xe);
121 		do *x1++ = 0;
122 			while(x1 <= xe);
123 		}
124 	else {
125 		/* truncate high-order word if necessary */
126 		if ( (i = nbits & (ULbits-1)) !=0)
127 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
128 		}
129 	if (havedig) {
130 	for(x1 = xe;; --x1) {
131 		if (*x1 != 0)
132 			break;
133 		if (x1 == x0) {
134 			*x1 = 1;
135 			break;
136 			}
137 		}
138 	}
139 
140 	/*
141 	 * FreeBSD local: Accept all the sequences allowed by C99 and update
142 	 * the tail pointer correctly. Don't accept any invalid sequences.
143 	 */
144 	if (c == '\0')	/* nan() calls this, too; tolerate a missing ')' */
145 		return STRTOG_NaNbits;
146 	if (c != ')') {
147 		while(c = *(CONST unsigned char*)++s) {
148 			if (c == ')')
149 				break;
150 			if (!isalnum(c) && c != '_')
151 				return STRTOG_NaNbits;
152 		}
153 	}
154 	*sp = s + 1;
155 	return STRTOG_NaNbits;
156 	}
157