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
30 	David M. Gay
31 	Bell Laboratories, Room 2C-463
32 	600 Mountain Avenue
33 	Murray Hill, NJ 07974-0636
34 	U.S.A.
35 	dmg@bell-labs.com
36  */
37 
38 /* Modified 06-21-2006 by Jeff Johnston to work with newlib.  */
39 
40 #define _DEFAULT_SOURCE
41 #include <string.h>
42 #include "mprec.h"
43 #include "gdtoa.h"
44 
45 #ifdef INFNAN_CHECK
46 int
match(const char ** sp,char * t)47 match (const char **sp,
48 	char *t)
49 {
50 	int c, d;
51 	const char *s = *sp;
52 
53 	while( (d = *t++) !=0) {
54 		if ((c = *++s) >= 'A' && c <= 'Z')
55 			c += 'a' - 'A';
56 		if (c != d)
57 			return 0;
58 		}
59 	*sp = s + 1;
60 	return 1;
61 }
62 
63 static void
L_shift(__ULong * x,__ULong * x1,int i)64 L_shift (__ULong *x,
65 	__ULong *x1,
66 	int i)
67 {
68 	int j;
69 
70 	i = 8 - i;
71 	i <<= 2;
72 	j = ULbits - i;
73 	do {
74 		*x |= x[1] << j;
75 		x[1] >>= i;
76 		} while(++x < x1);
77 }
78 
79 int
hexnan(const char ** sp,const FPI * fpi,__ULong * x0)80 hexnan (const char **sp,
81 	const FPI *fpi,
82 	__ULong *x0)
83 {
84 	__ULong c, h, *x, *x1, *xe;
85 	const char *s;
86 	int havedig, hd0, i, nbits;
87 
88 	nbits = fpi->nbits;
89 	x = x0 + (nbits >> kshift);
90 	if (nbits & kmask)
91 		x++;
92 	*--x = 0;
93 	x1 = xe = x;
94 	havedig = hd0 = i = 0;
95 	s = *sp;
96 	while((c = *(const unsigned char*)++s)) {
97 		if (!(h = __get_hexdig(c))) {
98 			if (c <= ' ') {
99 				if (hd0 < havedig) {
100 					if (x < x1 && i < 8)
101 						L_shift(x, x1, i);
102 					if (x <= x0) {
103 						i = 8;
104 						continue;
105 						}
106 					hd0 = havedig;
107 					*--x = 0;
108 					x1 = x;
109 					i = 0;
110 					}
111 				continue;
112 				}
113 			if (/*(*/ c == ')') {
114 				*sp = s + 1;
115 				break;
116 				}
117 			return STRTOG_NaN;
118 			}
119 		havedig++;
120 		if (++i > 8) {
121 			if (x <= x0)
122 				continue;
123 			i = 1;
124 			*--x = 0;
125 			}
126 		*x = ((*x << 4) | (h & 0xf));
127 		}
128 	if (!havedig)
129 		return STRTOG_NaN;
130 	if (x < x1 && i < 8)
131 		L_shift(x, x1, i);
132 	if (x > x0) {
133 		x1 = x0;
134 		do *x1++ = *x++;
135 			while(x <= xe);
136 		do *x1++ = 0;
137 			while(x1 <= xe);
138 		}
139 	else {
140 		/* truncate high-order word if necessary */
141 		if ( (i = nbits & (ULbits-1)) !=0)
142 			*xe &= ((__ULong)0xffffffff) >> (ULbits - i);
143 		}
144 	for(x1 = xe;; --x1) {
145 		if (*x1 != 0)
146 			break;
147 		if (x1 == x0) {
148 			*x1 = 1;
149 			break;
150 			}
151 		}
152 	return STRTOG_NaNbits;
153 }
154 #endif /* INFNAN_CHECK */
155