From 78b980767a11a347c915c353caf9057d38f549d3 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sun, 27 Jul 2025 22:39:59 +0900 Subject: [PATCH] mruby-bigint: migrate sliding window multiplication to new context architecture Convert mpz_mul_sliding_window from legacy MPZ_UNIFIED_BINARY_OP_INT macro to new strategy using mpz_init_temp/mpz_init_auto pattern. Inline core function and remove unused legacy macros and functions for cleaner implementation. Co-authored-by: Claude --- mrbgems/mruby-bigint/core/bigint.c | 87 ------------------------------ 1 file changed, 87 deletions(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index 90cfcb941..cccf7d1cd 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -60,93 +60,6 @@ static int mpz_mul_sliding_window(mpz_ctx_t *ctx, mpz_t *result, mpz_t *first, m pool_name##_storage.active = 0; \ } while(0) -/* Pool memory management helper macros */ -#define MPZ_POOL_ALLOC_GOTO(ctx, var, pool, size, label) do { \ - mpz_init_pool(ctx, &(var), pool, size); \ - if (!is_pool_memory(&(var), pool)) { \ - mpz_clear_pool(ctx, &(var), pool); \ - goto label; \ - } \ -} while(0) - -#define MPZ_POOL_VERIFY(var, pool) is_pool_memory(&(var), pool) - -#define MPZ_POOL_VERIFY_2(var1, var2, pool) \ - (MPZ_POOL_VERIFY(var1, pool) && MPZ_POOL_VERIFY(var2, pool)) - -#define MPZ_POOL_VERIFY_3(var1, var2, var3, pool) \ - (MPZ_POOL_VERIFY(var1, pool) && MPZ_POOL_VERIFY(var2, pool) && MPZ_POOL_VERIFY(var3, pool)) - -#define MPZ_POOL_VERIFY_4(var1, var2, var3, var4, pool) \ - (MPZ_POOL_VERIFY(var1, pool) && MPZ_POOL_VERIFY(var2, pool) && \ - MPZ_POOL_VERIFY(var3, pool) && MPZ_POOL_VERIFY(var4, pool)) - -#define MPZ_POOL_VERIFY_6(var1, var2, var3, var4, var5, var6, pool) \ - (MPZ_POOL_VERIFY(var1, pool) && MPZ_POOL_VERIFY(var2, pool) && \ - MPZ_POOL_VERIFY(var3, pool) && MPZ_POOL_VERIFY(var4, pool) && \ - MPZ_POOL_VERIFY(var5, pool) && MPZ_POOL_VERIFY(var6, pool)) - -/* Unified pool+heap helper macros for eliminating function duplication */ - -/* Copy result from pool to heap-allocated destination */ -#define MPZ_COPY_FROM_POOL(ctx, dest, src, pool) do { \ - if (is_pool_memory(&(src), pool)) { \ - trim(&(src)); \ - mpz_realloc(ctx, dest, (src).sz); \ - for (size_t i = 0; i < (src).sz; i++) { \ - (dest)->p[i] = (src).p[i]; \ - } \ - (dest)->sz = (src).sz; \ - (dest)->sn = (src).sn; \ - } \ -} while(0) - -/* Unified operation using existing *_core functions */ -/* For binary operations: mpz_operation(result, op1, op2) */ -#define MPZ_UNIFIED_BINARY_OP(ctx, core_func, result, op1, op2, estimated_size) do { \ - WITH_SCOPED_POOL(pool, { \ - mpz_t temp_result; \ - MPZ_POOL_ALLOC_GOTO(ctx, temp_result, pool, estimated_size, _pool_failed); \ - /* Pool allocation successful */ \ - core_func(&temp_result, op1, op2); \ - MPZ_COPY_FROM_POOL(ctx, result, temp_result, pool); \ - return; \ - _pool_failed: ; \ - }); \ - /* Pool failed - use heap allocation */ \ - core_func(result, op1, op2); \ -} while(0) - -/* For unary operations: mpz_operation(result, operand) */ -#define MPZ_UNIFIED_UNARY_OP(ctx, core_func, result, operand, estimated_size) do { \ - WITH_SCOPED_POOL(pool, { \ - mpz_t temp_result; \ - MPZ_POOL_ALLOC_GOTO(ctx, temp_result, pool, estimated_size, _pool_failed); \ - /* Pool allocation successful */ \ - core_func(&temp_result, operand); \ - MPZ_COPY_FROM_POOL(ctx, result, temp_result, pool); \ - return; \ - _pool_failed: ; \ - }); \ - /* Pool failed - use heap allocation */ \ - core_func(result, operand); \ -} while(0) - -/* For binary operations that return int: mpz_operation(result, op1, op2) -> int */ -#define MPZ_UNIFIED_BINARY_OP_INT(ctx, core_func, result, op1, op2, estimated_size, success_value) do { \ - WITH_SCOPED_POOL(pool, { \ - mpz_t temp_result; \ - MPZ_POOL_ALLOC_GOTO(ctx, temp_result, pool, estimated_size, _pool_failed); \ - /* Pool allocation successful */ \ - core_func(&temp_result, op1, op2); \ - MPZ_COPY_FROM_POOL(ctx, result, temp_result, pool); \ - return success_value; \ - _pool_failed: ; \ - }); \ - /* Pool failed - use heap allocation */ \ - core_func(result, op1, op2); \ - return success_value; \ -} while(0) /* Pool allocation functions */ static mp_limb*