From 791afef728b402bceaefa6f5af2a720139a62969 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 6 Jul 2020 05:42:10 +0200 Subject: [PATCH] adapt reflect8 fct --- common/commonutil.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/commonutil.c b/common/commonutil.c index 14f2d58e5..fc287fe9d 100644 --- a/common/commonutil.c +++ b/common/commonutil.c @@ -60,9 +60,21 @@ uint32_t reflect(uint32_t v, int b) { return v; } +// https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable + +// Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division): +uint8_t reflect8(uint8_t b) { + return (b * 0x0202020202ULL & 0x010884422010ULL) % 1023; +} + + +// Reverse the bits in a byte with 4 operations (64-bit multiply, no division): +/* uint8_t reflect8(uint8_t b) { return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; } +*/ + uint16_t reflect16(uint16_t b) { uint16_t v = 0; v |= (b & 0x8000) >> 15; @@ -117,10 +129,13 @@ void lsl(uint8_t *data, size_t len) { data[len - 1] <<= 1; } + +// BSWAP24 of array[3] uint32_t le24toh(uint8_t data[3]) { return (data[2] << 16) | (data[1] << 8) | data[0]; } +// BSWAP24, take u32, output array void htole24(uint32_t val, uint8_t data[3]) { data[0] = (uint8_t) val; data[1] = (uint8_t)(val >> 8);