mirror of
https://github.com/golang/go
synced 2024-11-24 04:50:07 -07:00
math/bits: optimize Reverse32 and Reverse64
Use ReverseBytes32 and ReverseBytes64 to speed up these functions. The byte reversal functions are intrinsics on most platforms and generally compile to a single instruction. name old time/op new time/op delta Reverse32 2.41ns ± 1% 1.94ns ± 3% -19.60% (p=0.000 n=20+19) Reverse64 3.85ns ± 1% 2.56ns ± 1% -33.32% (p=0.000 n=17+19) Change-Id: I160bf59a0c7bd5db94114803ec5a59fae448f096 Reviewed-on: https://go-review.googlesource.com/c/159358 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
b65ab889ab
commit
42a82ce1a7
@ -232,8 +232,7 @@ func Reverse32(x uint32) uint32 {
|
|||||||
x = x>>1&(m0&m) | x&(m0&m)<<1
|
x = x>>1&(m0&m) | x&(m0&m)<<1
|
||||||
x = x>>2&(m1&m) | x&(m1&m)<<2
|
x = x>>2&(m1&m) | x&(m1&m)<<2
|
||||||
x = x>>4&(m2&m) | x&(m2&m)<<4
|
x = x>>4&(m2&m) | x&(m2&m)<<4
|
||||||
x = x>>8&(m3&m) | x&(m3&m)<<8
|
return ReverseBytes32(x)
|
||||||
return x>>16 | x<<16
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse64 returns the value of x with its bits in reversed order.
|
// Reverse64 returns the value of x with its bits in reversed order.
|
||||||
@ -242,9 +241,7 @@ func Reverse64(x uint64) uint64 {
|
|||||||
x = x>>1&(m0&m) | x&(m0&m)<<1
|
x = x>>1&(m0&m) | x&(m0&m)<<1
|
||||||
x = x>>2&(m1&m) | x&(m1&m)<<2
|
x = x>>2&(m1&m) | x&(m1&m)<<2
|
||||||
x = x>>4&(m2&m) | x&(m2&m)<<4
|
x = x>>4&(m2&m) | x&(m2&m)<<4
|
||||||
x = x>>8&(m3&m) | x&(m3&m)<<8
|
return ReverseBytes64(x)
|
||||||
x = x>>16&(m4&m) | x&(m4&m)<<16
|
|
||||||
return x>>32 | x<<32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- ReverseBytes ---
|
// --- ReverseBytes ---
|
||||||
|
Loading…
Reference in New Issue
Block a user