blob: 4fa4c77458d47024d3a6786c96a7c12ae7e4db42 [file] [log] [blame]
Kai Tietz97a6a332008-04-10 09:29:08 +00001/****************************************************************
2
3The author of this software is David M. Gay.
4
5Copyright (C) 2000 by Lucent Technologies
6All Rights Reserved
7
8Permission to use, copy, modify, and distribute this software and
9its documentation for any purpose and without fee is hereby
10granted, provided that the above copyright notice appear in all
11copies and that both that the copyright notice and this
12permission notice and warranty disclaimer appear in supporting
13documentation, and that the name of Lucent or any of its entities
14not be used in advertising or publicity pertaining to
15distribution of the software without specific, written prior
16permission.
17
18LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25THIS 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
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000034static void L_shift (ULong *x, ULong *x1, int i)
Kai Tietz97a6a332008-04-10 09:29:08 +000035{
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;
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000044 } while(++x < x1);
45}
Kai Tietz97a6a332008-04-10 09:29:08 +000046
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000047int hexnan (const char **sp, FPI *fpi, ULong *x0)
Kai Tietz97a6a332008-04-10 09:29:08 +000048{
49 ULong c, h, *x, *x1, *xe;
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000050 const char *s;
Kai Tietz97a6a332008-04-10 09:29:08 +000051 int havedig, hd0, i, nbits;
52
53 if (!hexdig['0'])
54 hexdig_init_D2A();
55 nbits = fpi->nbits;
56 x = x0 + (nbits >> kshift);
57 if (nbits & kmask)
58 x++;
59 *--x = 0;
60 x1 = xe = x;
61 havedig = hd0 = i = 0;
62 s = *sp;
Ozkan Sezerd329d782009-04-15 15:20:10 +000063 /* allow optional initial 0x or 0X */
64 while((c = *(const unsigned char*)(s+1)) && c <= ' ')
65 ++s;
66 if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
67 && *(const unsigned char*)(s+3) > ' ')
68 s += 2;
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000069 while((c = *(const unsigned char*)++s)) {
Kai Tietz97a6a332008-04-10 09:29:08 +000070 if (!(h = hexdig[c])) {
71 if (c <= ' ') {
72 if (hd0 < havedig) {
73 if (x < x1 && i < 8)
74 L_shift(x, x1, i);
75 if (x <= x0) {
76 i = 8;
77 continue;
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000078 }
Kai Tietz97a6a332008-04-10 09:29:08 +000079 hd0 = havedig;
80 *--x = 0;
81 x1 = x;
82 i = 0;
Kai Tietz97a6a332008-04-10 09:29:08 +000083 }
Ozkan Sezerd329d782009-04-15 15:20:10 +000084 while(*(const unsigned char*)(s+1) <= ' ')
85 ++s;
86 if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
87 && *(const unsigned char*)(s+3) > ' ')
88 s += 2;
Ozkan Sezer455ce8b2009-04-13 15:02:01 +000089 continue;
90 }
Kai Tietz97a6a332008-04-10 09:29:08 +000091 if (/*(*/ c == ')' && havedig) {
92 *sp = s + 1;
93 break;
Kai Tietz97a6a332008-04-10 09:29:08 +000094 }
Ozkan Sezerd329d782009-04-15 15:20:10 +000095#ifndef GDTOA_NON_PEDANTIC_NANCHECK
96 do {
97 if (/*(*/ c == ')') {
98 *sp = s + 1;
99 break;
100 }
101 } while((c = *++s));
102#endif
Ozkan Sezer455ce8b2009-04-13 15:02:01 +0000103 return STRTOG_NaN;
104 }
Kai Tietz97a6a332008-04-10 09:29:08 +0000105 havedig++;
106 if (++i > 8) {
107 if (x <= x0)
108 continue;
109 i = 1;
110 *--x = 0;
Kai Tietz97a6a332008-04-10 09:29:08 +0000111 }
Ozkan Sezer455ce8b2009-04-13 15:02:01 +0000112 *x = (*x << 4) | (h & 0xf);
113 }
Kai Tietz97a6a332008-04-10 09:29:08 +0000114 if (!havedig)
115 return STRTOG_NaN;
116 if (x < x1 && i < 8)
117 L_shift(x, x1, i);
118 if (x > x0) {
119 x1 = x0;
120 do *x1++ = *x++;
121 while(x <= xe);
122 do *x1++ = 0;
123 while(x1 <= xe);
Ozkan Sezer455ce8b2009-04-13 15:02:01 +0000124 }
Kai Tietz97a6a332008-04-10 09:29:08 +0000125 else {
126 /* truncate high-order word if necessary */
127 if ( (i = nbits & (ULbits-1)) !=0)
128 *xe &= ((ULong)0xffffffff) >> (ULbits - i);
Ozkan Sezer455ce8b2009-04-13 15:02:01 +0000129 }
Kai Tietz97a6a332008-04-10 09:29:08 +0000130 for(x1 = xe;; --x1) {
131 if (*x1 != 0)
132 break;
133 if (x1 == x0) {
134 *x1 = 1;
135 break;
Kai Tietz97a6a332008-04-10 09:29:08 +0000136 }
Kai Tietz97a6a332008-04-10 09:29:08 +0000137 }
Ozkan Sezer455ce8b2009-04-13 15:02:01 +0000138 return STRTOG_NaNbits;
139}