Skip to content

Instantly share code, notes, and snippets.

@klauspost
Last active February 17, 2016 14:37
Show Gist options
  • Save klauspost/48e20a12d22f0e03dd41 to your computer and use it in GitHub Desktop.
Save klauspost/48e20a12d22f0e03dd41 to your computer and use it in GitHub Desktop.
PASS
BenchmarkShift-4 20000 69666 ns/op 940.71 MB/s
BenchmarkAnd-4 20000 66802 ns/op 981.04 MB/s
ok _/c_/Temp/shifttest 4.146s
PASS
BenchmarkShift-4 20000 68680 ns/op 954.21 MB/s
BenchmarkAnd-4 20000 67339 ns/op 973.21 MB/s
ok _/c_/Temp/shifttest 4.121s
PASS
BenchmarkShift-4 20000 68687 ns/op 954.12 MB/s
BenchmarkAnd-4 20000 67786 ns/op 966.80 MB/s
ok _/c_/Temp/shifttest 4.217s
package shifttest
func reference(a, b []uint64) {
for i := range a {
a[i] = (a[i] & 0x1ff) | (b[i] & 0xfffffffffffffc00)
}
}
func testShift(a, b []uint64)
func testAnd(a, b []uint64)
// func testShift(a, b []uint64)
TEXT ·testShift(SB), 7, $0
MOVQ a+0(FP), SI // SI: &a
MOVQ a_len+8(FP), R10 // R10: len(out)
MOVQ b+24(FP), DX // DX: &b
shloop:
MOVQ (SI), AX
MOVQ (DX), BX
SHLQ $10, AX // a <<= 10
SHRQ $54, BX // b >>= 54
SHRQ $10, AX // a >>= 10
SHLQ $54, BX // b <<= 54
ORQ AX, BX // b |= a
MOVQ BX, (SI) // *a = b
ADDQ $8, SI // move to next
ADDQ $8, DX // move to next
DECQ R10
JNZ shloop
RET
// func testAnd(a, b []uint64)
TEXT ·testAnd(SB), 7, $0
MOVQ a+0(FP), SI // SI: &a
MOVQ a_len+8(FP), R10 // R10: len(out)
MOVQ b+24(FP), DX // DX: &b
andloop:
MOVQ (SI), AX
MOVQ (DX), BX
MOVQ $0x1ff, R9
ANDQ R9, AX
MOVQ $0xfffffffffffffc00, CX
ANDQ CX, BX
ORQ AX, BX // b |= a
MOVQ BX, (SI) // *a = b
ADDQ $8, SI // move to next
ADDQ $8, DX // move to next
DECQ R10
JNZ andloop
RET
package shifttest
import (
"testing"
)
/*
func TestConstants(t *testing.T) {
fmt.Printf("%x\n", uint64(((1<<64)-1)-((1<<10)-1)))
}
*/
func BenchmarkShift(b *testing.B) {
const size = 64 << 10
var x [size]uint64
var y [size]uint64
b.SetBytes(size)
// Warm up
testShift(x[:], y[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
testShift(x[:], y[:])
}
}
func BenchmarkAnd(b *testing.B) {
const size = 64 << 10
var x [size]uint64
var y [size]uint64
// Warm up
testAnd(x[:], y[:])
b.SetBytes(size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
testAnd(x[:], y[:])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment