From f08319ed723eec36819027c33129a1324191c39c Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 25 Jul 2025 15:42:49 +0900 Subject: [PATCH] mruby-bigint: remove mpz_cmp_pool and use mpz_cmp directly Comparison operations don't need memory allocation, so there's no difference between pool and non-pool versions. This eliminates unnecessary code duplication. Co-authored-by: Claude --- mrbgems/mruby-bigint/core/bigint.c | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index 6751519ae..27d666318 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -3151,28 +3151,6 @@ mpz_sub_pool(mrb_state *mrb, mpz_t *result, mpz_t *a, mpz_t *b, mpz_pool_t *pool return success; } -static int mpz_cmp_pool(mrb_state *mrb, mpz_t *a, mpz_t *b, mpz_pool_t *pool) { - /* Simple comparison - doesn't need pool operations */ - if (a->sn != b->sn) { - return (a->sn > b->sn) ? 1 : -1; - } - - if (a->sz != b->sz) { - int size_cmp = (a->sz > b->sz) ? 1 : -1; - return (a->sn >= 0) ? size_cmp : -size_cmp; - } - - /* Compare limbs from most significant */ - for (size_t i = a->sz; i > 0; i--) { - size_t idx = i - 1; - if (a->p[idx] != b->p[idx]) { - int limb_cmp = (a->p[idx] > b->p[idx]) ? 1 : -1; - return (a->sn >= 0) ? limb_cmp : -limb_cmp; - } - } - - return 0; /* Equal */ -} /* Pool-based GCD using binary GCD algorithm with Lehmer acceleration */ static int @@ -3383,8 +3361,8 @@ mpz_gcd_pool(mrb_state *mrb, mpz_t *gg, mpz_t *aa, mpz_t *bb) goto cleanup_gcd; } - /* Ensure a >= b after transformation using pool-based comparison */ - if (mpz_cmp_pool(mrb, &a, &b, pool) < 0) { + /* Ensure a >= b after transformation */ + if (mpz_cmp(mrb, &a, &b) < 0) { /* In-place swap - just swap the mpz_t structures */ mpz_t temp_holder = a; a = b; @@ -3407,7 +3385,7 @@ mpz_gcd_pool(mrb_state *mrb, mpz_t *gg, mpz_t *aa, mpz_t *bb) } /* Now both a and b are odd. Ensure a >= b */ - if (mpz_cmp_pool(mrb, &a, &b, pool) < 0) { + if (mpz_cmp(mrb, &a, &b) < 0) { /* In-place swap without temporary variable */ mpz_t temp_holder = a; a = b;