hexnan.c revision 1.5 1 1.5 christos /* $NetBSD: hexnan.c,v 1.5 2011/03/21 04:52:09 christos Exp $ */
2 1.1 kleink
3 1.1 kleink /****************************************************************
4 1.1 kleink
5 1.1 kleink The author of this software is David M. Gay.
6 1.1 kleink
7 1.1 kleink Copyright (C) 2000 by Lucent Technologies
8 1.1 kleink All Rights Reserved
9 1.1 kleink
10 1.1 kleink Permission to use, copy, modify, and distribute this software and
11 1.1 kleink its documentation for any purpose and without fee is hereby
12 1.1 kleink granted, provided that the above copyright notice appear in all
13 1.1 kleink copies and that both that the copyright notice and this
14 1.1 kleink permission notice and warranty disclaimer appear in supporting
15 1.1 kleink documentation, and that the name of Lucent or any of its entities
16 1.1 kleink not be used in advertising or publicity pertaining to
17 1.1 kleink distribution of the software without specific, written prior
18 1.1 kleink permission.
19 1.1 kleink
20 1.1 kleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 1.1 kleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 1.1 kleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 1.1 kleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 1.1 kleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 1.1 kleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 1.1 kleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 1.1 kleink THIS SOFTWARE.
28 1.1 kleink
29 1.1 kleink ****************************************************************/
30 1.1 kleink
31 1.1 kleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
32 1.1 kleink * with " at " changed at "@" and " dot " changed to "."). */
33 1.1 kleink
34 1.1 kleink #include "gdtoaimp.h"
35 1.1 kleink
36 1.1 kleink static void
37 1.1 kleink #ifdef KR_headers
38 1.1 kleink L_shift(x, x1, i) ULong *x; ULong *x1; int i;
39 1.1 kleink #else
40 1.1 kleink L_shift(ULong *x, ULong *x1, int i)
41 1.1 kleink #endif
42 1.1 kleink {
43 1.1 kleink int j;
44 1.1 kleink
45 1.1 kleink i = 8 - i;
46 1.1 kleink i <<= 2;
47 1.1 kleink j = ULbits - i;
48 1.1 kleink do {
49 1.1 kleink *x |= x[1] << j;
50 1.1 kleink x[1] >>= i;
51 1.1 kleink } while(++x < x1);
52 1.1 kleink }
53 1.1 kleink
54 1.1 kleink int
55 1.1 kleink #ifdef KR_headers
56 1.1 kleink hexnan(sp, fpi, x0)
57 1.3 kleink CONST char **sp; CONST FPI *fpi; ULong *x0;
58 1.1 kleink #else
59 1.3 kleink hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0)
60 1.1 kleink #endif
61 1.1 kleink {
62 1.1 kleink ULong c, h, *x, *x1, *xe;
63 1.1 kleink CONST char *s;
64 1.1 kleink int havedig, hd0, i, nbits;
65 1.1 kleink
66 1.1 kleink if (!hexdig['0'])
67 1.1 kleink hexdig_init_D2A();
68 1.1 kleink nbits = fpi->nbits;
69 1.2 kleink x = x0 + ((unsigned int)nbits >> kshift);
70 1.1 kleink if (nbits & kmask)
71 1.1 kleink x++;
72 1.1 kleink *--x = 0;
73 1.1 kleink x1 = xe = x;
74 1.1 kleink havedig = hd0 = i = 0;
75 1.1 kleink s = *sp;
76 1.4 christos /* allow optional initial 0x or 0X */
77 1.4 christos while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
78 1.4 christos ++s;
79 1.4 christos if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
80 1.4 christos && *(CONST unsigned char*)(s+3) > ' ')
81 1.4 christos s += 2;
82 1.5 christos while((c = *(CONST unsigned char*)++s) != '\0') {
83 1.1 kleink if (!(h = hexdig[c])) {
84 1.1 kleink if (c <= ' ') {
85 1.1 kleink if (hd0 < havedig) {
86 1.1 kleink if (x < x1 && i < 8)
87 1.1 kleink L_shift(x, x1, i);
88 1.1 kleink if (x <= x0) {
89 1.1 kleink i = 8;
90 1.1 kleink continue;
91 1.1 kleink }
92 1.1 kleink hd0 = havedig;
93 1.1 kleink *--x = 0;
94 1.1 kleink x1 = x;
95 1.1 kleink i = 0;
96 1.1 kleink }
97 1.4 christos while(*(CONST unsigned char*)(s+1) <= ' ')
98 1.4 christos ++s;
99 1.4 christos if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
100 1.4 christos && *(CONST unsigned char*)(s+3) > ' ')
101 1.4 christos s += 2;
102 1.1 kleink continue;
103 1.1 kleink }
104 1.1 kleink if (/*(*/ c == ')' && havedig) {
105 1.1 kleink *sp = s + 1;
106 1.1 kleink break;
107 1.1 kleink }
108 1.4 christos #ifndef GDTOA_NON_PEDANTIC_NANCHECK
109 1.4 christos do {
110 1.4 christos if (/*(*/ c == ')') {
111 1.4 christos *sp = s + 1;
112 1.4 christos break;
113 1.4 christos }
114 1.5 christos } while((c = *++s) != '\0');
115 1.4 christos #endif
116 1.1 kleink return STRTOG_NaN;
117 1.1 kleink }
118 1.1 kleink havedig++;
119 1.1 kleink if (++i > 8) {
120 1.1 kleink if (x <= x0)
121 1.1 kleink continue;
122 1.1 kleink i = 1;
123 1.1 kleink *--x = 0;
124 1.1 kleink }
125 1.2 kleink *x = (*x << 4) | (h & 0xf);
126 1.1 kleink }
127 1.1 kleink if (!havedig)
128 1.1 kleink return STRTOG_NaN;
129 1.1 kleink if (x < x1 && i < 8)
130 1.1 kleink L_shift(x, x1, i);
131 1.1 kleink if (x > x0) {
132 1.1 kleink x1 = x0;
133 1.1 kleink do *x1++ = *x++;
134 1.1 kleink while(x <= xe);
135 1.1 kleink do *x1++ = 0;
136 1.1 kleink while(x1 <= xe);
137 1.1 kleink }
138 1.1 kleink else {
139 1.1 kleink /* truncate high-order word if necessary */
140 1.1 kleink if ( (i = nbits & (ULbits-1)) !=0)
141 1.1 kleink *xe &= ((ULong)0xffffffff) >> (ULbits - i);
142 1.1 kleink }
143 1.1 kleink for(x1 = xe;; --x1) {
144 1.1 kleink if (*x1 != 0)
145 1.1 kleink break;
146 1.1 kleink if (x1 == x0) {
147 1.1 kleink *x1 = 1;
148 1.1 kleink break;
149 1.1 kleink }
150 1.1 kleink }
151 1.1 kleink return STRTOG_NaNbits;
152 1.1 kleink }
153