bigdecimal 2.0.0 → 4.1.3

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.
@@ -0,0 +1,28 @@
1
+ #include <ruby/ruby.h>
2
+
3
+ #ifdef HAVE_RUBY_ATOMIC_H
4
+ # include <ruby/atomic.h>
5
+ #endif
6
+
7
+ #ifdef RUBY_ATOMIC_PTR_CAS
8
+ # define ATOMIC_PTR_CAS(var, old, new) RUBY_ATOMIC_PTR_CAS(var, old, new)
9
+ #endif
10
+
11
+ #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
12
+ /* GCC warns about unknown sanitizer, which is annoying. */
13
+ # undef NO_SANITIZE
14
+ # define NO_SANITIZE(x, y) \
15
+ _Pragma("GCC diagnostic push") \
16
+ _Pragma("GCC diagnostic ignored \"-Wattributes\"") \
17
+ __attribute__((__no_sanitize__(x))) y; \
18
+ _Pragma("GCC diagnostic pop") \
19
+ y
20
+ #endif
21
+
22
+ #undef strtod
23
+ #define strtod BigDecimal_strtod
24
+ #undef dtoa
25
+ #define dtoa BigDecimal_dtoa
26
+ #undef hdtoa
27
+ #define hdtoa BigDecimal_hdtoa
28
+ #include "missing/dtoa.c"
@@ -0,0 +1,106 @@
1
+ #ifndef MISSING_H
2
+ #define MISSING_H 1
3
+
4
+ #if defined(__cplusplus)
5
+ extern "C" {
6
+ #if 0
7
+ } /* satisfy cc-mode */
8
+ #endif
9
+ #endif
10
+
11
+ #ifndef RB_UNUSED_VAR
12
+ # if defined(_MSC_VER) && _MSC_VER >= 1911
13
+ # define RB_UNUSED_VAR(x) x [[maybe_unused]]
14
+
15
+ # elif defined(__has_cpp_attribute) && __has_cpp_attribute(maybe_unused)
16
+ # define RB_UNUSED_VAR(x) x [[maybe_unused]]
17
+
18
+ # elif defined(__has_c_attribute) && __has_c_attribute(maybe_unused)
19
+ # define RB_UNUSED_VAR(x) x [[maybe_unused]]
20
+
21
+ # elif defined(__GNUC__)
22
+ # define RB_UNUSED_VAR(x) x __attribute__ ((unused))
23
+
24
+ # else
25
+ # define RB_UNUSED_VAR(x) x
26
+ # endif
27
+ #endif /* RB_UNUSED_VAR */
28
+
29
+ #if defined(_MSC_VER) && _MSC_VER >= 1310
30
+ # define HAVE___ASSUME 1
31
+
32
+ #elif defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1300
33
+ # define HAVE___ASSUME 1
34
+ #endif
35
+
36
+ #ifndef UNREACHABLE
37
+ # if __has_builtin(__builtin_unreachable)
38
+ # define UNREACHABLE __builtin_unreachable()
39
+
40
+ # elif defined(HAVE___ASSUME)
41
+ # define UNREACHABLE __assume(0)
42
+
43
+ # else
44
+ # define UNREACHABLE /* unreachable */
45
+ # endif
46
+ #endif /* UNREACHABLE */
47
+
48
+ /* bool */
49
+
50
+ #ifndef __bool_true_false_are_defined
51
+ # include <stdbool.h>
52
+ #endif
53
+
54
+ /* dtoa */
55
+ char *BigDecimal_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);
56
+
57
+ /* complex */
58
+
59
+ #ifndef HAVE_RB_COMPLEX_REAL
60
+ static inline VALUE
61
+ rb_complex_real_fallback(VALUE cmp)
62
+ {
63
+ #ifdef RCOMPLEX
64
+ return RCOMPLEX(cmp)->real;
65
+ #else
66
+ return rb_funcall(cmp, rb_intern("real"), 0);
67
+ #endif
68
+ }
69
+ #define rb_complex_real rb_complex_real_fallback
70
+ #endif
71
+
72
+ #ifndef HAVE_RB_COMPLEX_IMAG
73
+ static inline VALUE
74
+ rb_complex_imag_fallback(VALUE cmp)
75
+ {
76
+ # ifdef RCOMPLEX
77
+ return RCOMPLEX(cmp)->imag;
78
+ # else
79
+ return rb_funcall(cmp, rb_intern("imag"), 0);
80
+ # endif
81
+ }
82
+ #define rb_complex_imag rb_complex_imag_fallback
83
+ #endif
84
+
85
+ /* st */
86
+
87
+ #ifndef ST2FIX
88
+ # undef RB_ST2FIX
89
+ # define RB_ST2FIX(h) LONG2FIX((long)(h))
90
+ # define ST2FIX(h) RB_ST2FIX(h)
91
+ #endif
92
+
93
+ /* warning */
94
+
95
+ #if !defined(HAVE_RB_CATEGORY_WARN) || !defined(HAVE_CONST_RB_WARN_CATEGORY_DEPRECATED)
96
+ # define rb_category_warn(category, ...) rb_warn(__VA_ARGS__)
97
+ #endif
98
+
99
+ #if defined(__cplusplus)
100
+ #if 0
101
+ { /* satisfy cc-mode */
102
+ #endif
103
+ } /* extern "C" { */
104
+ #endif
105
+
106
+ #endif /* MISSING_H */
@@ -0,0 +1,191 @@
1
+ // NTT (Number Theoretic Transform) implementation for BigDecimal multiplication
2
+
3
+ #define NTT_PRIMITIVE_ROOT 17
4
+ #define NTT_PRIME_BASE1 24
5
+ #define NTT_PRIME_BASE2 26
6
+ #define NTT_PRIME_BASE3 29
7
+ #define NTT_PRIME_SHIFT 27
8
+ #define NTT_PRIME1 (((uint32_t)NTT_PRIME_BASE1 << NTT_PRIME_SHIFT) | 1)
9
+ #define NTT_PRIME2 (((uint32_t)NTT_PRIME_BASE2 << NTT_PRIME_SHIFT) | 1)
10
+ #define NTT_PRIME3 (((uint32_t)NTT_PRIME_BASE3 << NTT_PRIME_SHIFT) | 1)
11
+ #define MAX_NTT32_BITS 27
12
+ #define NTT_DECDIG_BASE 1000000000
13
+
14
+ // Calculates base**ex % mod
15
+ static uint32_t
16
+ mod_pow(uint32_t base, uint32_t ex, uint32_t mod) {
17
+ uint32_t res = 1;
18
+ uint32_t bit = 1;
19
+ while (true) {
20
+ if (ex & bit) {
21
+ ex ^= bit;
22
+ res = ((uint64_t)res * base) % mod;
23
+ }
24
+ if (!ex) break;
25
+ base = ((uint64_t)base * base) % mod;
26
+ bit <<= 1;
27
+ }
28
+ return res;
29
+ }
30
+
31
+ // Recursively performs butterfly operations of NTT
32
+ static void
33
+ ntt_recursive(int size_bits, uint32_t *input, uint32_t *output, uint32_t *tmp, int depth, uint32_t r, uint32_t prime) {
34
+ if (depth > 0) {
35
+ ntt_recursive(size_bits, input, tmp, output, depth - 1, ((uint64_t)r * r) % prime, prime);
36
+ } else {
37
+ tmp = input;
38
+ }
39
+ uint32_t size_half = (uint32_t)1 << (size_bits - 1);
40
+ uint32_t stride = (uint32_t)1 << (size_bits - depth - 1);
41
+ uint32_t n = size_half / stride;
42
+ uint32_t rn = 1, rm = prime - 1;
43
+ for (uint32_t i = 0; i < n; i++) {
44
+ uint32_t *aptr = tmp + i * 2 * stride;
45
+ uint32_t *bptr = aptr + stride;
46
+ uint32_t *out1 = output + stride * i;
47
+ uint32_t *out2 = out1 + size_half;
48
+ for (uint32_t k = 0; k < stride; k++) {
49
+ uint32_t a = aptr[k], b = bptr[k];
50
+ out1[k] = (a + (uint64_t)rn * b) % prime;
51
+ out2[k] = (a + (uint64_t)rm * b) % prime;
52
+ }
53
+ rn = ((uint64_t)rn * r) % prime;
54
+ rm = ((uint64_t)rm * r) % prime;
55
+ }
56
+ }
57
+
58
+ /* Perform NTT on input array.
59
+ * base, shift: Represent the prime number as (base << shift | 1)
60
+ * r_base: Primitive root of unity modulo prime
61
+ * size_bits: log2 of the size of the input array. Should be less or equal to shift
62
+ * input: input array of size (1 << size_bits)
63
+ */
64
+ static void
65
+ ntt(int size_bits, uint32_t *input, uint32_t *output, uint32_t *tmp, int r_base, int base, int shift, int dir) {
66
+ uint32_t size = (uint32_t)1 << size_bits;
67
+ uint32_t prime = ((uint32_t)base << shift) | 1;
68
+
69
+ // rmax**(1 << shift) % prime == 1
70
+ // r**size % prime == 1
71
+ uint32_t rmax = mod_pow((uint32_t)r_base, (uint32_t)base, prime);
72
+ uint32_t r = mod_pow(rmax, (uint32_t)1 << (shift - size_bits), prime);
73
+
74
+ if (dir < 0) r = mod_pow(r, prime - 2, prime);
75
+ ntt_recursive(size_bits, input, output, tmp, size_bits - 1, r, prime);
76
+ if (dir < 0) {
77
+ uint32_t n_inv = mod_pow((uint32_t)size, prime - 2, prime);
78
+ for (uint32_t i = 0; i < size; i++) {
79
+ output[i] = ((uint64_t)output[i] * n_inv) % prime;
80
+ }
81
+ }
82
+ }
83
+
84
+ /* Calculate c that satisfies: c % PRIME1 == mod1 && c % PRIME2 == mod2 && c % PRIME3 == mod3
85
+ * c = (mod1 * 35002755423056150739595925972 + mod2 * 14584479687667766215746868453 + mod3 * 37919651490985126265126719818) % (PRIME1 * PRIME2 * PRIME3)
86
+ * Assume c <= 999999999**2*(1<<27)
87
+ */
88
+ static inline void
89
+ mod_restore_prime_24_26_29_shift_27(uint32_t mod1, uint32_t mod2, uint32_t mod3, uint32_t *digits) {
90
+ // Use mixed radix notation to eliminate modulo by PRIME1 * PRIME2 * PRIME3
91
+ // [DIG0, DIG1, DIG2] = DIG0 + DIG1 * PRIME1 + DIG2 * PRIME1 * PRIME2
92
+ // DIG0: 0...PRIME1, DIG1: 0...PRIME2, DIG2: 0...PRIME3
93
+ // 35002755423056150739595925972 = [1, 3489660916, 3113851359]
94
+ // 14584479687667766215746868453 = [0, 13, 1297437912]
95
+ // 37919651490985126265126719818 = [0, 0, 3373338954]
96
+ uint64_t c0 = mod1;
97
+ uint64_t c1 = (uint64_t)mod2 * 13 + (uint64_t)mod1 * 3489660916;
98
+ uint64_t c2 = (uint64_t)mod3 * 3373338954 % NTT_PRIME3 + (uint64_t)mod2 * 1297437912 % NTT_PRIME3 + (uint64_t)mod1 * 3113851359 % NTT_PRIME3;
99
+ c2 += c1 / NTT_PRIME2;
100
+ c1 %= NTT_PRIME2;
101
+ c2 %= NTT_PRIME3;
102
+ // Base conversion. c fits in 3 digits.
103
+ c1 += c2 % NTT_DECDIG_BASE * NTT_PRIME2;
104
+ c0 += c1 % NTT_DECDIG_BASE * NTT_PRIME1;
105
+ c1 /= NTT_DECDIG_BASE;
106
+ digits[0] = c0 % NTT_DECDIG_BASE;
107
+ c0 /= NTT_DECDIG_BASE;
108
+ c1 += c2 / NTT_DECDIG_BASE % NTT_DECDIG_BASE * NTT_PRIME2;
109
+ c0 += c1 % NTT_DECDIG_BASE * NTT_PRIME1;
110
+ c1 /= NTT_DECDIG_BASE;
111
+ digits[1] = c0 % NTT_DECDIG_BASE;
112
+ digits[2] = (uint32_t)(c0 / NTT_DECDIG_BASE + c1 % NTT_DECDIG_BASE * NTT_PRIME1);
113
+ }
114
+
115
+ /*
116
+ * NTT multiplication
117
+ * Uses three NTTs with mod (24 << 27 | 1), (26 << 27 | 1), and (29 << 27 | 1)
118
+ */
119
+ static void
120
+ ntt_multiply(size_t a_size, size_t b_size, uint32_t *a, uint32_t *b, uint32_t *c) {
121
+ if (a_size < b_size) {
122
+ ntt_multiply(b_size, a_size, b, a, c);
123
+ return;
124
+ }
125
+
126
+ int ntt_size_bits = (int)bit_length(b_size - 1) + 1;
127
+ if (ntt_size_bits > MAX_NTT32_BITS) {
128
+ rb_raise(rb_eArgError, "Multiply size too large");
129
+ }
130
+
131
+ // To calculate large_a * small_b faster, split into several batches.
132
+ uint32_t ntt_size = (uint32_t)1 << ntt_size_bits;
133
+ uint32_t batch_size = ntt_size - (uint32_t)b_size;
134
+ uint32_t batch_count = (uint32_t)((a_size + batch_size - 1) / batch_size);
135
+
136
+ uint32_t *mem = ruby_xcalloc(ntt_size * 9, sizeof(uint32_t));
137
+ uint32_t *ntt1 = mem;
138
+ uint32_t *ntt2 = mem + ntt_size;
139
+ uint32_t *ntt3 = mem + ntt_size * 2;
140
+ uint32_t *tmp1 = mem + ntt_size * 3;
141
+ uint32_t *tmp2 = mem + ntt_size * 4;
142
+ uint32_t *tmp3 = mem + ntt_size * 5;
143
+ uint32_t *conv1 = mem + ntt_size * 6;
144
+ uint32_t *conv2 = mem + ntt_size * 7;
145
+ uint32_t *conv3 = mem + ntt_size * 8;
146
+
147
+ // Calculate NTT for b in three primes. Result is reused for each batch of a.
148
+ memcpy(tmp1, b, b_size * sizeof(uint32_t));
149
+ memset(tmp1 + b_size, 0, (ntt_size - b_size) * sizeof(uint32_t));
150
+ ntt(ntt_size_bits, tmp1, ntt1, tmp2, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE1, NTT_PRIME_SHIFT, +1);
151
+ ntt(ntt_size_bits, tmp1, ntt2, tmp2, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE2, NTT_PRIME_SHIFT, +1);
152
+ ntt(ntt_size_bits, tmp1, ntt3, tmp2, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE3, NTT_PRIME_SHIFT, +1);
153
+
154
+ memset(c, 0, (a_size + b_size) * sizeof(uint32_t));
155
+ for (uint32_t idx = 0; idx < batch_count; idx++) {
156
+ uint32_t len = idx == batch_count - 1 ? (uint32_t)a_size - idx * batch_size : batch_size;
157
+ memcpy(tmp1, a + idx * batch_size, len * sizeof(uint32_t));
158
+ memset(tmp1 + len, 0, (ntt_size - len) * sizeof(uint32_t));
159
+ // Calculate convolution for this batch in three primes
160
+ ntt(ntt_size_bits, tmp1, tmp2, tmp3, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE1, NTT_PRIME_SHIFT, +1);
161
+ for (uint32_t i = 0; i < ntt_size; i++) tmp2[i] = ((uint64_t)tmp2[i] * ntt1[i]) % NTT_PRIME1;
162
+ ntt(ntt_size_bits, tmp2, conv1, tmp3, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE1, NTT_PRIME_SHIFT, -1);
163
+ ntt(ntt_size_bits, tmp1, tmp2, tmp3, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE2, NTT_PRIME_SHIFT, +1);
164
+ for (uint32_t i = 0; i < ntt_size; i++) tmp2[i] = ((uint64_t)tmp2[i] * ntt2[i]) % NTT_PRIME2;
165
+ ntt(ntt_size_bits, tmp2, conv2, tmp3, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE2, NTT_PRIME_SHIFT, -1);
166
+ ntt(ntt_size_bits, tmp1, tmp2, tmp3, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE3, NTT_PRIME_SHIFT, +1);
167
+ for (uint32_t i = 0; i < ntt_size; i++) tmp2[i] = ((uint64_t)tmp2[i] * ntt3[i]) % NTT_PRIME3;
168
+ ntt(ntt_size_bits, tmp2, conv3, tmp3, NTT_PRIMITIVE_ROOT, NTT_PRIME_BASE3, NTT_PRIME_SHIFT, -1);
169
+
170
+ // Restore the original convolution value from three convolutions calculated in three primes.
171
+ // Each convolution value is maximum 999999999**2*(1<<27)/2
172
+ for (uint32_t i = 0; i < ntt_size; i++) {
173
+ uint32_t dig[3];
174
+ mod_restore_prime_24_26_29_shift_27(conv1[i], conv2[i], conv3[i], dig);
175
+ // Maximum values of dig[0], dig[1], and dig[2] are 999999999, 999999999 and 67108863 respectively
176
+ // Maximum overlapped sum (considering overlaps between 2 batches) is less than 4134217722
177
+ // so this sum doesn't overflow uint32_t.
178
+ for (int j = 0; j < 3; j++) {
179
+ // Index check: if dig[j] is non-zero, assign index is within valid range.
180
+ if (dig[j]) c[idx * batch_size + i + 1 - (uint32_t)j] += dig[j];
181
+ }
182
+ }
183
+ }
184
+ uint32_t carry = 0;
185
+ for (int32_t i = (int32_t)(a_size + b_size - 1); i >= 0; i--) {
186
+ uint32_t v = c[i] + carry;
187
+ c[i] = v % NTT_DECDIG_BASE;
188
+ carry = v / NTT_DECDIG_BASE;
189
+ }
190
+ ruby_xfree(mem);
191
+ }
@@ -0,0 +1,54 @@
1
+ #ifndef BIGDECIMAL_STATIC_ASSERT_H
2
+ #define BIGDECIMAL_STATIC_ASSERT_H
3
+
4
+ #include "feature.h"
5
+
6
+ #ifdef HAVE_RUBY_INTERNAL_STATIC_ASSERT_H
7
+ # include <ruby/internal/static_assert.h>
8
+ #endif
9
+
10
+ #ifdef RBIMPL_STATIC_ASSERT
11
+ # define STATIC_ASSERT RBIMPL_STATIC_ASSERT
12
+ #endif
13
+
14
+ #ifndef STATIC_ASSERT
15
+ # /* The following section is copied from CRuby's static_assert.h */
16
+
17
+ # if defined(__cplusplus) && defined(__cpp_static_assert)
18
+ # /* https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations */
19
+ # define BIGDECIMAL_STATIC_ASSERT0 static_assert
20
+
21
+ # elif defined(__cplusplus) && defined(_MSC_VER) && _MSC_VER >= 1600
22
+ # define BIGDECIMAL_STATIC_ASSERT0 static_assert
23
+
24
+ # elif defined(__INTEL_CXX11_MODE__)
25
+ # define BIGDECIMAL_STATIC_ASSERT0 static_assert
26
+
27
+ # elif defined(__cplusplus) && __cplusplus >= 201103L
28
+ # define BIGDECIMAL_STATIC_ASSERT0 static_assert
29
+
30
+ # elif defined(__cplusplus) && __has_extension(cxx_static_assert)
31
+ # define BIGDECIMAL_STATIC_ASSERT0 __extension__ static_assert
32
+
33
+ # elif defined(__STDC_VERSION__) && __has_extension(c_static_assert)
34
+ # define BIGDECIMAL_STATIC_ASSERT0 __extension__ _Static_assert
35
+
36
+ # elif defined(__STDC_VERSION__) && defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
37
+ # define BIGDECIMAL_STATIC_ASSERT0 __extension__ _Static_assert
38
+ #endif
39
+
40
+ # if defined(__DOXYGEN__)
41
+ # define STATIC_ASSERT static_assert
42
+
43
+ # elif defined(BIGDECIMAL_STATIC_ASSERT0)
44
+ # define STATIC_ASSERT(name, expr) \
45
+ BIGDECIMAL_STATIC_ASSERT0(expr, #name ": " #expr)
46
+
47
+ # else
48
+ # define STATIC_ASSERT(name, expr) \
49
+ typedef int static_assert_ ## name ## _check[1 - 2 * !(expr)]
50
+ # endif
51
+ #endif /* STATIC_ASSERT */
52
+
53
+
54
+ #endif /* BIGDECIMAL_STATIC_ASSERT_H */
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'bigdecimal'
4
4
 
5
+ warn "'bigdecimal/jacobian' is deprecated and will be removed in a future release."
6
+
5
7
  # require 'bigdecimal/jacobian'
6
8
  #
7
9
  # Provides methods to compute the Jacobian matrix of a set of equations at a
@@ -42,8 +44,8 @@ module Jacobian
42
44
  end
43
45
 
44
46
 
45
- # Computes the derivative of f[i] at x[i].
46
- # fx is the value of f at x.
47
+ # Computes the derivative of +f[i]+ at +x[i]+.
48
+ # +fx+ is the value of +f+ at +x+.
47
49
  def dfdxi(f,fx,x,i)
48
50
  nRetry = 0
49
51
  n = x.size
@@ -75,7 +77,7 @@ module Jacobian
75
77
  deriv
76
78
  end
77
79
 
78
- # Computes the Jacobian of f at x. fx is the value of f at x.
80
+ # Computes the Jacobian of +f+ at +x+. +fx+ is the value of +f+ at +x+.
79
81
  def jacobian(f,fx,x)
80
82
  n = x.size
81
83
  dfdx = Array.new(n*n)
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: false
2
2
  require 'bigdecimal'
3
3
 
4
+ warn "'bigdecimal/ludcmp' is deprecated and will be removed in a future release."
5
+
4
6
  #
5
7
  # Solves a*x = b for x, using LU decomposition.
6
8
  #