Skip to content

Commit a3e2075

Browse files
[libc][bit_test] fix -Wimplicit-int-conversion (#126317)
Fixes: llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit conversion loses integer precision: 'int' to 'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion] 235 | return (value << rotate) | (value >> (N - rotate)); | ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit conversion loses integer precision: 'int' to 'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion] 247 | return (value >> rotate) | (value << (N - rotate)); | ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error: implicit conversion loses integer precision: 'int' to 'unsigned char' [-Werror,-Wimplicit-int-conversion] 45 | EXPECT_FALSE(has_single_bit<T>(two_bits_value)); | ~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~ Via the libc-cpp-utils-tests ninja target.
1 parent e9e717f commit a3e2075

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ rotl(T value, int rotate) {
232232
return value;
233233
if (rotate < 0)
234234
return cpp::rotr<T>(value, -rotate);
235-
return (value << rotate) | (value >> (N - rotate));
235+
return static_cast<T>((value << rotate) | (value >> (N - rotate)));
236236
}
237237

238238
template <typename T>
@@ -244,7 +244,7 @@ rotr(T value, int rotate) {
244244
return value;
245245
if (rotate < 0)
246246
return cpp::rotl<T>(value, -rotate);
247-
return (value >> rotate) | (value << (N - rotate));
247+
return static_cast<T>((value >> rotate) | (value << (N - rotate)));
248248
}
249249

250250
// TODO: Do we need this function at all? How is it different from

libc/test/src/__support/CPP/bit_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ TYPED_TEST(LlvmLibcBitTest, HasSingleBit, UnsignedTypes) {
4141
constexpr auto LSB = T(1);
4242
constexpr auto MSB = T(~(ALL_ONES >> 1));
4343
for (T value = 1; value; value <<= 1) {
44-
auto two_bits_value = value | ((value <= MIDPOINT) ? MSB : LSB);
44+
T two_bits_value =
45+
static_cast<T>(value | ((value <= MIDPOINT) ? MSB : LSB));
4546
EXPECT_FALSE(has_single_bit<T>(two_bits_value));
4647
}
4748
}

0 commit comments

Comments
 (0)