xref: /freebsd/contrib/gdtoa/hexnan.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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 #include "gdtoaimp.h"
39 
40  static void
41 #ifdef KR_headers
42 L_shift(x, x1, i) ULong *x; ULong *x1; int i;
43 #else
44 L_shift(ULong *x, ULong *x1, int i)
45 #endif
46 {
47 	int j;
48 
49 	i = 8 - i;
50 	i <<= 2;
51 	j = ULbits - i;
52 	do {
53 		*x |= x[1] << j;
54 		x[1] >>= i;
55 		} while(++x < x1);
56 	}
57 
58  int
59 #ifdef KR_headers
60 hexnan(sp, fpi, x0)
61 	CONST char **sp; FPI *fpi; ULong *x0;
62 #else
63 hexnan( CONST char **sp, FPI *fpi, ULong *x0)
64 #endif
65 {
66 	ULong c, h, *x, *x1, *xe;
67 	CONST char *s;
68 	int havedig, hd0, i, nbits;
69 
70 	if (!hexdig['0'])
71 		hexdig_init_D2A();
72 	nbits = fpi->nbits;
73 	x = x0 + (nbits >> kshift);
74 	if (nbits & kmask)
75 		x++;
76 	*--x = 0;
77 	x1 = xe = x;
78 	havedig = hd0 = i = 0;
79 	s = *sp;
80 	while(c = *(CONST unsigned char*)++s) {
81 		if (!(h = hexdig[c])) {
82 			if (c <= ' ') {
83 				if (hd0 < havedig) {
84 					if (x < x1 && i < 8)
85 						L_shift(x, x1, i);
86 					if (x <= x0) {
87 						i = 8;
88 						continue;
89 						}
90 					hd0 = havedig;
91 					*--x = 0;
92 					x1 = x;
93 					i = 0;
94 					}
95 				continue;
96 				}
97 			if (/*(*/ c == ')' && havedig) {
98 				*sp = s + 1;
99 				break;
100 				}
101 			return STRTOG_NaN;
102 			}
103 		havedig++;
104 		if (++i > 8) {
105 			if (x <= x0)
106 				continue;
107 			i = 1;
108 			*--x = 0;
109 			}
110 		*x = (*x << 4) | h & 0xf;
111 		}
112 	if (!havedig)
113 		return STRTOG_NaN;
114 	if (x < x1 && i < 8)
115 		L_shift(x, x1, i);
116 	if (x > x0) {
117 		x1 = x0;
118 		do *x1++ = *x++;
119 			while(x <= xe);
120 		do *x1++ = 0;
121 			while(x1 <= xe);
122 		}
123 	else {
124 		/* truncate high-order word if necessary */
125 		if ( (i = nbits & (ULbits-1)) !=0)
126 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
127 		}
128 	for(x1 = xe;; --x1) {
129 		if (*x1 != 0)
130 			break;
131 		if (x1 == x0) {
132 			*x1 = 1;
133 			break;
134 			}
135 		}
136 	return STRTOG_NaNbits;
137 	}
138