From 31f369d051cdd3c30b0f3bb9a2ca18719d855fb5 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 5 Feb 2023 20:40:54 +0100 Subject: [PATCH] Add signed versions of `swapOrder()` functions --- conversion/binaryconversion.h | 32 +++++++++++++++++++++++++++++++- tests/conversiontests.cpp | 3 +++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/conversion/binaryconversion.h b/conversion/binaryconversion.h index 36b515a..66ca7b7 100644 --- a/conversion/binaryconversion.h +++ b/conversion/binaryconversion.h @@ -2,12 +2,12 @@ #define CONVERSION_UTILITIES_BINARY_CONVERSION_H #include "../global.h" +#include "../misc/traits.h" #include // use helpers from bits header if available instead of custom code using bit operations #if __cplusplus >= 202002L -#include "../misc/traits.h" #include #endif @@ -167,6 +167,36 @@ CPP_UTILITIES_EXPORT constexpr std::uint64_t swapOrder(std::uint64_t value) | ((value & 0x000000000000FF00) << (5 * 8)) | ((value) << (7 * 8)); } +/*! + * \brief Swaps the byte order of the specified 16-bit integer. + */ +CPP_UTILITIES_EXPORT constexpr std::int16_t swapOrder(std::int16_t value) +{ + return static_cast(((static_cast(value) >> 8) & 0x00FF) | ((static_cast(value) << 8) & 0xFF00)); +} + +/*! + * \brief Swaps the byte order of the specified 32-bit integer. + */ +CPP_UTILITIES_EXPORT constexpr std::int32_t swapOrder(std::int32_t value) +{ + return static_cast((static_cast(value) >> 24) | ((static_cast(value) & 0x00FF0000) >> 8) + | ((static_cast(value) & 0x0000FF00) << 8) | (static_cast(value) << 24)); +} + +/*! + * \brief Swaps the byte order of the specified 64-bit integer. + */ +CPP_UTILITIES_EXPORT constexpr std::int64_t swapOrder(std::int64_t value) +{ + return static_cast((static_cast(value) >> (7 * 8)) + | ((static_cast(value) & 0x00FF000000000000) >> (5 * 8)) + | ((static_cast(value) & 0x0000FF0000000000) >> (3 * 8)) + | ((static_cast(value) & 0x000000FF00000000) >> (1 * 8)) + | ((static_cast(value) & 0x00000000FF000000) << (1 * 8)) + | ((static_cast(value) & 0x0000000000FF0000) << (3 * 8)) + | ((static_cast(value) & 0x000000000000FF00) << (5 * 8)) | ((static_cast(value)) << (7 * 8))); +} #endif /*! diff --git a/tests/conversiontests.cpp b/tests/conversiontests.cpp index 01c23f0..e64ff4b 100644 --- a/tests/conversiontests.cpp +++ b/tests/conversiontests.cpp @@ -27,6 +27,9 @@ static_assert(toNormalInt(383) == 255, "toNormalInt()"); static_assert(swapOrder(static_cast(0xABCD)) == 0xCDAB, "swapOrder(std::uint16_t)"); static_assert(swapOrder(static_cast(0xABCDEF12u)) == 0x12EFCDABu, "swapOrder(std::uint32_t)"); static_assert(swapOrder(static_cast(0xABCDEF1234567890ul)) == 0x9078563412EFCDABul, "swapOrder(std::uint64_t)"); +static_assert(swapOrder(static_cast(0xABCD)) == static_cast(0xCDAB), "swapOrder(std::int16_t)"); +static_assert(swapOrder(static_cast(0xABCDEF12)) == 0x12EFCDAB, "swapOrder(std::int32_t)"); +static_assert(swapOrder(static_cast(0xABCDEF1234567890l)) == static_cast(0x9078563412EFCDABl), "swapOrder(std::int64_t)"); /*! * \brief The ConversionTests class tests classes and functions provided by the files inside the conversion directory.