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