3 <<strtosfix16>>, <<strtosfix32>>, <<strtosfix64>>---string to signed fixed point
20 __int16 strtosfix16 (const char *<[s]>, char **<[ptr]>);
22 __int32 strtosfix32 (const char *<[s]>, char **<[ptr]>);
24 __int64 strtosfix64 (const char *<[s]>, char **<[ptr]>);
26 __int16 _strtosfix16_r (void *<[reent]>,
27 const char *<[s]>, char **<[ptr]>);
29 __int32 _strtosfix32_r (void *<[reent]>,
30 const char *<[s]>, char **<[ptr]>);
32 __int64 _strtosfix64_r (void *<[reent]>,
33 const char *<[s]>, char **<[ptr]>);
37 __int16 strtosfix16 (<[s]>, <[ptr]>)
41 __int32 strtosfix32 (<[s]>, <[ptr]>)
45 __int64 strtosfix64 (<[s]>, <[ptr]>)
49 __int16 _strtosfix16_r (<[reent]>, <[s]>, <[ptr]>)
54 __int32 _strtosfix32_r (<[reent]>, <[s]>, <[ptr]>)
59 __int64 _strtosfix64_r (<[reent]>, <[s]>, <[ptr]>)
65 The function <<strtosfix16>> converts the string <<*<[s]>>> to
66 a fixed-point sign + 15-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 <<strtosfix32>> is identical to <<strtosfix16>> except that it
84 converts to fixed-point sign + 31-bits fraction representation.
85 <<strtosfix64>> is also similar, except that it converts
86 to fixed-point sign + 63-bit fraction format.
88 The alternate functions <<_strtosfix16_r>>, <<_strtosfix32_r>>,
89 and <<_strtosfix64_r>> are reentrant versions.
90 The extra argument <[reent]> is a pointer to a reentrancy structure.
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 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 the minimum fixed-point negative
99 value, then the output is saturated to the minimum value and <<ERANGE>> is stored
100 in errno. Otherwise, the converted value is returned in the
101 specified fixed-point format.
104 <<strtosfix16>>, <<strtosfix32>>, and <<strtosfix64>> are non-standard.
106 The OS subroutines of <<strtod>> are required.
116 #include "vfieeefp.h"
119 * Convert a string to a fixed-point (sign + 15-bits) value.
121 * Ignores `locale' stuff.
124 _DEFUN (_strtosfix16_r
, (rptr
, nptr
, endptr
),
125 struct _reent
*rptr _AND
126 _CONST
char *nptr _AND
129 union double_union dbl
;
130 unsigned long tmp
, tmp2
;
131 int exp
, negexp
, sign
;
134 dbl
.d
= _strtod_r (rptr
, nptr
, endptr
);
136 /* treat NAN as domain error, +/- infinity as saturation */
144 rptr
->_errno
= ERANGE
;
145 if (word0(dbl
) & Sign_bit
)
150 /* check for normal saturation */
153 rptr
->_errno
= ERANGE
;
156 else if (dbl
.d
< -1.0)
158 rptr
->_errno
= ERANGE
;
162 /* otherwise we have normal number in range */
164 /* strip off sign and exponent */
165 sign
= word0(dbl
) & Sign_bit
;
166 exp
= ((word0(dbl
) & Exp_mask
) >> Exp_shift
) - Bias
;
170 /* add in implicit normalized bit */
171 tmp
= word0(dbl
) | Exp_msk1
;
172 /* remove exponent and sign */
176 /* perform rounding */
177 tmp2
= tmp
+ (1 << (negexp
- 1));
178 result
= (short)(tmp2
>> (negexp
+ 16));
179 /* check if rounding caused carry bit which must be added into result */
181 result
|= (1 << (16 - negexp
));
182 /* check if positive saturation has occurred because of rounding */
183 if (!sign
&& result
< 0)
185 rptr
->_errno
= ERANGE
;
191 /* we have -1.0, no rounding necessary */
195 return sign
? -result
: result
;
201 _DEFUN (strtosfix16
, (s
, ptr
, base
),
205 return _strtosfix16_r (_REENT
, s
, ptr
);