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 32-bit value. |
---|
12 | * |
---|
13 | * Ignores `locale' stuff. |
---|
14 | */ |
---|
15 | __uint32_t |
---|
16 | _strtoufix32_r (struct _reent *rptr, |
---|
17 | const char *nptr, |
---|
18 | char **endptr) |
---|
19 | { |
---|
20 | union double_union dbl; |
---|
21 | int exp, negexp; |
---|
22 | __uint32_t tmp, tmp2, result = 0; |
---|
23 | |
---|
24 | dbl.d = _strtod_r (rptr, nptr, endptr); |
---|
25 | |
---|
26 | /* treat NAN as domain error, +/- infinity as saturation */ |
---|
27 | if (!finite(dbl.d)) |
---|
28 | { |
---|
29 | if (isnan (dbl.d)) |
---|
30 | { |
---|
31 | rptr->_errno = EDOM; |
---|
32 | return 0; |
---|
33 | } |
---|
34 | rptr->_errno = ERANGE; |
---|
35 | if (word0(dbl) & Sign_bit) |
---|
36 | return 0; |
---|
37 | return ULONG_MAX; |
---|
38 | } |
---|
39 | |
---|
40 | /* check for normal saturation */ |
---|
41 | if (dbl.d >= 1.0) |
---|
42 | { |
---|
43 | rptr->_errno = ERANGE; |
---|
44 | return ULONG_MAX; |
---|
45 | } |
---|
46 | else if (dbl.d < 0) |
---|
47 | { |
---|
48 | rptr->_errno = ERANGE; |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | /* otherwise we have normal positive number in range */ |
---|
53 | |
---|
54 | /* strip off exponent */ |
---|
55 | exp = ((word0(dbl) & Exp_mask) >> Exp_shift) - Bias; |
---|
56 | negexp = -exp; |
---|
57 | if (negexp > 32) |
---|
58 | return 0; |
---|
59 | word0(dbl) &= ~(Exp_mask | Sign_bit); |
---|
60 | /* add in implicit normalized bit */ |
---|
61 | word0(dbl) |= Exp_msk1; |
---|
62 | /* shift so result is contained left-justified in word */ |
---|
63 | tmp = word0(dbl) << Ebits; |
---|
64 | tmp |= ((unsigned long)word1(dbl) >> (32 - Ebits)); |
---|
65 | /* perform rounding */ |
---|
66 | if (negexp > 1) |
---|
67 | { |
---|
68 | tmp2 = tmp + (1 << (negexp - 2)); |
---|
69 | result = (tmp2 >> (negexp - 1)); |
---|
70 | /* if rounding causes carry, add carry bit in */ |
---|
71 | if (tmp2 < tmp) |
---|
72 | result += 1 << (32 - negexp); |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | result = tmp + ((word1(dbl) & (1 << (32 - Ebits - 1))) != 0); |
---|
77 | /* if rounding causes carry, then saturation has occurred */ |
---|
78 | if (result < tmp) |
---|
79 | { |
---|
80 | rptr->_errno = ERANGE; |
---|
81 | return ULONG_MAX; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | return result; |
---|
86 | } |
---|
87 | |
---|
88 | #ifndef _REENT_ONLY |
---|
89 | |
---|
90 | __uint32_t |
---|
91 | strtoufix32 (const char *s, |
---|
92 | char **ptr) |
---|
93 | { |
---|
94 | return _strtoufix32_r (_REENT, s, ptr); |
---|
95 | } |
---|
96 | |
---|
97 | #endif |
---|
98 | |
---|
99 | #endif /* __SPE__ */ |
---|