Skip to content

Commit bc7a590

Browse files
authored
Simplify BMI use (#18)
TZCNTQ is always safe to use, no need to guard it. We don't care about the zero register. This removes the need for special cleanup.
1 parent c7474f0 commit bc7a590

4 files changed

Lines changed: 242 additions & 772 deletions

File tree

_generate/cleanup.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

_generate/gen.go

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
//go:generate go run gen.go -out ../asm_amd64.s -stubs ../asm_amd64.go -pkg=minlz
1818
//go:generate gofmt -w ../asm_amd64.go
19-
//go:generate go run cleanup.go ../asm_amd64.s
2019

2120
import (
2221
"flag"
@@ -42,15 +41,6 @@ func main() {
4241
Constraint(buildtags.Term("gc").ToConstraint())
4342
Constraint(buildtags.Not("purego").ToConstraint())
4443

45-
// We need a function to add comments.
46-
TEXT("_dummy_", 0, "func()")
47-
Comment("#ifdef GOAMD64_v4")
48-
Comment("#ifndef GOAMD64_v3")
49-
Comment("#define GOAMD64_v3")
50-
Comment("#endif")
51-
Comment("#endif")
52-
RET()
53-
5444
o := options{
5545
bmi1: false,
5646
bmi2: false,
@@ -1880,11 +1870,11 @@ func hashN(o options, hashBytes, tablebits int) hashGen {
18801870
o: o,
18811871
}
18821872
if o.bmi2 {
1883-
if hashBytes < 8 {
1884-
h.clear = GP64()
1885-
MOVQ(U8(hashBytes*8), h.clear)
1873+
if hashBytes < 8 && hashBytes != 4 {
1874+
MOVQ(U32(hashBytes*8), h.mulreg)
1875+
} else {
1876+
MOVQ(U32(tablebits), h.mulreg)
18861877
}
1887-
MOVQ(U8(tablebits), h.mulreg)
18881878
return h
18891879
}
18901880
primebytes := uint64(0)
@@ -1911,15 +1901,32 @@ func hashN(o options, hashBytes, tablebits int) hashGen {
19111901
// hash uses multiply to get hash of the value.
19121902
func (h hashGen) hash(val reg.GPVirtual) {
19131903
if h.o.bmi2 {
1904+
// Broken somehow...
19141905
if h.bytes < 8 {
1915-
BZHIQ(val, h.clear, val)
1906+
if h.bytes == 4 {
1907+
MOVL(val.As32(), val.As32())
1908+
} else {
1909+
//SHLQ(U8(64-8*h.bytes), val)
1910+
BZHIQ(val, h.mulreg, val)
1911+
}
19161912
}
19171913
CRC32Q(val, val)
1918-
BZHIQ(val, h.mulreg, val)
1914+
tmp := h.mulreg
1915+
if h.bytes < 8 && h.bytes != 4 {
1916+
tmp = GP64()
1917+
MOVQ(U32(h.tablebits), tmp)
1918+
}
1919+
//SHRQ(U8(32-h.tablebits), val)
1920+
BZHIQ(val, tmp, val)
1921+
return
19191922
}
19201923
// Move value to top of register.
19211924
if h.bytes < 8 {
1922-
SHLQ(U8(64-8*h.bytes), val)
1925+
if h.bytes == 4 {
1926+
MOVL(val.As32(), val.As32())
1927+
} else {
1928+
SHLQ(U8(64-8*h.bytes), val)
1929+
}
19231930
}
19241931
// 329 AMD64 :IMUL r64, r64 L: 0.86ns= 3.0c T: 0.29ns= 1.00c
19251932
// 2020 BMI2 :MULX r64, r64, r64 L: 1.14ns= 4.0c T: 0.29ns= 1.00c
@@ -3020,13 +3027,7 @@ func (o options) matchLen(name string, a, b, len, dst reg.GPVirtual, end LabelRe
30203027

30213028
Label("matchlen_bsf_16" + name)
30223029
// Not all match.
3023-
Comment("#ifdef GOAMD64_v3")
3024-
// 2016 BMI :TZCNT r64, r64 L: 0.57ns= 2.0c T: 0.29ns= 1.00c
3025-
// 315 AMD64 :BSF r64, r64 L: 0.88ns= 3.1c T: 0.86ns= 3.00c
30263030
TZCNTQ(tmp2, tmp2)
3027-
Comment("#else")
3028-
BSFQ(tmp2, tmp2)
3029-
Comment("#endif")
30303031

30313032
SARQ(U8(3), tmp2)
30323033
LEAL(Mem{Base: matched, Index: tmp2, Scale: 1, Disp: 8}, matched)
@@ -3045,14 +3046,8 @@ func (o options) matchLen(name string, a, b, len, dst reg.GPVirtual, end LabelRe
30453046
Label("matchlen_bsf_8_" + name)
30463047

30473048
// Not all match.
3048-
Comment("#ifdef GOAMD64_v3")
3049-
// 2016 BMI :TZCNT r64, r64 L: 0.57ns= 2.0c T: 0.29ns= 1.00c
3050-
// 315 AMD64 :BSF r64, r64 L: 0.88ns= 3.1c T: 0.86ns= 3.00c
30513049
TZCNTQ(tmp, tmp)
3052-
Comment("#else")
3053-
BSFQ(tmp, tmp)
3054-
Comment("#endif")
3055-
3050+
// tmp is the number of bits that matched.
30563051
SARQ(U8(3), tmp)
30573052
LEAL(Mem{Base: matched, Index: tmp, Scale: 1}, matched)
30583053
JMP(end)
@@ -3129,11 +3124,7 @@ func (o options) matchLenAVX2(name string, a, b, len reg.GPVirtual, cont, end La
31293124
Label(name + "cal_prefix")
31303125
{
31313126
NOTQ(equalMaskBits)
3132-
if o.bmi1 {
3133-
TZCNTQ(equalMaskBits, equalMaskBits)
3134-
} else {
3135-
BSFQ(equalMaskBits, equalMaskBits)
3136-
}
3127+
TZCNTQ(equalMaskBits, equalMaskBits)
31373128
ADDL(equalMaskBits.As32(), dst)
31383129
}
31393130
JMP(end)

asm_amd64.go

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)