Commit f81d73e8 authored by Martin Möhrmann's avatar Martin Möhrmann

strconv: add comment explaining bounded shift in formatBits

The compiler can generate better code for shifts bounded to be less than 32
and thereby known to be less than any register width.
See https://golang.org/cl/109776.

Change-Id: I0c4c9f0faafa065fce3c10fd328830deb92f9e38
Reviewed-on: https://go-review.googlesource.com/c/111735
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent a0f57c3f
......@@ -152,10 +152,14 @@ func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s
}
} else if isPowerOfTwo(base) {
// It is known that base is a power of two and
// 2 <= base <= len(digits).
// Use shifts and masks instead of / and %.
shift := uint(bits.TrailingZeros(uint(base))) & 31
// Base is a power of 2 and 2 <= base <= len(digits) where len(digits) is 36.
// The largest power of 2 below or equal to 36 is 32, which is 1 << 5;
// i.e., the largest possible shift count is 5. By &-ind that value with
// the constant 7 we tell the compiler that the shift count is always
// less than 8 which is smaller than any register width. This allows
// the compiler to generate better code for the shift operation.
shift := uint(bits.TrailingZeros(uint(base))) & 7
b := uint64(base)
m := uint(base) - 1 // == 1<<shift - 1
for u >= b {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment