00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
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 }
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
00111
00112 #define SZ_NaN "NaN"
00113 #define SZ_INF "Infinity"
00114 #define SZ_PINF "+Infinity"
00115 #define SZ_NINF "-Infinity"
00116
00117
00118
00119
00120
00121 #define VP_EXPORT static
00122
00123
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)
00129 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
00130
00131
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
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
00150 #define VP_SIGN_POSITIVE_ZERO 1
00151 #define VP_SIGN_NEGATIVE_ZERO -1
00152 #define VP_SIGN_POSITIVE_FINITE 2
00153 #define VP_SIGN_NEGATIVE_FINITE -2
00154 #define VP_SIGN_POSITIVE_INFINITE 3
00155 #define VP_SIGN_NEGATIVE_INFINITE -3
00156
00157 #ifdef __GNUC__
00158 #define FLEXIBLE_ARRAY_SIZE 0
00159 #else
00160 #define FLEXIBLE_ARRAY_SIZE 1
00161 #endif
00162
00163
00164
00165
00166
00167 typedef struct {
00168 VALUE obj;
00169 size_t MaxPrec;
00170
00171
00172 size_t Prec;
00173
00174
00175 SIGNED_VALUE exponent;
00176 short sign;
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 short flag;
00187 BDIGIT frac[FLEXIBLE_ARRAY_SIZE];
00188 } Real;
00189
00190
00191
00192
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
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
00219 VP_EXPORT size_t VpGetPrecLimit(void);
00220 VP_EXPORT size_t VpSetPrecLimit(size_t n);
00221
00222
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
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
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
00261 VP_EXPORT Real *VpOne(void);
00262
00263
00264
00265
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
00277
00278
00279 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
00280
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
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
00286 #define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
00287
00288
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
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
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
00314
00315 #if defined(__cplusplus)
00316 #if 0
00317 {
00318 #endif
00319 }
00320 #endif
00321 #endif
00322