1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
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
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
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
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) */
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;
68 #endif /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) */
74 __ULong
*x
, *x1
, *xe
, y
;
86 *x1
++ = (y
| (*x
<< n
)) & ALL_ON
;
96 if ((b
->_wds
= x1
- b
->_x
) == 0)
101 increment (struct _reent
*ptr
,
107 __ULong carry
= 1, y
;
114 if (*x
< (__ULong
)0xffffffffL
) {
131 if (b
->_wds
>= b
->_maxwds
) {
132 b1
= eBalloc(ptr
, b
->_k
+1);
137 b
->_x
[b
->_wds
++] = 1;
144 gethex (struct _reent
*ptr
, const char **sp
, const FPI
*fpi
,
145 Long
*exp
, _Bigint
**bp
, int sign
, locale_t loc
)
148 const unsigned char *decpt
, *s0
, *s
, *s1
;
149 int esign
, havedig
, irv
, k
, n
, nbits
, up
, zret
;
150 __ULong L
, lostbits
, *x
;
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];
158 const unsigned char *decimalpoint
= (const unsigned char *) ".";
159 const size_t decp_len
= 1;
160 const unsigned char decp_end
= (unsigned char) '.';
164 s0
= *(const unsigned char **)sp
+ 2;
165 while(s0
[havedig
] == '0')
172 if (!__get_hexdig(*s
)) {
174 if (strncmp ((const char *) s
, (const char *) decimalpoint
,
177 decpt
= (s
+= decp_len
);
178 if (!__get_hexdig(*s
))
182 if (__get_hexdig(*s
))
187 while(__get_hexdig(*s
))
189 if (strncmp ((const char *) s
, (const char *) decimalpoint
,
192 decpt
= (s
+= decp_len
);
193 while(__get_hexdig(*s
))
197 e
= -(((Long
)(s
-decpt
)) << 2);
211 if ((n
= __get_hexdig(*s
)) == 0 || n
> 0x19) {
216 while((n
= __get_hexdig(*++s
)) !=0 && n
<= 0x19)
217 e1
= 10*e1
+ n
- 0x10;
224 return havedig
? STRTOG_Zero
: STRTOG_NoNumber
;
226 for(k
= 0; n
> 7; n
>>= 1)
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! */
244 L
|= (__get_hexdig(*s1
) & 0x0f) << n
;
248 b
->_wds
= n
= x
- b
->_x
;
249 n
= 32*n
- hi0bits(L
);
258 if (x
[k
>>kshift
] & 1 << (k
& kmask
)) {
260 if (k
> 1 && any_on(b
,k
-1))
267 else if (n
< nbits
) {
269 b
= lshift(ptr
, b
, n
);
277 return STRTOG_Infinite
| STRTOG_Overflow
| STRTOG_Inexhi
;
281 irv
= STRTOG_Denormal
;
284 switch (fpi
->rounding
) {
286 if (n
== nbits
&& (n
< 2 || any_on(b
,n
-1)))
299 return STRTOG_Denormal
| STRTOG_Inexhi
305 return STRTOG_Zero
| STRTOG_Inexlo
| STRTOG_Underflow
;
311 lostbits
= any_on(b
,k
);
312 if (x
[k
>>kshift
] & 1 << (k
& kmask
))
320 switch(fpi
->rounding
) {
325 && ((lostbits
& 1) | (x
[0] & 1)))
336 b
= increment(ptr
, b
);
338 if (irv
== STRTOG_Denormal
) {
339 if (nbits
== fpi
->nbits
- 1
340 && x
[nbits
>> kshift
] & 1 << (nbits
& kmask
))
343 else if ((b
->_wds
> k
)
344 || ((n
= nbits
& kmask
) !=0
345 && (hi0bits(x
[k
-1]) < 32-n
))) {
350 irv
|= STRTOG_Inexhi
;
353 irv
|= STRTOG_Inexlo
;