[PATCH] D143882: [ADT] Add llvm::rotl and llvm::rotr to bit.h
Kazu Hirata via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 13 09:40:45 PST 2023
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGf1f62ed0c834: [ADT] Add llvm::rotl and llvm::rotr to bit.h (authored by kazu).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D143882/new/
https://reviews.llvm.org/D143882
Files:
llvm/include/llvm/ADT/bit.h
llvm/unittests/ADT/BitTest.cpp
Index: llvm/unittests/ADT/BitTest.cpp
===================================================================
--- llvm/unittests/ADT/BitTest.cpp
+++ llvm/unittests/ADT/BitTest.cpp
@@ -354,4 +354,48 @@
EXPECT_EQ(1, llvm::popcount(1U << I));
}
+TEST(BitTest, Rotl) {
+ EXPECT_EQ(0x53U, llvm::rotl<uint8_t>(0x53, 0));
+ EXPECT_EQ(0x4dU, llvm::rotl<uint8_t>(0x53, 2));
+ EXPECT_EQ(0xa6U, llvm::rotl<uint8_t>(0x53, 9));
+ EXPECT_EQ(0x9aU, llvm::rotl<uint8_t>(0x53, -5));
+
+ EXPECT_EQ(0xabcdU, llvm::rotl<uint16_t>(0xabcd, 0));
+ EXPECT_EQ(0xf36aU, llvm::rotl<uint16_t>(0xabcd, 6));
+ EXPECT_EQ(0xaf36U, llvm::rotl<uint16_t>(0xabcd, 18));
+ EXPECT_EQ(0xf36aU, llvm::rotl<uint16_t>(0xabcd, -10));
+
+ EXPECT_EQ(0xdeadbeefU, llvm::rotl<uint32_t>(0xdeadbeef, 0));
+ EXPECT_EQ(0x7ddfbd5bU, llvm::rotl<uint32_t>(0xdeadbeef, 17));
+ EXPECT_EQ(0x5b7ddfbdU, llvm::rotl<uint32_t>(0xdeadbeef, 41));
+ EXPECT_EQ(0xb6fbbf7aU, llvm::rotl<uint32_t>(0xdeadbeef, -22));
+
+ EXPECT_EQ(0x12345678deadbeefULL, llvm::rotl<uint64_t>(0x12345678deadbeefULL, 0));
+ EXPECT_EQ(0xf56df77891a2b3c6ULL, llvm::rotl<uint64_t>(0x12345678deadbeefULL, 35));
+ EXPECT_EQ(0x8d159e37ab6fbbc4ULL, llvm::rotl<uint64_t>(0x12345678deadbeefULL, 70));
+ EXPECT_EQ(0xb7dde2468acf1bd5ULL, llvm::rotl<uint64_t>(0x12345678deadbeefULL, -19));
+}
+
+TEST(BitTest, Rotr) {
+ EXPECT_EQ(0x53U, llvm::rotr<uint8_t>(0x53, 0));
+ EXPECT_EQ(0xd4U, llvm::rotr<uint8_t>(0x53, 2));
+ EXPECT_EQ(0xa9U, llvm::rotr<uint8_t>(0x53, 9));
+ EXPECT_EQ(0x6aU, llvm::rotr<uint8_t>(0x53, -5));
+
+ EXPECT_EQ(0xabcdU, llvm::rotr<uint16_t>(0xabcd, 0));
+ EXPECT_EQ(0x36afU, llvm::rotr<uint16_t>(0xabcd, 6));
+ EXPECT_EQ(0x6af3U, llvm::rotr<uint16_t>(0xabcd, 18));
+ EXPECT_EQ(0x36afU, llvm::rotr<uint16_t>(0xabcd, -10));
+
+ EXPECT_EQ(0xdeadbeefU, llvm::rotr<uint32_t>(0xdeadbeef, 0));
+ EXPECT_EQ(0xdf77ef56U, llvm::rotr<uint32_t>(0xdeadbeef, 17));
+ EXPECT_EQ(0x77ef56dfU, llvm::rotr<uint32_t>(0xdeadbeef, 41));
+ EXPECT_EQ(0xbbf7ab6fU, llvm::rotr<uint32_t>(0xdeadbeef, -22));
+
+ EXPECT_EQ(0x12345678deadbeefULL, llvm::rotr<uint64_t>(0x12345678deadbeefULL, 0));
+ EXPECT_EQ(0x1bd5b7dde2468acfULL, llvm::rotr<uint64_t>(0x12345678deadbeefULL, 35));
+ EXPECT_EQ(0xbc48d159e37ab6fbULL, llvm::rotr<uint64_t>(0x12345678deadbeefULL, 70));
+ EXPECT_EQ(0xb3c6f56df77891a2ULL, llvm::rotr<uint64_t>(0x12345678deadbeefULL, -19));
+}
+
} // anonymous namespace
Index: llvm/include/llvm/ADT/bit.h
===================================================================
--- llvm/include/llvm/ADT/bit.h
+++ llvm/include/llvm/ADT/bit.h
@@ -350,6 +350,37 @@
return detail::PopulationCounter<T, sizeof(T)>::count(Value);
}
+// 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;
+
+ R = R % N;
+ if (!R)
+ return V;
+
+ if (R < 0)
+ return llvm::rotr(V, -R);
+
+ 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;
+
+ R = R % N;
+ if (!R)
+ return V;
+
+ if (R < 0)
+ return llvm::rotl(V, -R);
+
+ return (V >> R) | (V << (N - R));
+}
+
} // namespace llvm
#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143882.497025.patch
Type: text/x-patch
Size: 3434 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230213/7b3a72f1/attachment.bin>
More information about the llvm-commits
mailing list