1 /* $NetBSD: gethex.c,v 1.6 2013/04/19 10:41:53 joerg Exp $ */
3 /****************************************************************
5 The author of this software is David M. Gay.
7 Copyright (C) 1998 by Lucent Technologies
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
29 ****************************************************************/
31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
32 * with " at " changed at "@" and " dot " changed to "."). */
41 gethex( CONST
char **sp
, CONST FPI
*fpi
, Long
*expt
, Bigint
**bp
, int sign
, locale_t loc
)
44 CONST
char *decpt
, *s
, *s0
, *s1
;
45 int big
, esign
, havedig
, irv
, j
, k
, n
, n0
, nbits
, up
, zret
;
46 ULong L
, lostbits
, *x
;
50 const char *decimalpoint
= localeconv_l(loc
)->decimal_point
;
53 if (!hexdig
[(unsigned char)'0'])
57 s0
= *(CONST
char **)sp
+ 2;
58 while(s0
[havedig
] == '0')
65 if (hexdig
[(unsigned char)*s
])
70 for(i
= 0; decimalpoint
[i
]; ++i
) {
71 if (s
[i
] != decimalpoint
[i
])
80 if (!hexdig
[(unsigned char)*s
])
84 if (hexdig
[(unsigned char)*s
])
89 while(hexdig
[(unsigned char)*s
])
92 if (*s
== *decimalpoint
&& !decpt
) {
93 for(i
= 1; decimalpoint
[i
]; ++i
) {
94 if (s
[i
] != decimalpoint
[i
])
99 if (*s
== '.' && !decpt
) {
102 while(hexdig
[(unsigned char)*s
])
106 e
= -(((Long
)(s
-decpt
)) << 2);
120 if ((n
= hexdig
[(unsigned char)*s
]) == 0 || n
> 0x19) {
125 while((n
= hexdig
[(unsigned char)*++s
]) !=0 && n
<= 0x19) {
128 e1
= 10*e1
+ n
- 0x10;
136 *sp
= (char*)__UNCONST(s0
) - 1;
141 switch(fpi
->rounding
) {
158 switch(fpi
->rounding
) {
172 n0
= n
= (unsigned int)nbits
>> kshift
;
175 for(j
= n
, k
= 0; (j
= (unsigned int)j
>> 1) != 0; ++k
);
178 for(j
= 0; j
< n0
; ++j
)
181 b
->x
[j
] = ULbits
>> (ULbits
- (nbits
& kmask
));
183 return STRTOG_Normal
| STRTOG_Inexlo
;
185 n
= (int)(s1
- s0
) - 1;
186 for(k
= 0; n
> (1 << (kshift
-2)) - 1; n
= (unsigned int)n
>> 1)
190 return STRTOG_NoMemory
;
195 for(i
= 0; decimalpoint
[i
+1]; ++i
);
199 if (*--s1
== decimalpoint
[i
]) {
212 L
|= (hexdig
[(unsigned char)*s1
] & 0x0f) << n
;
216 b
->wds
= n
= (int)(x
- b
->x
);
217 n
= ULbits
*n
- hi0bits(L
);
226 if (x
[(unsigned int)k
>>kshift
] & 1 << (k
& kmask
)) {
228 if (k
> 0 && any_on(b
,k
))
235 else if (n
< nbits
) {
239 return STRTOG_NoMemory
;
250 return STRTOG_Infinite
| STRTOG_Overflow
| STRTOG_Inexhi
;
254 irv
= STRTOG_Denormal
;
257 switch (fpi
->rounding
) {
259 if (n
== nbits
&& (n
< 2 || any_on(b
,n
-1)))
276 return STRTOG_Denormal
| STRTOG_Inexhi
285 return STRTOG_Zero
| STRTOG_Inexlo
| STRTOG_Underflow
;
291 lostbits
= any_on(b
,k
);
292 if (x
[(unsigned int)k
>>kshift
] & 1 << (k
& kmask
))
300 switch(fpi
->rounding
) {
305 && (lostbits
| x
[0]) & 1)
318 if (irv
== STRTOG_Denormal
) {
319 if (nbits
== fpi
->nbits
- 1
320 && x
[(unsigned int)nbits
>> kshift
] & 1 << (nbits
& kmask
))
324 || ((n
= nbits
& kmask
) !=0
325 && hi0bits(x
[k
-1]) < 32-n
)) {
330 irv
|= STRTOG_Inexhi
;
333 irv
|= STRTOG_Inexlo
;