From 8c9b57fadd3829f663c447ba8597f3fedbaa87fc Mon Sep 17 00:00:00 2001 From: Manuel Serrano Date: Thu, 12 Dec 2024 10:17:17 +0100 Subject: [PATCH] Fix generic bignums overflow (bootstrap) --- runtime/Clib/cbignum.c | 7 ++++--- runtime/Unsafe/bignumber-generic.sch | 8 ++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/runtime/Clib/cbignum.c b/runtime/Clib/cbignum.c index ebf55d2d..581c561a 100644 --- a/runtime/Clib/cbignum.c +++ b/runtime/Clib/cbignum.c @@ -3,7 +3,7 @@ /* ------------------------------------------------------------- */ /* Author : José Romildo Malaquias */ /* Creation : Fri Nov 10 11:51:17 2006 */ -/* Last change : Fri Nov 15 07:36:49 2024 (serrano) */ +/* Last change : Thu Dec 12 09:52:45 2024 (serrano) */ /* Copyright : 2003-24 Manuel Serrano */ /* ------------------------------------------------------------- */ /* C implementation of bignum */ @@ -846,10 +846,11 @@ obj_t bgl_safe_bignum_to_fixnum(obj_t bx) { size_t bs = mpz_sizeinbase(&(BIGNUM(bx).mpz), 2); - if (bs < BGL_INT_BIT_SIZE) + if (bs < BGL_INT_BIT_SIZE) { return BINT(bgl_bignum_to_long(bx)); - else + } else { return bx; + } } /*---------------------------------------------------------------------*/ diff --git a/runtime/Unsafe/bignumber-generic.sch b/runtime/Unsafe/bignumber-generic.sch index e7e947f0..77e7e928 100644 --- a/runtime/Unsafe/bignumber-generic.sch +++ b/runtime/Unsafe/bignumber-generic.sch @@ -3,7 +3,7 @@ ;* ------------------------------------------------------------- */ ;* Author : Manuel Serrano */ ;* Creation : Thu Aug 29 07:41:07 2024 */ -;* Last change : Thu Dec 12 08:48:41 2024 (serrano) */ +;* Last change : Thu Dec 12 10:10:18 2024 (serrano) */ ;* Copyright : 2024 Manuel Serrano */ ;* ------------------------------------------------------------- */ ;* Portable implementation of bignums. This is used only when no */ @@ -454,7 +454,11 @@ ;*---------------------------------------------------------------------*/ ;* Overflow operations ... */ ;*---------------------------------------------------------------------*/ -(define ($bignum->fixnum-safe x) (or ($bignum->fixnum x) x)) +(define ($bignum->fixnum-safe x) + (let ((n ($bignum->fixnum x))) + (if (and n (=bx (fixnum->bignum n) x)) + n + x))) (define ($+fx-safe x y) (+fx/ov x y)) (define ($-fx-safe x y) (-fx/ov x y)) (define ($*fx-safe x y) (*fx/ov x y))