Skip to content

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
@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