[libc-commits] [libc] [libc] Fix BigInt's operator %= (PR #98484)

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Thu Jul 11 06:59:41 PDT 2024


https://github.com/mikhailramalho created https://github.com/llvm/llvm-project/pull/98484

This patch fixes cases where we try to do var %= 1. Previous this operator was calling .div directly since it would perform the inplace division and return the remainder, however, as an early exit condition a division by one returns zero as the remainder. The remainder being returned by div was not being assigned to var.

>From 7ad3746b27ed84700206dcd33f528b5fd773b142 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Wed, 10 Jul 2024 22:24:50 -0300
Subject: [PATCH] [libc] Fix BigInt's operator %=

This patch fixes cases where we try to do var %= 1. Previous this
operator was calling .div directly since it would perform the inplace
division and return the remainder, however, as an early exit condition a
division by one returns zero as the remainder. The remainder being
returned by div was not being assigned to var.
---
 libc/src/__support/big_int.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 82a5251418854..8eeb4db9d650b 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -732,7 +732,8 @@ struct BigInt {
   }
 
   LIBC_INLINE constexpr BigInt operator%=(const BigInt &other) {
-    return *this->div(other);
+    *this = *this % other;
+    return *this;
   }
 
   LIBC_INLINE constexpr BigInt &operator*=(const BigInt &other) {



More information about the libc-commits mailing list