1 /* $NetBSD: gethex.c,v 1.4 2008/03/21 23:13:48 christos 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 "."). */
42 gethex(sp
, fpi
, expt
, bp
, sign
)
43 CONST
char **sp
; CONST FPI
*fpi
; Long
*expt
; Bigint
**bp
; int sign
;
45 gethex( CONST
char **sp
, CONST FPI
*fpi
, Long
*expt
, Bigint
**bp
, int sign
)
49 CONST
unsigned char *decpt
, *s0
, *s
, *s1
;
50 int esign
, havedig
, irv
, k
, n
, nbits
, up
, zret
;
51 ULong L
, lostbits
, *x
;
54 unsigned char decimalpoint
= *localeconv()->decimal_point
;
56 #define decimalpoint '.'
62 s0
= *(CONST
unsigned char **)sp
+ 2;
63 while(s0
[havedig
] == '0')
72 if (*s
!= decimalpoint
)
86 if (*s
== decimalpoint
&& !decpt
) {
92 e
= -(((Long
)(s
-decpt
)) << 2);
106 if ((n
= hexdig
[*s
]) == 0 || n
> 0x19) {
111 while((n
= hexdig
[*++s
]) !=0 && n
<= 0x19)
112 e1
= 10*e1
+ n
- 0x10;
119 return havedig
? STRTOG_Zero
: STRTOG_NoNumber
;
121 for(k
= 0; n
> 7; n
= (unsigned int)n
>> 1)
125 return STRTOG_NoMemory
;
130 if (*--s1
== decimalpoint
)
137 L
|= (hexdig
[*s1
] & 0x0f) << n
;
141 b
->wds
= n
= x
- b
->x
;
142 n
= 32*n
- hi0bits(L
);
151 if (x
[(unsigned int)k
>>kshift
] & 1 << (k
& kmask
)) {
153 if (k
> 1 && any_on(b
,k
-1))
160 else if (n
< nbits
) {
164 return STRTOG_NoMemory
;
172 return STRTOG_Infinite
| STRTOG_Overflow
| STRTOG_Inexhi
;
176 irv
= STRTOG_Denormal
;
179 switch (fpi
->rounding
) {
181 if (n
== nbits
&& (n
< 2 || any_on(b
,n
-1)))
194 return STRTOG_Denormal
| STRTOG_Inexhi
200 return STRTOG_Zero
| STRTOG_Inexlo
| STRTOG_Underflow
;
206 lostbits
= any_on(b
,k
);
207 if (x
[(unsigned int)k
>>kshift
] & 1 << (k
& kmask
))
215 switch(fpi
->rounding
) {
220 && (lostbits
& 1) | (x
[0] & 1))
233 if (irv
== STRTOG_Denormal
) {
234 if (nbits
== fpi
->nbits
- 1
235 && x
[(unsigned int)nbits
>> kshift
] & 1 << (nbits
& kmask
))
239 || ((n
= nbits
& kmask
) !=0
240 && hi0bits(x
[k
-1]) < 32-n
)) {
245 irv
|= STRTOG_Inexhi
;
248 irv
|= STRTOG_Inexlo
;