Ruby  2.5.0dev(2017-10-22revision60238)
bigdecimal.h
Go to the documentation of this file.
1 /*
2  *
3  * Ruby BigDecimal(Variable decimal precision) extension library.
4  *
5  * Copyright(C) 2002 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
6  *
7  */
8 
9 #ifndef RUBY_BIG_DECIMAL_H
10 #define RUBY_BIG_DECIMAL_H 1
11 
12 #define RUBY_NO_OLD_COMPATIBILITY
13 
14 #include "ruby/ruby.h"
15 #include <float.h>
16 
17 #ifndef RB_UNUSED_VAR
18 # ifdef __GNUC__
19 # define RB_UNUSED_VAR(x) x __attribute__ ((unused))
20 # else
21 # define RB_UNUSED_VAR(x) x
22 # endif
23 #endif
24 
25 #ifndef UNREACHABLE
26 # define UNREACHABLE /* unreachable */
27 #endif
28 
29 #undef BDIGIT
30 #undef SIZEOF_BDIGITS
31 #undef BDIGIT_DBL
32 #undef BDIGIT_DBL_SIGNED
33 #undef PRI_BDIGIT_PREFIX
34 #undef PRI_BDIGIT_DBL_PREFIX
35 
36 #ifdef HAVE_INT64_T
37 # define BDIGIT uint32_t
38 # define BDIGIT_DBL uint64_t
39 # define BDIGIT_DBL_SIGNED int64_t
40 # define SIZEOF_BDIGITS 4
41 # define PRI_BDIGIT_PREFIX ""
42 # ifdef PRI_LL_PREFIX
43 # define PRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX
44 # else
45 # define PRI_BDIGIT_DBL_PREFIX "l"
46 # endif
47 #else
48 # define BDIGIT uint16_t
49 # define BDIGIT_DBL uint32_t
50 # define BDIGIT_DBL_SIGNED int32_t
51 # define SIZEOF_BDIGITS 2
52 # define PRI_BDIGIT_PREFIX "h"
53 # define PRI_BDIGIT_DBL_PREFIX ""
54 #endif
55 
56 #define PRIdBDIGIT PRI_BDIGIT_PREFIX"d"
57 #define PRIiBDIGIT PRI_BDIGIT_PREFIX"i"
58 #define PRIoBDIGIT PRI_BDIGIT_PREFIX"o"
59 #define PRIuBDIGIT PRI_BDIGIT_PREFIX"u"
60 #define PRIxBDIGIT PRI_BDIGIT_PREFIX"x"
61 #define PRIXBDIGIT PRI_BDIGIT_PREFIX"X"
62 
63 #define PRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d"
64 #define PRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i"
65 #define PRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o"
66 #define PRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u"
67 #define PRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x"
68 #define PRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X"
69 
70 #if defined(__cplusplus)
71 extern "C" {
72 #if 0
73 } /* satisfy cc-mode */
74 #endif
75 #endif
76 
77 #ifndef HAVE_LABS
78 static inline long
79 labs(long const x)
80 {
81  if (x < 0) return -x;
82  return x;
83 }
84 #endif
85 
86 #ifndef HAVE_LLABS
87 static inline LONG_LONG
88 llabs(LONG_LONG const x)
89 {
90  if (x < 0) return -x;
91  return x;
92 }
93 #endif
94 
95 #ifndef HAVE_FINITE
96 static int
97 finite(double)
98 {
99  return !isnan(n) && !isinf(n);
100 }
101 #endif
102 
103 #ifndef isfinite
104 # ifndef HAVE_ISFINITE
105 # define HAVE_ISFINITE 1
106 # define isfinite(x) finite(x)
107 # endif
108 #endif
109 
110 #ifndef FIX_CONST_VALUE_PTR
111 # if defined(__fcc__) || defined(__fcc_version) || \
112  defined(__FCC__) || defined(__FCC_VERSION)
113 /* workaround for old version of Fujitsu C Compiler (fcc) */
114 # define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
115 # else
116 # define FIX_CONST_VALUE_PTR(x) (x)
117 # endif
118 #endif
119 
120 #ifndef HAVE_RB_ARRAY_CONST_PTR
121 static inline const VALUE *
122 rb_array_const_ptr(VALUE a)
123 {
124  return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
125  RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
126 }
127 #endif
128 
129 #ifndef RARRAY_CONST_PTR
130 # define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
131 #endif
132 
133 #ifndef RARRAY_AREF
134 # define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
135 #endif
136 
137 #ifndef HAVE_RB_SYM2STR
138 static inline VALUE
140 {
141  return rb_id2str(SYM2ID(sym));
142 }
143 #endif
144 
145 #ifdef vabs
146 # undef vabs
147 #endif
148 #if SIZEOF_VALUE <= SIZEOF_INT
149 # define vabs abs
150 #elif SIZEOF_VALUE <= SIZEOF_LONG
151 # define vabs labs
152 #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
153 # define vabs llabs
154 #endif
155 
156 extern VALUE rb_cBigDecimal;
157 
158 #if 0 || SIZEOF_BDIGITS >= 16
159 # define RMPD_COMPONENT_FIGURES 38
160 # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
161 #elif SIZEOF_BDIGITS >= 8
162 # define RMPD_COMPONENT_FIGURES 19
163 # define RMPD_BASE ((BDIGIT)10000000000000000000U)
164 #elif SIZEOF_BDIGITS >= 4
165 # define RMPD_COMPONENT_FIGURES 9
166 # define RMPD_BASE ((BDIGIT)1000000000U)
167 #elif SIZEOF_BDIGITS >= 2
168 # define RMPD_COMPONENT_FIGURES 4
169 # define RMPD_BASE ((BDIGIT)10000U)
170 #else
171 # define RMPD_COMPONENT_FIGURES 2
172 # define RMPD_BASE ((BDIGIT)100U)
173 #endif
174 
175 
176 /*
177  * NaN & Infinity
178  */
179 #define SZ_NaN "NaN"
180 #define SZ_INF "Infinity"
181 #define SZ_PINF "+Infinity"
182 #define SZ_NINF "-Infinity"
183 
184 /*
185  * #define VP_EXPORT other than static to let VP_ routines
186  * be called from outside of this module.
187  */
188 #define VP_EXPORT static
189 
190 /* Exception codes */
191 #define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
192 #define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
193 #define VP_EXCEPTION_NaN ((unsigned short)0x0002)
194 #define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
195 #define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001) /* 0x0008) */
196 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
197 
198 /* Following 2 exceptions can't controlled by user */
199 #define VP_EXCEPTION_OP ((unsigned short)0x0020)
200 #define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
201 
202 #define RMPD_EXCEPTION_MODE_DEFAULT 0U
203 
204 /* Computation mode */
205 #define VP_ROUND_MODE ((unsigned short)0x0100)
206 #define VP_ROUND_UP 1
207 #define VP_ROUND_DOWN 2
208 #define VP_ROUND_HALF_UP 3
209 #define VP_ROUND_HALF_DOWN 4
210 #define VP_ROUND_CEIL 5
211 #define VP_ROUND_FLOOR 6
212 #define VP_ROUND_HALF_EVEN 7
213 
214 #define RMPD_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
215 
216 #define VP_SIGN_NaN 0 /* NaN */
217 #define VP_SIGN_POSITIVE_ZERO 1 /* Positive zero */
218 #define VP_SIGN_NEGATIVE_ZERO -1 /* Negative zero */
219 #define VP_SIGN_POSITIVE_FINITE 2 /* Positive finite number */
220 #define VP_SIGN_NEGATIVE_FINITE -2 /* Negative finite number */
221 #define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
222 #define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
223 
224 #ifdef __GNUC__
225 #define FLEXIBLE_ARRAY_SIZE 0
226 #else
227 #define FLEXIBLE_ARRAY_SIZE 1
228 #endif
229 
230 /*
231  * VP representation
232  * r = 0.xxxxxxxxx *BASE**exponent
233  */
234 typedef struct {
235  VALUE obj; /* Back pointer(VALUE) for Ruby object. */
236  size_t MaxPrec; /* Maximum precision size */
237  /* This is the actual size of frac[] */
238  /*(frac[0] to frac[MaxPrec] are available). */
239  size_t Prec; /* Current precision size. */
240  /* This indicates how much the */
241  /* array frac[] is actually used. */
242  SIGNED_VALUE exponent; /* Exponent part. */
243  short sign; /* Attributes of the value. */
244  /*
245  * ==0 : NaN
246  * 1 : Positive zero
247  * -1 : Negative zero
248  * 2 : Positive number
249  * -2 : Negative number
250  * 3 : Positive infinite number
251  * -3 : Negative infinite number
252  */
253  short flag; /* Not used in vp_routines,space for user. */
254  BDIGIT frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
255 } Real;
256 
257 /*
258  * ------------------
259  * EXPORTables.
260  * ------------------
261  */
262 
263 VP_EXPORT Real *
264 VpNewRbClass(size_t mx, char const *str, VALUE klass);
265 
266 VP_EXPORT Real *VpCreateRbObject(size_t mx,const char *str);
267 
268 static inline BDIGIT
269 rmpd_base_value(void) { return RMPD_BASE; }
270 static inline size_t
271 rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
272 static inline size_t
273 rmpd_double_figures(void) { return 1+DBL_DIG; }
274 
275 #define VpBaseFig() rmpd_component_figures()
276 #define VpDblFig() rmpd_double_figures()
277 #define VpBaseVal() rmpd_base_value()
278 
279 /* Zero,Inf,NaN (isinf(),isnan() used to check) */
280 VP_EXPORT double VpGetDoubleNaN(void);
281 VP_EXPORT double VpGetDoublePosInf(void);
282 VP_EXPORT double VpGetDoubleNegInf(void);
283 VP_EXPORT double VpGetDoubleNegZero(void);
284 
285 /* These 2 functions added at v1.1.7 */
286 VP_EXPORT size_t VpGetPrecLimit(void);
287 VP_EXPORT size_t VpSetPrecLimit(size_t n);
288 
289 /* Round mode */
290 VP_EXPORT int VpIsRoundMode(unsigned short n);
291 VP_EXPORT unsigned short VpGetRoundMode(void);
292 VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
293 
294 VP_EXPORT int VpException(unsigned short f,const char *str,int always);
295 #if 0 /* unused */
296 VP_EXPORT int VpIsNegDoubleZero(double v);
297 #endif
298 VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
299 VP_EXPORT size_t VpInit(BDIGIT BaseVal);
300 VP_EXPORT void *VpMemAlloc(size_t mb);
301 VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
302 VP_EXPORT void VpFree(Real *pv);
303 VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
304 VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
305 VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
306 VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
307 VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
308 VP_EXPORT int VpComp(Real *a,Real *b);
309 VP_EXPORT ssize_t VpExponent10(Real *a);
310 VP_EXPORT void VpSzMantissa(Real *a,char *psz);
311 VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
312 VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
313 VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
314 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);
315 VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
316 VP_EXPORT void VpDtoV(Real *m,double d);
317 #if 0 /* unused */
318 VP_EXPORT void VpItoV(Real *m,S_INT ival);
319 #endif
320 VP_EXPORT int VpSqrt(Real *y,Real *x);
321 VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
322 VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
323 VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
324 VP_EXPORT void VpFrac(Real *y, Real *x);
325 VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
326 
327 /* VP constants */
328 VP_EXPORT Real *VpOne(void);
329 
330 /*
331  * ------------------
332  * MACRO definitions.
333  * ------------------
334  */
335 #define Abs(a) (((a)>= 0)?(a):(-(a)))
336 #define Max(a, b) (((a)>(b))?(a):(b))
337 #define Min(a, b) (((a)>(b))?(b):(a))
338 
339 #define VpMaxPrec(a) ((a)->MaxPrec)
340 #define VpPrec(a) ((a)->Prec)
341 #define VpGetFlag(a) ((a)->flag)
342 
343 /* Sign */
344 
345 /* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
346 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
347 /* Change sign of a to a>0,a<0 if s = 1,-1 respectively */
348 #define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
349 /* Sets sign of a to a>0,a<0 if s = 1,-1 respectively */
350 #define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
351 
352 /* 1 */
353 #define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
354 
355 /* ZEROs */
356 #define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
357 #define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
358 #define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
359 #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
360 #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
361 #define VpSetZero(a,s) (void)(((s)>0)?VpSetPosZero(a):VpSetNegZero(a))
362 
363 /* NaN */
364 #define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
365 #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
366 
367 /* Infinity */
368 #define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
369 #define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
370 #define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
371 #define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
372 #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
373 #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
374 #define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
375 #define VpHasVal(a) (a->frac[0])
376 #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
377 #define VpExponent(a) (a->exponent)
378 #ifdef BIGDECIMAL_DEBUG
379 int VpVarCheck(Real * v);
380 #endif /* BIGDECIMAL_DEBUG */
381 
382 #if defined(__cplusplus)
383 #if 0
384 { /* satisfy cc-mode */
385 #endif
386 } /* extern "C" { */
387 #endif
388 #endif /* RUBY_BIG_DECIMAL_H */
VP_EXPORT void * VpMemRealloc(void *ptr, size_t mb)
Definition: bigdecimal.c:3523
VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il)
Definition: bigdecimal.c:6141
VP_EXPORT ssize_t VpExponent10(Real *a)
Definition: bigdecimal.c:5286
SIGNED_VALUE exponent
Definition: bigdecimal.h:242
VP_EXPORT double VpGetDoubleNaN(void)
Definition: bigdecimal.c:3714
#define FIX_CONST_VALUE_PTR(x)
Definition: ruby.h:2059
#define DBL_DIG
Definition: numeric.c:54
VALUE obj
Definition: bigdecimal.h:235
#define rb_id2str(id)
Definition: vm_backtrace.c:29
size_t Prec
Definition: bigdecimal.h:239
#define SYM2ID(x)
Definition: ruby.h:384
VP_EXPORT size_t VpGetPrecLimit(void)
Definition: bigdecimal.c:3598
VP_EXPORT size_t VpDivd(Real *c, Real *r, Real *a, Real *b)
Definition: bigdecimal.c:4808
VP_EXPORT size_t VpMult(Real *c, Real *a, Real *b)
Definition: bigdecimal.c:4679
VP_EXPORT int VpComp(Real *a, Real *b)
Definition: bigdecimal.c:5061
VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m)
Definition: bigdecimal.c:5659
#define sym(x)
Definition: date_core.c:3721
VP_EXPORT void VpFrac(Real *y, Real *x)
Definition: bigdecimal.c:6236
VP_EXPORT size_t VpAddSub(Real *c, Real *a, Real *b, int operation)
Definition: bigdecimal.c:4224
VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus)
Definition: bigdecimal.c:5433
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf)
Definition: bigdecimal.c:5964
VP_EXPORT Real * VpNewRbClass(size_t mx, char const *str, VALUE klass)
Definition: bigdecimal.c:660
#define RARRAY(obj)
Definition: ruby.h:1203
#define RMPD_COMPONENT_FIGURES
Definition: bigdecimal.h:168
VP_EXPORT void * VpMemAlloc(size_t mb)
Definition: bigdecimal.c:3509
#define ne(x, y)
Definition: time.c:72
VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus)
Definition: bigdecimal.c:5385
#define VP_EXPORT
Definition: bigdecimal.h:188
#define RMPD_BASE
Definition: bigdecimal.h:169
VP_EXPORT size_t VpNumOfChars(Real *vp, const char *pszFmt)
Definition: bigdecimal.c:3882
VALUE rb_cBigDecimal
Definition: bigdecimal.c:45
VP_EXPORT void VpFree(Real *pv)
Definition: bigdecimal.c:3533
VP_EXPORT Real * VpCreateRbObject(size_t mx, const char *str)
Definition: bigdecimal.c:670
RUBY_EXTERN int isinf(double)
Definition: isinf.c:56
VP_EXPORT double VpGetDoubleNegZero(void)
Definition: bigdecimal.c:3738
VP_EXPORT int VpIsRoundMode(unsigned short n)
Definition: bigdecimal.c:3649
short flag
Definition: bigdecimal.h:253
VP_EXPORT int VpSqrt(Real *y, Real *x)
Definition: bigdecimal.c:5855
VP_EXPORT void VpDtoV(Real *m, double d)
Definition: bigdecimal.c:5723
VP_EXPORT int VpToSpecialString(Real *a, char *psz, int fPlus)
Definition: bigdecimal.c:5350
unsigned long VALUE
Definition: ruby.h:85
VP_EXPORT double VpGetDoubleNegInf(void)
Definition: bigdecimal.c:3730
#define RBASIC(obj)
Definition: ruby.h:1197
VP_EXPORT size_t VpSetPrecLimit(size_t n)
Definition: bigdecimal.c:3614
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw)
Definition: bigdecimal.c:4180
#define isnan(x)
Definition: win32.h:346
short sign
Definition: bigdecimal.h:243
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)
Definition: bigdecimal.c:5499
#define f
VP_EXPORT void VpSzMantissa(Real *a, char *psz)
Definition: bigdecimal.c:5303
VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n)
Definition: bigdecimal.c:6284
VP_EXPORT unsigned short VpGetRoundMode(void)
Definition: bigdecimal.c:3633
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf)
Definition: bigdecimal.c:6126
VP_EXPORT Real * VpAlloc(size_t mx, const char *szVal)
Definition: bigdecimal.c:4014
#define FLEXIBLE_ARRAY_SIZE
Definition: bigdecimal.h:227
VP_EXPORT size_t VpInit(BDIGIT BaseVal)
Definition: bigdecimal.c:3926
VP_EXPORT double VpGetDoublePosInf(void)
Definition: bigdecimal.c:3722
VP_EXPORT int VpException(unsigned short f, const char *str, int always)
Definition: bigdecimal.c:3755
VP_EXPORT Real * VpOne(void)
Definition: bigdecimal.c:3957
VP_EXPORT unsigned short VpSetRoundMode(unsigned short n)
Definition: bigdecimal.c:3667
#define BDIGIT
Definition: bigdecimal.h:48
size_t MaxPrec
Definition: bigdecimal.h:236
#define rb_sym2str(sym)
Definition: console.c:107
#define SIGNED_VALUE
Definition: ruby.h:87