00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef RUBY_BIG_DECIMAL_H
00010 #define RUBY_BIG_DECIMAL_H 1
00011
00012 #include "ruby/ruby.h"
00013 #include <float.h>
00014
00015 #ifndef RB_UNUSED_VAR
00016 # ifdef __GNUC__
00017 # define RB_UNUSED_VAR(x) x __attribute__ ((unused))
00018 # else
00019 # define RB_UNUSED_VAR(x) x
00020 # endif
00021 #endif
00022
00023 #ifndef UNREACHABLE
00024 # define UNREACHABLE
00025 #endif
00026
00027 #undef BDIGIT
00028 #undef SIZEOF_BDIGITS
00029 #undef BDIGIT_DBL
00030 #undef BDIGIT_DBL_SIGNED
00031 #undef PRI_BDIGIT_PREFIX
00032 #undef PRI_BDIGIT_DBL_PREFIX
00033
00034 #ifdef HAVE_INT64_T
00035 # define BDIGIT uint32_t
00036 # define BDIGIT_DBL uint64_t
00037 # define BDIGIT_DBL_SIGNED int64_t
00038 # define SIZEOF_BDIGITS 4
00039 #else
00040 # define BDIGIT uint16_t
00041 # define BDIGIT_DBL uint32_t
00042 # define BDIGIT_DBL_SIGNED int32_t
00043 # define SIZEOF_BDIGITS 2
00044 #endif
00045
00046 #if defined(__cplusplus)
00047 extern "C" {
00048 #if 0
00049 }
00050 #endif
00051 #endif
00052
00053 #ifndef HAVE_LABS
00054 static inline long
00055 labs(long const x)
00056 {
00057 if (x < 0) return -x;
00058 return x;
00059 }
00060 #endif
00061
00062 #ifndef HAVE_LLABS
00063 static inline LONG_LONG
00064 llabs(LONG_LONG const x)
00065 {
00066 if (x < 0) return -x;
00067 return x;
00068 }
00069 #endif
00070
00071 #ifdef vabs
00072 # undef vabs
00073 #endif
00074 #if SIZEOF_VALUE <= SIZEOF_INT
00075 # define vabs abs
00076 #elif SIZEOF_VALUE <= SIZEOF_LONG
00077 # define vabs labs
00078 #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
00079 # define vabs llabs
00080 #endif
00081
00082 extern VALUE rb_cBigDecimal;
00083
00084 #if 0 || SIZEOF_BDIGITS >= 16
00085 # define RMPD_COMPONENT_FIGURES 38
00086 # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
00087 #elif SIZEOF_BDIGITS >= 8
00088 # define RMPD_COMPONENT_FIGURES 19
00089 # define RMPD_BASE ((BDIGIT)10000000000000000000U)
00090 #elif SIZEOF_BDIGITS >= 4
00091 # define RMPD_COMPONENT_FIGURES 9
00092 # define RMPD_BASE ((BDIGIT)1000000000U)
00093 #elif SIZEOF_BDIGITS >= 2
00094 # define RMPD_COMPONENT_FIGURES 4
00095 # define RMPD_BASE ((BDIGIT)10000U)
00096 #else
00097 # define RMPD_COMPONENT_FIGURES 2
00098 # define RMPD_BASE ((BDIGIT)100U)
00099 #endif
00100
00101
00102
00103
00104
00105 #define SZ_NaN "NaN"
00106 #define SZ_INF "Infinity"
00107 #define SZ_PINF "+Infinity"
00108 #define SZ_NINF "-Infinity"
00109
00110
00111
00112
00113
00114 #define VP_EXPORT static
00115
00116
00117 #define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
00118 #define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
00119 #define VP_EXCEPTION_NaN ((unsigned short)0x0002)
00120 #define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
00121 #define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001)
00122 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
00123
00124
00125 #define VP_EXCEPTION_OP ((unsigned short)0x0020)
00126 #define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
00127
00128 #define RMPD_EXCEPTION_MODE_DEFAULT 0U
00129
00130
00131 #define VP_ROUND_MODE ((unsigned short)0x0100)
00132 #define VP_ROUND_UP 1
00133 #define VP_ROUND_DOWN 2
00134 #define VP_ROUND_HALF_UP 3
00135 #define VP_ROUND_HALF_DOWN 4
00136 #define VP_ROUND_CEIL 5
00137 #define VP_ROUND_FLOOR 6
00138 #define VP_ROUND_HALF_EVEN 7
00139
00140 #define RMPD_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
00141
00142 #define VP_SIGN_NaN 0
00143 #define VP_SIGN_POSITIVE_ZERO 1
00144 #define VP_SIGN_NEGATIVE_ZERO -1
00145 #define VP_SIGN_POSITIVE_FINITE 2
00146 #define VP_SIGN_NEGATIVE_FINITE -2
00147 #define VP_SIGN_POSITIVE_INFINITE 3
00148 #define VP_SIGN_NEGATIVE_INFINITE -3
00149
00150 #ifdef __GNUC__
00151 #define FLEXIBLE_ARRAY_SIZE 0
00152 #else
00153 #define FLEXIBLE_ARRAY_SIZE 1
00154 #endif
00155
00156
00157
00158
00159
00160 typedef struct {
00161 VALUE obj;
00162 size_t MaxPrec;
00163
00164
00165 size_t Prec;
00166
00167
00168 SIGNED_VALUE exponent;
00169 short sign;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 short flag;
00180 BDIGIT frac[FLEXIBLE_ARRAY_SIZE];
00181 } Real;
00182
00183
00184
00185
00186
00187
00188
00189 VP_EXPORT Real *
00190 VpNewRbClass(size_t mx, char const *str, VALUE klass);
00191
00192 VP_EXPORT Real *VpCreateRbObject(size_t mx,const char *str);
00193
00194 static inline BDIGIT
00195 rmpd_base_value(void) { return RMPD_BASE; }
00196 static inline size_t
00197 rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
00198 static inline size_t
00199 rmpd_double_figures(void) { return 1+DBL_DIG; }
00200
00201 #define VpBaseFig() rmpd_component_figures()
00202 #define VpDblFig() rmpd_double_figures()
00203 #define VpBaseVal() rmpd_base_value()
00204
00205
00206 VP_EXPORT double VpGetDoubleNaN(void);
00207 VP_EXPORT double VpGetDoublePosInf(void);
00208 VP_EXPORT double VpGetDoubleNegInf(void);
00209 VP_EXPORT double VpGetDoubleNegZero(void);
00210
00211
00212 VP_EXPORT size_t VpGetPrecLimit(void);
00213 VP_EXPORT size_t VpSetPrecLimit(size_t n);
00214
00215
00216 VP_EXPORT int VpIsRoundMode(unsigned short n);
00217 VP_EXPORT unsigned short VpGetRoundMode(void);
00218 VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
00219
00220 VP_EXPORT int VpException(unsigned short f,const char *str,int always);
00221 #if 0
00222 VP_EXPORT int VpIsNegDoubleZero(double v);
00223 #endif
00224 VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
00225 VP_EXPORT size_t VpInit(BDIGIT BaseVal);
00226 VP_EXPORT void *VpMemAlloc(size_t mb);
00227 VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
00228 VP_EXPORT void VpFree(Real *pv);
00229 VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
00230 VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
00231 VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
00232 VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
00233 VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
00234 VP_EXPORT int VpComp(Real *a,Real *b);
00235 VP_EXPORT ssize_t VpExponent10(Real *a);
00236 VP_EXPORT void VpSzMantissa(Real *a,char *psz);
00237 VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
00238 VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
00239 VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
00240 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);
00241 VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
00242 VP_EXPORT void VpDtoV(Real *m,double d);
00243 #if 0
00244 VP_EXPORT void VpItoV(Real *m,S_INT ival);
00245 #endif
00246 VP_EXPORT int VpSqrt(Real *y,Real *x);
00247 VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
00248 VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
00249 VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
00250 VP_EXPORT void VpFrac(Real *y, Real *x);
00251 VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
00252
00253
00254 VP_EXPORT Real *VpOne(void);
00255
00256
00257
00258
00259
00260
00261 #define Abs(a) (((a)>= 0)?(a):(-(a)))
00262 #define Max(a, b) (((a)>(b))?(a):(b))
00263 #define Min(a, b) (((a)>(b))?(b):(a))
00264
00265 #define VpMaxPrec(a) ((a)->MaxPrec)
00266 #define VpPrec(a) ((a)->Prec)
00267 #define VpGetFlag(a) ((a)->flag)
00268
00269
00270
00271
00272 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
00273
00274 #define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
00275
00276 #define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
00277
00278
00279 #define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
00280
00281
00282 #define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
00283 #define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
00284 #define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
00285 #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
00286 #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
00287 #define VpSetZero(a,s) ( ((s)>0)?VpSetPosZero(a):VpSetNegZero(a) )
00288
00289
00290 #define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
00291 #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
00292
00293
00294 #define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
00295 #define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
00296 #define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
00297 #define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
00298 #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
00299 #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
00300 #define VpSetInf(a,s) ( ((s)>0)?VpSetPosInf(a):VpSetNegInf(a) )
00301 #define VpHasVal(a) (a->frac[0])
00302 #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
00303 #define VpExponent(a) (a->exponent)
00304 #ifdef BIGDECIMAL_DEBUG
00305 int VpVarCheck(Real * v);
00306 #endif
00307
00308 #if defined(__cplusplus)
00309 #if 0
00310 {
00311 #endif
00312 }
00313 #endif
00314 #endif
00315