[libc-commits] [PATCH] D140070: [libc] fix shifting exact multiples of 64 in uint

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Dec 14 16:54:05 PST 2022


michaelrj created this revision.
michaelrj added a reviewer: lntue.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
michaelrj requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140070

Files:
  libc/src/__support/UInt.h
  libc/test/src/__support/uint_test.cpp


Index: libc/test/src/__support/uint_test.cpp
===================================================================
--- libc/test/src/__support/uint_test.cpp
+++ 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) {
Index: libc/src/__support/UInt.h
===================================================================
--- libc/src/__support/UInt.h
+++ 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140070.483038.patch
Type: text/x-patch
Size: 1834 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221215/b4b695f8/attachment.bin>


More information about the libc-commits mailing list