[llvm] r210976 - Adding llvm::sys::swapByteOrder() for the common use-case of byte-swapping a value in place
Artyom Skrobov
Artyom.Skrobov at arm.com
Sat Jun 14 05:52:55 PDT 2014
Author: askrobov
Date: Sat Jun 14 07:52:55 2014
New Revision: 210976
URL: http://llvm.org/viewvc/llvm-project?rev=210976&view=rev
Log:
Adding llvm::sys::swapByteOrder() for the common use-case of byte-swapping a value in place
Modified:
llvm/trunk/include/llvm/Support/SwapByteOrder.h
llvm/trunk/unittests/Support/SwapByteOrderTest.cpp
Modified: llvm/trunk/include/llvm/Support/SwapByteOrder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SwapByteOrder.h?rev=210976&r1=210975&r2=210976&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/SwapByteOrder.h (original)
+++ llvm/trunk/include/llvm/Support/SwapByteOrder.h Sat Jun 14 07:52:55 2014
@@ -95,6 +95,11 @@ inline signed long long getSwappedBytes(
return SwapByteOrder_64(C);
}
+template<typename T>
+inline void swapByteOrder(T &Value) {
+ Value = getSwappedBytes(Value);
+}
+
} // end namespace sys
} // end namespace llvm
Modified: llvm/trunk/unittests/Support/SwapByteOrderTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/SwapByteOrderTest.cpp?rev=210976&r1=210975&r2=210976&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/SwapByteOrderTest.cpp (original)
+++ llvm/trunk/unittests/Support/SwapByteOrderTest.cpp Sat Jun 14 07:52:55 2014
@@ -125,4 +125,52 @@ TEST(getSwappedBytes, int64_t) {
sys::getSwappedBytes(int64_t(0x8877665544332211LL)));
}
+TEST(swapByteOrder, uint8_t) {
+ uint8_t value = 0x11;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(uint8_t(0x11), value);
+}
+
+TEST(swapByteOrder, uint16_t) {
+ uint16_t value = 0x2211;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(uint16_t(0x1122), value);
+}
+
+TEST(swapByteOrder, uint32_t) {
+ uint32_t value = 0x44332211;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(uint32_t(0x11223344), value);
+}
+
+TEST(swapByteOrder, uint64_t) {
+ uint64_t value = 0x8877665544332211ULL;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(uint64_t(0x1122334455667788ULL), value);
+}
+
+TEST(swapByteOrder, int8_t) {
+ int8_t value = 0x11;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(int8_t(0x11), value);
+}
+
+TEST(swapByteOrder, int16_t) {
+ int16_t value = 0x2211;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(int16_t(0x1122), value);
+}
+
+TEST(swapByteOrder, int32_t) {
+ int32_t value = 0x44332211;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(int32_t(0x11223344), value);
+}
+
+TEST(swapByteOrder, int64_t) {
+ int64_t value = 0x8877665544332211LL;
+ sys::swapByteOrder(value);
+ EXPECT_EQ(int64_t(0x1122334455667788LL), value);
+}
+
}
More information about the llvm-commits
mailing list