aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2026-02-06 12:43:26 +0100
committerGitHub Enterprise <[email protected]>2026-02-06 12:43:26 +0100
commiteabf78d92a560f2480b0ca4409069be08e1e1f15 (patch)
tree04a915e8326c12897a73d84b7814a4d68a925d61 /src
parentfix target folder cleanup in builds download (#746) (diff)
downloadzen-eabf78d92a560f2480b0ca4409069be08e1e1f15.tar.xz
zen-eabf78d92a560f2480b0ca4409069be08e1e1f15.zip
ported optimizations of MeasureVarUInt (#747)
Diffstat (limited to 'src')
-rw-r--r--src/zencore/include/zencore/varint.h14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/zencore/include/zencore/varint.h b/src/zencore/include/zencore/varint.h
index ae9aceed6..9fe905f25 100644
--- a/src/zencore/include/zencore/varint.h
+++ b/src/zencore/include/zencore/varint.h
@@ -76,14 +76,24 @@ MeasureVarInt(const void* InData)
inline uint32_t
MeasureVarUInt(uint32_t InValue)
{
- return uint32_t(int32_t(FloorLog2(InValue)) / 7 + 1);
+ // return
+ // This branching implementation is faster than branchless alternatives in real-world benchmarks.
+ // Replaces: uint32_t(int32_t(FloorLog2(InValue)) / 7 + 1);
+ return (InValue < (uint32_t(1) << 28))
+ ? ((InValue < (uint32_t(1) << 14)) ? ((InValue < (uint32_t(1) << 7)) ? 1 : 2) : ((InValue < (uint32_t(1) << 21)) ? 3 : 4))
+ : 5;
}
/** Measure the number of bytes (1-9) required to encode the 64-bit input. */
inline uint32_t
MeasureVarUInt(uint64_t InValue)
{
- return uint32_t(std::min(int32_t(FloorLog2_64(InValue)) / 7 + 1, 9));
+ // This branching implementation is faster than branchless alternatives in real-world benchmarks.
+ // Replaces: uint32_t(std::min(int32_t(FloorLog2_64(InValue)) / 7 + 1, 9));
+ return (InValue < (uint64_t(1) << 28))
+ ? ((InValue < (uint64_t(1) << 14)) ? ((InValue < (uint64_t(1) << 7)) ? 1 : 2) : ((InValue < (uint64_t(1) << 21)) ? 3 : 4))
+ : ((InValue < (uint64_t(1) << 42)) ? ((InValue < (uint64_t(1) << 35)) ? 5 : 6)
+ : ((InValue < (uint64_t(1) << 49)) ? 7 : ((InValue < (uint64_t(1) << 56)) ? 8 : 9)));
}
/** Measure the number of bytes (1-5) required to encode the 32-bit input. \see \ref MeasureVarUInt */