Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / gdtoa-gethex.c
blob74f30e69013dcb7141967c7465729e773aa4fee3
1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
6 All Rights Reserved
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.
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.
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
32 #include <_ansi.h>
33 #include <reent.h>
34 #include <string.h>
35 #include <locale.h>
36 #include "mprec.h"
37 #include "gdtoa.h"
39 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG)
40 const unsigned char __hexdig[256]=
42 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
43 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
44 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
45 16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,
46 0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
47 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
48 0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
49 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
50 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
51 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
59 #else /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) */
60 unsigned char
61 __hexdig_fun (unsigned char c)
63 if(c>='0' && c<='9') return c-'0'+0x10;
64 else if(c>='a' && c<='f') return c-'a'+0x10+10;
65 else if(c>='A' && c<='F') return c-'A'+0x10+10;
66 else return 0;
68 #endif /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) */
70 static void
71 rshift (_Bigint *b,
72 int k)
74 __ULong *x, *x1, *xe, y;
75 int n;
77 x = x1 = b->_x;
78 n = k >> kshift;
79 if (n < b->_wds) {
80 xe = x + b->_wds;
81 x += n;
82 if (k &= kmask) {
83 n = ULbits - k;
84 y = *x++ >> k;
85 while(x < xe) {
86 *x1++ = (y | (*x << n)) & ALL_ON;
87 y = *x++ >> k;
89 if ((*x1 = y) !=0)
90 x1++;
92 else
93 while(x < xe)
94 *x1++ = *x++;
96 if ((b->_wds = x1 - b->_x) == 0)
97 b->_x[0] = 0;
100 static _Bigint *
101 increment (struct _reent *ptr,
102 _Bigint *b)
104 __ULong *x, *xe;
105 _Bigint *b1;
106 #ifdef Pack_16
107 __ULong carry = 1, y;
108 #endif
110 x = b->_x;
111 xe = x + b->_wds;
112 #ifdef Pack_32
113 do {
114 if (*x < (__ULong)0xffffffffL) {
115 ++*x;
116 return b;
118 *x++ = 0;
119 } while(x < xe);
120 #else
121 do {
122 y = *x + carry;
123 carry = y >> 16;
124 *x++ = y & 0xffff;
125 if (!carry)
126 return b;
127 } while(x < xe);
128 if (carry)
129 #endif
131 if (b->_wds >= b->_maxwds) {
132 b1 = eBalloc(ptr, b->_k+1);
133 Bcopy(b1, b);
134 Bfree(ptr, b);
135 b = b1;
137 b->_x[b->_wds++] = 1;
139 return b;
144 gethex (struct _reent *ptr, const char **sp, const FPI *fpi,
145 Long *exp, _Bigint **bp, int sign, locale_t loc)
147 _Bigint *b;
148 const unsigned char *decpt, *s0, *s, *s1;
149 int esign, havedig, irv, k, n, nbits, up, zret;
150 __ULong L, lostbits, *x;
151 Long e, e1;
152 #ifdef __HAVE_LOCALE_INFO__
153 const unsigned char *decimalpoint = (const unsigned char *)
154 __get_numeric_locale(loc)->decimal_point;
155 const size_t decp_len = strlen ((const char *) decimalpoint);
156 const unsigned char decp_end = decimalpoint[decp_len - 1];
157 #else
158 const unsigned char *decimalpoint = (const unsigned char *) ".";
159 const size_t decp_len = 1;
160 const unsigned char decp_end = (unsigned char) '.';
161 #endif
163 havedig = 0;
164 s0 = *(const unsigned char **)sp + 2;
165 while(s0[havedig] == '0')
166 havedig++;
167 s0 += havedig;
168 s = s0;
169 decpt = 0;
170 zret = 0;
171 e = 0;
172 if (!__get_hexdig(*s)) {
173 zret = 1;
174 if (strncmp ((const char *) s, (const char *) decimalpoint,
175 decp_len) != 0)
176 goto pcheck;
177 decpt = (s += decp_len);
178 if (!__get_hexdig(*s))
179 goto pcheck;
180 while(*s == '0')
181 s++;
182 if (__get_hexdig(*s))
183 zret = 0;
184 havedig = 1;
185 s0 = s;
187 while(__get_hexdig(*s))
188 s++;
189 if (strncmp ((const char *) s, (const char *) decimalpoint,
190 decp_len) == 0
191 && !decpt) {
192 decpt = (s += decp_len);
193 while(__get_hexdig(*s))
194 s++;
196 if (decpt)
197 e = -(((Long)(s-decpt)) << 2);
198 pcheck:
199 s1 = s;
200 switch(*s) {
201 case 'p':
202 case 'P':
203 esign = 0;
204 switch(*++s) {
205 case '-':
206 esign = 1;
207 /* no break */
208 case '+':
209 s++;
211 if ((n = __get_hexdig(*s)) == 0 || n > 0x19) {
212 s = s1;
213 break;
215 e1 = n - 0x10;
216 while((n = __get_hexdig(*++s)) !=0 && n <= 0x19)
217 e1 = 10*e1 + n - 0x10;
218 if (esign)
219 e1 = -e1;
220 e += e1;
222 *sp = (char*)s;
223 if (zret)
224 return havedig ? STRTOG_Zero : STRTOG_NoNumber;
225 n = s1 - s0 - 1;
226 for(k = 0; n > 7; n >>= 1)
227 k++;
228 b = eBalloc(ptr, k);
229 x = b->_x;
230 n = 0;
231 L = 0;
232 while(s1 > s0) {
233 if (*--s1 == decp_end && s1 - decp_len + 1 >= s0
234 && strncmp ((const char *) s1 - decp_len + 1,
235 (const char *) decimalpoint, decp_len) == 0) {
236 s1 -= decp_len - 1; /* Note the --s1 above! */
237 continue;
239 if (n == 32) {
240 *x++ = L;
241 L = 0;
242 n = 0;
244 L |= (__get_hexdig(*s1) & 0x0f) << n;
245 n += 4;
247 *x++ = L;
248 b->_wds = n = x - b->_x;
249 n = 32*n - hi0bits(L);
250 nbits = fpi->nbits;
251 lostbits = 0;
252 x = b->_x;
253 if (n > nbits) {
254 n -= nbits;
255 if (any_on(b,n)) {
256 lostbits = 1;
257 k = n - 1;
258 if (x[k>>kshift] & 1 << (k & kmask)) {
259 lostbits = 2;
260 if (k > 1 && any_on(b,k-1))
261 lostbits = 3;
264 rshift(b, n);
265 e += n;
267 else if (n < nbits) {
268 n = nbits - n;
269 b = lshift(ptr, b, n);
270 e -= n;
271 x = b->_x;
273 if (e > fpi->emax) {
274 ovfl:
275 Bfree(ptr, b);
276 *bp = 0;
277 return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi;
279 irv = STRTOG_Normal;
280 if (e < fpi->emin) {
281 irv = STRTOG_Denormal;
282 n = fpi->emin - e;
283 if (n >= nbits) {
284 switch (fpi->rounding) {
285 case FPI_Round_near:
286 if (n == nbits && (n < 2 || any_on(b,n-1)))
287 goto one_bit;
288 break;
289 case FPI_Round_up:
290 if (!sign)
291 goto one_bit;
292 break;
293 case FPI_Round_down:
294 if (sign) {
295 one_bit:
296 *exp = fpi->emin;
297 x[0] = b->_wds = 1;
298 *bp = b;
299 return STRTOG_Denormal | STRTOG_Inexhi
300 | STRTOG_Underflow;
303 Bfree(ptr, b);
304 *bp = 0;
305 return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow;
307 k = n - 1;
308 if (lostbits)
309 lostbits = 1;
310 else if (k > 0)
311 lostbits = any_on(b,k);
312 if (x[k>>kshift] & 1 << (k & kmask))
313 lostbits |= 2;
314 nbits -= n;
315 rshift(b,n);
316 e = fpi->emin;
318 if (lostbits) {
319 up = 0;
320 switch(fpi->rounding) {
321 case FPI_Round_zero:
322 break;
323 case FPI_Round_near:
324 if ((lostbits & 2)
325 && ((lostbits & 1) | (x[0] & 1)))
326 up = 1;
327 break;
328 case FPI_Round_up:
329 up = 1 - sign;
330 break;
331 case FPI_Round_down:
332 up = sign;
334 if (up) {
335 k = b->_wds;
336 b = increment(ptr, b);
337 x = b->_x;
338 if (irv == STRTOG_Denormal) {
339 if (nbits == fpi->nbits - 1
340 && x[nbits >> kshift] & 1 << (nbits & kmask))
341 irv = STRTOG_Normal;
343 else if ((b->_wds > k)
344 || ((n = nbits & kmask) !=0
345 && (hi0bits(x[k-1]) < 32-n))) {
346 rshift(b,1);
347 if (++e > fpi->emax)
348 goto ovfl;
350 irv |= STRTOG_Inexhi;
352 else
353 irv |= STRTOG_Inexlo;
355 *bp = b;
356 *exp = e;
357 return irv;