diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h --- a/libc/src/__support/UInt.h +++ b/libc/src/__support/UInt.h @@ -364,7 +364,7 @@ val[1] = uint64_t(tmp >> 64); return; } -#endif // __SIZEOF_INT128__ +#endif // __SIZEOF_INT128__ if (unlikely(s == 0)) return; @@ -376,7 +376,8 @@ i = WordCount - 1; size_t j = WordCount - 1 - drop; for (; j > 0; --i, --j) { - val[i] = (val[j] << shift) | (val[j - 1] >> (64 - shift)); + val[i] = + (val[j] << shift) | (shift == 0 ? 0 : (val[j - 1] >> (64 - shift))); } val[i] = val[0] << shift; } @@ -424,7 +425,8 @@ if (drop < WordCount) { if (shift > 0) { for (size_t j = drop; j < WordCount - 1; ++i, ++j) { - val[i] = (val[j] >> shift) | (val[j + 1] << (64 - shift)); + val[i] = (val[j] >> shift) | + (shift == 0 ? 0 : (val[j + 1] << (64 - shift))); } val[i] = val[WordCount - 1] >> shift; ++i; diff --git a/libc/test/src/__support/uint_test.cpp b/libc/test/src/__support/uint_test.cpp --- a/libc/test/src/__support/uint_test.cpp +++ b/libc/test/src/__support/uint_test.cpp @@ -326,6 +326,10 @@ 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 @@ 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) {