From ea7843edd43a2d1c9ee182391ab48096d3d86f5e Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 31 Jul 2025 15:18:42 +0900 Subject: [PATCH] mruby-bigint: replace mpz_init_capa with mpz_init_heap Co-authored-by: Gemini --- mrbgems/mruby-bigint/core/bigint.c | 64 +++++++++++++++--------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index d900057b2..58a458add 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -100,9 +100,9 @@ mpz_init(mpz_ctx_t *ctx, mpz_t *s) /* New simplified API - temporary names during migration */ -/* Heap-preferred allocation (future: mpz_init) */ +/* Heap-preferred allocation */ static void -mpz_init_capa(mpz_ctx_t *ctx, mpz_t *s, size_t hint) +mpz_init_heap(mpz_ctx_t *ctx, mpz_t *s, size_t hint) { s->sn = 0; if (hint > 0) { @@ -136,10 +136,10 @@ mpz_init_temp(mpz_ctx_t *ctx, mpz_t *s, size_t hint) } } /* Fallback to heap allocation */ - mpz_init_capa(ctx, s, hint); + mpz_init_heap(ctx, s, hint); } #else -#define mpz_init_temp(ctx, s, hint) mpz_init_capa(ctx, s, hint) +#define mpz_init_temp(ctx, s, hint) mpz_init_heap(ctx, s, hint) #endif /* Check if mpz_t uses pool memory */ @@ -539,7 +539,7 @@ mpz_add(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mpz_t *y) if (y->sz == 1 && x->sz > 1) { mp_limb y_limb = y->p[0]; mpz_t z; - mpz_init_capa(ctx, &z, x->sz + 1); + mpz_init_heap(ctx, &z, x->sz + 1); if ((x->sn > 0 && y->sn > 0) || (x->sn < 0 && y->sn < 0)) { /* Same signs: addition */ @@ -608,7 +608,7 @@ mpz_add(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mpz_t *y) mpz_t z; size_t estimated_size = ((x->sz > y->sz) ? x->sz : y->sz) + 1; - mpz_init_capa(ctx, &z, estimated_size); + mpz_init_heap(ctx, &z, estimated_size); if (x->sn > 0 && y->sn > 0) { uadd(&z, x, y); @@ -927,7 +927,7 @@ mpz_mul_basic(mpz_ctx_t *ctx, mpz_t *ww, mpz_t *u, mpz_t *v) if (b->sz == 1) { mp_limb scalar = b->p[0]; mpz_t w; - mpz_init_capa(ctx, &w, a->sz + 1); + mpz_init_heap(ctx, &w, a->sz + 1); limb_zero(w.p, a->sz + 1); mp_limb carry = limb_addmul_1(w.p, a->p, a->sz, scalar); @@ -940,7 +940,7 @@ mpz_mul_basic(mpz_ctx_t *ctx, mpz_t *ww, mpz_t *u, mpz_t *v) } mpz_t w; - mpz_init_capa(ctx, &w, a->sz + b->sz); + mpz_init_heap(ctx, &w, a->sz + b->sz); limb_zero(w.p, a->sz + b->sz); for (size_t j = 0; j < a->sz; j++) { @@ -1224,7 +1224,7 @@ urshift(mpz_ctx_t *ctx, mpz_t *c1, mpz_t *a, size_t n) mp_limb cc = 0; mp_dbl_limb rm = (((mp_dbl_limb)1<sz); + mpz_init_heap(ctx, &c, a->sz); for (size_t i=a->sz-1;; i--) { c.p[i] = ((a->p[i] >> n) | cc) & DIG_MASK; cc = (a->p[i] & rm) << (DIG_SIZE - n); @@ -1251,7 +1251,7 @@ ulshift(mpz_ctx_t *ctx, mpz_t *c1, mpz_t *a, size_t n) mpz_t c; mp_limb rm = (((mp_dbl_limb)1<sz+1); + mpz_init_heap(ctx, &c, a->sz+1); size_t i; for (i=0; isz; i++) { @@ -1311,7 +1311,7 @@ div_limb(mpz_ctx_t *ctx, mpz_t *q, mpz_t *r, mpz_t *x, mp_limb d) } else { size_t new_size = x->sz - limb_shift; - mpz_init_capa(ctx, &temp_q, new_size); + mpz_init_heap(ctx, &temp_q, new_size); mpz_init(ctx, &temp_r); if (bit_shift == 0) { @@ -1349,8 +1349,8 @@ div_limb(mpz_ctx_t *ctx, mpz_t *q, mpz_t *r, mpz_t *x, mp_limb d) /* General single-limb division */ if (x->sz == 1) { /* Both dividend and divisor are single limb */ - mpz_init_capa(ctx, &temp_q, 1); - mpz_init_capa(ctx, &temp_r, 1); + mpz_init_heap(ctx, &temp_q, 1); + mpz_init_heap(ctx, &temp_r, 1); temp_q.p[0] = x->p[0] / d; temp_r.p[0] = x->p[0] % d; @@ -1367,8 +1367,8 @@ div_limb(mpz_ctx_t *ctx, mpz_t *q, mpz_t *r, mpz_t *x, mp_limb d) /* Multi-limb dividend, single-limb divisor */ size_t n = x->sz; - mpz_init_capa(ctx, &temp_q, n); - mpz_init_capa(ctx, &temp_r, 1); + mpz_init_heap(ctx, &temp_q, n); + mpz_init_heap(ctx, &temp_r, 1); mp_dbl_limb remainder = 0; @@ -1718,7 +1718,7 @@ mpz_mod(mpz_ctx_t *ctx, mpz_t *r, mpz_t *x, mpz_t *y) mpz_t mu; mpz_init_temp(ctx, &mu, y->sz + 1); mpz_barrett_mu(ctx, &mu, y); - mpz_init_capa(ctx, r, y->sz); + mpz_init_heap(ctx, r, y->sz); mpz_barrett_reduce(ctx, r, x, y, &mu); r->sn = sn; if (uzero_p(r)) @@ -1730,7 +1730,7 @@ mpz_mod(mpz_ctx_t *ctx, mpz_t *r, mpz_t *x, mpz_t *y) /* General division fallback */ mpz_t q; mpz_init_temp(ctx, &q, x->sz); - mpz_init_capa(ctx, r, y->sz); + mpz_init_heap(ctx, r, y->sz); udiv(ctx, &q, r, x, y); r->sn = sn; if (uzero_p(r)) @@ -2065,7 +2065,7 @@ mpz_mul_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e) size_t bs = e % DIG_SIZE; mpz_t y; - mpz_init_capa(ctx, &y, x->sz+digs); + mpz_init_heap(ctx, &y, x->sz+digs); for (size_t i=0;isz;i++) y.p[i+digs] = x->p[i]; if (bs) { @@ -2084,7 +2084,7 @@ mpz_div_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e) { short sn = x->sn; if (e == 0) { - mpz_init_capa(ctx, z, x->sz); + mpz_init_heap(ctx, z, x->sz); mpz_set(ctx, z, x); } else { @@ -2098,7 +2098,7 @@ mpz_div_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e) for (size_t i = 0; i < x->sz - digs; i++) y.p[i] = x->p[i + digs]; if (bs) { - mpz_init_capa(ctx, z, new_size); + mpz_init_heap(ctx, z, new_size); urshift(ctx, z, &y, bs); mpz_clear(ctx, &y); } @@ -2116,7 +2116,7 @@ mpz_div_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e) static void mpz_neg(mpz_ctx_t *ctx, mpz_t *x, mpz_t *y) { - mpz_init_capa(ctx, x, y->sz); + mpz_init_heap(ctx, x, y->sz); mpz_set(ctx, x, y); x->sn = -(y->sn); } @@ -2137,14 +2137,14 @@ mpz_mod_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e) if (eint >= sz) { /* x < 2^e, so x mod 2^e = x */ - mpz_init_capa(ctx, z, x->sz); + mpz_init_heap(ctx, z, x->sz); mpz_set(ctx, z, x); return; } /* Need to mask off high bits */ size_t result_sz = eint + (bs > 0 ? 1 : 0); - mpz_init_capa(ctx, z, result_sz); + mpz_init_heap(ctx, z, result_sz); mpz_realloc(ctx, z, result_sz); z->sn = x->sn; z->sz = result_sz; @@ -2176,7 +2176,7 @@ mpz_and(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mpz_t *y) mrb_assert(x->sz > 0 || y->sz > 0); size_t max_sz = (x->sz > y->sz) ? x->sz : y->sz; - mpz_init_capa(ctx, z, max_sz); + mpz_init_heap(ctx, z, max_sz); mpz_realloc(ctx, z, max_sz); z->sn = (x->sn == y->sn) ? x->sn : 1; @@ -2197,19 +2197,19 @@ static void mpz_or(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mpz_t *y) /* not the most efficient way to do this */ { if (zero_p(x)) { - mpz_init_capa(ctx, z, y->sz); + mpz_init_heap(ctx, z, y->sz); mpz_set(ctx, z, y); return; } if (zero_p(y)) { - mpz_init_capa(ctx, z, x->sz); + mpz_init_heap(ctx, z, x->sz); mpz_set(ctx, z, x); return; } mrb_assert(x->sz > 0 || y->sz > 0); size_t max_sz = (x->sz > y->sz) ? x->sz : y->sz; - mpz_init_capa(ctx, z, max_sz); + mpz_init_heap(ctx, z, max_sz); mpz_realloc(ctx, z, max_sz); z->sn = (x->sn == y->sn) ? x->sn : -1; @@ -2230,19 +2230,19 @@ static void mpz_xor(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mpz_t *y) /* not the most efficient way to do this */ { if (zero_p(x)) { - mpz_init_capa(ctx, z, y->sz); + mpz_init_heap(ctx, z, y->sz); mpz_set(ctx, z, y); return; } if (zero_p(y)) { - mpz_init_capa(ctx, z, x->sz); + mpz_init_heap(ctx, z, x->sz); mpz_set(ctx, z, x); return; } mrb_assert(x->sz > 0 || y->sz > 0); size_t max_sz = (x->sz > y->sz) ? x->sz : y->sz; - mpz_init_capa(ctx, z, max_sz); + mpz_init_heap(ctx, z, max_sz); mpz_realloc(ctx, z, max_sz); z->sn = (x->sn == y->sn) ? 1 : -1; @@ -2443,7 +2443,7 @@ mpz_abs_copy(mpz_ctx_t *ctx, mpz_t *result, mpz_t *operand) { static void mpz_abs(mpz_ctx_t *ctx, mpz_t *x, mpz_t *y) { - mpz_init_capa(ctx, x, y->sz); + mpz_init_heap(ctx, x, y->sz); mpz_realloc(ctx, x, y->sz); mpz_abs_copy(ctx, x, y); } @@ -3629,7 +3629,7 @@ mrb_bint_from_bytes(mrb_state *mrb, const uint8_t *bytes, mrb_int len) size_t limb_len = (len + sizeof(mp_limb) - 1) / sizeof(mp_limb); MPZ_CTX_INIT(mrb, ctx, pool); - mpz_init_capa(ctx, &z, limb_len); + mpz_init_heap(ctx, &z, limb_len); memcpy(z.p, bytes, len); z.sn = (len > 0) ? 1 : 0; z.sz = limb_len;