[libc-commits] [libc] Enable -Wconversion (PR #127523)

Vinay Deshmukh via libc-commits libc-commits at lists.llvm.org
Sat Feb 22 20:53:37 PST 2025


https://github.com/vinay-deshmukh updated https://github.com/llvm/llvm-project/pull/127523

>From 338a9c070c390c5b9ff05777750510166919f308 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Mon, 17 Feb 2025 12:07:47 -0500
Subject: [PATCH 01/26] Enable -Wconversion

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index f33db5826537b..75eb1a06332ac 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -36,7 +36,7 @@ function(_get_common_test_compile_options output_var c_test flags)
     if(NOT LIBC_WNO_ERROR)
       # list(APPEND compile_options "-Werror")
     endif()
-    # list(APPEND compile_options "-Wconversion")
+    list(APPEND compile_options "-Wconversion")
     # list(APPEND compile_options "-Wno-sign-conversion")
     list(APPEND compile_options "-Wimplicit-fallthrough")
     list(APPEND compile_options "-Wwrite-strings")

>From 2ff79ef22c5f0f6f4a8d631ea0d91291d98ac88f Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 12:33:08 -0500
Subject: [PATCH 02/26] bit.h

---
 libc/src/__support/CPP/bit.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 82b9eb5128262..31d2cc337750a 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -126,7 +126,7 @@ countl_zero(T value) {
 }
 #else
 template <typename T>
-[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
+[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, unsigned int>
 countl_zero(T value) {
   if (!value)
     return cpp::numeric_limits<T>::digits;
@@ -183,7 +183,7 @@ countr_one(T value) {
 ///
 /// Ex. bit_width(5) == 3.
 template <typename T>
-[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
+[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, unsigned int>
 bit_width(T value) {
   return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);
 }

>From b307e58f9193e806d6236902bebac48385d1f1b7 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 12:33:25 -0500
Subject: [PATCH 03/26] string_view.h

---
 libc/src/__support/CPP/string_view.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/string_view.h b/libc/src/__support/CPP/string_view.h
index 745c62c35f0a0..635eb1a1252c1 100644
--- a/libc/src/__support/CPP/string_view.h
+++ b/libc/src/__support/CPP/string_view.h
@@ -12,6 +12,7 @@
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
+#include <cstdint>
 #include <stddef.h>
 
 namespace LIBC_NAMESPACE_DECL {
@@ -61,7 +62,7 @@ class string_view {
 
   // special value equal to the maximum value representable by the type
   // size_type.
-  LIBC_INLINE_VAR static constexpr size_t npos = -1;
+  LIBC_INLINE_VAR static constexpr size_t npos = SIZE_MAX;
 
   LIBC_INLINE constexpr string_view() : Data(nullptr), Len(0) {}
 

>From e54ac9e245158ee6d7964bc2915a55027e1624f5 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 12:33:36 -0500
Subject: [PATCH 04/26] utils.h

---
 libc/src/string/memory_utils/utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/string/memory_utils/utils.h b/libc/src/string/memory_utils/utils.h
index 5c9bc72208f85..434dafb4dbab2 100644
--- a/libc/src/string/memory_utils/utils.h
+++ b/libc/src/string/memory_utils/utils.h
@@ -293,7 +293,7 @@ LIBC_INLINE void store64_aligned(uint64_t value, Ptr dst, size_t offset) {
 // Advances the pointers p1 and p2 by offset bytes and decrease count by the
 // same amount.
 template <typename T1, typename T2>
-LIBC_INLINE void adjust(ptrdiff_t offset, T1 *__restrict &p1,
+LIBC_INLINE void adjust(uintptr_t offset, T1 *__restrict &p1,
                         T2 *__restrict &p2, size_t &count) {
   p1 += offset;
   p2 += offset;

>From 2cb08dce4e2b68929a5597d07efb62a6a5bea4cd Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 12:41:45 -0500
Subject: [PATCH 05/26] undefined if end is not after start anyway

---
 libc/src/__support/CPP/string_view.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/string_view.h b/libc/src/__support/CPP/string_view.h
index 635eb1a1252c1..914a7225ae867 100644
--- a/libc/src/__support/CPP/string_view.h
+++ b/libc/src/__support/CPP/string_view.h
@@ -41,7 +41,7 @@ class string_view {
   LIBC_INLINE static constexpr size_t length(const char *Str) {
     for (const char *End = Str;; ++End)
       if (*End == '\0')
-        return End - Str;
+        return static_cast<size_t>(End - Str);
   }
 
   LIBC_INLINE bool equals(string_view Other) const {

>From 29a15a648be3716ac32e35c7fe7d827ea5ac70db Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 12:42:00 -0500
Subject: [PATCH 06/26] "count" is always unsigned/size_t

---
 libc/src/__support/big_int.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index fb5ad99d53e7b..2bc1393996097 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -311,8 +311,8 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
 
 #define DECLARE_COUNTBIT(NAME, INDEX_EXPR)                                     \
   template <typename word, size_t N>                                           \
-  LIBC_INLINE constexpr int NAME(const cpp::array<word, N> &val) {             \
-    int bit_count = 0;                                                         \
+  LIBC_INLINE constexpr size_t NAME(const cpp::array<word, N> &val) {             \
+    size_t bit_count = 0;                                                         \
     for (size_t i = 0; i < N; ++i) {                                           \
       const int word_count = cpp::NAME<word>(val[INDEX_EXPR]);                 \
       bit_count += word_count;                                                 \
@@ -1007,7 +1007,7 @@ struct BigInt {
     BigInt quotient;
     if (remainder >= divider) {
       BigInt subtractor = divider;
-      int cur_bit = multiword::countl_zero(subtractor.val) -
+      size_t cur_bit = multiword::countl_zero(subtractor.val) -
                     multiword::countl_zero(remainder.val);
       subtractor <<= cur_bit;
       for (; cur_bit >= 0 && remainder > 0; --cur_bit, subtractor >>= 1) {

>From a573f7f3fc8d6ca4f174fbe0b31f8ad155aa0f23 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 12:50:44 -0500
Subject: [PATCH 07/26] index is size_t

---
 libc/src/__support/big_int.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 2bc1393996097..54aa9ebb26dd7 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -272,15 +272,15 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
   if (LIBC_UNLIKELY(offset == 0))
     return array;
   const bool is_neg = is_signed && is_negative(array);
-  constexpr auto at = [](size_t index) -> int {
+  constexpr auto at = [](size_t index) -> size_t {
     // reverse iteration when direction == LEFT.
     if constexpr (direction == LEFT)
-      return int(N) - int(index) - 1;
-    return int(index);
+      return N - index - 1;
+    return index;
   };
   const auto safe_get_at = [&](size_t index) -> word {
     // return appropriate value when accessing out of bound elements.
-    const int i = at(index);
+    const size_t i = at(index);
     if (i < 0)
       return 0;
     if (i >= int(N))

>From 3043de57e172fba1468edadd05a5906e77e899c7 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 13:02:01 -0500
Subject: [PATCH 08/26] -1 == max

---
 libc/src/__support/big_int.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 54aa9ebb26dd7..534edd315d561 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -284,7 +284,7 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
     if (i < 0)
       return 0;
     if (i >= int(N))
-      return is_neg ? -1 : 0;
+      return is_neg ? cpp::numeric_limits<word>::max(); : 0;
     return array[i];
   };
   const size_t index_offset = offset / WORD_BITS;

>From 439f3307981345b3d62f90dea272434604627904 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 13:02:29 -0500
Subject: [PATCH 09/26] char == uint8_t

---
 libc/src/__support/CPP/string.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h
index dbc0ae04e5e6f..e38a0b3496c54 100644
--- a/libc/src/__support/CPP/string.h
+++ b/libc/src/__support/CPP/string.h
@@ -67,6 +67,7 @@ class string {
       : string(cstr, ::LIBC_NAMESPACE::internal::string_length(cstr)) {}
   LIBC_INLINE string(size_t size_, char value) {
     resize(size_);
+    static_assert(sizeof(char) == sizeof(uint8_t));
     inline_memset((void *)buffer_, value, size_);
   }
 

>From ecced049bbce3b2e21866c586bcbd38535a91e92 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 13:08:08 -0500
Subject: [PATCH 10/26] size_t not int

---
 libc/src/__support/big_int.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 534edd315d561..7e225aef34256 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -314,7 +314,7 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
   LIBC_INLINE constexpr size_t NAME(const cpp::array<word, N> &val) {             \
     size_t bit_count = 0;                                                         \
     for (size_t i = 0; i < N; ++i) {                                           \
-      const int word_count = cpp::NAME<word>(val[INDEX_EXPR]);                 \
+      const size_t word_count = cpp::NAME<word>(val[INDEX_EXPR]);                 \
       bit_count += word_count;                                                 \
       if (word_count != cpp::numeric_limits<word>::digits)                     \
         break;                                                                 \

>From 6feebdb12282f761d67e5a2292b86b271a3e99d8 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 13:08:31 -0500
Subject: [PATCH 11/26] cast

---
 libc/src/string/string_utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index 583d35014d398..f4abe1c4d5b7c 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -66,7 +66,7 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) {
   for (; reinterpret_cast<uintptr_t>(char_ptr) % sizeof(Word) != 0;
        ++char_ptr) {
     if (*char_ptr == '\0')
-      return char_ptr - src;
+      return static_cast<size_t>(char_ptr - src);
   }
   // Step 2: read blocks
   for (const Word *block_ptr = reinterpret_cast<const Word *>(char_ptr);

>From a5f3a1f434eda7104b356c8871614404e8d08b7e Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 14:04:15 -0500
Subject: [PATCH 12/26] libc/src/string/string_utils

---
 libc/src/string/string_utils.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index f4abe1c4d5b7c..d1fe65f71f1a0 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -77,7 +77,7 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) {
   for (; *char_ptr != '\0'; ++char_ptr) {
     ;
   }
-  return char_ptr - src;
+  return static_cast<size_t>(char_ptr - src);
 }
 
 // Returns the length of a string, denoted by the first occurrence
@@ -169,7 +169,7 @@ LIBC_INLINE size_t complementary_span(const char *src, const char *segment) {
   for (; *src && !bitset.test(*reinterpret_cast<const unsigned char *>(src));
        ++src)
     ;
-  return src - initial;
+  return static_cast<size_t>(src - initial);
 }
 
 // Given the similarities between strtok and strtok_r, we can implement both
@@ -189,12 +189,13 @@ LIBC_INLINE char *string_token(char *__restrict src,
   if (LIBC_UNLIKELY(src == nullptr && ((src = *saveptr) == nullptr)))
     return nullptr;
 
+  static_assert(sizeof(char) == sizeof(cpp::byte), "bitset of 256 assumes char is 8 bits");
   cpp::bitset<256> delimiter_set;
   for (; *delimiter_string != '\0'; ++delimiter_string)
-    delimiter_set.set(*delimiter_string);
+    delimiter_set.set(static_cast<size_t>(*delimiter_string));
 
   if constexpr (SkipDelim)
-    for (; *src != '\0' && delimiter_set.test(*src); ++src)
+    for (; *src != '\0' && delimiter_set.test(static_cast<size_t>(*src)); ++src)
       ;
   if (*src == '\0') {
     *saveptr = src;
@@ -202,7 +203,7 @@ LIBC_INLINE char *string_token(char *__restrict src,
   }
   char *token = src;
   for (; *src != '\0'; ++src) {
-    if (delimiter_set.test(*src)) {
+    if (delimiter_set.test(static_cast<size_t>(*src))) {
       *src = '\0';
       ++src;
       break;

>From 32f26cc0287a8490ab0faddb1b7135955374e960 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 14:16:19 -0500
Subject: [PATCH 13/26] string.h

---
 libc/src/__support/CPP/string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h
index e38a0b3496c54..1ac04c7f1f9dc 100644
--- a/libc/src/__support/CPP/string.h
+++ b/libc/src/__support/CPP/string.h
@@ -68,7 +68,7 @@ class string {
   LIBC_INLINE string(size_t size_, char value) {
     resize(size_);
     static_assert(sizeof(char) == sizeof(uint8_t));
-    inline_memset((void *)buffer_, value, size_);
+    inline_memset((void *)buffer_, static_cast<uint8_t>(value), size_);
   }
 
   LIBC_INLINE string &operator=(const string &other) {

>From b2939976bd3191a47dc17d90bb1b0043484f9f94 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 14:20:01 -0500
Subject: [PATCH 14/26] typo in big_int.h

---
 libc/src/__support/big_int.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 7e225aef34256..cd922c061c474 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -284,7 +284,7 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
     if (i < 0)
       return 0;
     if (i >= int(N))
-      return is_neg ? cpp::numeric_limits<word>::max(); : 0;
+      return is_neg ? cpp::numeric_limits<word>::max() : 0;
     return array[i];
   };
   const size_t index_offset = offset / WORD_BITS;

>From 6f596d7b31052a7b60300c0b8c413db759db4005 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 14:20:25 -0500
Subject: [PATCH 15/26] numeric_limits

---
 libc/src/__support/CPP/span.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/span.h b/libc/src/__support/CPP/span.h
index a41c9b744e370..4bb27932b6f74 100644
--- a/libc/src/__support/CPP/span.h
+++ b/libc/src/__support/CPP/span.h
@@ -11,6 +11,7 @@
 #include <stddef.h> // For size_t
 
 #include "array.h" // For array
+#include "limits.h"
 #include "src/__support/macros/config.h"
 #include "type_traits.h" // For remove_cv_t, enable_if_t, is_same_v, is_const_v
 
@@ -48,7 +49,7 @@ template <typename T> class span {
   using const_reference = const T &;
   using iterator = T *;
 
-  LIBC_INLINE_VAR static constexpr size_type dynamic_extent = -1;
+  LIBC_INLINE_VAR static constexpr size_type dynamic_extent = cpp::numeric_limits<size_type>::max();
 
   LIBC_INLINE constexpr span() : span_data(nullptr), span_size(0) {}
 

>From cd9e931c6c5a17504d1b1250035ab6a2f1fa4d29 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 14:35:16 -0500
Subject: [PATCH 16/26] back to original bit.h

---
 libc/src/__support/CPP/bit.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 31d2cc337750a..38a71df252123 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -126,7 +126,7 @@ countl_zero(T value) {
 }
 #else
 template <typename T>
-[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, unsigned int>
+[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
 countl_zero(T value) {
   if (!value)
     return cpp::numeric_limits<T>::digits;

>From 620e028b1d2e0a647bf2efe9ba1cda281399985c Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 15:07:59 -0500
Subject: [PATCH 17/26] Kind of done with bit etc

---
 libc/src/__support/CPP/bit.h             |  6 +++---
 libc/src/__support/big_int.h             | 16 ++++++++--------
 libc/src/__support/integer_literals.h    |  2 +-
 libc/src/__support/integer_to_string.h   |  4 ++--
 libc/test/src/__support/big_int_test.cpp |  8 ++++----
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 38a71df252123..b59fe2d41cd14 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -101,7 +101,7 @@ countr_zero(T value) {
     shift >>= 1;
     mask >>= shift;
   }
-  return zero_bits;
+  return static_cast<int>(zero_bits);
 }
 #if __has_builtin(__builtin_ctzs)
 ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
@@ -140,7 +140,7 @@ countl_zero(T value) {
     else
       zero_bits |= shift;
   }
-  return zero_bits;
+  return static_cast<int>(zero_bits);
 }
 #if __has_builtin(__builtin_clzs)
 ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
@@ -183,7 +183,7 @@ countr_one(T value) {
 ///
 /// Ex. bit_width(5) == 3.
 template <typename T>
-[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, unsigned int>
+[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
 bit_width(T value) {
   return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);
 }
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index cd922c061c474..c4a0ecc9176e8 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -311,10 +311,10 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
 
 #define DECLARE_COUNTBIT(NAME, INDEX_EXPR)                                     \
   template <typename word, size_t N>                                           \
-  LIBC_INLINE constexpr size_t NAME(const cpp::array<word, N> &val) {             \
-    size_t bit_count = 0;                                                         \
+  LIBC_INLINE constexpr int NAME(const cpp::array<word, N> &val) {             \
+    int bit_count = 0;                                                         \
     for (size_t i = 0; i < N; ++i) {                                           \
-      const size_t word_count = cpp::NAME<word>(val[INDEX_EXPR]);                 \
+      const int word_count = cpp::NAME<word>(val[INDEX_EXPR]);                 \
       bit_count += word_count;                                                 \
       if (word_count != cpp::numeric_limits<word>::digits)                     \
         break;                                                                 \
@@ -697,7 +697,7 @@ struct BigInt {
     }
     BigInt quotient;
     WordType x_word = static_cast<WordType>(x);
-    constexpr size_t LOG2_WORD_SIZE = cpp::bit_width(WORD_SIZE) - 1;
+    constexpr size_t LOG2_WORD_SIZE = static_cast<size_t>(cpp::bit_width(WORD_SIZE) - 1);
     constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1;
     constexpr WordType HALF_MASK = ((WordType(1) << HALF_WORD_SIZE) - 1);
     // lower = smallest multiple of WORD_SIZE that is >= e.
@@ -1007,8 +1007,8 @@ struct BigInt {
     BigInt quotient;
     if (remainder >= divider) {
       BigInt subtractor = divider;
-      size_t cur_bit = multiword::countl_zero(subtractor.val) -
-                    multiword::countl_zero(remainder.val);
+      size_t cur_bit = static_cast<size_t>(multiword::countl_zero(subtractor.val) -
+                    multiword::countl_zero(remainder.val));
       subtractor <<= cur_bit;
       for (; cur_bit >= 0 && remainder > 0; --cur_bit, subtractor >>= 1) {
         if (remainder < subtractor)
@@ -1312,7 +1312,7 @@ mask_trailing_ones() {
   T out; // zero initialized
   for (size_t i = 0; i <= QUOTIENT; ++i)
     out[i] = i < QUOTIENT
-                 ? -1
+                 ? cpp::numeric_limits<typename T::word_type>::max()
                  : mask_trailing_ones<typename T::word_type, REMAINDER>();
   return out;
 }
@@ -1328,7 +1328,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> mask_leading_ones() {
   T out; // zero initialized
   for (size_t i = QUOTIENT; i < T::WORD_COUNT; ++i)
     out[i] = i > QUOTIENT
-                 ? -1
+                 ? cpp::numeric_limits<typename T::word_type>::max()
                  : mask_leading_ones<typename T::word_type, REMAINDER>();
   return out;
 }
diff --git a/libc/src/__support/integer_literals.h b/libc/src/__support/integer_literals.h
index 0298ec7d088d6..f68b7ef12c879 100644
--- a/libc/src/__support/integer_literals.h
+++ b/libc/src/__support/integer_literals.h
@@ -47,7 +47,7 @@ LIBC_INLINE constexpr T accumulate(int base, const uint8_t *digits,
                                    size_t size) {
   T value{};
   for (; size; ++digits, --size) {
-    value *= base;
+    value *= static_cast<unsigned int>(base);
     value += *digits;
   }
   return value;
diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 13dd83e9ed2b1..5d661f685acde 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -405,7 +405,7 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString {
     // Returns the absolute value of 'value' as 'UNSIGNED_T'.
     LIBC_INLINE static UNSIGNED_T abs(T value) {
       if (cpp::is_unsigned_v<T> || value >= 0)
-        return value; // already of the right sign.
+        return static_cast<UNSIGNED_T>(value); // already of the right sign.
 
       // Signed integers are asymmetric (e.g., int8_t ∈ [-128, 127]).
       // Thus negating the type's minimum value would overflow.
@@ -422,7 +422,7 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString {
       if (value == cpp::numeric_limits<T>::min()) {
         return cpp::bit_cast<UNSIGNED_T>(value);
       } else {
-        return -value; // legal and representable both as T and UNSIGNED_T.`
+        return static_cast<UNSIGNED_T>(-value); // legal and representable both as T and UNSIGNED_T.`
       }
     }
 
diff --git a/libc/test/src/__support/big_int_test.cpp b/libc/test/src/__support/big_int_test.cpp
index 2666ed978dad7..93a66d59650b4 100644
--- a/libc/test/src/__support/big_int_test.cpp
+++ b/libc/test/src/__support/big_int_test.cpp
@@ -197,8 +197,8 @@ TYPED_TEST(LlvmLibcUIntClassTest, CountBits, Types) {
     for (size_t i = 0; i < T::BITS; ++i) {
       const auto l_one = T::all_ones() << i; // 0b111...000
       const auto r_one = T::all_ones() >> i; // 0b000...111
-      const int zeros = i;
-      const int ones = T::BITS - zeros;
+      const int zeros = static_cast<int>(i);
+      const int ones = static_cast<int>(T::BITS - static_cast<size_t>(zeros));
       ASSERT_EQ(cpp::countr_one(r_one), ones);
       ASSERT_EQ(cpp::countl_one(l_one), ones);
       ASSERT_EQ(cpp::countr_zero(l_one), zeros);
@@ -871,13 +871,13 @@ TEST(LlvmLibcUIntClassTest, ConstructorFromUInt128Tests) {
   ASSERT_EQ(static_cast<int>(c >> 64), 123);
   ASSERT_EQ(static_cast<uint64_t>(d), static_cast<uint64_t>(b));
   ASSERT_EQ(static_cast<uint64_t>(d >> 64), static_cast<uint64_t>(b >> 64));
-  ASSERT_EQ(c + d, LL_Int128(a + b));
+  ASSERT_EQ(c + d, LL_Int128(a + static_cast<__uint128_t>(b)));
 
   ASSERT_EQ(static_cast<int>(e), 1);
   ASSERT_EQ(static_cast<int>(e >> 64), 123);
   ASSERT_EQ(static_cast<uint64_t>(f), static_cast<uint64_t>(b));
   ASSERT_EQ(static_cast<uint64_t>(f >> 64), static_cast<uint64_t>(b >> 64));
-  ASSERT_EQ(LL_UInt192(e + f), LL_UInt192(a + b));
+  ASSERT_EQ(LL_UInt192(e + f), LL_UInt192(a + static_cast<__uint128_t>(b)));
 }
 
 TEST(LlvmLibcUIntClassTest, WordTypeUInt128Tests) {

>From 465f92881f441a236a82a2956708e7c7807d70a8 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 20:00:30 -0500
Subject: [PATCH 18/26] digits

---
 libc/src/__support/CPP/bit.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index b59fe2d41cd14..2cd5abab13b94 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -226,7 +226,7 @@ rotr(T value, int rotate);
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
 rotl(T value, int rotate) {
-  constexpr unsigned N = cpp::numeric_limits<T>::digits;
+  constexpr int N = cpp::numeric_limits<T>::digits;
   rotate = rotate % N;
   if (!rotate)
     return value;

>From 0c0cd426b623f73845b8437df86a44fb66dee17e Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 20:01:30 -0500
Subject: [PATCH 19/26] digits

---
 libc/src/__support/CPP/bit.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 2cd5abab13b94..7d138201783bf 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -238,7 +238,7 @@ rotl(T value, int rotate) {
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
 rotr(T value, int rotate) {
-  constexpr unsigned N = cpp::numeric_limits<T>::digits;
+  constexpr int N = cpp::numeric_limits<T>::digits;
   rotate = rotate % N;
   if (!rotate)
     return value;

>From 84103a7b66763ceceb033a6347d9ed21ac72741f Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 20:41:32 -0500
Subject: [PATCH 20/26] span

---
 libc/src/__support/CPP/span.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/span.h b/libc/src/__support/CPP/span.h
index 4bb27932b6f74..316959908114d 100644
--- a/libc/src/__support/CPP/span.h
+++ b/libc/src/__support/CPP/span.h
@@ -59,7 +59,7 @@ template <typename T> class span {
       : span_data(first), span_size(count) {}
 
   LIBC_INLINE constexpr span(pointer first, pointer end)
-      : span_data(first), span_size(end - first) {}
+      : span_data(first), span_size(static_cast<size_t>(end - first)) {}
 
   template <typename U, size_t N,
             cpp::enable_if_t<is_compatible_v<U>, bool> = true>

>From 749eeeb1926cc70b373f87f3ad0c795fa5ea1380 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 20:41:54 -0500
Subject: [PATCH 21/26] casts

---
 libc/src/__support/FPUtil/FPBits.h | 2 +-
 libc/src/__support/big_int.h       | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 90b6e406e0f31..7f672b75d49a2 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -247,7 +247,7 @@ template <FPType fp_type> struct FPStorage : public FPLayout<fp_type> {
     using UP::UP;
 
     LIBC_INLINE constexpr BiasedExponent(Exponent exp)
-        : UP(static_cast<int32_t>(exp) + EXP_BIAS) {}
+        : UP(static_cast<uint32_t>(static_cast<int32_t>(exp) + EXP_BIAS)) {}
 
     // Cast operator to get convert from BiasedExponent to Exponent.
     LIBC_INLINE constexpr operator Exponent() const {
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index c4a0ecc9176e8..5aff243ef9398 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -1276,26 +1276,26 @@ rotr(T value, int rotate);
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
 rotl(T value, int rotate) {
-  constexpr unsigned N = cpp::numeric_limits<T>::digits;
+  constexpr int N = cpp::numeric_limits<T>::digits;
   rotate = rotate % N;
   if (!rotate)
     return value;
   if (rotate < 0)
     return cpp::rotr<T>(value, -rotate);
-  return (value << rotate) | (value >> (N - rotate));
+  return (value << static_cast<size_t>(rotate)) | (value >> (N - static_cast<size_t>(rotate)));
 }
 
 // Specialization of cpp::rotr ('bit.h') for BigInt.
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
 rotr(T value, int rotate) {
-  constexpr unsigned N = cpp::numeric_limits<T>::digits;
+  constexpr int N = cpp::numeric_limits<T>::digits;
   rotate = rotate % N;
   if (!rotate)
     return value;
   if (rotate < 0)
     return cpp::rotl<T>(value, -rotate);
-  return (value >> rotate) | (value << (N - rotate));
+  return (value >> static_cast<size_t>(rotate)) | (value << (N - static_cast<size_t>(rotate)));
 }
 
 } // namespace cpp

>From 8ce8ecbe1c49eeca5c889d262615b9f685d1c2fb Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 20:42:08 -0500
Subject: [PATCH 22/26] integer promotions

---
 libc/test/src/__support/CPP/bit_test.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index 1f2315281bc1d..21c4e914ef287 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -50,28 +50,28 @@ TYPED_TEST(LlvmLibcBitTest, HasSingleBit, UnsignedTypes) {
 TYPED_TEST(LlvmLibcBitTest, CountLZero, UnsignedTypes) {
   EXPECT_EQ(countl_zero<T>(T(0)), cpp::numeric_limits<T>::digits);
   int expected = 0;
-  for (T value = ~T(0); value; value >>= 1, ++expected)
+  for (T value = T(~0); value; value >>= 1, ++expected)
     EXPECT_EQ(countl_zero<T>(value), expected);
 }
 
 TYPED_TEST(LlvmLibcBitTest, CountRZero, UnsignedTypes) {
   EXPECT_EQ(countr_zero<T>(T(0)), cpp::numeric_limits<T>::digits);
   int expected = 0;
-  for (T value = ~T(0); value; value <<= 1, ++expected)
+  for (T value = T(~0); value; value <<= 1, ++expected)
     EXPECT_EQ(countr_zero<T>(value), expected);
 }
 
 TYPED_TEST(LlvmLibcBitTest, CountLOne, UnsignedTypes) {
   EXPECT_EQ(countl_one<T>(T(0)), 0);
   int expected = cpp::numeric_limits<T>::digits;
-  for (T value = ~T(0); value; value <<= 1, --expected)
+  for (T value = T(~0); value; value <<= 1, --expected)
     EXPECT_EQ(countl_one<T>(value), expected);
 }
 
 TYPED_TEST(LlvmLibcBitTest, CountROne, UnsignedTypes) {
   EXPECT_EQ(countr_one<T>(T(0)), 0);
   int expected = cpp::numeric_limits<T>::digits;
-  for (T value = ~T(0); value; value >>= 1, --expected)
+  for (T value = T(~0); value; value >>= 1, --expected)
     EXPECT_EQ(countr_one<T>(value), expected);
 }
 
@@ -163,7 +163,7 @@ TEST(LlvmLibcBitTest, BitFloor) {
 
 TYPED_TEST(LlvmLibcBitTest, RotateIsInvariantForZeroAndOne, UnsignedTypes) {
   constexpr T all_zeros = T(0);
-  constexpr T all_ones = ~T(0);
+  constexpr T all_ones = T(~0);
   for (int i = 0; i < cpp::numeric_limits<T>::digits; ++i) {
     EXPECT_EQ(rotl<T>(all_zeros, i), all_zeros);
     EXPECT_EQ(rotl<T>(all_ones, i), all_ones);

>From 3568e2a089a36099ed26321c8d11a7316cb5e524 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 20:47:36 -0500
Subject: [PATCH 23/26] implicit

---
 libc/test/src/__support/CPP/bit_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index 21c4e914ef287..7d22ef22555e1 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -227,7 +227,7 @@ TEST(LlvmLibcBitTest, Rotr) {
 TYPED_TEST(LlvmLibcBitTest, CountOnes, UnsignedTypes) {
   EXPECT_EQ(popcount(T(0)), 0);
   for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
-    EXPECT_EQ(popcount<T>(cpp::numeric_limits<T>::max() >> i),
+    EXPECT_EQ(popcount<T>(cpp::numeric_limits<T>::max() >> static_cast<size_t>(i)),
               cpp::numeric_limits<T>::digits - i);
 }
 

>From 7e263a64c40a468d9b35e9338f5e8b0bb67e84a0 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 21:08:43 -0500
Subject: [PATCH 24/26] fenv_darwin_impl.h

---
 .../FPUtil/aarch64/fenv_darwin_impl.h         | 33 ++++++++++---------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
index 969e70796d1f1..eabb61aa5a137 100644
--- a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
+++ b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
@@ -12,6 +12,7 @@
 #include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
+#include <_types/_uint32_t.h>
 
 #if !defined(LIBC_TARGET_ARCH_IS_AARCH64) || !defined(__APPLE__)
 #error "Invalid include"
@@ -63,7 +64,7 @@ struct FEnv {
   // __fpcr_flush_to_zero bit in the FPCR register.  This control bit is
   // located in a different place from FE_FLUSHTOZERO status bit relative to
   // the other exceptions.
-  LIBC_INLINE static uint32_t exception_value_from_status(int status) {
+  LIBC_INLINE static uint32_t exception_value_from_status(uint32_t status) {
     return ((status & FE_INVALID) ? EX_INVALID : 0) |
            ((status & FE_DIVBYZERO) ? EX_DIVBYZERO : 0) |
            ((status & FE_OVERFLOW) ? EX_OVERFLOW : 0) |
@@ -72,7 +73,7 @@ struct FEnv {
            ((status & FE_FLUSHTOZERO) ? EX_FLUSHTOZERO : 0);
   }
 
-  LIBC_INLINE static uint32_t exception_value_from_control(int control) {
+  LIBC_INLINE static uint32_t exception_value_from_control(uint32_t control) {
     return ((control & __fpcr_trap_invalid) ? EX_INVALID : 0) |
            ((control & __fpcr_trap_divbyzero) ? EX_DIVBYZERO : 0) |
            ((control & __fpcr_trap_overflow) ? EX_OVERFLOW : 0) |
@@ -81,7 +82,7 @@ struct FEnv {
            ((control & __fpcr_flush_to_zero) ? EX_FLUSHTOZERO : 0);
   }
 
-  LIBC_INLINE static int exception_value_to_status(uint32_t excepts) {
+  LIBC_INLINE static uint32_t exception_value_to_status(uint32_t excepts) {
     return ((excepts & EX_INVALID) ? FE_INVALID : 0) |
            ((excepts & EX_DIVBYZERO) ? FE_DIVBYZERO : 0) |
            ((excepts & EX_OVERFLOW) ? FE_OVERFLOW : 0) |
@@ -90,7 +91,7 @@ struct FEnv {
            ((excepts & EX_FLUSHTOZERO) ? FE_FLUSHTOZERO : 0);
   }
 
-  LIBC_INLINE static int exception_value_to_control(uint32_t excepts) {
+  LIBC_INLINE static uint32_t exception_value_to_control(uint32_t excepts) {
     return ((excepts & EX_INVALID) ? __fpcr_trap_invalid : 0) |
            ((excepts & EX_DIVBYZERO) ? __fpcr_trap_divbyzero : 0) |
            ((excepts & EX_OVERFLOW) ? __fpcr_trap_overflow : 0) |
@@ -113,34 +114,34 @@ struct FEnv {
 };
 
 LIBC_INLINE int enable_except(int excepts) {
-  uint32_t new_excepts = FEnv::exception_value_from_status(excepts);
+  uint32_t new_excepts = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   uint32_t control_word = FEnv::get_control_word();
   uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
   if (new_excepts != old_excepts) {
     control_word |= FEnv::exception_value_to_control(new_excepts);
     FEnv::set_control_word(control_word);
   }
-  return FEnv::exception_value_to_status(old_excepts);
+  return static_cast<int>(FEnv::exception_value_to_status(old_excepts));
 }
 
 LIBC_INLINE int disable_except(int excepts) {
-  uint32_t disabled_excepts = FEnv::exception_value_from_status(excepts);
+  uint32_t disabled_excepts = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   uint32_t control_word = FEnv::get_control_word();
   uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
   control_word &= ~FEnv::exception_value_to_control(disabled_excepts);
   FEnv::set_control_word(control_word);
-  return FEnv::exception_value_to_status(old_excepts);
+  return static_cast<int>(FEnv::exception_value_to_status(old_excepts));
 }
 
 LIBC_INLINE int get_except() {
   uint32_t control_word = FEnv::get_control_word();
   uint32_t enabled_excepts = FEnv::exception_value_from_control(control_word);
-  return FEnv::exception_value_to_status(enabled_excepts);
+  return static_cast<int>(FEnv::exception_value_to_status(enabled_excepts));
 }
 
 LIBC_INLINE int clear_except(int excepts) {
   uint32_t status_word = FEnv::get_status_word();
-  uint32_t except_value = FEnv::exception_value_from_status(excepts);
+  uint32_t except_value = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   status_word &= ~FEnv::exception_value_to_status(except_value);
   FEnv::set_status_word(status_word);
   return 0;
@@ -148,13 +149,13 @@ LIBC_INLINE int clear_except(int excepts) {
 
 LIBC_INLINE int test_except(int excepts) {
   uint32_t statusWord = FEnv::get_status_word();
-  uint32_t ex_value = FEnv::exception_value_from_status(excepts);
-  return statusWord & FEnv::exception_value_to_status(ex_value);
+  uint32_t ex_value = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  return static_cast<int>(statusWord & FEnv::exception_value_to_status(ex_value));
 }
 
 LIBC_INLINE int set_except(int excepts) {
   uint32_t status_word = FEnv::get_status_word();
-  uint32_t new_exceptions = FEnv::exception_value_from_status(excepts);
+  uint32_t new_exceptions = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   status_word |= FEnv::exception_value_to_status(new_exceptions);
   FEnv::set_status_word(status_word);
   return 0;
@@ -174,7 +175,7 @@ LIBC_INLINE int raise_except(int excepts) {
                          : "s0", "s1" /* s0 and s1 are clobbered */);
   };
 
-  uint32_t to_raise = FEnv::exception_value_from_status(excepts);
+  uint32_t to_raise = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   int result = 0;
 
   if (to_raise & FEnv::EX_INVALID) {
@@ -237,7 +238,7 @@ LIBC_INLINE int get_round() {
 }
 
 LIBC_INLINE int set_round(int mode) {
-  uint16_t bit_value;
+  uint32_t bit_value;
   switch (mode) {
   case FE_TONEAREST:
     bit_value = FEnv::TONEAREST;
@@ -256,7 +257,7 @@ LIBC_INLINE int set_round(int mode) {
   }
 
   uint32_t control_word = FEnv::get_control_word();
-  control_word &= ~(0x3 << FEnv::ROUNDING_CONTROL_BIT_POSITION);
+  control_word &= ~(0x3u << FEnv::ROUNDING_CONTROL_BIT_POSITION);
   control_word |= (bit_value << FEnv::ROUNDING_CONTROL_BIT_POSITION);
   FEnv::set_control_word(control_word);
 

>From eac2f32b0859e9fdc387b0c77f2f342e1ea66470 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 23:43:53 -0500
Subject: [PATCH 25/26] more fixes

---
 libc/src/__support/FPUtil/FPBits.h      | 2 +-
 libc/src/__support/FPUtil/NormalFloat.h | 5 +++--
 libc/src/__support/str_to_integer.h     | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 7f672b75d49a2..e42f5dbace617 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -251,7 +251,7 @@ template <FPType fp_type> struct FPStorage : public FPLayout<fp_type> {
 
     // Cast operator to get convert from BiasedExponent to Exponent.
     LIBC_INLINE constexpr operator Exponent() const {
-      return Exponent(UP::value - EXP_BIAS);
+      return Exponent(static_cast<int32_t>(UP::value - EXP_BIAS));
     }
 
     LIBC_INLINE constexpr BiasedExponent &operator++() {
diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index b4cbb5042a68b..c0dd039c2e343 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -16,6 +16,7 @@
 #include "src/__support/macros/config.h"
 
 #include <stdint.h>
+#include <sys/_types/_int32_t.h>
 
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
@@ -105,7 +106,7 @@ template <typename T> struct NormalFloat {
 
     constexpr int SUBNORMAL_EXPONENT = -FPBits<T>::EXP_BIAS + 1;
     if (exponent < SUBNORMAL_EXPONENT) {
-      unsigned shift = SUBNORMAL_EXPONENT - exponent;
+      unsigned shift = static_cast<unsigned>(SUBNORMAL_EXPONENT - exponent);
       // Since exponent > subnormalExponent, shift is strictly greater than
       // zero.
       if (shift <= FPBits<T>::FRACTION_LEN + 1) {
@@ -160,7 +161,7 @@ template <typename T> struct NormalFloat {
     if (bits.is_subnormal()) {
       unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
       mantissa = static_cast<StorageType>(bits.get_mantissa() << shift);
-      exponent = 1 - FPBits<T>::EXP_BIAS - shift;
+      exponent = 1 - FPBits<T>::EXP_BIAS - static_cast<int32_t>(shift);
     } else {
       exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;
       mantissa = ONE | bits.get_mantissa();
diff --git a/libc/src/__support/str_to_integer.h b/libc/src/__support/str_to_integer.h
index 8e569e8a7feb0..223533b5d6fdc 100644
--- a/libc/src/__support/str_to_integer.h
+++ b/libc/src/__support/str_to_integer.h
@@ -96,7 +96,7 @@ strtointeger(const char *__restrict src, int base,
   if (base < 0 || base == 1 || base > 36)
     return {0, 0, EINVAL};
 
-  src_cur = first_non_whitespace(src, src_len) - src;
+  src_cur = static_cast<size_t>(first_non_whitespace(src, src_len) - src);
 
   char result_sign = '+';
   if (src[src_cur] == '+' || src[src_cur] == '-') {

>From dd2ed0295dec77cc61f319a03b558b5d91109f4f Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 22 Feb 2025 23:53:07 -0500
Subject: [PATCH 26/26] clang-format

---
 libc/src/__support/CPP/span.h                 |  3 ++-
 .../FPUtil/aarch64/fenv_darwin_impl.h         | 21 ++++++++++++-------
 libc/src/__support/big_int.h                  | 14 ++++++++-----
 libc/src/__support/integer_to_string.h        |  3 ++-
 libc/src/string/string_utils.h                |  3 ++-
 libc/test/src/__support/CPP/bit_test.cpp      |  5 +++--
 6 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/libc/src/__support/CPP/span.h b/libc/src/__support/CPP/span.h
index 316959908114d..9234a26d201cd 100644
--- a/libc/src/__support/CPP/span.h
+++ b/libc/src/__support/CPP/span.h
@@ -49,7 +49,8 @@ template <typename T> class span {
   using const_reference = const T &;
   using iterator = T *;
 
-  LIBC_INLINE_VAR static constexpr size_type dynamic_extent = cpp::numeric_limits<size_type>::max();
+  LIBC_INLINE_VAR static constexpr size_type dynamic_extent =
+      cpp::numeric_limits<size_type>::max();
 
   LIBC_INLINE constexpr span() : span_data(nullptr), span_size(0) {}
 
diff --git a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
index eabb61aa5a137..7c6fc03bf5275 100644
--- a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
+++ b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
@@ -114,7 +114,8 @@ struct FEnv {
 };
 
 LIBC_INLINE int enable_except(int excepts) {
-  uint32_t new_excepts = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  uint32_t new_excepts =
+      FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   uint32_t control_word = FEnv::get_control_word();
   uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
   if (new_excepts != old_excepts) {
@@ -125,7 +126,8 @@ LIBC_INLINE int enable_except(int excepts) {
 }
 
 LIBC_INLINE int disable_except(int excepts) {
-  uint32_t disabled_excepts = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  uint32_t disabled_excepts =
+      FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   uint32_t control_word = FEnv::get_control_word();
   uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
   control_word &= ~FEnv::exception_value_to_control(disabled_excepts);
@@ -141,7 +143,8 @@ LIBC_INLINE int get_except() {
 
 LIBC_INLINE int clear_except(int excepts) {
   uint32_t status_word = FEnv::get_status_word();
-  uint32_t except_value = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  uint32_t except_value =
+      FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   status_word &= ~FEnv::exception_value_to_status(except_value);
   FEnv::set_status_word(status_word);
   return 0;
@@ -149,13 +152,16 @@ LIBC_INLINE int clear_except(int excepts) {
 
 LIBC_INLINE int test_except(int excepts) {
   uint32_t statusWord = FEnv::get_status_word();
-  uint32_t ex_value = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
-  return static_cast<int>(statusWord & FEnv::exception_value_to_status(ex_value));
+  uint32_t ex_value =
+      FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  return static_cast<int>(statusWord &
+                          FEnv::exception_value_to_status(ex_value));
 }
 
 LIBC_INLINE int set_except(int excepts) {
   uint32_t status_word = FEnv::get_status_word();
-  uint32_t new_exceptions = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  uint32_t new_exceptions =
+      FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   status_word |= FEnv::exception_value_to_status(new_exceptions);
   FEnv::set_status_word(status_word);
   return 0;
@@ -175,7 +181,8 @@ LIBC_INLINE int raise_except(int excepts) {
                          : "s0", "s1" /* s0 and s1 are clobbered */);
   };
 
-  uint32_t to_raise = FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
+  uint32_t to_raise =
+      FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
   int result = 0;
 
   if (to_raise & FEnv::EX_INVALID) {
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 5aff243ef9398..38c23207aac5b 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -697,7 +697,8 @@ struct BigInt {
     }
     BigInt quotient;
     WordType x_word = static_cast<WordType>(x);
-    constexpr size_t LOG2_WORD_SIZE = static_cast<size_t>(cpp::bit_width(WORD_SIZE) - 1);
+    constexpr size_t LOG2_WORD_SIZE =
+        static_cast<size_t>(cpp::bit_width(WORD_SIZE) - 1);
     constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1;
     constexpr WordType HALF_MASK = ((WordType(1) << HALF_WORD_SIZE) - 1);
     // lower = smallest multiple of WORD_SIZE that is >= e.
@@ -1007,8 +1008,9 @@ struct BigInt {
     BigInt quotient;
     if (remainder >= divider) {
       BigInt subtractor = divider;
-      size_t cur_bit = static_cast<size_t>(multiword::countl_zero(subtractor.val) -
-                    multiword::countl_zero(remainder.val));
+      size_t cur_bit =
+          static_cast<size_t>(multiword::countl_zero(subtractor.val) -
+                              multiword::countl_zero(remainder.val));
       subtractor <<= cur_bit;
       for (; cur_bit >= 0 && remainder > 0; --cur_bit, subtractor >>= 1) {
         if (remainder < subtractor)
@@ -1282,7 +1284,8 @@ rotl(T value, int rotate) {
     return value;
   if (rotate < 0)
     return cpp::rotr<T>(value, -rotate);
-  return (value << static_cast<size_t>(rotate)) | (value >> (N - static_cast<size_t>(rotate)));
+  return (value << static_cast<size_t>(rotate)) |
+         (value >> (N - static_cast<size_t>(rotate)));
 }
 
 // Specialization of cpp::rotr ('bit.h') for BigInt.
@@ -1295,7 +1298,8 @@ rotr(T value, int rotate) {
     return value;
   if (rotate < 0)
     return cpp::rotl<T>(value, -rotate);
-  return (value >> static_cast<size_t>(rotate)) | (value << (N - static_cast<size_t>(rotate)));
+  return (value >> static_cast<size_t>(rotate)) |
+         (value << (N - static_cast<size_t>(rotate)));
 }
 
 } // namespace cpp
diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 5d661f685acde..65bdcf16b3867 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -422,7 +422,8 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString {
       if (value == cpp::numeric_limits<T>::min()) {
         return cpp::bit_cast<UNSIGNED_T>(value);
       } else {
-        return static_cast<UNSIGNED_T>(-value); // legal and representable both as T and UNSIGNED_T.`
+        return static_cast<UNSIGNED_T>(
+            -value); // legal and representable both as T and UNSIGNED_T.`
       }
     }
 
diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index d1fe65f71f1a0..e4659f65c93e2 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -189,7 +189,8 @@ LIBC_INLINE char *string_token(char *__restrict src,
   if (LIBC_UNLIKELY(src == nullptr && ((src = *saveptr) == nullptr)))
     return nullptr;
 
-  static_assert(sizeof(char) == sizeof(cpp::byte), "bitset of 256 assumes char is 8 bits");
+  static_assert(sizeof(char) == sizeof(cpp::byte),
+                "bitset of 256 assumes char is 8 bits");
   cpp::bitset<256> delimiter_set;
   for (; *delimiter_string != '\0'; ++delimiter_string)
     delimiter_set.set(static_cast<size_t>(*delimiter_string));
diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index 7d22ef22555e1..22ced53c67fb0 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -227,8 +227,9 @@ TEST(LlvmLibcBitTest, Rotr) {
 TYPED_TEST(LlvmLibcBitTest, CountOnes, UnsignedTypes) {
   EXPECT_EQ(popcount(T(0)), 0);
   for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
-    EXPECT_EQ(popcount<T>(cpp::numeric_limits<T>::max() >> static_cast<size_t>(i)),
-              cpp::numeric_limits<T>::digits - i);
+    EXPECT_EQ(
+        popcount<T>(cpp::numeric_limits<T>::max() >> static_cast<size_t>(i)),
+        cpp::numeric_limits<T>::digits - i);
 }
 
 } // namespace cpp



More information about the libc-commits mailing list