]>
git.saurik.com Git - apple/libc.git/blob - gdtoa/gdtoa-gethex-fbsd.c
f970946b1ef43d4521c2aae06757048cac7d4c03
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 "."). */
32 #include "xlocale_private.h"
42 gethex(sp
, fpi
, exp
, bp
, sign
, loc
)
43 CONST
char **sp
; FPI
*fpi
; Long
*exp
; Bigint
**bp
; int sign
; locale_t loc
;
45 gethex( CONST
char **sp
, FPI
*fpi
, Long
*exp
, Bigint
**bp
, int sign
, locale_t loc
)
49 CONST
unsigned char *decpt
, *s0
, *s
, *s1
;
50 int big
, esign
, havedig
, irv
, j
, k
, n
, n0
, nbits
, up
, zret
;
51 ULong L
, lostbits
, *x
;
55 NORMALIZE_LOCALE(loc
);
56 #ifdef NO_LOCALE_CACHE
57 const unsigned char *decimalpoint
= (unsigned char*)localeconv_l(loc
)->decimal_point
;
59 const unsigned char *decimalpoint
;
60 static unsigned char *decimalpoint_cache
;
61 if (!(s0
= decimalpoint_cache
)) {
62 s0
= (unsigned char*)localeconv_l(loc
)->decimal_point
;
63 if ((decimalpoint_cache
= (char*)malloc(strlen(s0
) + 1))) {
64 strcpy(decimalpoint_cache
, s0
);
65 s0
= decimalpoint_cache
;
76 s0
= *(CONST
unsigned char **)sp
+ 2;
77 while(s0
[havedig
] == '0')
89 for(i
= 0; decimalpoint
[i
]; ++i
) {
90 if (s
[i
] != decimalpoint
[i
])
111 if (*s
== *decimalpoint
&& !decpt
) {
112 for(i
= 1; decimalpoint
[i
]; ++i
) {
113 if (s
[i
] != decimalpoint
[i
])
118 if (*s
== '.' && !decpt
) {
125 e
= -(((Long
)(s
-decpt
)) << 2);
139 if ((n
= hexdig
[*s
]) == 0 || n
> 0x19) {
144 while((n
= hexdig
[*++s
]) !=0 && n
<= 0x19) {
147 e1
= 10*e1
+ n
- 0x10;
160 switch(fpi
->rounding
) {
177 switch(fpi
->rounding
) {
191 n0
= n
= nbits
>> kshift
;
194 for(j
= n
, k
= 0; j
>>= 1; ++k
);
197 for(j
= 0; j
< n0
; ++j
)
200 b
->x
[j
] = ULbits
>> (ULbits
- (nbits
& kmask
));
202 return STRTOG_Normal
| STRTOG_Inexlo
;
205 for(k
= 0; n
> 7; n
>>= 1)
212 for(i
= 0; decimalpoint
[i
+1]; ++i
);
216 if (*--s1
== decimalpoint
[i
]) {
229 L
|= (hexdig
[*s1
] & 0x0f) << n
;
233 b
->wds
= n
= x
- b
->x
;
234 n
= 32*n
- hi0bits(L
);
243 if (x
[k
>>kshift
] & 1 << (k
& kmask
)) {
245 if (k
> 0 && any_on(b
,k
))
252 else if (n
< nbits
) {
265 return STRTOG_Infinite
| STRTOG_Overflow
| STRTOG_Inexhi
;
269 irv
= STRTOG_Denormal
;
272 switch (fpi
->rounding
) {
274 if (n
== nbits
&& (n
< 2 || any_on(b
,n
-1)))
291 return STRTOG_Denormal
| STRTOG_Inexhi
300 return STRTOG_Zero
| STRTOG_Inexlo
| STRTOG_Underflow
;
306 lostbits
= any_on(b
,k
);
307 if (x
[k
>>kshift
] & 1 << (k
& kmask
))
315 switch(fpi
->rounding
) {
320 && (lostbits
& 1) | x
[0] & 1)
333 if (irv
== STRTOG_Denormal
) {
334 if (nbits
== fpi
->nbits
- 1
335 && x
[nbits
>> kshift
] & 1 << (nbits
& kmask
))
339 || (n
= nbits
& kmask
) !=0
340 && hi0bits(x
[k
-1]) < 32-n
) {
345 irv
|= STRTOG_Inexhi
;
348 irv
|= STRTOG_Inexlo
;