Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strtordd.c
Go to the documentation of this file.
1 /****************************************************************
2 
3 The author of this software is David M. Gay.
4 
5 Copyright (C) 1998, 2000 by Lucent Technologies
6 All Rights Reserved
7 
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17 
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26 
27 ****************************************************************/
28 
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to "."). */
31 
32 #include "gdtoaimp.h"
33 
34 void
35 #ifdef KR_headers
36  ULtodd(L, bits, exp, k) ULong* L;
37 ULong* bits;
38 Long exp;
39 int k;
40 #else
41 ULtodd(ULong *L, ULong *bits, Long exp, int k)
42 #endif
43 {
44  int i, j;
45 
46  switch(k & STRTOG_Retmask)
47  {
48  case STRTOG_NoNumber:
49  case STRTOG_Zero:
50  L[0] = L[1] = L[2] = L[3] = 0;
51  break;
52 
53  case STRTOG_Normal:
54  L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL;
55  L[_0] =
56  bits[2] >> 21 | (bits[3] << 11 & 0xfffff) | (unsigned)((exp + 0x3ff + 105) << 20);
57  exp += 0x3ff + 52;
58  if(bits[1] &= 0x1fffff)
59  {
60  i = hi0bits(bits[1]) - 11;
61  if(i >= exp)
62  {
63  i = exp - 1;
64  exp = 0;
65  }
66  else
67  {
68  {
69  exp -= i;
70  }
71  }
72  if(i > 0)
73  {
74  bits[1] = bits[1] << i | bits[0] >> (32 - i);
75  bits[0] = bits[0] << i & (ULong)0xffffffffL;
76  }
77  }
78  else if(bits[0])
79  {
80  i = hi0bits(bits[0]) + 21;
81  if(i >= exp)
82  {
83  i = exp - 1;
84  exp = 0;
85  }
86  else
87  {
88  {
89  exp -= i;
90  }
91  }
92  if(i < 32)
93  {
94  bits[1] = bits[0] >> (32 - i);
95  bits[0] = bits[0] << i & (ULong)0xffffffffL;
96  }
97  else
98  {
99  bits[1] = bits[0] << (i - 32);
100  bits[0] = 0;
101  }
102  }
103  else
104  {
105  L[2] = L[3] = 0;
106  break;
107  }
108  L[2 + _1] = bits[0];
109  L[2 + _0] = (bits[1] & 0xfffff) | (unsigned)(exp << 20);
110  break;
111 
112  case STRTOG_Denormal:
113  if(bits[3])
114  {
115  {
116  goto nearly_normal;
117  }
118  }
119  if(bits[2])
120  {
121  {
122  goto partly_normal;
123  }
124  }
125  if(bits[1] & 0xffe00000)
126  {
127  {
128  goto hardly_normal;
129  }
130  }
131  /* completely denormal */
132  L[2] = L[3] = 0;
133  L[_1] = bits[0];
134  L[_0] = bits[1];
135  break;
136 
137  nearly_normal:
138  i = hi0bits(bits[3]) - 11; /* i >= 12 */
139  j = 32 - i;
140  L[_0] = ((bits[3] << i | bits[2] >> j) & 0xfffff) | (unsigned)((65 - i) << 20);
141  L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
142  L[2 + _0] = bits[1] & ((ULong)1L << j) - 1;
143  L[2 + _1] = bits[0];
144  break;
145 
146  partly_normal:
147  i = hi0bits(bits[2]) - 11;
148  if(i < 0)
149  {
150  j = -i;
151  i += 32;
152  L[_0] = (bits[2] >> j & 0xfffff) | (unsigned)((33 + j) << 20);
153  L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
154  L[2 + _0] = bits[1] & ((ULong)1L << j) - 1;
155  L[2 + _1] = bits[0];
156  break;
157  }
158  if(i == 0)
159  {
160  L[_0] = (bits[2] & 0xfffff) | 33 << 20;
161  L[_1] = bits[1];
162  L[2 + _0] = 0;
163  L[2 + _1] = bits[0];
164  break;
165  }
166  j = 32 - i;
167  L[_0] = ((bits[2] << i | bits[1] >> j) & 0xfffff) | (unsigned)((j + 1) << 20);
168  L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
169  L[2 + _0] = 0;
170  L[2 + _1] = bits[0] & (1L << j) - 1;
171  break;
172 
173  hardly_normal:
174  j = 11 - hi0bits(bits[1]);
175  i = 32 - j;
176  L[_0] = (bits[1] >> j & 0xfffff) | (unsigned)((j + 1) << 20);
177  L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
178  L[2 + _0] = 0;
179  L[2 + _1] = bits[0] & ((ULong)1L << j) - 1;
180  break;
181 
182  case STRTOG_Infinite:
183  L[_0] = L[2 + _0] = 0x7ff00000;
184  L[_1] = L[2 + _1] = 0;
185  break;
186 
187  case STRTOG_NaN:
188  L[0] = L[2] = d_QNAN0;
189  L[1] = L[3] = d_QNAN1;
190  break;
191 
192  case STRTOG_NaNbits:
193  L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL;
194  L[_0] = bits[2] >> 21 | bits[3] << 11 | (ULong)0x7ff00000L;
195  L[2 + _1] = bits[0];
196  L[2 + _0] = bits[1] | (ULong)0x7ff00000L;
197  }
198  if(k & STRTOG_Neg)
199  {
200  L[_0] |= 0x80000000L;
201  L[2 + _0] |= 0x80000000L;
202  }
203 }
204 
205 int
206 #ifdef KR_headers
207  strtordd(s, sp, rounding, dd) CONST char* s;
208 char** sp;
209 int rounding;
210 double* dd;
211 #else
212 strtordd(CONST char *s, char **sp, int rounding, double *dd)
213 #endif
214 {
215 #ifdef Sudden_Underflow
216  static FPI fpi0 = {106, 1 - 1023, 2046 - 1023 - 106 + 1, 1, 1};
217 #else
218  static FPI fpi0 = {106, 1 - 1023 - 53 + 1, 2046 - 1023 - 106 + 1, 1, 0};
219 #endif
220  FPI *fpi, fpi1;
221  ULong bits[4];
222  Long exp;
223  int k;
224 
225  fpi = &fpi0;
226  if(rounding != FPI_Round_near)
227  {
228  fpi1 = fpi0;
229  fpi1.rounding = rounding;
230  fpi = &fpi1;
231  }
232  k = strtodg(s, sp, fpi, &exp, bits);
233  ULtodd((ULong*)dd, bits, exp, k);
234  return k;
235 }
void ULtodd(ULong *L, ULong *bits, Long exp, int k)
Definition: strtordd.c:41
#define CONST
Definition: gdtoa.h:61
#define fpi1
#define hi0bits(x)
Definition: gdtoaimp.h:525
unsigned Long ULong
Definition: gdtoa.h:41
Definition: gdtoa.h:86
#define Long
Definition: gdtoa.h:38
int strtordd(CONST char *s, char **sp, int rounding, double *dd)
Definition: strtordd.c:212
int strtodg(CONST char *s00, char **se, FPI *fpi, Long *exp, ULong *bits)
Definition: strtodg.c:443