fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / machine / powerpc / strtoufix16.c
blobd90255c0ec21697cf04eb91296f40e5b17e1252f
1 /*
2 FUNCTION
3 <<strtoufix16>>, <<strtoufix32>>, <<strtoufix64>>---string to signed fixed point
5 INDEX
6 strtoufix16
7 INDEX
8 strtoufix32
9 INDEX
10 strtoufix64
11 INDEX
12 _strtoufix16_r
13 INDEX
14 _strtoufix32_r
15 INDEX
16 _strtoufix64_r
18 ANSI_SYNOPSIS
19 #include <stdlib.h>
20 __uint16_t strtoufix16 (const char *<[s]>, char **<[ptr]>);
22 __uint32_t strtoufix32 (const char *<[s]>, char **<[ptr]>);
24 __uint64_t strtoufix64 (const char *<[s]>, char **<[ptr]>);
26 __uint16_t _strtoufix16_r (void *<[reent]>,
27 const char *<[s]>, char **<[ptr]>);
29 __uint32_t _strtoufix32_r (void *<[reent]>,
30 const char *<[s]>, char **<[ptr]>);
32 __uint64_t _strtoufix64_r (void *<[reent]>,
33 const char *<[s]>, char **<[ptr]>);
35 TRAD_SYNOPSIS
36 #include <stdlib.h>
37 __uint16_t strtoufix16 (<[s]>, <[ptr]>)
38 char *<[s]>;
39 char **<[ptr]>;
41 __uint32_t strtoufix32 (<[s]>, <[ptr]>)
42 char *<[s]>;
43 char **<[ptr]>;
45 __uint64_t strtoufix64 (<[s]>, <[ptr]>)
46 char *<[s]>;
47 char **<[ptr]>;
49 __uint16_t _strtoufix16_r (<[reent]>, <[s]>, <[ptr]>)
50 char *<[reent]>;
51 char *<[s]>;
52 char **<[ptr]>;
54 __uint32_t _strtoufix32_r (<[reent]>, <[s]>, <[ptr]>)
55 char *<[reent]>;
56 char *<[s]>;
57 char **<[ptr]>;
59 __uint64_t _strtoufix64_r (<[reent]>, <[s]>, <[ptr]>)
60 char *<[reent]>;
61 char *<[s]>;
62 char **<[ptr]>;
64 DESCRIPTION
65 The function <<strtoufix16>> converts the string <<*<[s]>>> to
66 a fixed-point 16-bits fraction representation. The function
67 follows the same rules as <<strtod>>.
69 The substring converted is the longest initial
70 subsequence of <[s]>, beginning with the first
71 non-whitespace character, that has the format:
72 .[+|-]<[digits]>[.][<[digits]>][(e|E)[+|-]<[digits]>]
73 The substring contains no characters if <[s]> is empty, consists
74 entirely of whitespace, or if the first non-whitespace
75 character is something other than <<+>>, <<->>, <<.>>, or a
76 digit. If the substring is empty, no conversion is done, and
77 the value of <[s]> is stored in <<*<[ptr]>>>. Otherwise,
78 the substring is converted, and a pointer to the final string
79 (which will contain at least the terminating null character of
80 <[s]>) is stored in <<*<[ptr]>>>. If you want no
81 assignment to <<*<[ptr]>>>, pass a null pointer as <[ptr]>.
83 <<strtoufix32>> is identical to <<strtoufix16>> except that it
84 converts to fixed-point 32-bit fraction representation.
85 <<strtoufix64>> is also similar, except that it converts
86 to fixed-point 64-bit fraction.
88 The alternate functions <<_strtoufix16_r>>, <<_strtoufix32_r>>,
89 and <<_strtoufix64_r>> are reentrant versions.
90 The extra argument <[reent]> is a pointer to a reentrancy structure.
92 RETURNS
93 The functions return the converted substring value, if any. If
94 no conversion can be performed, then 0 is returned. If the converted
95 value is a NaN, 0 is returned and errno is set to <<EDOM>>.
96 If the converted value exceeds the maximum positive unsigned fixed-point value,
97 the output value is saturated to the maximum value and <<ERANGE>> is stored in
98 errno. If the converted value is less than 0, then the output is saturated to 0
99 and <<ERANGE>> is stored in errno. Otherwise, the converted value is returned in the
100 specified fixed-point format.
102 PORTABILITY
103 <<strtoufix16>>, <<strtoufix32>>, and <<strtoufix64>> are non-standard.
105 The OS subroutines of <<strtod>> are required.
108 #ifdef __SPE__
110 #include <_ansi.h>
111 #include <limits.h>
112 #include <errno.h>
113 #include <stdlib.h>
114 #include <reent.h>
115 #include "vfieeefp.h"
118 * Convert a string to a fixed-point 16-bit value.
120 * Ignores `locale' stuff.
122 __uint16_t
123 _DEFUN (_strtoufix16_r, (rptr, nptr, endptr),
124 struct _reent *rptr _AND
125 _CONST char *nptr _AND
126 char **endptr)
128 union double_union dbl;
129 unsigned long tmp, tmp2, result;
130 int exp, negexp;
132 dbl.d = _strtod_r (rptr, nptr, endptr);
134 /* treat NAN as domain error, +/- infinity as saturation */
135 if (!finite(dbl.d))
137 if (isnan (dbl.d))
139 rptr->_errno = EDOM;
140 return 0;
142 rptr->_errno = ERANGE;
143 if (word0(dbl) & Sign_bit)
144 return 0;
145 return USHRT_MAX;
148 /* check for normal saturation */
149 if (dbl.d >= 1.0)
151 rptr->_errno = ERANGE;
152 return USHRT_MAX;
154 else if (dbl.d < 0)
156 rptr->_errno = ERANGE;
157 return 0;
160 /* otherwise we have normal postive number in range */
162 /* strip off exponent */
163 exp = ((word0(dbl) & Exp_mask) >> Exp_shift) - Bias;
164 negexp = -exp;
165 if (negexp > 16)
166 return 0;
167 /* add in implicit normalized bit */
168 tmp = word0(dbl) | Exp_msk1;
169 /* remove exponent and sign */
170 tmp <<= Ebits;
171 /* perform rounding */
172 tmp2 = tmp + (1 << (negexp + 14));
173 result = tmp2 >> (negexp + 15);
174 /* if rounding causes carry, must add carry bit in */
175 if (tmp2 < tmp)
177 if (negexp == 0)
179 /* we have overflow which means saturation */
180 rptr->_errno = ERANGE;
181 return USHRT_MAX;
183 result |= (1 << (16 - negexp));
186 return (__uint16_t)result;
189 #ifndef _REENT_ONLY
191 __uint16_t
192 _DEFUN (strtoufix16, (s, ptr, base),
193 _CONST char *s _AND
194 char **ptr)
196 return _strtoufix16_r (_REENT, s, ptr);
199 #endif
201 #endif /* __SPE__ */