Use absl::countl_zero rather than absl::bit_width, to save one instruction, and 4 bytes of x86 code, per use.

PiperOrigin-RevId: 502668997
pull/11520/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent b80cc536de
commit 8ea499d662
  1. 12
      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<size_t>((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<size_t>((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<size_t>((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<size_t>((log2value * 9 + 73 + 64) / 64);
}

Loading…
Cancel
Save