From 8ea499d6620e3a4abdf7babe1b0d8d7bd0aedd22 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 17 Jan 2023 13:11:59 -0800 Subject: [PATCH] Use absl::countl_zero rather than absl::bit_width, to save one instruction, and 4 bytes of x86 code, per use. PiperOrigin-RevId: 502668997 --- src/google/protobuf/io/coded_stream.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h index fc4c55c978..1c923905eb 100644 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h @@ -1727,15 +1727,15 @@ inline size_t CodedOutputStream::VarintSize32(uint32_t value) { // Use an explicit multiplication to implement the divide of // a number in the 1..31 range. // - // Explicit OR 0x1 to avoid calling absl::bit_width(0), which is + // Explicit OR 0x1 to avoid calling absl::countl_zero(0), which // requires a branch to check for on many platforms. - uint32_t log2value = absl::bit_width(value | 0x1) - 1; + uint32_t log2value = 31 - absl::countl_zero(value | 0x1); return static_cast((log2value * 9 + 73) / 64); } inline size_t CodedOutputStream::VarintSize32PlusOne(uint32_t value) { // Same as above, but one more. - uint32_t log2value = absl::bit_width(value | 0x1) - 1; + uint32_t log2value = 31 - absl::countl_zero(value | 0x1); return static_cast((log2value * 9 + 73 + 64) / 64); } @@ -1744,15 +1744,15 @@ inline size_t CodedOutputStream::VarintSize64(uint64_t value) { // Use an explicit multiplication to implement the divide of // a number in the 1..63 range. // - // Explicit OR 0x1 to avoid calling absl::bit_width(0), which is + // Explicit OR 0x1 to avoid calling absl::countl_zero(0), which // requires a branch to check for on many platforms. - uint32_t log2value = absl::bit_width(value | 0x1) - 1; + uint32_t log2value = 63 - absl::countl_zero(value | 0x1); return static_cast((log2value * 9 + 73) / 64); } inline size_t CodedOutputStream::VarintSize64PlusOne(uint64_t value) { // Same as above, but one more. - uint32_t log2value = absl::bit_width(value | 0x1) - 1; + uint32_t log2value = 63 - absl::countl_zero(value | 0x1); return static_cast((log2value * 9 + 73 + 64) / 64); }