Skip to content

Commit

Permalink
Use absl::countl_zero rather than absl::bit_width, to save one instru…
Browse files Browse the repository at this point in the history
…ction, and 4 bytes of x86 code, per use.

PiperOrigin-RevId: 502668997
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 17, 2023
1 parent b80cc53 commit 8ea499d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/google/protobuf/io/coded_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 8ea499d

Please sign in to comment.