Contiki 2.6

small_mprec.h

00001 /****************************************************************
00002  *
00003  * The author of this software is David M. Gay.
00004  *
00005  * Copyright (c) 1991 by AT&T.
00006  *
00007  * Permission to use, copy, modify, and distribute this software for any
00008  * purpose without fee is hereby granted, provided that this entire notice
00009  * is included in all copies of any software which is or includes a copy
00010  * or modification of this software and in all copies of the supporting
00011  * documentation for such software.
00012  *
00013  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
00014  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
00015  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
00016  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
00017  *
00018  ***************************************************************/
00019 
00020 /* Please send bug reports to
00021         David M. Gay
00022         AT&T Bell Laboratories, Room 2C-463
00023         600 Mountain Avenue
00024         Murray Hill, NJ 07974-2070
00025         U.S.A.
00026         dmg@research.att.com or research!dmg
00027  */
00028 
00029 
00030 #include <ieeefp.h>
00031 #include <math.h>
00032 #include <float.h>
00033 #include <errno.h>
00034 #include <sys/config.h>
00035 #include <sys/types.h>
00036 
00037 #ifdef __IEEE_LITTLE_ENDIAN
00038 #define IEEE_8087
00039 #endif
00040 
00041 #ifdef __IEEE_BIG_ENDIAN
00042 #define IEEE_MC68k
00043 #endif
00044 
00045 #ifdef __Z8000__
00046 #define Just_16
00047 #endif
00048 
00049 #ifdef DEBUG
00050 #include "stdio.h"
00051 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
00052 #endif
00053 
00054 #ifdef Unsigned_Shifts
00055 #define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000;
00056 #else
00057 #define Sign_Extend(a,b) /*no-op*/
00058 #endif
00059 
00060 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
00061 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
00062 #endif
00063 
00064 /* If we are going to examine or modify specific bits in a double using
00065    the word0 and/or word1 macros, then we must wrap the double inside
00066    a union.  This is necessary to avoid undefined behavior according to
00067    the ANSI C spec.  */
00068 union double_union
00069 {
00070   double d;
00071   __uint32_t i[2];
00072 };
00073 
00074 #ifdef IEEE_8087
00075 #define word0(x) (x.i[1])
00076 #define word1(x) (x.i[0])
00077 #else
00078 #define word0(x) (x.i[0])
00079 #define word1(x) (x.i[1])
00080 #endif
00081 
00082 /* The following definition of Storeinc is appropriate for MIPS processors.
00083  * An alternative that might be better on some machines is
00084  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
00085  */
00086 #if defined (__IEEE_BYTES_LITTLE_ENDIAN) + defined (IEEE_8087) + defined (VAX)
00087 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
00088 ((unsigned short *)a)[0] = (unsigned short)c, a++)
00089 #else
00090 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
00091 ((unsigned short *)a)[1] = (unsigned short)c, a++)
00092 #endif
00093 
00094 /* #define P DBL_MANT_DIG */
00095 /* Ten_pmax = floor(P*log(2)/log(5)) */
00096 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
00097 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
00098 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
00099 
00100 #if defined(IEEE_8087) + defined(IEEE_MC68k)
00101 #if defined (_DOUBLE_IS_32BITS) 
00102 #define Exp_shift   23
00103 #define Exp_shift1  23
00104 #define Exp_msk1    ((__uint32_t)0x00800000L)
00105 #define Exp_msk11   ((__uint32_t)0x00800000L)
00106 #define Exp_mask    ((__uint32_t)0x7f800000L)
00107 #define P           24
00108 #define Bias        127
00109 #if 0
00110 #define IEEE_Arith  /* it is, but the code doesn't handle IEEE singles yet */
00111 #endif
00112 #define Emin        (-126)
00113 #define Exp_1       ((__uint32_t)0x3f800000L)
00114 #define Exp_11      ((__uint32_t)0x3f800000L)
00115 #define Ebits       8
00116 #define Frac_mask   ((__uint32_t)0x007fffffL)
00117 #define Frac_mask1  ((__uint32_t)0x007fffffL)
00118 #define Ten_pmax    10
00119 #define Sign_bit    ((__uint32_t)0x80000000L)
00120 #define Ten_pmax    10
00121 #define Bletch      2
00122 #define Bndry_mask  ((__uint32_t)0x007fffffL)
00123 #define Bndry_mask1 ((__uint32_t)0x007fffffL)
00124 #define LSB 1
00125 #define Sign_bit    ((__uint32_t)0x80000000L)
00126 #define Log2P       1
00127 #define Tiny0       0
00128 #define Tiny1       1
00129 #define Quick_max   5
00130 #define Int_max     6
00131 #define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L))
00132 #undef word0
00133 #undef word1
00134 
00135 #define word0(x) (x.i[0])
00136 #define word1(x) 0
00137 #else
00138 
00139 #define Exp_shift  20
00140 #define Exp_shift1 20
00141 #define Exp_msk1    ((__uint32_t)0x100000L)
00142 #define Exp_msk11   ((__uint32_t)0x100000L)
00143 #define Exp_mask  ((__uint32_t)0x7ff00000L)
00144 #define P 53
00145 #define Bias 1023
00146 #define IEEE_Arith
00147 #define Emin (-1022)
00148 #define Exp_1  ((__uint32_t)0x3ff00000L)
00149 #define Exp_11 ((__uint32_t)0x3ff00000L)
00150 #define Ebits 11
00151 #define Frac_mask  ((__uint32_t)0xfffffL)
00152 #define Frac_mask1 ((__uint32_t)0xfffffL)
00153 #define Ten_pmax 22
00154 #define Bletch 0x10
00155 #define Bndry_mask  ((__uint32_t)0xfffffL)
00156 #define Bndry_mask1 ((__uint32_t)0xfffffL)
00157 #define LSB 1
00158 #define Sign_bit ((__uint32_t)0x80000000L)
00159 #define Log2P 1
00160 #define Tiny0 0
00161 #define Tiny1 1
00162 #define Quick_max 14
00163 #define Int_max 14
00164 #define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */
00165 #endif
00166 
00167 #else
00168 #undef  Sudden_Underflow
00169 #define Sudden_Underflow
00170 #ifdef IBM
00171 #define Exp_shift  24
00172 #define Exp_shift1 24
00173 #define Exp_msk1   ((__uint32_t)0x1000000L)
00174 #define Exp_msk11  ((__uint32_t)0x1000000L)
00175 #define Exp_mask  ((__uint32_t)0x7f000000L)
00176 #define P 14
00177 #define Bias 65
00178 #define Exp_1  ((__uint32_t)0x41000000L)
00179 #define Exp_11 ((__uint32_t)0x41000000L)
00180 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
00181 #define Frac_mask  ((__uint32_t)0xffffffL)
00182 #define Frac_mask1 ((__uint32_t)0xffffffL)
00183 #define Bletch 4
00184 #define Ten_pmax 22
00185 #define Bndry_mask  ((__uint32_t)0xefffffL)
00186 #define Bndry_mask1 ((__uint32_t)0xffffffL)
00187 #define LSB 1
00188 #define Sign_bit ((__uint32_t)0x80000000L)
00189 #define Log2P 4
00190 #define Tiny0 ((__uint32_t)0x100000L)
00191 #define Tiny1 0
00192 #define Quick_max 14
00193 #define Int_max 15
00194 #else /* VAX */
00195 #define Exp_shift  23
00196 #define Exp_shift1 7
00197 #define Exp_msk1    0x80
00198 #define Exp_msk11   ((__uint32_t)0x800000L)
00199 #define Exp_mask  ((__uint32_t)0x7f80L)
00200 #define P 56
00201 #define Bias 129
00202 #define Exp_1  ((__uint32_t)0x40800000L)
00203 #define Exp_11 ((__uint32_t)0x4080L)
00204 #define Ebits 8
00205 #define Frac_mask  ((__uint32_t)0x7fffffL)
00206 #define Frac_mask1 ((__uint32_t)0xffff007fL)
00207 #define Ten_pmax 24
00208 #define Bletch 2
00209 #define Bndry_mask  ((__uint32_t)0xffff007fL)
00210 #define Bndry_mask1 ((__uint32_t)0xffff007fL)
00211 #define LSB ((__uint32_t)0x10000L)
00212 #define Sign_bit ((__uint32_t)0x8000L)
00213 #define Log2P 1
00214 #define Tiny0 0x80
00215 #define Tiny1 0
00216 #define Quick_max 15
00217 #define Int_max 15
00218 #endif
00219 #endif
00220 
00221 #ifndef IEEE_Arith
00222 #define ROUND_BIASED
00223 #endif
00224 
00225 #ifdef RND_PRODQUOT
00226 #define rounded_product(a,b) a = rnd_prod(a, b)
00227 #define rounded_quotient(a,b) a = rnd_quot(a, b)
00228 #ifdef KR_headers
00229 extern double rnd_prod(), rnd_quot();
00230 #else
00231 extern double rnd_prod(double, double), rnd_quot(double, double);
00232 #endif
00233 #else
00234 #define rounded_product(a,b) a *= b
00235 #define rounded_quotient(a,b) a /= b
00236 #endif
00237 
00238 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
00239 #define Big1 ((__uint32_t)0xffffffffL)
00240 
00241 #ifndef Just_16
00242 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
00243  * This makes some inner loops simpler and sometimes saves work
00244  * during multiplications, but it often seems to make things slightly
00245  * slower.  Hence the default is now to store 32 bits per long.
00246  */
00247 
00248 #ifndef Pack_32
00249 #define Pack_32
00250 #endif
00251 #endif
00252 
00253 
00254 #ifdef __cplusplus
00255 extern "C" double strtod(const char *s00, char **se);
00256 extern "C" char *dtoa(double d, int mode, int ndigits,
00257                         int *decpt, int *sign, char **rve);
00258 #endif
00259 
00260 
00261 typedef struct _Bigint _Bigint;
00262 
00263 #if (defined (_SMALL_PRINTF) || defined(SMALL_SCANF) )
00264 #define SMALL_LIB
00265 #endif
00266 
00267 #ifdef SMALL_LIB
00268  
00269 
00270  
00271  
00272 #define small_Balloc    _small_Balloc
00273 #define small_Bfree     _small_Bfree
00274 #define small_multadd _small_multadd
00275 #define small_s2b       _small_s2b
00276 #define small_lo0bits _small_lo0bits
00277 #define small_hi0bits _small_hi0bits
00278 #define small_i2b       _small_i2b
00279 #define small_mult      _small_multiply
00280 #define small_pow5mult  _small_pow5mult
00281 #define small_lshift    _small_lshift
00282 #define small_cmp       __small_mcmp
00283 #define small_diff      __small_mdiff
00284 #define small_ulp       _small_ulp
00285 #define small_b2d       _small_b2d
00286 #define small_d2b       _small_d2b
00287 #define small_ratio     _small_ratio
00288 
00289 #define small_tens __small_mprec_tens
00290 #define small_bigtens __small_mprec_bigtens
00291 #define small_tinytens __small_mprec_tinytens
00292 
00293 struct _reent ;
00294 double          _EXFUN(small_ulp,(double x));
00295 double          _EXFUN(small_b2d,(_Bigint *a , int *e));
00296 _Bigint *       _EXFUN(small_multadd,(struct _reent *p, _Bigint *, int, int,_Bigint tab[]));
00297 _Bigint *       _EXFUN(small_s2b,(struct _reent *, const char*, int, int, __ULong,_Bigint tab[]));
00298 _Bigint *       _EXFUN(small_i2b,(struct _reent *,int,_Bigint tab[]));
00299 _Bigint *       _EXFUN(small_mult, (struct _reent *, _Bigint *, _Bigint *,_Bigint tab[]));
00300 _Bigint *       _EXFUN(small_pow5mult, (struct _reent *, _Bigint *, int k,_Bigint tab[]));
00301 int             _EXFUN(small_hi0bits,(__ULong));
00302 int             _EXFUN(small_lo0bits,(__ULong *));
00303 _Bigint *        _EXFUN(small_d2b,(struct _reent *p, double d, int *e, int *bits,_Bigint tab[]));
00304 _Bigint *        _EXFUN(small_lshift,(struct _reent *p, _Bigint *b, int k,_Bigint tab[]));
00305 _Bigint *        _EXFUN(small_diff,(struct _reent *p, _Bigint *a, _Bigint *b,_Bigint tab[]));
00306 int             _EXFUN(small_cmp,(_Bigint *a, _Bigint *b));
00307 
00308 double          _EXFUN(small_ratio,(_Bigint *a, _Bigint *b));
00309 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(__Long) + 2*sizeof(int))
00310 
00311 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
00312 #define n_bigtens 2
00313 #else
00314 #define n_bigtens 5
00315 #endif
00316 
00317 extern _CONST double small_tinytens[];
00318 extern _CONST double small_bigtens[];
00319 extern _CONST double small_tens[];
00320 
00321 
00322 double _EXFUN(_small_mprec_log10,(int));
00323 
00324 
00325 #else // NO SMALL_LIB
00326 
00327 
00328 
00329 #define Balloc  _Balloc
00330 #define Bfree   _Bfree
00331 #define multadd _multadd
00332 #define s2b     _s2b
00333 #define lo0bits _lo0bits
00334 #define hi0bits _hi0bits
00335 #define i2b     _i2b
00336 #define mult    _multiply
00337 #define pow5mult        _pow5mult
00338 #define lshift  _lshift
00339 #define cmp     __mcmp
00340 #define diff    __mdiff
00341 #define ulp     _ulp
00342 #define b2d     _b2d
00343 #define d2b     _d2b
00344 #define ratio   _ratio
00345 
00346 #define tens __mprec_tens
00347 #define bigtens __mprec_bigtens
00348 #define tinytens __mprec_tinytens
00349 
00350 struct _reent ;
00351 double          _EXFUN(ulp,(double x));
00352 double          _EXFUN(b2d,(_Bigint *a , int *e));
00353 _Bigint *       _EXFUN(Balloc,(struct _reent *p, int k));
00354 void            _EXFUN(Bfree,(struct _reent *p, _Bigint *v));
00355 _Bigint *       _EXFUN(multadd,(struct _reent *p, _Bigint *, int, int));
00356 _Bigint *       _EXFUN(s2b,(struct _reent *, const char*, int, int, __ULong));
00357 _Bigint *       _EXFUN(i2b,(struct _reent *,int));
00358 _Bigint *       _EXFUN(mult, (struct _reent *, _Bigint *, _Bigint *));
00359 _Bigint *       _EXFUN(pow5mult, (struct _reent *, _Bigint *, int k));
00360 int             _EXFUN(hi0bits,(__ULong));
00361 int             _EXFUN(lo0bits,(__ULong *));
00362 _Bigint *        _EXFUN(d2b,(struct _reent *p, double d, int *e, int *bits));
00363 _Bigint *        _EXFUN(lshift,(struct _reent *p, _Bigint *b, int k));
00364 _Bigint *        _EXFUN(diff,(struct _reent *p, _Bigint *a, _Bigint *b));
00365 int             _EXFUN(cmp,(_Bigint *a, _Bigint *b));
00366 
00367 double          _EXFUN(ratio,(_Bigint *a, _Bigint *b));
00368 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(__Long) + 2*sizeof(int))
00369 
00370 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
00371 #define n_bigtens 2
00372 #else
00373 #define n_bigtens 5
00374 #endif
00375 
00376 extern _CONST double tinytens[];
00377 extern _CONST double bigtens[];
00378 extern _CONST double tens[];
00379 
00380 
00381 double _EXFUN(_mprec_log10,(int));
00382 
00383 #endif