Skip to content

Commit 31bbc8b

Browse files
committed
[libc] multiply and divide by 2 when INT_MIN
Signed-off-by: Shreeyash Pandey <[email protected]>
1 parent f5ff751 commit 31bbc8b

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

libc/src/__support/fixed_point/fx_bits.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#include "fx_rep.h"
2424

25-
#include <stdio.h>
26-
2725
#ifdef LIBC_COMPILER_HAS_FIXED_POINT
2826

2927
namespace LIBC_NAMESPACE_DECL {
@@ -255,7 +253,8 @@ template <typename XType> LIBC_INLINE constexpr XType divi(int n, int d) {
255253
uint64_t dv = static_cast<uint64_t>(d64 < 0 ? -d64 : d64);
256254

257255
if (d == INT_MIN) {
258-
dv = dv - 1; // Two's complement
256+
nv <<= 1;
257+
dv >>= 1;
259258
}
260259

261260
uint32_t clz = cpp::countl_zero<uint32_t>(static_cast<uint32_t>(dv)) - 1;

libc/test/src/stdfix/DivITest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class DivITest : public LIBC_NAMESPACE::testing::Test {
6262

6363
EXPECT_EQ(func(INT_MIN, 1), FRACT_MIN);
6464
EXPECT_LT(func(1, INT_MIN) - 0.r, epsilon);
65+
66+
EXPECT_EQ(func(INT_MIN, INT_MIN), 1.r);
6567
}
6668
};
6769

0 commit comments

Comments
 (0)