[libc-commits] [libc] 1ee6a1e - [libc] fix -Wshift-count-overflow in UInt.h (#74649)

via libc-commits libc-commits at lists.llvm.org
Thu Dec 7 08:20:51 PST 2023


Author: Nick Desaulniers
Date: 2023-12-07T08:20:46-08:00
New Revision: 1ee6a1e38aa0c3773d892fcd01bb2af8e446e67f

URL: https://github.com/llvm/llvm-project/commit/1ee6a1e38aa0c3773d892fcd01bb2af8e446e67f
DIFF: https://github.com/llvm/llvm-project/commit/1ee6a1e38aa0c3773d892fcd01bb2af8e446e67f.diff

LOG: [libc] fix -Wshift-count-overflow in UInt.h (#74649)

Not that I'm very good at SFINAE, but it seems that conversion operators
are
perhaps difficult to compose with SFINAE. I saw an example that used one
layer
of indirection to have an explicit return type that could then be used
with
enable_if_t.

Link: https://stackoverflow.com/a/7604580
Fixes: #74623

Added: 
    

Modified: 
    libc/src/__support/UInt.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 3bec2e3a47130..f72b995f8788d 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -103,13 +103,20 @@ template <size_t Bits, bool Signed> struct BigInt {
       val[i] = words[i];
   }
 
-  template <typename T, typename = cpp::enable_if_t<cpp::is_integral_v<T> &&
-                                                    sizeof(T) <= 16 &&
-                                                    !cpp::is_same_v<T, bool>>>
-  LIBC_INLINE constexpr explicit operator T() const {
-    if constexpr (sizeof(T) <= 8)
-      return static_cast<T>(val[0]);
+  template <typename T> LIBC_INLINE constexpr explicit operator T() const {
+    return to<T>();
+  }
 
+  template <typename T>
+  LIBC_INLINE constexpr cpp::enable_if_t<
+      cpp::is_integral_v<T> && sizeof(T) <= 8 && !cpp::is_same_v<T, bool>, T>
+  to() const {
+    return static_cast<T>(val[0]);
+  }
+  template <typename T>
+  LIBC_INLINE constexpr cpp::enable_if_t<
+      cpp::is_integral_v<T> && sizeof(T) == 16, T>
+  to() const {
     // T is 128-bit.
     T lo = static_cast<T>(val[0]);
 
@@ -121,7 +128,6 @@ template <size_t Bits, bool Signed> struct BigInt {
         return lo;
       }
     } else {
-      // TODO: silence shift warning
       return static_cast<T>((static_cast<T>(val[1]) << 64) + lo);
     }
   }


        


More information about the libc-commits mailing list