Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
smisc.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, 1999 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 Bigint* s2b
35 #ifdef KR_headers
36  (s, nd0, nd, y9) CONST char* s;
37 int nd0, nd;
38 ULong y9;
39 #else
40  (CONST char* s, int nd0, int nd, ULong y9)
41 #endif
42 {
43  Bigint* b;
44  int i, k;
45  Long x, y;
46 
47  x = (nd + 8) / 9;
48  for(k = 0, y = 1; x > y; y <<= 1, k++)
49  {
50  {
51  ;
52  }
53  }
54 #ifdef Pack_32
55  b = Balloc(k);
56  b->x[0] = y9;
57  b->wds = 1;
58 #else
59  b = Balloc(k + 1);
60  b->x[0] = y9 & 0xffff;
61  b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
62 #endif
63 
64  i = 9;
65  if(9 < nd0)
66  {
67  s += 9;
68  do
69  {
70  {
71  b = multadd(b, 10, *s++ - '0');
72  }
73  } while(++i < nd0);
74  s++;
75  }
76  else
77  {
78  {
79  s += 10;
80  }
81  }
82  for(; i < nd; i++)
83  {
84  {
85  b = multadd(b, 10, *s++ - '0');
86  }
87  }
88  return b;
89 }
90 
91 double ratio
92 #ifdef KR_headers
93  (a, b) Bigint *a,
94  *b;
95 #else
96  (Bigint* a, Bigint* b)
97 #endif
98 {
99  double da, db;
100  int k, ka, kb;
101 
102  dval(da) = b2d(a, &ka);
103  dval(db) = b2d(b, &kb);
104  k = ka - kb + ULbits * (a->wds - b->wds);
105 #ifdef IBM
106  if(k > 0)
107  {
108  word0(da) += (k >> 2) * Exp_msk1;
109  if(k &= 3)
110  dval(da) *= 1 << k;
111  }
112  else
113  {
114  k = -k;
115  word0(db) += (k >> 2) * Exp_msk1;
116  if(k &= 3)
117  dval(db) *= 1 << k;
118  }
119 #else
120  if(k > 0)
121  {
122  {
123  word0(da) += (unsigned)(k * Exp_msk1);
124  }
125  }
126  else
127  {
128  k = -k;
129  word0(db) += (unsigned)(k * Exp_msk1);
130  }
131 #endif
132  return dval(da) / dval(db);
133 }
134 
135 #ifdef INFNAN_CHECK
136 
137 int match
138 #ifdef KR_headers
139  (sp, t) char **sp,
140  *t;
141 #else
142  (CONST char** sp, char* t)
143 #endif
144 {
145  int d;
146  CONST char* s = *sp;
147 
148  while((d = *t++) != 0)
149  {
150  int c;
151  if((c = *++s) >= 'A' && c <= 'Z')
152  {
153  c += 'a' - 'A';
154  }
155  if(c != d)
156  {
157  return 0;
158  }
159  }
160  *sp = s + 1;
161  return 1;
162 }
163 #endif /* INFNAN_CHECK */
164 
165 void
166 #ifdef KR_headers
167  copybits(c, n, b) ULong* c;
168 int n;
169 Bigint* b;
170 #else
171 copybits(ULong *c, int n, Bigint *b)
172 #endif
173 {
174  ULong *ce, *x, *xe;
175 #ifdef Pack_16
176  int nw, nw1;
177 #endif
178 
179  ce = c + ((n - 1) >> kshift) + 1;
180  x = b->x;
181 #ifdef Pack_32
182  xe = x + b->wds;
183  while(x < xe)
184  {
185  {
186  *c++ = *x++;
187  }
188  }
189 #else
190  nw = b->wds;
191  nw1 = nw & 1;
192  for(xe = x + (nw - nw1); x < xe; x += 2)
193  Storeinc(c, x[1], x[0]);
194  if(nw1)
195  *c++ = *x;
196 #endif
197  while(c < ce)
198  {
199  {
200  *c++ = 0;
201  }
202  }
203 }
204 
205 ULong
206 #ifdef KR_headers
207  any_on(b, k) Bigint* b;
208 int k;
209 #else
210 any_on(Bigint *b, int k)
211 #endif
212 {
213  int n, nwds;
214  ULong *x, *x0, x1, x2;
215 
216  x = b->x;
217  nwds = b->wds;
218  n = k >> kshift;
219  if(n > nwds)
220  {
221  {
222  n = nwds;
223  }
224  }
225  else if(n < nwds && (k &= kmask))
226  {
227  x1 = x2 = x[n];
228  x1 >>= k;
229  x1 <<= k;
230  if(x1 != x2)
231  {
232  {
233  return 1;
234  }
235  }
236  }
237  x0 = x;
238  x += n;
239  while(x > x0)
240  {
241  {
242  if(*--x)
243  {
244  {
245  return 1;
246  }
247  }
248  }
249  }
250  return 0;
251 }
ULong x[1]
Definition: gdtoaimp.h:488
ULong any_on(Bigint *b, int k)
Definition: smisc.c:210
int wds
Definition: gdtoaimp.h:487
#define CONST
Definition: gdtoa.h:61
#define Exp_msk1
Definition: gdtoaimp.h:396
double b2d(Bigint *a, int *e)
Definition: misc.c:724
#define match
Definition: gdtoaimp.h:530
#define dval(x)
Definition: gdtoaimp.h:307
#define word0(x)
Definition: gdtoaimp.h:304
#define Storeinc(a, b, c)
Definition: gdtoaimp.h:318
#define kshift
Definition: gdtoaimp.h:467
double ratio(Bigint *a, Bigint *b)
Definition: smisc.c:96
unsigned Long ULong
Definition: gdtoa.h:41
void copybits(ULong *c, int n, Bigint *b)
Definition: smisc.c:171
Bigint * s2b(CONST char *s, int nd0, int nd, ULong y9)
Definition: smisc.c:40
#define kmask
Definition: gdtoaimp.h:468
Bigint * multadd(Bigint *b, int m, int a)
Definition: misc.c:171
Bigint * Balloc(int k)
Definition: misc.c:47
#define Long
Definition: gdtoa.h:38
#define ULbits
Definition: gdtoaimp.h:466