From f4b01f69a65de8cd212306d884ccbcbb62c59128 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Tue, 17 Sep 2024 22:44:39 +0800 Subject: [PATCH 1/3] fix: reflect64 --- common/commonutil.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/commonutil.c b/common/commonutil.c index 439e1e3cbc..be7576a8c6 100644 --- a/common/commonutil.c +++ b/common/commonutil.c @@ -146,11 +146,11 @@ uint32_t reflect32(uint32_t b) { uint64_t reflect64(uint64_t b) { // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable - uint64_t v = b; // 32-bit word to reverse bit order - // swap 2-byte long pairs + uint64_t v = b; // 64-bit word to reverse bit order + // swap 4-byte long pairs uint64_t v1 = reflect32(v >> 32); uint64_t v2 = reflect32(v); - v = (v1 << 32) | (v2 & 0xFFFFFFFF); + v = (v2 << 32) | (v1 & 0xFFFFFFFF); return v; } From eda204bf8789a38b0a3f8411ed92696fffd85c9d Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Tue, 17 Sep 2024 22:54:43 +0800 Subject: [PATCH 2/3] add: reflect48 --- common/commonutil.c | 7 +++++++ common/commonutil.h | 1 + 2 files changed, 8 insertions(+) diff --git a/common/commonutil.c b/common/commonutil.c index be7576a8c6..f9f039df18 100644 --- a/common/commonutil.c +++ b/common/commonutil.c @@ -144,6 +144,13 @@ uint32_t reflect32(uint32_t b) { return v; } +uint64_t reflect48(uint64_t v) { + uint64_t vhi = reflect16(v >> 32); + uint64_t vlo = reflect32(v); + v = (vlo << 32) | (vhi & 0xFFFF); + return v; +} + uint64_t reflect64(uint64_t b) { // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable uint64_t v = b; // 64-bit word to reverse bit order diff --git a/common/commonutil.h b/common/commonutil.h index 62b1b78e73..a3476a2841 100644 --- a/common/commonutil.h +++ b/common/commonutil.h @@ -74,6 +74,7 @@ uint32_t reflect(uint32_t v, int b); // used in crc.c ... uint8_t reflect8(uint8_t b); // dedicated 8bit reversal uint16_t reflect16(uint16_t b); // dedicated 16bit reversal uint32_t reflect32(uint32_t b); // dedicated 32bit reversal +uint64_t reflect48(uint64_t b); // dedicated 48bit reversal uint64_t reflect64(uint64_t b); // dedicated 64bit reversal void num_to_bytes(uint64_t n, size_t len, uint8_t *dest); From 11353044ca7a2ffca3388623be6619f16be95d86 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Tue, 17 Sep 2024 23:19:30 +0800 Subject: [PATCH 3/3] refactor: cleanup & optimize reflect* --- common/commonutil.c | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/common/commonutil.c b/common/commonutil.c index f9f039df18..966547d6d9 100644 --- a/common/commonutil.c +++ b/common/commonutil.c @@ -98,39 +98,13 @@ 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; - v |= (b & 0x4000) >> 13; - v |= (b & 0x2000) >> 11; - v |= (b & 0x1000) >> 9; - v |= (b & 0x0800) >> 7; - v |= (b & 0x0400) >> 5; - v |= (b & 0x0200) >> 3; - v |= (b & 0x0100) >> 1; - - v |= (b & 0x0080) << 1; - v |= (b & 0x0040) << 3; - v |= (b & 0x0020) << 5; - v |= (b & 0x0010) << 7; - v |= (b & 0x0008) << 9; - v |= (b & 0x0004) << 11; - v |= (b & 0x0002) << 13; - v |= (b & 0x0001) << 15; +uint16_t reflect16(uint16_t v) { + v = (reflect8(v) << 8) | (reflect8(v >> 8) & 0xFF); return v; } -uint32_t reflect32(uint32_t b) { +uint32_t reflect32(uint32_t v) { // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable - uint32_t v = b; // 32-bit word to reverse bit order // swap odd and even bits v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1); // swap consecutive pairs @@ -140,7 +114,7 @@ uint32_t reflect32(uint32_t b) { // swap bytes v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); // swap 2-byte long pairs - v = (v >> 16) | (v << 16); + v = ( v >> 16 ) | ( v << 16); return v; } @@ -151,9 +125,8 @@ uint64_t reflect48(uint64_t v) { return v; } -uint64_t reflect64(uint64_t b) { +uint64_t reflect64(uint64_t v) { // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable - uint64_t v = b; // 64-bit word to reverse bit order // swap 4-byte long pairs uint64_t v1 = reflect32(v >> 32); uint64_t v2 = reflect32(v);