[libc-commits] [libc] c4b7e8a - [libc] fix shifting exact multiples of 64 in uint
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Thu Dec 15 13:01:37 PST 2022
Author: Michael Jones
Date: 2022-12-15T13:01:32-08:00
New Revision: c4b7e8a035d524c797bed79b3c7aeae0707e07b5
URL: https://github.com/llvm/llvm-project/commit/c4b7e8a035d524c797bed79b3c7aeae0707e07b5
DIFF: https://github.com/llvm/llvm-project/commit/c4b7e8a035d524c797bed79b3c7aeae0707e07b5.diff
LOG: [libc] fix shifting exact multiples of 64 in uint
The internal uint class had a bug introduced recently when optimizing
the shift routines. When calculating the value of a block, it would
shift an adjacent block by 64 - the shift amount. If the shift amount
was 0, this would be a shift of 64, which is undefined for a 64 bit
integer. This patch fixes this by adding a conditional to catch this
case.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D140070
Added:
Modified:
libc/src/__support/UInt.h
libc/test/src/__support/uint_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index e4d8a8f580ec0..7bb32b9037432 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -364,7 +364,7 @@ template <size_t Bits> struct UInt {
val[1] = uint64_t(tmp >> 64);
return;
}
-#endif // __SIZEOF_INT128__
+#endif // __SIZEOF_INT128__
if (unlikely(s == 0))
return;
@@ -374,11 +374,17 @@ template <size_t Bits> struct UInt {
if (drop < WordCount) {
i = WordCount - 1;
- size_t j = WordCount - 1 - drop;
- for (; j > 0; --i, --j) {
- val[i] = (val[j] << shift) | (val[j - 1] >> (64 - shift));
+ if (shift > 0) {
+ for (size_t j = WordCount - 1 - drop; j > 0; --i, --j) {
+ val[i] = (val[j] << shift) | (val[j - 1] >> (64 - shift));
+ }
+ val[i] = val[0] << shift;
+ } else {
+ for (size_t j = WordCount - 1 - drop; j > 0; --i, --j) {
+ val[i] = val[j];
+ }
+ val[i] = val[0];
}
- val[i] = val[0] << shift;
}
for (size_t j = 0; j < i; ++j) {
diff --git a/libc/test/src/__support/uint_test.cpp b/libc/test/src/__support/uint_test.cpp
index 3ee20470f2821..80b4e09b12e64 100644
--- a/libc/test/src/__support/uint_test.cpp
+++ b/libc/test/src/__support/uint_test.cpp
@@ -326,6 +326,10 @@ TEST(LlvmLibcUIntClassTest, ShiftLeftTests) {
LL_UInt128 result6({0, 0});
EXPECT_EQ((val2 << 128), result6);
EXPECT_EQ((val2 << 256), result6);
+
+ LL_UInt192 val3({1, 0, 0});
+ LL_UInt192 result7({0, 1, 0});
+ EXPECT_EQ((val3 << 64), result7);
}
TEST(LlvmLibcUIntClassTest, ShiftRightTests) {
@@ -363,6 +367,10 @@ TEST(LlvmLibcUIntClassTest, ShiftRightTests) {
EXPECT_EQ((v2 >> 64), r2);
EXPECT_EQ((v2 >> 128), r3);
EXPECT_EQ((r2 >> 64), r3);
+
+ LL_UInt192 val3({0, 0, 1});
+ LL_UInt192 result7({0, 1, 0});
+ EXPECT_EQ((val3 >> 64), result7);
}
TEST(LlvmLibcUIntClassTest, AndTests) {
More information about the libc-commits
mailing list