From e2d6008d2e13a3265d0939d02d2c963c5b940704 Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Sat, 14 Jun 2014 12:52:55 +0000 Subject: [PATCH] Adding llvm::sys::swapByteOrder() for the common use-case of byte-swapping a value in place llvm-svn: 210976 --- llvm/include/llvm/Support/SwapByteOrder.h | 5 +++ llvm/unittests/Support/SwapByteOrderTest.cpp | 48 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/llvm/include/llvm/Support/SwapByteOrder.h b/llvm/include/llvm/Support/SwapByteOrder.h index 468a8235..340954f 100644 --- a/llvm/include/llvm/Support/SwapByteOrder.h +++ b/llvm/include/llvm/Support/SwapByteOrder.h @@ -95,6 +95,11 @@ inline signed long long getSwappedBytes(signed long long C) { return SwapByteOrder_64(C); } +template +inline void swapByteOrder(T &Value) { + Value = getSwappedBytes(Value); +} + } // end namespace sys } // end namespace llvm diff --git a/llvm/unittests/Support/SwapByteOrderTest.cpp b/llvm/unittests/Support/SwapByteOrderTest.cpp index a07282e..525cfc1 100644 --- a/llvm/unittests/Support/SwapByteOrderTest.cpp +++ b/llvm/unittests/Support/SwapByteOrderTest.cpp @@ -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); +} + } -- 2.7.4