summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-10-11 17:33:59 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-10-12 00:45:14 +0900
commit373b399823e0c5ab315df76f82467fca27ec40d6 (patch)
tree5fd82d3bce6be409d40c7436ea0d293416bf37b2 /bignum.c
parent6fa1af7ee5ab88d0be5ae61f5956f7b51b552ee0 (diff)
bary_mul_balance_with_mulfunc: move working buffer allocation
Move the allocation of working buffer before the loop.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4949
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/bignum.c b/bignum.c
index 1f2913da44..c74df3f4da 100644
--- a/bignum.c
+++ b/bignum.c
@@ -1655,6 +1655,14 @@ bary_mul_balance_with_mulfunc(BDIGIT *const zds, const size_t zn,
BDIGITS_ZERO(zds, xn);
+ if (wn < xn) {
+ const size_t r = (yn % xn) ? (yn % xn) : xn;
+ if ((2 * xn + yn + r) > zn) {
+ wn = xn;
+ wds = ALLOCV_N(BDIGIT, work, wn);
+ }
+ }
+
n = 0;
while (yn > n) {
const size_t r = (xn > (yn - n) ? (yn - n) : xn);
@@ -1670,8 +1678,13 @@ bary_mul_balance_with_mulfunc(BDIGIT *const zds, const size_t zn,
else {
BDIGIT *const tds = zds + n;
if (wn < xn) {
+ /* xn is invariant, only once here */
+#if 0
wn = xn;
wds = ALLOCV_N(BDIGIT, work, wn);
+#else
+ rb_bug("wds is not enough: %" PRIdSIZE " for %" PRIdSIZE, wn, xn);
+#endif
}
MEMCPY(wds, zds + n, BDIGIT, xn);
mulfunc(tds, tn, xds, xn, yds + n, r, wds+xn, wn-xn);