Contiki 2.5
small_mprec.h
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991 by AT&T.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose without fee is hereby granted, provided that this entire notice
9  * is included in all copies of any software which is or includes a copy
10  * or modification of this software and in all copies of the supporting
11  * documentation for such software.
12  *
13  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14  * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17  *
18  ***************************************************************/
19 
20 /* Please send bug reports to
21  David M. Gay
22  AT&T Bell Laboratories, Room 2C-463
23  600 Mountain Avenue
24  Murray Hill, NJ 07974-2070
25  U.S.A.
26  dmg@research.att.com or research!dmg
27  */
28 
29 
30 #include <ieeefp.h>
31 #include <math.h>
32 #include <float.h>
33 #include <errno.h>
34 #include <sys/config.h>
35 #include <sys/types.h>
36 
37 #ifdef __IEEE_LITTLE_ENDIAN
38 #define IEEE_8087
39 #endif
40 
41 #ifdef __IEEE_BIG_ENDIAN
42 #define IEEE_MC68k
43 #endif
44 
45 #ifdef __Z8000__
46 #define Just_16
47 #endif
48 
49 #ifdef DEBUG
50 #include "stdio.h"
51 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
52 #endif
53 
54 #ifdef Unsigned_Shifts
55 #define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000;
56 #else
57 #define Sign_Extend(a,b) /*no-op*/
58 #endif
59 
60 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
61 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
62 #endif
63 
64 /* If we are going to examine or modify specific bits in a double using
65  the word0 and/or word1 macros, then we must wrap the double inside
66  a union. This is necessary to avoid undefined behavior according to
67  the ANSI C spec. */
68 union double_union
69 {
70  double d;
71  __uint32_t i[2];
72 };
73 
74 #ifdef IEEE_8087
75 #define word0(x) (x.i[1])
76 #define word1(x) (x.i[0])
77 #else
78 #define word0(x) (x.i[0])
79 #define word1(x) (x.i[1])
80 #endif
81 
82 /* The following definition of Storeinc is appropriate for MIPS processors.
83  * An alternative that might be better on some machines is
84  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
85  */
86 #if defined (__IEEE_BYTES_LITTLE_ENDIAN) + defined (IEEE_8087) + defined (VAX)
87 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
88 ((unsigned short *)a)[0] = (unsigned short)c, a++)
89 #else
90 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
91 ((unsigned short *)a)[1] = (unsigned short)c, a++)
92 #endif
93 
94 /* #define P DBL_MANT_DIG */
95 /* Ten_pmax = floor(P*log(2)/log(5)) */
96 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
97 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
98 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
99 
100 #if defined(IEEE_8087) + defined(IEEE_MC68k)
101 #if defined (_DOUBLE_IS_32BITS)
102 #define Exp_shift 23
103 #define Exp_shift1 23
104 #define Exp_msk1 ((__uint32_t)0x00800000L)
105 #define Exp_msk11 ((__uint32_t)0x00800000L)
106 #define Exp_mask ((__uint32_t)0x7f800000L)
107 #define P 24
108 #define Bias 127
109 #if 0
110 #define IEEE_Arith /* it is, but the code doesn't handle IEEE singles yet */
111 #endif
112 #define Emin (-126)
113 #define Exp_1 ((__uint32_t)0x3f800000L)
114 #define Exp_11 ((__uint32_t)0x3f800000L)
115 #define Ebits 8
116 #define Frac_mask ((__uint32_t)0x007fffffL)
117 #define Frac_mask1 ((__uint32_t)0x007fffffL)
118 #define Ten_pmax 10
119 #define Sign_bit ((__uint32_t)0x80000000L)
120 #define Ten_pmax 10
121 #define Bletch 2
122 #define Bndry_mask ((__uint32_t)0x007fffffL)
123 #define Bndry_mask1 ((__uint32_t)0x007fffffL)
124 #define LSB 1
125 #define Sign_bit ((__uint32_t)0x80000000L)
126 #define Log2P 1
127 #define Tiny0 0
128 #define Tiny1 1
129 #define Quick_max 5
130 #define Int_max 6
131 #define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L))
132 #undef word0
133 #undef word1
134 
135 #define word0(x) (x.i[0])
136 #define word1(x) 0
137 #else
138 
139 #define Exp_shift 20
140 #define Exp_shift1 20
141 #define Exp_msk1 ((__uint32_t)0x100000L)
142 #define Exp_msk11 ((__uint32_t)0x100000L)
143 #define Exp_mask ((__uint32_t)0x7ff00000L)
144 #define P 53
145 #define Bias 1023
146 #define IEEE_Arith
147 #define Emin (-1022)
148 #define Exp_1 ((__uint32_t)0x3ff00000L)
149 #define Exp_11 ((__uint32_t)0x3ff00000L)
150 #define Ebits 11
151 #define Frac_mask ((__uint32_t)0xfffffL)
152 #define Frac_mask1 ((__uint32_t)0xfffffL)
153 #define Ten_pmax 22
154 #define Bletch 0x10
155 #define Bndry_mask ((__uint32_t)0xfffffL)
156 #define Bndry_mask1 ((__uint32_t)0xfffffL)
157 #define LSB 1
158 #define Sign_bit ((__uint32_t)0x80000000L)
159 #define Log2P 1
160 #define Tiny0 0
161 #define Tiny1 1
162 #define Quick_max 14
163 #define Int_max 14
164 #define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */
165 #endif
166 
167 #else
168 #undef Sudden_Underflow
169 #define Sudden_Underflow
170 #ifdef IBM
171 #define Exp_shift 24
172 #define Exp_shift1 24
173 #define Exp_msk1 ((__uint32_t)0x1000000L)
174 #define Exp_msk11 ((__uint32_t)0x1000000L)
175 #define Exp_mask ((__uint32_t)0x7f000000L)
176 #define P 14
177 #define Bias 65
178 #define Exp_1 ((__uint32_t)0x41000000L)
179 #define Exp_11 ((__uint32_t)0x41000000L)
180 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
181 #define Frac_mask ((__uint32_t)0xffffffL)
182 #define Frac_mask1 ((__uint32_t)0xffffffL)
183 #define Bletch 4
184 #define Ten_pmax 22
185 #define Bndry_mask ((__uint32_t)0xefffffL)
186 #define Bndry_mask1 ((__uint32_t)0xffffffL)
187 #define LSB 1
188 #define Sign_bit ((__uint32_t)0x80000000L)
189 #define Log2P 4
190 #define Tiny0 ((__uint32_t)0x100000L)
191 #define Tiny1 0
192 #define Quick_max 14
193 #define Int_max 15
194 #else /* VAX */
195 #define Exp_shift 23
196 #define Exp_shift1 7
197 #define Exp_msk1 0x80
198 #define Exp_msk11 ((__uint32_t)0x800000L)
199 #define Exp_mask ((__uint32_t)0x7f80L)
200 #define P 56
201 #define Bias 129
202 #define Exp_1 ((__uint32_t)0x40800000L)
203 #define Exp_11 ((__uint32_t)0x4080L)
204 #define Ebits 8
205 #define Frac_mask ((__uint32_t)0x7fffffL)
206 #define Frac_mask1 ((__uint32_t)0xffff007fL)
207 #define Ten_pmax 24
208 #define Bletch 2
209 #define Bndry_mask ((__uint32_t)0xffff007fL)
210 #define Bndry_mask1 ((__uint32_t)0xffff007fL)
211 #define LSB ((__uint32_t)0x10000L)
212 #define Sign_bit ((__uint32_t)0x8000L)
213 #define Log2P 1
214 #define Tiny0 0x80
215 #define Tiny1 0
216 #define Quick_max 15
217 #define Int_max 15
218 #endif
219 #endif
220 
221 #ifndef IEEE_Arith
222 #define ROUND_BIASED
223 #endif
224 
225 #ifdef RND_PRODQUOT
226 #define rounded_product(a,b) a = rnd_prod(a, b)
227 #define rounded_quotient(a,b) a = rnd_quot(a, b)
228 #ifdef KR_headers
229 extern double rnd_prod(), rnd_quot();
230 #else
231 extern double rnd_prod(double, double), rnd_quot(double, double);
232 #endif
233 #else
234 #define rounded_product(a,b) a *= b
235 #define rounded_quotient(a,b) a /= b
236 #endif
237 
238 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
239 #define Big1 ((__uint32_t)0xffffffffL)
240 
241 #ifndef Just_16
242 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
243  * This makes some inner loops simpler and sometimes saves work
244  * during multiplications, but it often seems to make things slightly
245  * slower. Hence the default is now to store 32 bits per long.
246  */
247 
248 #ifndef Pack_32
249 #define Pack_32
250 #endif
251 #endif
252 
253 
254 #ifdef __cplusplus
255 extern "C" double strtod(const char *s00, char **se);
256 extern "C" char *dtoa(double d, int mode, int ndigits,
257  int *decpt, int *sign, char **rve);
258 #endif
259 
260 
261 typedef struct _Bigint _Bigint;
262 
263 #if (defined (_SMALL_PRINTF) || defined(SMALL_SCANF) )
264 #define SMALL_LIB
265 #endif
266 
267 #ifdef SMALL_LIB
268 
269 
270 
271 
272 #define small_Balloc _small_Balloc
273 #define small_Bfree _small_Bfree
274 #define small_multadd _small_multadd
275 #define small_s2b _small_s2b
276 #define small_lo0bits _small_lo0bits
277 #define small_hi0bits _small_hi0bits
278 #define small_i2b _small_i2b
279 #define small_mult _small_multiply
280 #define small_pow5mult _small_pow5mult
281 #define small_lshift _small_lshift
282 #define small_cmp __small_mcmp
283 #define small_diff __small_mdiff
284 #define small_ulp _small_ulp
285 #define small_b2d _small_b2d
286 #define small_d2b _small_d2b
287 #define small_ratio _small_ratio
288 
289 #define small_tens __small_mprec_tens
290 #define small_bigtens __small_mprec_bigtens
291 #define small_tinytens __small_mprec_tinytens
292 
293 struct _reent ;
294 double _EXFUN(small_ulp,(double x));
295 double _EXFUN(small_b2d,(_Bigint *a , int *e));
296 _Bigint * _EXFUN(small_multadd,(struct _reent *p, _Bigint *, int, int,_Bigint tab[]));
297 _Bigint * _EXFUN(small_s2b,(struct _reent *, const char*, int, int, __ULong,_Bigint tab[]));
298 _Bigint * _EXFUN(small_i2b,(struct _reent *,int,_Bigint tab[]));
299 _Bigint * _EXFUN(small_mult, (struct _reent *, _Bigint *, _Bigint *,_Bigint tab[]));
300 _Bigint * _EXFUN(small_pow5mult, (struct _reent *, _Bigint *, int k,_Bigint tab[]));
301 int _EXFUN(small_hi0bits,(__ULong));
302 int _EXFUN(small_lo0bits,(__ULong *));
303 _Bigint * _EXFUN(small_d2b,(struct _reent *p, double d, int *e, int *bits,_Bigint tab[]));
304 _Bigint * _EXFUN(small_lshift,(struct _reent *p, _Bigint *b, int k,_Bigint tab[]));
305 _Bigint * _EXFUN(small_diff,(struct _reent *p, _Bigint *a, _Bigint *b,_Bigint tab[]));
306 int _EXFUN(small_cmp,(_Bigint *a, _Bigint *b));
307 
308 double _EXFUN(small_ratio,(_Bigint *a, _Bigint *b));
309 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(__Long) + 2*sizeof(int))
310 
311 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
312 #define n_bigtens 2
313 #else
314 #define n_bigtens 5
315 #endif
316 
317 extern _CONST double small_tinytens[];
318 extern _CONST double small_bigtens[];
319 extern _CONST double small_tens[];
320 
321 
322 double _EXFUN(_small_mprec_log10,(int));
323 
324 
325 #else // NO SMALL_LIB
326 
327 
328 
329 #define Balloc _Balloc
330 #define Bfree _Bfree
331 #define multadd _multadd
332 #define s2b _s2b
333 #define lo0bits _lo0bits
334 #define hi0bits _hi0bits
335 #define i2b _i2b
336 #define mult _multiply
337 #define pow5mult _pow5mult
338 #define lshift _lshift
339 #define cmp __mcmp
340 #define diff __mdiff
341 #define ulp _ulp
342 #define b2d _b2d
343 #define d2b _d2b
344 #define ratio _ratio
345 
346 #define tens __mprec_tens
347 #define bigtens __mprec_bigtens
348 #define tinytens __mprec_tinytens
349 
350 struct _reent ;
351 double _EXFUN(ulp,(double x));
352 double _EXFUN(b2d,(_Bigint *a , int *e));
353 _Bigint * _EXFUN(Balloc,(struct _reent *p, int k));
354 void _EXFUN(Bfree,(struct _reent *p, _Bigint *v));
355 _Bigint * _EXFUN(multadd,(struct _reent *p, _Bigint *, int, int));
356 _Bigint * _EXFUN(s2b,(struct _reent *, const char*, int, int, __ULong));
357 _Bigint * _EXFUN(i2b,(struct _reent *,int));
358 _Bigint * _EXFUN(mult, (struct _reent *, _Bigint *, _Bigint *));
359 _Bigint * _EXFUN(pow5mult, (struct _reent *, _Bigint *, int k));
360 int _EXFUN(hi0bits,(__ULong));
361 int _EXFUN(lo0bits,(__ULong *));
362 _Bigint * _EXFUN(d2b,(struct _reent *p, double d, int *e, int *bits));
363 _Bigint * _EXFUN(lshift,(struct _reent *p, _Bigint *b, int k));
364 _Bigint * _EXFUN(diff,(struct _reent *p, _Bigint *a, _Bigint *b));
365 int _EXFUN(cmp,(_Bigint *a, _Bigint *b));
366 
367 double _EXFUN(ratio,(_Bigint *a, _Bigint *b));
368 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(__Long) + 2*sizeof(int))
369 
370 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
371 #define n_bigtens 2
372 #else
373 #define n_bigtens 5
374 #endif
375 
376 extern _CONST double tinytens[];
377 extern _CONST double bigtens[];
378 extern _CONST double tens[];
379 
380 
381 double _EXFUN(_mprec_log10,(int));
382 
383 #endif