ext/bigdecimal/bigdecimal.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Ruby BigDecimal(Variable decimal precision) extension library.
00004  *
00005  * Copyright(C) 2002 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
00006  *
00007  * You may distribute under the terms of either the GNU General Public
00008  * License or the Artistic License, as specified in the README file
00009  * of this BigDecimal distribution.
00010  *
00011  * NOTES:
00012  *   2003-03-28 V1.0 checked in.
00013  *
00014  */
00015 
00016 #ifndef  RUBY_BIG_DECIMAL_H
00017 #define  RUBY_BIG_DECIMAL_H 1
00018 
00019 #include "ruby/ruby.h"
00020 #include <float.h>
00021 
00022 #ifndef RB_UNUSED_VAR
00023 # ifdef __GNUC__
00024 #  define RB_UNUSED_VAR(x) x __attribute__ ((unused))
00025 # else
00026 #  define RB_UNUSED_VAR(x) x
00027 # endif
00028 #endif
00029 
00030 #ifndef UNREACHABLE
00031 # define UNREACHABLE            /* unreachable */
00032 #endif
00033 
00034 #undef BDIGIT
00035 #undef SIZEOF_BDIGITS
00036 #undef BDIGIT_DBL
00037 #undef BDIGIT_DBL_SIGNED
00038 #undef PRI_BDIGIT_PREFIX
00039 #undef PRI_BDIGIT_DBL_PREFIX
00040 
00041 #ifdef HAVE_INT64_T
00042 # define BDIGIT uint32_t
00043 # define BDIGIT_DBL uint64_t
00044 # define BDIGIT_DBL_SIGNED int64_t
00045 # define SIZEOF_BDIGITS 4
00046 #else
00047 # define BDIGIT uint16_t
00048 # define BDIGIT_DBL uint32_t
00049 # define BDIGIT_DBL_SIGNED int32_t
00050 # define SIZEOF_BDIGITS 2
00051 #endif
00052 
00053 #if defined(__cplusplus)
00054 extern "C" {
00055 #if 0
00056 } /* satisfy cc-mode */
00057 #endif
00058 #endif
00059 
00060 #ifndef HAVE_LABS
00061 static inline long
00062 labs(long const x)
00063 {
00064     if (x < 0) return -x;
00065     return x;
00066 }
00067 #endif
00068 
00069 #ifndef HAVE_LLABS
00070 static inline LONG_LONG
00071 llabs(LONG_LONG const x)
00072 {
00073     if (x < 0) return -x;
00074     return x;
00075 }
00076 #endif
00077 
00078 #ifdef vabs
00079 # undef vabs
00080 #endif
00081 #if SIZEOF_VALUE <= SIZEOF_INT
00082 # define vabs abs
00083 #elif SIZEOF_VALUE <= SIZEOF_LONG
00084 # define vabs labs
00085 #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
00086 # define vabs llabs
00087 #endif
00088 
00089 extern VALUE rb_cBigDecimal;
00090 
00091 #if 0 || SIZEOF_BDIGITS >= 16
00092 # define RMPD_COMPONENT_FIGURES 38
00093 # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
00094 #elif SIZEOF_BDIGITS >= 8
00095 # define RMPD_COMPONENT_FIGURES 19
00096 # define RMPD_BASE ((BDIGIT)10000000000000000000U)
00097 #elif SIZEOF_BDIGITS >= 4
00098 # define RMPD_COMPONENT_FIGURES 9
00099 # define RMPD_BASE ((BDIGIT)1000000000U)
00100 #elif SIZEOF_BDIGITS >= 2
00101 # define RMPD_COMPONENT_FIGURES 4
00102 # define RMPD_BASE ((BDIGIT)10000U)
00103 #else
00104 # define RMPD_COMPONENT_FIGURES 2
00105 # define RMPD_BASE ((BDIGIT)100U)
00106 #endif
00107 
00108 
00109 /*
00110  *  NaN & Infinity
00111  */
00112 #define SZ_NaN  "NaN"
00113 #define SZ_INF  "Infinity"
00114 #define SZ_PINF "+Infinity"
00115 #define SZ_NINF "-Infinity"
00116 
00117 /*
00118  *   #define VP_EXPORT other than static to let VP_ routines
00119  *   be called from outside of this module.
00120  */
00121 #define VP_EXPORT static
00122 
00123 /* Exception codes */
00124 #define VP_EXCEPTION_ALL        ((unsigned short)0x00FF)
00125 #define VP_EXCEPTION_INFINITY   ((unsigned short)0x0001)
00126 #define VP_EXCEPTION_NaN        ((unsigned short)0x0002)
00127 #define VP_EXCEPTION_UNDERFLOW  ((unsigned short)0x0004)
00128 #define VP_EXCEPTION_OVERFLOW   ((unsigned short)0x0001) /* 0x0008) */
00129 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
00130 
00131 /* Following 2 exceptions can't controlled by user */
00132 #define VP_EXCEPTION_OP         ((unsigned short)0x0020)
00133 #define VP_EXCEPTION_MEMORY     ((unsigned short)0x0040)
00134 
00135 #define RMPD_EXCEPTION_MODE_DEFAULT 0U
00136 
00137 /* Computation mode */
00138 #define VP_ROUND_MODE            ((unsigned short)0x0100)
00139 #define VP_ROUND_UP         1
00140 #define VP_ROUND_DOWN       2
00141 #define VP_ROUND_HALF_UP    3
00142 #define VP_ROUND_HALF_DOWN  4
00143 #define VP_ROUND_CEIL       5
00144 #define VP_ROUND_FLOOR      6
00145 #define VP_ROUND_HALF_EVEN  7
00146 
00147 #define RMPD_ROUNDING_MODE_DEFAULT  VP_ROUND_HALF_UP
00148 
00149 #define VP_SIGN_NaN                0 /* NaN                      */
00150 #define VP_SIGN_POSITIVE_ZERO      1 /* Positive zero            */
00151 #define VP_SIGN_NEGATIVE_ZERO     -1 /* Negative zero            */
00152 #define VP_SIGN_POSITIVE_FINITE    2 /* Positive finite number   */
00153 #define VP_SIGN_NEGATIVE_FINITE   -2 /* Negative finite number   */
00154 #define VP_SIGN_POSITIVE_INFINITE  3 /* Positive infinite number */
00155 #define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
00156 
00157 #ifdef __GNUC__
00158 #define FLEXIBLE_ARRAY_SIZE 0
00159 #else
00160 #define FLEXIBLE_ARRAY_SIZE 1
00161 #endif
00162 
00163 /*
00164  * VP representation
00165  *  r = 0.xxxxxxxxx *BASE**exponent
00166  */
00167 typedef struct {
00168     VALUE  obj;     /* Back pointer(VALUE) for Ruby object.     */
00169     size_t MaxPrec; /* Maximum precision size                   */
00170                     /* This is the actual size of pfrac[]       */
00171                     /*(frac[0] to frac[MaxPrec] are available). */
00172     size_t Prec;    /* Current precision size.                  */
00173                     /* This indicates how much the.             */
00174                     /* the array frac[] is actually used.       */
00175     SIGNED_VALUE exponent; /* Exponent part.                    */
00176     short  sign;    /* Attributes of the value.                 */
00177                     /*
00178                      *        ==0 : NaN
00179                      *          1 : Positive zero
00180                      *         -1 : Negative zero
00181                      *          2 : Positive number
00182                      *         -2 : Negative number
00183                      *          3 : Positive infinite number
00184                      *         -3 : Negative infinite number
00185                      */
00186     short  flag;    /* Not used in vp_routines,space for user.  */
00187     BDIGIT frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
00188 } Real;
00189 
00190 /*
00191  *  ------------------
00192  *   EXPORTables.
00193  *  ------------------
00194  */
00195 
00196 VP_EXPORT  Real *
00197 VpNewRbClass(size_t mx, char const *str, VALUE klass);
00198 
00199 VP_EXPORT  Real *VpCreateRbObject(size_t mx,const char *str);
00200 
00201 static inline BDIGIT
00202 rmpd_base_value(void) { return RMPD_BASE; }
00203 static inline size_t
00204 rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
00205 static inline size_t
00206 rmpd_double_figures(void) { return 1+DBL_DIG; }
00207 
00208 #define VpBaseFig() rmpd_component_figures()
00209 #define VpDblFig() rmpd_double_figures()
00210 #define VpBaseVal() rmpd_base_value()
00211 
00212 /* Zero,Inf,NaN (isinf(),isnan() used to check) */
00213 VP_EXPORT double VpGetDoubleNaN(void);
00214 VP_EXPORT double VpGetDoublePosInf(void);
00215 VP_EXPORT double VpGetDoubleNegInf(void);
00216 VP_EXPORT double VpGetDoubleNegZero(void);
00217 
00218 /* These 2 functions added at v1.1.7 */
00219 VP_EXPORT size_t VpGetPrecLimit(void);
00220 VP_EXPORT size_t VpSetPrecLimit(size_t n);
00221 
00222 /* Round mode */
00223 VP_EXPORT int            VpIsRoundMode(unsigned short n);
00224 VP_EXPORT unsigned short VpGetRoundMode(void);
00225 VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
00226 
00227 VP_EXPORT int VpException(unsigned short f,const char *str,int always);
00228 #if 0  /* unused */
00229 VP_EXPORT int VpIsNegDoubleZero(double v);
00230 #endif
00231 VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
00232 VP_EXPORT size_t VpInit(BDIGIT BaseVal);
00233 VP_EXPORT void *VpMemAlloc(size_t mb);
00234 VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
00235 VP_EXPORT void VpFree(Real *pv);
00236 VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
00237 VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
00238 VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
00239 VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
00240 VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
00241 VP_EXPORT int VpComp(Real *a,Real *b);
00242 VP_EXPORT ssize_t VpExponent10(Real *a);
00243 VP_EXPORT void VpSzMantissa(Real *a,char *psz);
00244 VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
00245 VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
00246 VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
00247 VP_EXPORT int VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, const char *exp_chr, size_t ne);
00248 VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
00249 VP_EXPORT void VpDtoV(Real *m,double d);
00250 #if 0  /* unused */
00251 VP_EXPORT void VpItoV(Real *m,S_INT ival);
00252 #endif
00253 VP_EXPORT int VpSqrt(Real *y,Real *x);
00254 VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
00255 VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
00256 VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
00257 VP_EXPORT void VpFrac(Real *y, Real *x);
00258 VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
00259 
00260 /* VP constants */
00261 VP_EXPORT Real *VpOne(void);
00262 
00263 /*
00264  *  ------------------
00265  *  MACRO definitions.
00266  *  ------------------
00267  */
00268 #define Abs(a)     (((a)>= 0)?(a):(-(a)))
00269 #define Max(a, b)  (((a)>(b))?(a):(b))
00270 #define Min(a, b)  (((a)>(b))?(b):(a))
00271 
00272 #define VpMaxPrec(a)   ((a)->MaxPrec)
00273 #define VpPrec(a)      ((a)->Prec)
00274 #define VpGetFlag(a)   ((a)->flag)
00275 
00276 /* Sign */
00277 
00278 /* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
00279 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
00280 /* Change sign of a to a>0,a<0 if s = 1,-1 respectively */
00281 #define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
00282 /* Sets sign of a to a>0,a<0 if s = 1,-1 respectively */
00283 #define VpSetSign(a,s)    {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
00284 
00285 /* 1 */
00286 #define VpSetOne(a)       {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
00287 
00288 /* ZEROs */
00289 #define VpIsPosZero(a)  ((a)->sign==VP_SIGN_POSITIVE_ZERO)
00290 #define VpIsNegZero(a)  ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
00291 #define VpIsZero(a)     (VpIsPosZero(a) || VpIsNegZero(a))
00292 #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
00293 #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
00294 #define VpSetZero(a,s)  ( ((s)>0)?VpSetPosZero(a):VpSetNegZero(a) )
00295 
00296 /* NaN */
00297 #define VpIsNaN(a)      ((a)->sign==VP_SIGN_NaN)
00298 #define VpSetNaN(a)     ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
00299 
00300 /* Infinity */
00301 #define VpIsPosInf(a)   ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
00302 #define VpIsNegInf(a)   ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
00303 #define VpIsInf(a)      (VpIsPosInf(a) || VpIsNegInf(a))
00304 #define VpIsDef(a)      ( !(VpIsNaN(a)||VpIsInf(a)) )
00305 #define VpSetPosInf(a)  ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
00306 #define VpSetNegInf(a)  ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
00307 #define VpSetInf(a,s)   ( ((s)>0)?VpSetPosInf(a):VpSetNegInf(a) )
00308 #define VpHasVal(a)     (a->frac[0])
00309 #define VpIsOne(a)      ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
00310 #define VpExponent(a)   (a->exponent)
00311 #ifdef BIGDECIMAL_DEBUG
00312 int VpVarCheck(Real * v);
00313 #endif /* BIGDECIMAL_DEBUG */
00314 
00315 #if defined(__cplusplus)
00316 #if 0
00317 { /* satisfy cc-mode */
00318 #endif
00319 }  /* extern "C" { */
00320 #endif
00321 #endif /* RUBY_BIG_DECIMAL_H */
00322 

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7