11 * Convert a string to a fixed-point (sign + 31-bits) value.
13 * Ignores `locale' stuff.
16 _DEFUN (_strtosfix32_r
, (rptr
, nptr
, endptr
),
17 struct _reent
*rptr _AND
18 _CONST
char *nptr _AND
21 union double_union dbl
;
22 int exp
, negexp
, sign
;
23 unsigned long tmp
, tmp2
;
26 dbl
.d
= _strtod_r (rptr
, nptr
, endptr
);
28 /* treat NAN as domain error, +/- infinity as saturation */
36 rptr
->_errno
= ERANGE
;
37 if (word0(dbl
) & Sign_bit
)
42 /* check for normal saturation */
45 rptr
->_errno
= ERANGE
;
48 else if (dbl
.d
< -1.0)
50 rptr
->_errno
= ERANGE
;
54 /* otherwise we have normal number in range */
56 /* strip off sign and exponent */
57 sign
= word0(dbl
) & Sign_bit
;
58 exp
= ((word0(dbl
) & Exp_mask
) >> Exp_shift
) - Bias
;
62 word0(dbl
) &= ~(Exp_mask
| Sign_bit
);
63 /* add in implicit normalized bit */
64 word0(dbl
) |= Exp_msk1
;
65 /* shift so result is contained in single word */
66 tmp
= word0(dbl
) << Ebits
;
67 tmp
|= ((unsigned long)word1(dbl
) >> (32 - Ebits
));
70 /* perform rounding */
71 tmp2
= tmp
+ (1 << (negexp
- 1));
72 result
= (long)(tmp2
>> negexp
);
73 /* check if rounding caused carry bit which must be added into result */
75 result
|= (1 << (32 - negexp
));
76 /* check if positive saturation has occurred because of rounding */
77 if (!sign
&& result
< 0)
79 rptr
->_errno
= ERANGE
;
85 /* we have -1.0, no rounding necessary */
89 return sign
? -result
: result
;
95 _DEFUN (strtosfix32
, (s
, ptr
, base
),
99 return _strtosfix32_r (_REENT
, s
, ptr
);