Skip to content

Commit 330522e

Browse files
authored
[libc++] Simplify some of the <bit> functions (#160267)
1 parent b98f0b8 commit 330522e

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

libcxx/include/__bit/countl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ template <__unsigned_integer _Tp>
3737

3838
template <__unsigned_integer _Tp>
3939
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
40-
return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
40+
return std::countl_zero(static_cast<_Tp>(~__t));
4141
}
4242

4343
#endif // _LIBCPP_STD_VER >= 20

libcxx/include/__bit/countr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ template <__unsigned_integer _Tp>
3737

3838
template <__unsigned_integer _Tp>
3939
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
40-
return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
40+
return std::countr_zero(static_cast<_Tp>(~__t));
4141
}
4242

4343
#endif // _LIBCPP_STD_VER >= 20

libcxx/include/__bit/has_single_bit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2525

2626
template <__unsigned_integer _Tp>
2727
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
28-
return __t != 0 && (((__t & (__t - 1)) == 0));
28+
return __t != 0 && ((__t & (__t - 1)) == 0);
2929
}
3030

3131
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__bit/rotate.h

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,35 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2222
// Writing two full functions for rotl and rotr makes it easier for the compiler
2323
// to optimize the code. On x86 this function becomes the ROL instruction and
2424
// the rotr function becomes the ROR instruction.
25-
template <class _Tp>
26-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {
27-
static_assert(__is_unsigned_integer_v<_Tp>, "__rotl requires an unsigned integer type");
25+
26+
#if _LIBCPP_STD_VER >= 20
27+
28+
template <__unsigned_integer _Tp>
29+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
2830
const int __n = numeric_limits<_Tp>::digits;
29-
int __r = __s % __n;
31+
int __r = __cnt % __n;
3032

3133
if (__r == 0)
32-
return __x;
34+
return __t;
3335

3436
if (__r > 0)
35-
return (__x << __r) | (__x >> (__n - __r));
37+
return (__t << __r) | (__t >> (__n - __r));
3638

37-
return (__x >> -__r) | (__x << (__n + __r));
39+
return (__t >> -__r) | (__t << (__n + __r));
3840
}
3941

40-
template <class _Tp>
41-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotr(_Tp __x, int __s) _NOEXCEPT {
42-
static_assert(__is_unsigned_integer_v<_Tp>, "__rotr requires an unsigned integer type");
42+
template <__unsigned_integer _Tp>
43+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept {
4344
const int __n = numeric_limits<_Tp>::digits;
44-
int __r = __s % __n;
45+
int __r = __cnt % __n;
4546

4647
if (__r == 0)
47-
return __x;
48+
return __t;
4849

4950
if (__r > 0)
50-
return (__x >> __r) | (__x << (__n - __r));
51-
52-
return (__x << -__r) | (__x >> (__n + __r));
53-
}
51+
return (__t >> __r) | (__t << (__n - __r));
5452

55-
#if _LIBCPP_STD_VER >= 20
56-
57-
template <__unsigned_integer _Tp>
58-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
59-
return std::__rotl(__t, __cnt);
60-
}
61-
62-
template <__unsigned_integer _Tp>
63-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept {
64-
return std::__rotr(__t, __cnt);
53+
return (__t << -__r) | (__t >> (__n + __r));
6554
}
6655

6756
#endif // _LIBCPP_STD_VER >= 20

libcxx/test/libcxx/numerics/bit.ops.pass.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111

1212
#include <__bit/bit_log2.h>
1313
#include <__bit/countl.h>
14-
#include <__bit/rotate.h>
1514
#include <cassert>
1615

1716
#include "test_macros.h"
1817

1918
TEST_CONSTEXPR_CXX14 bool test() {
2019
const unsigned v = 0x12345678;
2120

22-
ASSERT_SAME_TYPE(unsigned, decltype(std::__rotr(v, 3)));
2321
ASSERT_SAME_TYPE(int, decltype(std::__countl_zero(v)));
2422

25-
assert(std::__rotr(v, 3) == 0x02468acfU);
2623
assert(std::__countl_zero(v) == 3);
2724

2825
#if TEST_STD_VER > 17

0 commit comments

Comments
 (0)