bigdecimal 3.1.8 → 4.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bigdecimal.gemspec +6 -1
- data/ext/bigdecimal/bigdecimal.c +1020 -2607
- data/ext/bigdecimal/bigdecimal.h +45 -60
- data/ext/bigdecimal/bits.h +3 -0
- data/ext/bigdecimal/div.h +192 -0
- data/ext/bigdecimal/extconf.rb +17 -16
- data/ext/bigdecimal/missing/dtoa.c +184 -137
- data/ext/bigdecimal/missing.c +2 -1
- data/ext/bigdecimal/missing.h +5 -95
- data/ext/bigdecimal/ntt.h +191 -0
- data/lib/bigdecimal/jacobian.rb +2 -0
- data/lib/bigdecimal/ludcmp.rb +2 -0
- data/lib/bigdecimal/math.rb +829 -134
- data/lib/bigdecimal/newton.rb +2 -0
- data/lib/bigdecimal/util.rb +17 -16
- data/lib/bigdecimal.rb +399 -0
- data/sample/linear.rb +73 -37
- data/sample/nlsolve.rb +47 -30
- data/sample/pi.rb +2 -7
- data/sig/big_decimal.rbs +1502 -0
- data/sig/big_decimal_util.rbs +158 -0
- data/sig/big_math.rbs +423 -0
- metadata +9 -7
data/ext/bigdecimal/bigdecimal.h
CHANGED
|
@@ -17,24 +17,15 @@
|
|
|
17
17
|
# include <float.h>
|
|
18
18
|
#endif
|
|
19
19
|
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
# define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
|
|
28
|
-
# else
|
|
29
|
-
# define PRI_DECDIG_DBL_PREFIX "l"
|
|
30
|
-
# endif
|
|
20
|
+
#define DECDIG uint32_t
|
|
21
|
+
#define DECDIG_DBL uint64_t
|
|
22
|
+
#define DECDIG_DBL_SIGNED int64_t
|
|
23
|
+
#define SIZEOF_DECDIG 4
|
|
24
|
+
#define PRI_DECDIG_PREFIX ""
|
|
25
|
+
#ifdef PRI_LL_PREFIX
|
|
26
|
+
# define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
|
|
31
27
|
#else
|
|
32
|
-
# define
|
|
33
|
-
# define DECDIG_DBL uint32_t
|
|
34
|
-
# define DECDIG_DBL_SIGNED int32_t
|
|
35
|
-
# define SIZEOF_DECDIG 2
|
|
36
|
-
# define PRI_DECDIG_PREFIX "h"
|
|
37
|
-
# define PRI_DECDIG_DBL_PREFIX ""
|
|
28
|
+
# define PRI_DECDIG_DBL_PREFIX "l"
|
|
38
29
|
#endif
|
|
39
30
|
|
|
40
31
|
#define PRIdDECDIG PRI_DECDIG_PREFIX"d"
|
|
@@ -51,31 +42,15 @@
|
|
|
51
42
|
#define PRIxDECDIG_DBL PRI_DECDIG_DBL_PREFIX"x"
|
|
52
43
|
#define PRIXDECDIG_DBL PRI_DECDIG_DBL_PREFIX"X"
|
|
53
44
|
|
|
54
|
-
#
|
|
55
|
-
#
|
|
56
|
-
# define BIGDECIMAL_COMPONENT_FIGURES 9
|
|
45
|
+
#define BIGDECIMAL_BASE ((DECDIG)1000000000U)
|
|
46
|
+
#define BIGDECIMAL_COMPONENT_FIGURES 9
|
|
57
47
|
/*
|
|
58
48
|
* The number of components required for a 64-bit integer.
|
|
59
49
|
*
|
|
60
50
|
* INT64_MAX: 9_223372036_854775807
|
|
61
51
|
* UINT64_MAX: 18_446744073_709551615
|
|
62
52
|
*/
|
|
63
|
-
#
|
|
64
|
-
|
|
65
|
-
#elif SIZEOF_DECDIG == 2
|
|
66
|
-
# define BIGDECIMAL_BASE ((DECDIG)10000U)
|
|
67
|
-
# define BIGDECIMAL_COMPONENT_FIGURES 4
|
|
68
|
-
/*
|
|
69
|
-
* The number of components required for a 64-bit integer.
|
|
70
|
-
*
|
|
71
|
-
* INT64_MAX: 922_3372_0368_5477_5807
|
|
72
|
-
* UINT64_MAX: 1844_6744_0737_0955_1615
|
|
73
|
-
*/
|
|
74
|
-
# define BIGDECIMAL_INT64_MAX_LENGTH 5
|
|
75
|
-
|
|
76
|
-
#else
|
|
77
|
-
# error Unknown size of DECDIG
|
|
78
|
-
#endif
|
|
53
|
+
#define BIGDECIMAL_INT64_MAX_LENGTH 3
|
|
79
54
|
|
|
80
55
|
#define BIGDECIMAL_DOUBLE_FIGURES (1+DBL_DIG)
|
|
81
56
|
|
|
@@ -167,7 +142,6 @@ enum rbd_rounding_mode {
|
|
|
167
142
|
* r = 0.xxxxxxxxx *BASE**exponent
|
|
168
143
|
*/
|
|
169
144
|
typedef struct {
|
|
170
|
-
VALUE obj; /* Back pointer(VALUE) for Ruby object. */
|
|
171
145
|
size_t MaxPrec; /* Maximum precision size */
|
|
172
146
|
/* This is the actual size of frac[] */
|
|
173
147
|
/*(frac[0] to frac[MaxPrec] are available). */
|
|
@@ -189,19 +163,23 @@ typedef struct {
|
|
|
189
163
|
DECDIG frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
|
|
190
164
|
} Real;
|
|
191
165
|
|
|
166
|
+
typedef struct {
|
|
167
|
+
VALUE bigdecimal;
|
|
168
|
+
Real *real;
|
|
169
|
+
} BDVALUE;
|
|
170
|
+
|
|
171
|
+
typedef struct {
|
|
172
|
+
VALUE bigdecimal_or_nil;
|
|
173
|
+
Real *real_or_null;
|
|
174
|
+
} NULLABLE_BDVALUE;
|
|
175
|
+
|
|
192
176
|
/*
|
|
193
177
|
* ------------------
|
|
194
178
|
* EXPORTables.
|
|
195
179
|
* ------------------
|
|
196
180
|
*/
|
|
197
181
|
|
|
198
|
-
VP_EXPORT Real *VpNewRbClass(size_t mx, char const *str, VALUE klass, bool strict_p, bool raise_exception);
|
|
199
|
-
|
|
200
|
-
VP_EXPORT Real *VpCreateRbObject(size_t mx, const char *str, bool raise_exception);
|
|
201
|
-
|
|
202
182
|
#define VpBaseFig() BIGDECIMAL_COMPONENT_FIGURES
|
|
203
|
-
#define VpDblFig() BIGDECIMAL_DOUBLE_FIGURES
|
|
204
|
-
#define VpBaseVal() BIGDECIMAL_BASE
|
|
205
183
|
|
|
206
184
|
/* Zero,Inf,NaN (isinf(),isnan() used to check) */
|
|
207
185
|
VP_EXPORT double VpGetDoubleNaN(void);
|
|
@@ -211,7 +189,7 @@ VP_EXPORT double VpGetDoubleNegZero(void);
|
|
|
211
189
|
|
|
212
190
|
/* These 2 functions added at v1.1.7 */
|
|
213
191
|
VP_EXPORT size_t VpGetPrecLimit(void);
|
|
214
|
-
VP_EXPORT
|
|
192
|
+
VP_EXPORT void VpSetPrecLimit(size_t n);
|
|
215
193
|
|
|
216
194
|
/* Round mode */
|
|
217
195
|
VP_EXPORT int VpIsRoundMode(unsigned short n);
|
|
@@ -219,16 +197,14 @@ VP_EXPORT unsigned short VpGetRoundMode(void);
|
|
|
219
197
|
VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
|
|
220
198
|
|
|
221
199
|
VP_EXPORT int VpException(unsigned short f,const char *str,int always);
|
|
222
|
-
#if 0 /* unused */
|
|
223
|
-
VP_EXPORT int VpIsNegDoubleZero(double v);
|
|
224
|
-
#endif
|
|
225
200
|
VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
|
|
226
201
|
VP_EXPORT size_t VpInit(DECDIG BaseVal);
|
|
227
|
-
VP_EXPORT
|
|
202
|
+
VP_EXPORT NULLABLE_BDVALUE VpAlloc(const char *szVal, int strict_p, int exc);
|
|
228
203
|
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
|
|
229
204
|
VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
|
|
230
205
|
VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
|
|
231
206
|
VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
|
|
207
|
+
VP_EXPORT int VpNmlz(Real *a);
|
|
232
208
|
VP_EXPORT int VpComp(Real *a,Real *b);
|
|
233
209
|
VP_EXPORT ssize_t VpExponent10(Real *a);
|
|
234
210
|
VP_EXPORT void VpSzMantissa(Real *a, char *buf, size_t bufsize);
|
|
@@ -237,21 +213,35 @@ VP_EXPORT void VpToString(Real *a, char *buf, size_t bufsize, size_t fFmt, int f
|
|
|
237
213
|
VP_EXPORT void VpToFString(Real *a, char *buf, size_t bufsize, size_t fFmt, int fPlus);
|
|
238
214
|
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);
|
|
239
215
|
VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
|
|
240
|
-
VP_EXPORT void VpDtoV(Real *m,double d);
|
|
241
|
-
#if 0 /* unused */
|
|
242
|
-
VP_EXPORT void VpItoV(Real *m,S_INT ival);
|
|
243
|
-
#endif
|
|
244
|
-
VP_EXPORT int VpSqrt(Real *y,Real *x);
|
|
245
216
|
VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
|
|
246
217
|
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
|
|
247
218
|
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
|
|
248
219
|
VP_EXPORT void VpFrac(Real *y, Real *x);
|
|
249
|
-
VP_EXPORT int
|
|
250
|
-
#define VpPower VpPowerByInt
|
|
220
|
+
VP_EXPORT int AddExponent(Real *a, SIGNED_VALUE n);
|
|
251
221
|
|
|
252
222
|
/* VP constants */
|
|
253
223
|
VP_EXPORT Real *VpOne(void);
|
|
254
224
|
|
|
225
|
+
/*
|
|
226
|
+
* **** BigDecimal part ****
|
|
227
|
+
*/
|
|
228
|
+
VP_EXPORT VALUE BigDecimal_lt(VALUE self, VALUE r);
|
|
229
|
+
VP_EXPORT VALUE BigDecimal_ge(VALUE self, VALUE r);
|
|
230
|
+
VP_EXPORT VALUE BigDecimal_exponent(VALUE self);
|
|
231
|
+
VP_EXPORT VALUE BigDecimal_fix(VALUE self);
|
|
232
|
+
VP_EXPORT VALUE BigDecimal_frac(VALUE self);
|
|
233
|
+
VP_EXPORT VALUE BigDecimal_add(VALUE self, VALUE b);
|
|
234
|
+
VP_EXPORT VALUE BigDecimal_sub(VALUE self, VALUE b);
|
|
235
|
+
VP_EXPORT VALUE BigDecimal_mult(VALUE self, VALUE b);
|
|
236
|
+
VP_EXPORT VALUE BigDecimal_add2(VALUE self, VALUE b, VALUE n);
|
|
237
|
+
VP_EXPORT VALUE BigDecimal_sub2(VALUE self, VALUE b, VALUE n);
|
|
238
|
+
VP_EXPORT VALUE BigDecimal_mult2(VALUE self, VALUE b, VALUE n);
|
|
239
|
+
VP_EXPORT VALUE BigDecimal_split(VALUE self);
|
|
240
|
+
VP_EXPORT VALUE BigDecimal_decimal_shift(VALUE self, VALUE v);
|
|
241
|
+
VP_EXPORT inline BDVALUE GetBDValueMust(VALUE v);
|
|
242
|
+
VP_EXPORT inline BDVALUE rbd_allocate_struct_zero_wrap(int sign, size_t const digits);
|
|
243
|
+
#define NewZeroWrap rbd_allocate_struct_zero_wrap
|
|
244
|
+
|
|
255
245
|
/*
|
|
256
246
|
* ------------------
|
|
257
247
|
* MACRO definitions.
|
|
@@ -261,10 +251,6 @@ VP_EXPORT Real *VpOne(void);
|
|
|
261
251
|
#define Max(a, b) (((a)>(b))?(a):(b))
|
|
262
252
|
#define Min(a, b) (((a)>(b))?(b):(a))
|
|
263
253
|
|
|
264
|
-
#define VpMaxPrec(a) ((a)->MaxPrec)
|
|
265
|
-
#define VpPrec(a) ((a)->Prec)
|
|
266
|
-
#define VpGetFlag(a) ((a)->flag)
|
|
267
|
-
|
|
268
254
|
/* Sign */
|
|
269
255
|
|
|
270
256
|
/* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
|
|
@@ -299,7 +285,6 @@ VP_EXPORT Real *VpOne(void);
|
|
|
299
285
|
#define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
|
|
300
286
|
#define VpHasVal(a) (a->frac[0])
|
|
301
287
|
#define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
|
|
302
|
-
#define VpExponent(a) (a->exponent)
|
|
303
288
|
#ifdef BIGDECIMAL_DEBUG
|
|
304
289
|
int VpVarCheck(Real * v);
|
|
305
290
|
#endif /* BIGDECIMAL_DEBUG */
|
data/ext/bigdecimal/bits.h
CHANGED
|
@@ -26,6 +26,9 @@
|
|
|
26
26
|
((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
|
|
27
27
|
((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))
|
|
28
28
|
|
|
29
|
+
#define ADD_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
|
|
30
|
+
((a) > 0) == ((b) > 0) && ((a) > 0 ? (max) - (a) < (b) : (min) - (a) > (b)))
|
|
31
|
+
|
|
29
32
|
#ifdef HAVE_UINT128_T
|
|
30
33
|
# define bit_length(x) \
|
|
31
34
|
(unsigned int) \
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Calculate the inverse of x using the Newton-Raphson method.
|
|
2
|
+
static VALUE
|
|
3
|
+
newton_raphson_inverse(VALUE x, size_t prec) {
|
|
4
|
+
BDVALUE bdone = NewZeroWrap(1, 1);
|
|
5
|
+
VpSetOne(bdone.real);
|
|
6
|
+
VALUE one = bdone.bigdecimal;
|
|
7
|
+
|
|
8
|
+
// Initial approximation in 2 digits
|
|
9
|
+
BDVALUE bdx = GetBDValueMust(x);
|
|
10
|
+
BDVALUE inv0 = NewZeroWrap(1, 2 * BIGDECIMAL_COMPONENT_FIGURES);
|
|
11
|
+
VpSetOne(inv0.real);
|
|
12
|
+
DECDIG_DBL numerator = (DECDIG_DBL)BIGDECIMAL_BASE * 100;
|
|
13
|
+
DECDIG_DBL denominator = (DECDIG_DBL)bdx.real->frac[0] * 100 + (DECDIG_DBL)(bdx.real->Prec >= 2 ? bdx.real->frac[1] : 0) * 100 / BIGDECIMAL_BASE;
|
|
14
|
+
inv0.real->frac[0] = (DECDIG)(numerator / denominator);
|
|
15
|
+
inv0.real->frac[1] = (DECDIG)((numerator % denominator) * (BIGDECIMAL_BASE / 100) / denominator * 100);
|
|
16
|
+
inv0.real->Prec = 2;
|
|
17
|
+
inv0.real->exponent = 1 - bdx.real->exponent;
|
|
18
|
+
VpNmlz(inv0.real);
|
|
19
|
+
RB_GC_GUARD(bdx.bigdecimal);
|
|
20
|
+
VALUE inv = inv0.bigdecimal;
|
|
21
|
+
|
|
22
|
+
int bl = 1;
|
|
23
|
+
while (((size_t)1 << bl) < prec) bl++;
|
|
24
|
+
|
|
25
|
+
for (int i = bl; i >= 0; i--) {
|
|
26
|
+
size_t n = (prec >> i) + 2;
|
|
27
|
+
if (n > prec) n = prec;
|
|
28
|
+
// Newton-Raphson iteration: inv_next = inv + inv * (1 - x * inv)
|
|
29
|
+
VALUE one_minus_x_inv = BigDecimal_sub2(
|
|
30
|
+
one,
|
|
31
|
+
BigDecimal_mult(BigDecimal_mult2(x, one, SIZET2NUM(n + 1)), inv),
|
|
32
|
+
SIZET2NUM(SIZET2NUM(n / 2))
|
|
33
|
+
);
|
|
34
|
+
inv = BigDecimal_add2(
|
|
35
|
+
inv,
|
|
36
|
+
BigDecimal_mult(inv, one_minus_x_inv),
|
|
37
|
+
SIZET2NUM(n)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return inv;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Calculates divmod by multiplying approximate reciprocal of y
|
|
44
|
+
static void
|
|
45
|
+
divmod_by_inv_mul(VALUE x, VALUE y, VALUE inv, VALUE *res_div, VALUE *res_mod) {
|
|
46
|
+
VALUE div = BigDecimal_fix(BigDecimal_mult(x, inv));
|
|
47
|
+
VALUE mod = BigDecimal_sub(x, BigDecimal_mult(div, y));
|
|
48
|
+
while (RTEST(BigDecimal_lt(mod, INT2FIX(0)))) {
|
|
49
|
+
mod = BigDecimal_add(mod, y);
|
|
50
|
+
div = BigDecimal_sub(div, INT2FIX(1));
|
|
51
|
+
}
|
|
52
|
+
while (RTEST(BigDecimal_ge(mod, y))) {
|
|
53
|
+
mod = BigDecimal_sub(mod, y);
|
|
54
|
+
div = BigDecimal_add(div, INT2FIX(1));
|
|
55
|
+
}
|
|
56
|
+
*res_div = div;
|
|
57
|
+
*res_mod = mod;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static void
|
|
61
|
+
slice_copy(DECDIG *dest, Real *src, size_t rshift, size_t length) {
|
|
62
|
+
ssize_t start = src->exponent - (ssize_t)rshift - (ssize_t)length;
|
|
63
|
+
if (start >= (ssize_t)src->Prec) return;
|
|
64
|
+
if (start < 0) {
|
|
65
|
+
dest -= start;
|
|
66
|
+
length -= (size_t)(-start);
|
|
67
|
+
start = 0;
|
|
68
|
+
}
|
|
69
|
+
size_t max_length = (size_t)((ssize_t)src->Prec - start);
|
|
70
|
+
memcpy(dest, src->frac + start, Min(length, max_length) * sizeof(DECDIG));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Calculates divmod using Newton-Raphson method.
|
|
74
|
+
* x and y must be a BigDecimal representing an integer value.
|
|
75
|
+
*
|
|
76
|
+
* To calculate with low cost, we need to split x into blocks and perform divmod for each block.
|
|
77
|
+
* x_digits = remaining_digits(<= y_digits) + block_digits * num_blocks
|
|
78
|
+
*
|
|
79
|
+
* Example:
|
|
80
|
+
* xxx_xxxxx_xxxxx_xxxxx(18 digits) / yyyyy(5 digits)
|
|
81
|
+
* remaining_digits = 3, block_digits = 5, num_blocks = 3
|
|
82
|
+
* repeating xxxxx_xxxxxx.divmod(yyyyy) calculation 3 times.
|
|
83
|
+
*
|
|
84
|
+
* In each divmod step, dividend is at most (y_digits + block_digits) digits and divisor is y_digits digits.
|
|
85
|
+
* Reciprocal of y needs block_digits + 1 precision.
|
|
86
|
+
*/
|
|
87
|
+
static void
|
|
88
|
+
divmod_newton(VALUE x, VALUE y, VALUE *div_out, VALUE *mod_out) {
|
|
89
|
+
size_t x_digits = NUM2SIZET(BigDecimal_exponent(x));
|
|
90
|
+
size_t y_digits = NUM2SIZET(BigDecimal_exponent(y));
|
|
91
|
+
if (x_digits <= y_digits) x_digits = y_digits + 1;
|
|
92
|
+
|
|
93
|
+
size_t n = x_digits / y_digits;
|
|
94
|
+
size_t block_figs = (x_digits - y_digits) / n / BIGDECIMAL_COMPONENT_FIGURES + 1;
|
|
95
|
+
size_t block_digits = block_figs * BIGDECIMAL_COMPONENT_FIGURES;
|
|
96
|
+
size_t num_blocks = (x_digits - y_digits + block_digits - 1) / block_digits;
|
|
97
|
+
size_t y_figs = (y_digits - 1) / BIGDECIMAL_COMPONENT_FIGURES + 1;
|
|
98
|
+
VALUE yinv = newton_raphson_inverse(y, block_digits + 1);
|
|
99
|
+
|
|
100
|
+
BDVALUE divident = NewZeroWrap(1, BIGDECIMAL_COMPONENT_FIGURES * (y_figs + block_figs));
|
|
101
|
+
BDVALUE div_result = NewZeroWrap(1, BIGDECIMAL_COMPONENT_FIGURES * (num_blocks * block_figs + 1));
|
|
102
|
+
BDVALUE bdx = GetBDValueMust(x);
|
|
103
|
+
|
|
104
|
+
VALUE mod = BigDecimal_fix(BigDecimal_decimal_shift(x, SSIZET2NUM(-(ssize_t)(num_blocks * block_digits))));
|
|
105
|
+
for (ssize_t i = (ssize_t)(num_blocks - 1); i >= 0; i--) {
|
|
106
|
+
memset(divident.real->frac, 0, (y_figs + block_figs) * sizeof(DECDIG));
|
|
107
|
+
|
|
108
|
+
BDVALUE bdmod = GetBDValueMust(mod);
|
|
109
|
+
slice_copy(divident.real->frac, bdmod.real, 0, y_figs);
|
|
110
|
+
slice_copy(divident.real->frac + y_figs, bdx.real, (size_t)i * block_figs, block_figs);
|
|
111
|
+
RB_GC_GUARD(bdmod.bigdecimal);
|
|
112
|
+
|
|
113
|
+
VpSetSign(divident.real, 1);
|
|
114
|
+
divident.real->exponent = (ssize_t)(y_figs + block_figs);
|
|
115
|
+
divident.real->Prec = y_figs + block_figs;
|
|
116
|
+
VpNmlz(divident.real);
|
|
117
|
+
|
|
118
|
+
VALUE div;
|
|
119
|
+
divmod_by_inv_mul(divident.bigdecimal, y, yinv, &div, &mod);
|
|
120
|
+
BDVALUE bddiv = GetBDValueMust(div);
|
|
121
|
+
slice_copy(div_result.real->frac + (num_blocks - (size_t)i - 1) * block_figs, bddiv.real, 0, block_figs + 1);
|
|
122
|
+
RB_GC_GUARD(bddiv.bigdecimal);
|
|
123
|
+
}
|
|
124
|
+
VpSetSign(div_result.real, 1);
|
|
125
|
+
div_result.real->exponent = (ssize_t)(num_blocks * block_figs + 1);
|
|
126
|
+
div_result.real->Prec = num_blocks * block_figs + 1;
|
|
127
|
+
VpNmlz(div_result.real);
|
|
128
|
+
RB_GC_GUARD(bdx.bigdecimal);
|
|
129
|
+
RB_GC_GUARD(divident.bigdecimal);
|
|
130
|
+
RB_GC_GUARD(div_result.bigdecimal);
|
|
131
|
+
*div_out = div_result.bigdecimal;
|
|
132
|
+
*mod_out = mod;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static VALUE
|
|
136
|
+
VpDivdNewtonInner(VALUE args_ptr)
|
|
137
|
+
{
|
|
138
|
+
Real **args = (Real**)args_ptr;
|
|
139
|
+
Real *c = args[0], *r = args[1], *a = args[2], *b = args[3];
|
|
140
|
+
BDVALUE a2, b2, c2, r2;
|
|
141
|
+
VALUE div, mod, a2_frac = Qnil;
|
|
142
|
+
size_t div_prec = c->MaxPrec - 1;
|
|
143
|
+
size_t base_prec = b->Prec;
|
|
144
|
+
|
|
145
|
+
a2 = NewZeroWrap(1, a->Prec * BIGDECIMAL_COMPONENT_FIGURES);
|
|
146
|
+
b2 = NewZeroWrap(1, b->Prec * BIGDECIMAL_COMPONENT_FIGURES);
|
|
147
|
+
VpAsgn(a2.real, a, 1);
|
|
148
|
+
VpAsgn(b2.real, b, 1);
|
|
149
|
+
VpSetSign(a2.real, 1);
|
|
150
|
+
VpSetSign(b2.real, 1);
|
|
151
|
+
a2.real->exponent = (ssize_t)(base_prec + div_prec);
|
|
152
|
+
b2.real->exponent = (ssize_t)base_prec;
|
|
153
|
+
|
|
154
|
+
if ((ssize_t)a2.real->Prec > a2.real->exponent) {
|
|
155
|
+
a2_frac = BigDecimal_frac(a2.bigdecimal);
|
|
156
|
+
VpMidRound(a2.real, VP_ROUND_DOWN, 0);
|
|
157
|
+
}
|
|
158
|
+
divmod_newton(a2.bigdecimal, b2.bigdecimal, &div, &mod);
|
|
159
|
+
if (a2_frac != Qnil) mod = BigDecimal_add(mod, a2_frac);
|
|
160
|
+
|
|
161
|
+
c2 = GetBDValueMust(div);
|
|
162
|
+
r2 = GetBDValueMust(mod);
|
|
163
|
+
VpAsgn(c, c2.real, VpGetSign(a) * VpGetSign(b));
|
|
164
|
+
VpAsgn(r, r2.real, VpGetSign(a));
|
|
165
|
+
AddExponent(c, a->exponent);
|
|
166
|
+
AddExponent(c, -b->exponent);
|
|
167
|
+
AddExponent(c, -(ssize_t)div_prec);
|
|
168
|
+
AddExponent(r, a->exponent);
|
|
169
|
+
AddExponent(r, -(ssize_t)(base_prec + div_prec));
|
|
170
|
+
RB_GC_GUARD(a2.bigdecimal);
|
|
171
|
+
RB_GC_GUARD(a2.bigdecimal);
|
|
172
|
+
RB_GC_GUARD(c2.bigdecimal);
|
|
173
|
+
RB_GC_GUARD(r2.bigdecimal);
|
|
174
|
+
return Qnil;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static VALUE
|
|
178
|
+
ensure_restore_prec_limit(VALUE limit)
|
|
179
|
+
{
|
|
180
|
+
VpSetPrecLimit(NUM2SIZET(limit));
|
|
181
|
+
return Qnil;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static void
|
|
185
|
+
VpDivdNewton(Real *c, Real *r, Real *a, Real *b)
|
|
186
|
+
{
|
|
187
|
+
Real *args[4] = {c, r, a, b};
|
|
188
|
+
size_t pl = VpGetPrecLimit();
|
|
189
|
+
VpSetPrecLimit(0);
|
|
190
|
+
// Ensure restoring prec limit because some methods used in VpDivdNewtonInner may raise an exception
|
|
191
|
+
rb_ensure(VpDivdNewtonInner, (VALUE)args, ensure_restore_prec_limit, SIZET2NUM(pl));
|
|
192
|
+
}
|
data/ext/bigdecimal/extconf.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# frozen_string_literal:
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
require 'mkmf'
|
|
3
3
|
|
|
4
4
|
def have_builtin_func(name, check_expr, opt = "", &b)
|
|
@@ -24,39 +24,40 @@ have_header("math.h")
|
|
|
24
24
|
have_header("stdbool.h")
|
|
25
25
|
have_header("stdlib.h")
|
|
26
26
|
|
|
27
|
-
have_header("x86intrin.h")
|
|
28
|
-
have_func("_lzcnt_u32", "x86intrin.h")
|
|
29
|
-
have_func("_lzcnt_u64", "x86intrin.h")
|
|
30
|
-
|
|
31
|
-
have_header("intrin.h")
|
|
32
|
-
have_func("__lzcnt", "intrin.h")
|
|
33
|
-
have_func("__lzcnt64", "intrin.h")
|
|
34
|
-
have_func("_BitScanReverse", "intrin.h")
|
|
35
|
-
have_func("_BitScanReverse64", "intrin.h")
|
|
27
|
+
if have_header("x86intrin.h")
|
|
28
|
+
have_func("_lzcnt_u32", "x86intrin.h")
|
|
29
|
+
have_func("_lzcnt_u64", "x86intrin.h")
|
|
30
|
+
end
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
have_func("
|
|
39
|
-
have_func("
|
|
40
|
-
have_func("
|
|
32
|
+
if have_header("intrin.h")
|
|
33
|
+
have_func("__lzcnt", "intrin.h")
|
|
34
|
+
have_func("__lzcnt64", "intrin.h")
|
|
35
|
+
have_func("_BitScanReverse", "intrin.h")
|
|
36
|
+
have_func("_BitScanReverse64", "intrin.h")
|
|
37
|
+
end
|
|
41
38
|
|
|
42
39
|
have_header("ruby/atomic.h")
|
|
43
40
|
have_header("ruby/internal/has/builtin.h")
|
|
44
41
|
have_header("ruby/internal/static_assert.h")
|
|
45
42
|
|
|
46
|
-
have_func("rb_rational_num", "ruby.h")
|
|
47
|
-
have_func("rb_rational_den", "ruby.h")
|
|
48
43
|
have_func("rb_complex_real", "ruby.h")
|
|
49
44
|
have_func("rb_complex_imag", "ruby.h")
|
|
50
45
|
have_func("rb_opts_exception_p", "ruby.h")
|
|
51
46
|
have_func("rb_category_warn", "ruby.h")
|
|
52
47
|
have_const("RB_WARN_CATEGORY_DEPRECATED", "ruby.h")
|
|
53
48
|
|
|
49
|
+
if RUBY_ENGINE == "ruby"
|
|
50
|
+
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
|
|
51
|
+
end
|
|
52
|
+
|
|
54
53
|
if File.file?(File.expand_path('../lib/bigdecimal.rb', __FILE__))
|
|
55
54
|
bigdecimal_rb = "$(srcdir)/lib/bigdecimal.rb"
|
|
56
55
|
else
|
|
57
56
|
bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
|
|
58
57
|
end
|
|
59
58
|
|
|
59
|
+
$defs.push '-DBIGDECIMAL_USE_VP_TEST_METHODS' if ENV['BIGDECIMAL_USE_VP_TEST_METHODS'] == 'true'
|
|
60
|
+
|
|
60
61
|
create_makefile('bigdecimal') {|mf|
|
|
61
62
|
mf << "BIGDECIMAL_RB = #{bigdecimal_rb}\n"
|
|
62
63
|
}
|