From 54644844036a5eeaf3830ad327ad8a2ba197a4dd Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 10 Apr 2022 02:10:13 +0200 Subject: byte_util.hpp: Add manual optimization to generic bswap on uint8_t src -> dest --- include/jau/byte_util.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'include/jau/byte_util.hpp') diff --git a/include/jau/byte_util.hpp b/include/jau/byte_util.hpp index e8b4e53..9805747 100644 --- a/include/jau/byte_util.hpp +++ b/include/jau/byte_util.hpp @@ -63,6 +63,8 @@ namespace jau { #define __has_builtin_bswap64 1 #endif + // Remember: constexpr specifier used in a function or static data member (since C++17) declaration implies inline. + constexpr uint16_t bswap(uint16_t const source) noexcept { #if defined __has_builtin_bswap16 return __builtin_bswap16(source); @@ -98,9 +100,10 @@ namespace jau { #endif } - constexpr void bswap(uint8_t * const sink, uint8_t const * const source, nsize_t const len) { - for(nsize_t i=0; i < len; ++i) { - sink[i] = source[len-1-i]; + constexpr void bswap(uint8_t * sink, uint8_t const * source, nsize_t len) { + source += len - 1; + for (; len > 0; len--) { + *sink++ = *source--; } } -- cgit v1.2.3