Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc] Fix BigInt's operator %= #98484

Merged
merged 1 commit into from
Jul 11, 2024
Merged

Conversation

mikhailramalho
Copy link
Member

@mikhailramalho mikhailramalho commented Jul 11, 2024

This patch fixes cases where we try to do var %= 1. Previously 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.

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.
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-libc

Author: Mikhail R. Gadelha (mikhailramalho)

Changes

This patch fixes cases where we try to do var %= 1. Previously 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.


Full diff: https://github.com/llvm/llvm-project/pull/98484.diff

1 Files Affected:

  • (modified) libc/src/__support/big_int.h (+2-1)
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) {

@mikhailramalho mikhailramalho merged commit dffa28f into llvm:main Jul 11, 2024
6 of 7 checks passed
@mikhailramalho mikhailramalho deleted the fix-fmod branch July 11, 2024 14:43
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
This patch fixes cases where we try to do var %= 1. Previously 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants