fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / machine / powerpc / strtosfix32.c
blob2e22c14581bbb50fc3b1248d0e4bac893662f431
1 #ifdef __SPE__
3 #include <_ansi.h>
4 #include <limits.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <reent.h>
8 #include "vfieeefp.h"
11 * Convert a string to a fixed-point (sign + 31-bits) value.
13 * Ignores `locale' stuff.
15 __int32_t
16 _DEFUN (_strtosfix32_r, (rptr, nptr, endptr),
17 struct _reent *rptr _AND
18 _CONST char *nptr _AND
19 char **endptr)
21 union double_union dbl;
22 int exp, negexp, sign;
23 unsigned long tmp, tmp2;
24 long result = 0;
26 dbl.d = _strtod_r (rptr, nptr, endptr);
28 /* treat NAN as domain error, +/- infinity as saturation */
29 if (!finite(dbl.d))
31 if (isnan (dbl.d))
33 rptr->_errno = EDOM;
34 return 0;
36 rptr->_errno = ERANGE;
37 if (word0(dbl) & Sign_bit)
38 return LONG_MIN;
39 return LONG_MAX;
42 /* check for normal saturation */
43 if (dbl.d >= 1.0)
45 rptr->_errno = ERANGE;
46 return LONG_MAX;
48 else if (dbl.d < -1.0)
50 rptr->_errno = ERANGE;
51 return LONG_MIN;
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;
59 negexp = -exp;
60 if (negexp > 31)
61 return 0;
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));
68 if (negexp != 0)
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 */
74 if (tmp2 < tmp)
75 result |= (1 << (32 - negexp));
76 /* check if positive saturation has occurred because of rounding */
77 if (!sign && result < 0)
79 rptr->_errno = ERANGE;
80 return LONG_MAX;
83 else
85 /* we have -1.0, no rounding necessary */
86 return LONG_MIN;
89 return sign ? -result : result;
92 #ifndef _REENT_ONLY
94 __int32_t
95 _DEFUN (strtosfix32, (s, ptr, base),
96 _CONST char *s _AND
97 char **ptr)
99 return _strtosfix32_r (_REENT, s, ptr);
102 #endif
104 #endif /* __SPE__ */