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