Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libcxx/include/__bit/countl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ template <__unsigned_integer _Tp>

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

#endif // _LIBCPP_STD_VER >= 20
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__bit/countr.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ template <__unsigned_integer _Tp>

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

#endif // _LIBCPP_STD_VER >= 20
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__bit/has_single_bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD

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

_LIBCPP_END_NAMESPACE_STD
Expand Down
41 changes: 15 additions & 26 deletions libcxx/include/__bit/rotate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,35 @@ _LIBCPP_BEGIN_NAMESPACE_STD
// Writing two full functions for rotl and rotr makes it easier for the compiler
// to optimize the code. On x86 this function becomes the ROL instruction and
// the rotr function becomes the ROR instruction.
template <class _Tp>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {
static_assert(__is_unsigned_integer_v<_Tp>, "__rotl requires an unsigned integer type");

#if _LIBCPP_STD_VER >= 20

template <__unsigned_integer _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
const int __n = numeric_limits<_Tp>::digits;
int __r = __s % __n;
int __r = __cnt % __n;

if (__r == 0)
return __x;
return __t;

if (__r > 0)
return (__x << __r) | (__x >> (__n - __r));
return (__t << __r) | (__t >> (__n - __r));

return (__x >> -__r) | (__x << (__n + __r));
return (__t >> -__r) | (__t << (__n + __r));
}

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

if (__r == 0)
return __x;
return __t;

if (__r > 0)
return (__x >> __r) | (__x << (__n - __r));

return (__x << -__r) | (__x >> (__n + __r));
}
return (__t >> __r) | (__t << (__n - __r));

#if _LIBCPP_STD_VER >= 20

template <__unsigned_integer _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
return std::__rotl(__t, __cnt);
}

template <__unsigned_integer _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept {
return std::__rotr(__t, __cnt);
return (__t << -__r) | (__t >> (__n + __r));
}

#endif // _LIBCPP_STD_VER >= 20
Expand Down
3 changes: 0 additions & 3 deletions libcxx/test/libcxx/numerics/bit.ops.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@

#include <__bit/bit_log2.h>
#include <__bit/countl.h>
#include <__bit/rotate.h>
#include <cassert>

#include "test_macros.h"

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

ASSERT_SAME_TYPE(unsigned, decltype(std::__rotr(v, 3)));
ASSERT_SAME_TYPE(int, decltype(std::__countl_zero(v)));

assert(std::__rotr(v, 3) == 0x02468acfU);
assert(std::__countl_zero(v) == 3);

#if TEST_STD_VER > 17
Expand Down
Loading