[libcxx-commits] [libcxx] [libc++][bit] Improves rotate functions. (PR #98032)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 30 09:14:49 PDT 2024


================
@@ -20,24 +20,37 @@
 
 _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 __rotr(_Tp __t, int __cnt) _NOEXCEPT {
-  static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
-  const unsigned int __dig = numeric_limits<_Tp>::digits;
-  if ((__cnt % __dig) == 0)
-    return __t;
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {
+  static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
+  const unsigned int __N = numeric_limits<_Tp>::digits;
----------------
ldionne wrote:

Agreed with other reviewers, I think we want:

```suggestion
  const int __N = numeric_limits<_Tp>::digits;
```

We can't do `constexpr` because of C++03, but it shouldn't matter.

https://github.com/llvm/llvm-project/pull/98032


More information about the libcxx-commits mailing list