From 45923c08b00ccdfd888caf6d2b555a137c563988 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 18 Oct 2017 16:13:24 +0000 Subject: Better packing / unpacking code Godbolted for x86_64 with gcc and clang. --- src/libstddjb/uint64_reverse.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/libstddjb/uint64_reverse.c') diff --git a/src/libstddjb/uint64_reverse.c b/src/libstddjb/uint64_reverse.c index 5a616f5..a870b31 100644 --- a/src/libstddjb/uint64_reverse.c +++ b/src/libstddjb/uint64_reverse.c @@ -1,15 +1,16 @@ /* ISC license. */ +#include #include void uint64_reverse (char *s, size_t n) { while (n--) { - char c = s[0] ; s[0] = s[7] ; s[7] = c ; - c = s[1] ; s[1] = s[6] ; s[6] = c ; - c = s[2] ; s[2] = s[5] ; s[5] = c ; - c = s[3] ; s[3] = s[4] ; s[4] = c ; - s += 8 ; + uint64_t x ; + memcpy(&x, s, sizeof(uint64_t)) ; + x = uint64_bswap(x) ; + memcpy(s, &x, sizeof(uint64_t)) ; + s += sizeof(uint64_t) ; } } -- cgit v1.2.3