OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [machine/] [powerpc/] [strtoufix16.c] - Blame information for rev 207

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
2
FUNCTION
3
        <<strtoufix16>>, <<strtoufix32>>, <<strtoufix64>>---string to signed fixed point
4
 
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
17
 
18
ANSI_SYNOPSIS
19
        #include <stdlib.h>
20
        __uint16_t strtoufix16 (const char *<[s]>, char **<[ptr]>);
21
 
22
        __uint32_t strtoufix32 (const char *<[s]>, char **<[ptr]>);
23
 
24
        __uint64_t strtoufix64 (const char *<[s]>, char **<[ptr]>);
25
 
26
        __uint16_t _strtoufix16_r (void *<[reent]>,
27
                       const char *<[s]>, char **<[ptr]>);
28
 
29
        __uint32_t _strtoufix32_r (void *<[reent]>,
30
                       const char *<[s]>, char **<[ptr]>);
31
 
32
        __uint64_t _strtoufix64_r (void *<[reent]>,
33
                       const char *<[s]>, char **<[ptr]>);
34
 
35
TRAD_SYNOPSIS
36
        #include <stdlib.h>
37
        __uint16_t strtoufix16 (<[s]>, <[ptr]>)
38
        char *<[s]>;
39
        char **<[ptr]>;
40
 
41
        __uint32_t strtoufix32 (<[s]>, <[ptr]>)
42
        char *<[s]>;
43
        char **<[ptr]>;
44
 
45
        __uint64_t strtoufix64 (<[s]>, <[ptr]>)
46
        char *<[s]>;
47
        char **<[ptr]>;
48
 
49
        __uint16_t _strtoufix16_r (<[reent]>, <[s]>, <[ptr]>)
50
        char *<[reent]>;
51
        char *<[s]>;
52
        char **<[ptr]>;
53
 
54
        __uint32_t _strtoufix32_r (<[reent]>, <[s]>, <[ptr]>)
55
        char *<[reent]>;
56
        char *<[s]>;
57
        char **<[ptr]>;
58
 
59
        __uint64_t _strtoufix64_r (<[reent]>, <[s]>, <[ptr]>)
60
        char *<[reent]>;
61
        char *<[s]>;
62
        char **<[ptr]>;
63
 
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>>.
68
 
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]>.
82
 
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.
87
 
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.
91
 
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.
101
 
102
PORTABILITY
103
        <<strtoufix16>>, <<strtoufix32>>, and <<strtoufix64>> are non-standard.
104
 
105
        The OS subroutines of <<strtod>> are required.
106
*/
107
 
108
#ifdef __SPE__
109
 
110
#include <_ansi.h>
111
#include <limits.h>
112
#include <errno.h>
113
#include <stdlib.h>
114
#include <reent.h>
115
#include "vfieeefp.h"
116
 
117
/*
118
 * Convert a string to a fixed-point 16-bit value.
119
 *
120
 * Ignores `locale' stuff.
121
 */
122
__uint16_t
123
_DEFUN (_strtoufix16_r, (rptr, nptr, endptr),
124
        struct _reent *rptr _AND
125
        _CONST char *nptr _AND
126
        char **endptr)
127
{
128
  union double_union dbl;
129
  unsigned long tmp, tmp2, result;
130
  int exp, negexp;
131
 
132
  dbl.d = _strtod_r (rptr, nptr, endptr);
133
 
134
  /* treat NAN as domain error, +/- infinity as saturation */
135
  if (!finite(dbl.d))
136
    {
137
      if (isnan (dbl.d))
138
        {
139
          rptr->_errno = EDOM;
140
          return 0;
141
        }
142
      rptr->_errno = ERANGE;
143
      if (word0(dbl) & Sign_bit)
144
        return 0;
145
      return USHRT_MAX;
146
    }
147
 
148
  /* check for normal saturation */
149
  if (dbl.d >= 1.0)
150
    {
151
      rptr->_errno = ERANGE;
152
      return USHRT_MAX;
153
    }
154
  else if (dbl.d < 0)
155
    {
156
      rptr->_errno = ERANGE;
157
      return 0;
158
    }
159
 
160
  /* otherwise we have normal postive number in range */
161
 
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)
176
    {
177
      if (negexp == 0)
178
        {
179
          /* we have overflow which means saturation */
180
          rptr->_errno = ERANGE;
181
          return USHRT_MAX;
182
        }
183
      result |= (1 << (16 - negexp));
184
    }
185
 
186
  return (__uint16_t)result;
187
}
188
 
189
#ifndef _REENT_ONLY
190
 
191
__uint16_t
192
_DEFUN (strtoufix16, (s, ptr, base),
193
        _CONST char *s _AND
194
        char **ptr)
195
{
196
  return _strtoufix16_r (_REENT, s, ptr);
197
}
198
 
199
#endif
200
 
201
#endif /* __SPE__ */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.