[444] | 1 | #ifdef __SPE__ |
---|
| 2 | |
---|
| 3 | #include <_ansi.h> |
---|
| 4 | #include <limits.h> |
---|
| 5 | #include <errno.h> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <reent.h> |
---|
| 8 | #include "vfieeefp.h" |
---|
| 9 | |
---|
| 10 | /* |
---|
| 11 | * Convert a string to a fixed-point (sign + 31-bits) value. |
---|
| 12 | * |
---|
| 13 | * Ignores `locale' stuff. |
---|
| 14 | */ |
---|
| 15 | __int32_t |
---|
| 16 | _strtosfix32_r (struct _reent *rptr, |
---|
| 17 | const char *nptr, |
---|
| 18 | char **endptr) |
---|
| 19 | { |
---|
| 20 | union double_union dbl; |
---|
| 21 | int exp, negexp, sign; |
---|
| 22 | unsigned long tmp, tmp2; |
---|
| 23 | long result = 0; |
---|
| 24 | |
---|
| 25 | dbl.d = _strtod_r (rptr, nptr, endptr); |
---|
| 26 | |
---|
| 27 | /* treat NAN as domain error, +/- infinity as saturation */ |
---|
| 28 | if (!finite(dbl.d)) |
---|
| 29 | { |
---|
| 30 | if (isnan (dbl.d)) |
---|
| 31 | { |
---|
| 32 | rptr->_errno = EDOM; |
---|
| 33 | return 0; |
---|
| 34 | } |
---|
| 35 | rptr->_errno = ERANGE; |
---|
| 36 | if (word0(dbl) & Sign_bit) |
---|
| 37 | return LONG_MIN; |
---|
| 38 | return LONG_MAX; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /* check for normal saturation */ |
---|
| 42 | if (dbl.d >= 1.0) |
---|
| 43 | { |
---|
| 44 | rptr->_errno = ERANGE; |
---|
| 45 | return LONG_MAX; |
---|
| 46 | } |
---|
| 47 | else if (dbl.d < -1.0) |
---|
| 48 | { |
---|
| 49 | rptr->_errno = ERANGE; |
---|
| 50 | return LONG_MIN; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /* otherwise we have normal number in range */ |
---|
| 54 | |
---|
| 55 | /* strip off sign and exponent */ |
---|
| 56 | sign = word0(dbl) & Sign_bit; |
---|
| 57 | exp = ((word0(dbl) & Exp_mask) >> Exp_shift) - Bias; |
---|
| 58 | negexp = -exp; |
---|
| 59 | if (negexp > 31) |
---|
| 60 | return 0; |
---|
| 61 | word0(dbl) &= ~(Exp_mask | Sign_bit); |
---|
| 62 | /* add in implicit normalized bit */ |
---|
| 63 | word0(dbl) |= Exp_msk1; |
---|
| 64 | /* shift so result is contained in single word */ |
---|
| 65 | tmp = word0(dbl) << Ebits; |
---|
| 66 | tmp |= ((unsigned long)word1(dbl) >> (32 - Ebits)); |
---|
| 67 | if (negexp != 0) |
---|
| 68 | { |
---|
| 69 | /* perform rounding */ |
---|
| 70 | tmp2 = tmp + (1 << (negexp - 1)); |
---|
| 71 | result = (long)(tmp2 >> negexp); |
---|
| 72 | /* check if rounding caused carry bit which must be added into result */ |
---|
| 73 | if (tmp2 < tmp) |
---|
| 74 | result |= (1 << (32 - negexp)); |
---|
| 75 | /* check if positive saturation has occurred because of rounding */ |
---|
| 76 | if (!sign && result < 0) |
---|
| 77 | { |
---|
| 78 | rptr->_errno = ERANGE; |
---|
| 79 | return LONG_MAX; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | /* we have -1.0, no rounding necessary */ |
---|
| 85 | return LONG_MIN; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | return sign ? -result : result; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | #ifndef _REENT_ONLY |
---|
| 92 | |
---|
| 93 | __int32_t |
---|
| 94 | strtosfix32 (const char *s, |
---|
| 95 | char **ptr) |
---|
| 96 | { |
---|
| 97 | return _strtosfix32_r (_REENT, s, ptr); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | #endif |
---|
| 101 | |
---|
| 102 | #endif /* __SPE__ */ |
---|