From 51bc27be0188d4950421dba3a06d39ee73ba3893 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 12 Jul 2026 00:04:35 +0100 Subject: [PATCH] Fix `big_int_shr` for arithmetic shift (i.e. negative numbers) --- src/big_int.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/big_int.cpp b/src/big_int.cpp index a8ea2079b..f59dccf24 100644 --- a/src/big_int.cpp +++ b/src/big_int.cpp @@ -353,9 +353,15 @@ gb_internal void big_int_shl(BigInt *dst, BigInt const *x, BigInt const *y) { gb_internal void big_int_shr(BigInt *dst, BigInt const *x, BigInt const *y) { u32 yy = mp_get_u32(y); - BigInt d = {}; - mp_div_2d(x, yy, dst, &d); - big_int_dealloc(&d); + + BigInt rem = {}; + defer (mp_clear(&rem)); + + mp_div_2d(x, yy, dst, &rem); + + if (mp_isneg(x) && !mp_iszero(&rem)) { + mp_sub_d(dst, 1, dst); + } } gb_internal void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y) {