aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
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 */