[llvm] e3609ae - [ADT] Simplify rotl/rotr implementations (NFC) (#164055)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 18 10:17:09 PDT 2025
Author: Kazu Hirata
Date: 2025-10-18T10:17:05-07:00
New Revision: e3609ae585088789bd5d5ca5425a2ca66b4c58df
URL: https://github.com/llvm/llvm-project/commit/e3609ae585088789bd5d5ca5425a2ca66b4c58df
DIFF: https://github.com/llvm/llvm-project/commit/e3609ae585088789bd5d5ca5425a2ca66b4c58df.diff
LOG: [ADT] Simplify rotl/rotr implementations (NFC) (#164055)
This patch simplifies rotl and rotr by ANDing the rotate amount with
N - 1. This way, we can remove the mutual dependencies and the
forward declaration of rotr.
Added:
Modified:
llvm/include/llvm/ADT/bit.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/bit.h b/llvm/include/llvm/ADT/bit.h
index 66c4f94813241..8c68d0a90c753 100644
--- a/llvm/include/llvm/ADT/bit.h
+++ b/llvm/include/llvm/ADT/bit.h
@@ -336,33 +336,28 @@ template <typename T> [[nodiscard]] T bit_ceil(T Value) {
return T(1) << llvm::bit_width<T>(Value - 1u);
}
-// Forward-declare rotr so that rotl can use it.
-template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
-[[nodiscard]] constexpr T rotr(T V, int R);
-
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
[[nodiscard]] constexpr T rotl(T V, int R) {
- unsigned N = std::numeric_limits<T>::digits;
+ constexpr unsigned N = std::numeric_limits<T>::digits;
- R = R % N;
- if (!R)
- return V;
+ static_assert(has_single_bit(N), "& (N - 1) is only valid for powers of two");
+ R = R & (N - 1);
- if (R < 0)
- return llvm::rotr(V, -R);
+ if (R == 0)
+ return V;
return (V << R) | (V >> (N - R));
}
-template <typename T, typename> [[nodiscard]] constexpr T rotr(T V, int R) {
- unsigned N = std::numeric_limits<T>::digits;
+template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
+[[nodiscard]] constexpr T rotr(T V, int R) {
+ constexpr unsigned N = std::numeric_limits<T>::digits;
- R = R % N;
- if (!R)
- return V;
+ static_assert(has_single_bit(N), "& (N - 1) is only valid for powers of two");
+ R = R & (N - 1);
- if (R < 0)
- return llvm::rotl(V, -R);
+ if (R == 0)
+ return V;
return (V >> R) | (V << (N - R));
}
More information about the llvm-commits
mailing list