xref: /netbsd-src/lib/libc/gdtoa/hexnan.c (revision a23ced05b46782f187d1f9695e23df1746b87472)
1*a23ced05Schristos /* $NetBSD: hexnan.c,v 1.5 2011/03/21 04:52:09 christos Exp $ */
27684d5e0Skleink 
37684d5e0Skleink /****************************************************************
47684d5e0Skleink 
57684d5e0Skleink The author of this software is David M. Gay.
67684d5e0Skleink 
77684d5e0Skleink Copyright (C) 2000 by Lucent Technologies
87684d5e0Skleink All Rights Reserved
97684d5e0Skleink 
107684d5e0Skleink Permission to use, copy, modify, and distribute this software and
117684d5e0Skleink its documentation for any purpose and without fee is hereby
127684d5e0Skleink granted, provided that the above copyright notice appear in all
137684d5e0Skleink copies and that both that the copyright notice and this
147684d5e0Skleink permission notice and warranty disclaimer appear in supporting
157684d5e0Skleink documentation, and that the name of Lucent or any of its entities
167684d5e0Skleink not be used in advertising or publicity pertaining to
177684d5e0Skleink distribution of the software without specific, written prior
187684d5e0Skleink permission.
197684d5e0Skleink 
207684d5e0Skleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
217684d5e0Skleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
227684d5e0Skleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
237684d5e0Skleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
247684d5e0Skleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
257684d5e0Skleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
267684d5e0Skleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
277684d5e0Skleink THIS SOFTWARE.
287684d5e0Skleink 
297684d5e0Skleink ****************************************************************/
307684d5e0Skleink 
317684d5e0Skleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
327684d5e0Skleink  * with " at " changed at "@" and " dot " changed to ".").	*/
337684d5e0Skleink 
347684d5e0Skleink #include "gdtoaimp.h"
357684d5e0Skleink 
367684d5e0Skleink  static void
377684d5e0Skleink #ifdef KR_headers
L_shift(x,x1,i)387684d5e0Skleink L_shift(x, x1, i) ULong *x; ULong *x1; int i;
397684d5e0Skleink #else
407684d5e0Skleink L_shift(ULong *x, ULong *x1, int i)
417684d5e0Skleink #endif
427684d5e0Skleink {
437684d5e0Skleink 	int j;
447684d5e0Skleink 
457684d5e0Skleink 	i = 8 - i;
467684d5e0Skleink 	i <<= 2;
477684d5e0Skleink 	j = ULbits - i;
487684d5e0Skleink 	do {
497684d5e0Skleink 		*x |= x[1] << j;
507684d5e0Skleink 		x[1] >>= i;
517684d5e0Skleink 		} while(++x < x1);
527684d5e0Skleink 	}
537684d5e0Skleink 
547684d5e0Skleink  int
557684d5e0Skleink #ifdef KR_headers
hexnan(sp,fpi,x0)567684d5e0Skleink hexnan(sp, fpi, x0)
57f8228ce4Skleink 	CONST char **sp; CONST FPI *fpi; ULong *x0;
587684d5e0Skleink #else
59f8228ce4Skleink hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0)
607684d5e0Skleink #endif
617684d5e0Skleink {
627684d5e0Skleink 	ULong c, h, *x, *x1, *xe;
637684d5e0Skleink 	CONST char *s;
647684d5e0Skleink 	int havedig, hd0, i, nbits;
657684d5e0Skleink 
667684d5e0Skleink 	if (!hexdig['0'])
677684d5e0Skleink 		hexdig_init_D2A();
687684d5e0Skleink 	nbits = fpi->nbits;
69ac898a26Skleink 	x = x0 + ((unsigned int)nbits >> kshift);
707684d5e0Skleink 	if (nbits & kmask)
717684d5e0Skleink 		x++;
727684d5e0Skleink 	*--x = 0;
737684d5e0Skleink 	x1 = xe = x;
747684d5e0Skleink 	havedig = hd0 = i = 0;
757684d5e0Skleink 	s = *sp;
7661e56760Schristos 	/* allow optional initial 0x or 0X */
7761e56760Schristos 	while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
7861e56760Schristos 		++s;
7961e56760Schristos 	if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
8061e56760Schristos 	 && *(CONST unsigned char*)(s+3) > ' ')
8161e56760Schristos 		s += 2;
82*a23ced05Schristos 	while((c = *(CONST unsigned char*)++s) != '\0') {
837684d5e0Skleink 		if (!(h = hexdig[c])) {
847684d5e0Skleink 			if (c <= ' ') {
857684d5e0Skleink 				if (hd0 < havedig) {
867684d5e0Skleink 					if (x < x1 && i < 8)
877684d5e0Skleink 						L_shift(x, x1, i);
887684d5e0Skleink 					if (x <= x0) {
897684d5e0Skleink 						i = 8;
907684d5e0Skleink 						continue;
917684d5e0Skleink 						}
927684d5e0Skleink 					hd0 = havedig;
937684d5e0Skleink 					*--x = 0;
947684d5e0Skleink 					x1 = x;
957684d5e0Skleink 					i = 0;
967684d5e0Skleink 					}
9761e56760Schristos 				while(*(CONST unsigned char*)(s+1) <= ' ')
9861e56760Schristos 					++s;
9961e56760Schristos 				if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
10061e56760Schristos 				 && *(CONST unsigned char*)(s+3) > ' ')
10161e56760Schristos 					s += 2;
1027684d5e0Skleink 				continue;
1037684d5e0Skleink 				}
1047684d5e0Skleink 			if (/*(*/ c == ')' && havedig) {
1057684d5e0Skleink 				*sp = s + 1;
1067684d5e0Skleink 				break;
1077684d5e0Skleink 				}
10861e56760Schristos #ifndef GDTOA_NON_PEDANTIC_NANCHECK
10961e56760Schristos 			do {
11061e56760Schristos 				if (/*(*/ c == ')') {
11161e56760Schristos 					*sp = s + 1;
11261e56760Schristos 					break;
11361e56760Schristos 					}
114*a23ced05Schristos 				} while((c = *++s) != '\0');
11561e56760Schristos #endif
1167684d5e0Skleink 			return STRTOG_NaN;
1177684d5e0Skleink 			}
1187684d5e0Skleink 		havedig++;
1197684d5e0Skleink 		if (++i > 8) {
1207684d5e0Skleink 			if (x <= x0)
1217684d5e0Skleink 				continue;
1227684d5e0Skleink 			i = 1;
1237684d5e0Skleink 			*--x = 0;
1247684d5e0Skleink 			}
125ac898a26Skleink 		*x = (*x << 4) | (h & 0xf);
1267684d5e0Skleink 		}
1277684d5e0Skleink 	if (!havedig)
1287684d5e0Skleink 		return STRTOG_NaN;
1297684d5e0Skleink 	if (x < x1 && i < 8)
1307684d5e0Skleink 		L_shift(x, x1, i);
1317684d5e0Skleink 	if (x > x0) {
1327684d5e0Skleink 		x1 = x0;
1337684d5e0Skleink 		do *x1++ = *x++;
1347684d5e0Skleink 			while(x <= xe);
1357684d5e0Skleink 		do *x1++ = 0;
1367684d5e0Skleink 			while(x1 <= xe);
1377684d5e0Skleink 		}
1387684d5e0Skleink 	else {
1397684d5e0Skleink 		/* truncate high-order word if necessary */
1407684d5e0Skleink 		if ( (i = nbits & (ULbits-1)) !=0)
1417684d5e0Skleink 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
1427684d5e0Skleink 		}
1437684d5e0Skleink 	for(x1 = xe;; --x1) {
1447684d5e0Skleink 		if (*x1 != 0)
1457684d5e0Skleink 			break;
1467684d5e0Skleink 		if (x1 == x0) {
1477684d5e0Skleink 			*x1 = 1;
1487684d5e0Skleink 			break;
1497684d5e0Skleink 			}
1507684d5e0Skleink 		}
1517684d5e0Skleink 	return STRTOG_NaNbits;
1527684d5e0Skleink 	}
153