1
0
mirror of https://github.com/golang/go synced 2024-09-30 22:58:34 -06:00

math/bits: faster Reverse, ReverseBytes

- moved from: x&m>>k | x&^m<<k to: x&m>>k | x<<k&m
  This permits use of the same constant m twice (*) which may be
  better for machines that can't use large immediate constants
  directly with an AND instruction and have to load them explicitly.
  *) CPUs don't usually have a &^ instruction, so x&^m becomes x&(^m)

- simplified returns
  This improves the generated code because the compiler recognizes
  x>>k | x<<k as ROT when k is the bitsize of x.

The 8-bit versions of these instructions can be significantly faster
still if they are replaced with table lookups, as long as the table
is in cache. If the table is not in cache, table-lookup is probably
slower, hence the choice of an explicit register-only implementation
for now.

BenchmarkReverse-8            8.50          6.86          -19.29%
BenchmarkReverse8-8           2.17          1.74          -19.82%
BenchmarkReverse16-8          2.89          2.34          -19.03%
BenchmarkReverse32-8          3.55          2.95          -16.90%
BenchmarkReverse64-8          6.81          5.57          -18.21%
BenchmarkReverseBytes-8       3.49          2.48          -28.94%
BenchmarkReverseBytes16-8     0.93          0.62          -33.33%
BenchmarkReverseBytes32-8     1.55          1.13          -27.10%
BenchmarkReverseBytes64-8     2.47          2.47          +0.00%

Reverse-8         8.50ns ± 0%  6.86ns ± 0%   ~             (p=1.000 n=1+1)
Reverse8-8        2.17ns ± 0%  1.74ns ± 0%   ~             (p=1.000 n=1+1)
Reverse16-8       2.89ns ± 0%  2.34ns ± 0%   ~             (p=1.000 n=1+1)
Reverse32-8       3.55ns ± 0%  2.95ns ± 0%   ~             (p=1.000 n=1+1)
Reverse64-8       6.81ns ± 0%  5.57ns ± 0%   ~             (p=1.000 n=1+1)
ReverseBytes-8    3.49ns ± 0%  2.48ns ± 0%   ~             (p=1.000 n=1+1)
ReverseBytes16-8  0.93ns ± 0%  0.62ns ± 0%   ~             (p=1.000 n=1+1)
ReverseBytes32-8  1.55ns ± 0%  1.13ns ± 0%   ~             (p=1.000 n=1+1)
ReverseBytes64-8  2.47ns ± 0%  2.47ns ± 0%   ~     (all samples are equal)

Change-Id: I0064de8c7e0e568ca7885d6f7064344bef91a06d
Reviewed-on: https://go-review.googlesource.com/37215
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2017-02-17 13:27:09 -08:00
parent c61cf5e6b7
commit 4498b68390

View File

@ -101,7 +101,6 @@ const m1 = 0xcccccccccccccccc // 11001100 ...
const m2 = 0xf0f0f0f0f0f0f0f0 // 11110000 ...
const m3 = 0xff00ff00ff00ff00 // etc.
const m4 = 0xffff0000ffff0000
const m5 = 0xffffffff00000000
// Reverse returns the value of x with its bits in reversed order.
func Reverse(x uint) uint {
@ -114,43 +113,39 @@ func Reverse(x uint) uint {
// Reverse8 returns the value of x with its bits in reversed order.
func Reverse8(x uint8) uint8 {
const m = 1<<8 - 1
x = x&(m0&m)>>1 | x&^(m0&m)<<1
x = x&(m1&m)>>2 | x&^(m1&m)<<2
x = x&(m2&m)>>4 | x&^(m2&m)<<4
return x
x = x&(m0&m)>>1 | x<<1&(m0&m)
x = x&(m1&m)>>2 | x<<2&(m1&m)
return x>>4 | x<<4
}
// Reverse16 returns the value of x with its bits in reversed order.
func Reverse16(x uint16) uint16 {
const m = 1<<16 - 1
x = x&(m0&m)>>1 | x&^(m0&m)<<1
x = x&(m1&m)>>2 | x&^(m1&m)<<2
x = x&(m2&m)>>4 | x&^(m2&m)<<4
x = x&(m3&m)>>8 | x&^(m3&m)<<8
return x
x = x&(m0&m)>>1 | x<<1&(m0&m)
x = x&(m1&m)>>2 | x<<2&(m1&m)
x = x&(m2&m)>>4 | x<<4&(m2&m)
return x>>8 | x<<8
}
// Reverse32 returns the value of x with its bits in reversed order.
func Reverse32(x uint32) uint32 {
const m = 1<<32 - 1
x = x&(m0&m)>>1 | x&^(m0&m)<<1
x = x&(m1&m)>>2 | x&^(m1&m)<<2
x = x&(m2&m)>>4 | x&^(m2&m)<<4
x = x&(m3&m)>>8 | x&^(m3&m)<<8
x = x&(m4&m)>>16 | x&^(m4&m)<<16
return x
x = x&(m0&m)>>1 | x<<1&(m0&m)
x = x&(m1&m)>>2 | x<<2&(m1&m)
x = x&(m2&m)>>4 | x<<4&(m2&m)
x = x&(m3&m)>>8 | x<<8&(m3&m)
return x>>16 | x<<16
}
// Reverse64 returns the value of x with its bits in reversed order.
func Reverse64(x uint64) uint64 {
const m = 1<<64 - 1
x = x&(m0&m)>>1 | x&^(m0&m)<<1
x = x&(m1&m)>>2 | x&^(m1&m)<<2
x = x&(m2&m)>>4 | x&^(m2&m)<<4
x = x&(m3&m)>>8 | x&^(m3&m)<<8
x = x&(m4&m)>>16 | x&^(m4&m)<<16
x = x&(m5&m)>>32 | x&^(m5&m)<<32
return x
x = x&(m0&m)>>1 | x<<1&(m0&m)
x = x&(m1&m)>>2 | x<<2&(m1&m)
x = x&(m2&m)>>4 | x<<4&(m2&m)
x = x&(m3&m)>>8 | x<<8&(m3&m)
x = x&(m4&m)>>16 | x<<16&(m4&m)
return x>>32 | x<<32
}
// --- ReverseBytes ---
@ -165,26 +160,22 @@ func ReverseBytes(x uint) uint {
// ReverseBytes16 returns the value of x with its bytes in reversed order.
func ReverseBytes16(x uint16) uint16 {
const m = 1<<16 - 1
x = x&(m3&m)>>8 | x&^(m3&m)<<8
return x
return x>>8 | x<<8
}
// ReverseBytes32 returns the value of x with its bytes in reversed order.
func ReverseBytes32(x uint32) uint32 {
const m = 1<<32 - 1
x = x&(m3&m)>>8 | x&^(m3&m)<<8
x = x&(m4&m)>>16 | x&^(m4&m)<<16
return x
x = x&(m3&m)>>8 | x<<8&(m3&m)
return x>>16 | x<<16
}
// ReverseBytes64 returns the value of x with its bytes in reversed order.
func ReverseBytes64(x uint64) uint64 {
const m = 1<<64 - 1
x = x&(m3&m)>>8 | x&^(m3&m)<<8
x = x&(m4&m)>>16 | x&^(m4&m)<<16
x = x&(m5&m)>>32 | x&^(m5&m)<<32
return x
x = x&(m3&m)>>8 | x<<8&(m3&m)
x = x&(m4&m)>>16 | x<<16&(m4&m)
return x>>32 | x<<32
}
// --- Len ---