Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Memset() #9

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions and_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ func popcnt(a []byte) int {
ret += popcntGeneric(a[l:])
return ret
}

func memset(dst []byte, b byte) {
l := uint64(0)
if hasAVX2() {
l = uint64(len(dst)) >> 5
if l != 0 {
memsetAVX2(&dst[0], l, b)
}
l <<= 5
}
memsetGeneric(dst[l:], b)
}
14 changes: 14 additions & 0 deletions and_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,17 @@ loop:
JNZ loop
MOVQ DX, ret+16(FP)
RET

// func memsetAVX2(dst *byte, l uint64, b byte)
// Requires: AVX, AVX2
TEXT ·memsetAVX2(SB), NOSPLIT, $0-17
MOVQ dst+0(FP), AX
MOVQ l+8(FP), CX
VPBROADCASTB b+16(FP), Y0

loop:
VMOVDQU Y0, (AX)
ADDQ $0x00000020, AX
SUBQ $0x00000001, CX
JNZ loop
RET
5 changes: 5 additions & 0 deletions and_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ func popcnt(a []byte) int {
// TODO: Write a NEON version for this
return popcntGeneric(a)
}

func memset(dst []byte, b byte) {
// TODO: Write a NEON version for this
memsetGeneric(dst, b)
}
5 changes: 5 additions & 0 deletions and_stubs_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func andNot(dst, a, b []byte) {
func popcnt(a []byte) int {
return popcntGeneric(a)
}

func memset(dst []byte, b byte) {
return memsetGeneric(dst, b)
}
31 changes: 31 additions & 0 deletions internal/asm/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func main() {
gen("or", VPOR, "Sets dst to the bitwise or of a and b")
gen("andNot", VPANDN, "Sets dst to the bitwise and of not(a) and b")
genPopcnt()
genMemset()
Generate()
}

Expand Down Expand Up @@ -86,3 +87,33 @@ func genPopcnt() {
Store(ret, ReturnIndex(0))
RET()
}

func genMemset() {
const rounds = 1
TEXT("memsetAVX2", NOSPLIT, "func(dst *byte, l uint64, b byte)")

Pragma("noescape")

Doc("Sets each byte in dst to b")
dst := Load(Param("dst"), GP64())
l := Load(Param("l"), GP64())

bRepeated := YMM()
b, err := Param("b").Resolve()
if err != nil {
panic(err)
}
VPBROADCASTB(b.Addr, bRepeated)

Label("loop")

for i := 0; i < rounds; i++ {
VMOVDQU(bRepeated, Mem{Base: dst, Disp: 32 * i})
}

ADDQ(U32(32*rounds), dst)
SUBQ(U32(1), l)
JNZ(LabelRef("loop"))

RET()
}
11 changes: 11 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,14 @@ func popcntGeneric(a []byte) int {
}
return ret
}

// Memset sets dst[*] to b.
func Memset(dst []byte, b byte) {
memset(dst, b)
}

func memsetGeneric(dst []byte, b byte) {
for i := range dst {
dst[i] = b
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least we should write uint64 at a time.

}
}
48 changes: 48 additions & 0 deletions memset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package and

import (
"math/rand/v2"
"testing"
)

func testMemset(t *testing.T, size int) {
a := make([]byte, size)
Memset(a, 0xff)
for i, v := range a {
if v != 0xff {
t.Errorf("Memset failed to set a[%d] to 0xff", i)
}
}
}

func TestMemsetAgainstGeneric(t *testing.T) {
for i := 0; i < 20; i++ {
size := 1 << i
testMemset(t, size)
for j := 0; j < 10; j++ {
testMemset(t, size+rand.IntN(100))
}
}
}

func BenchmarkMemset(b *testing.B) {
b.StopTimer()
size := 1000000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
Memset(a, 0xff)
}
}

func BenchmarkMemsetGeneric(b *testing.B) {
b.StopTimer()
size := 1000000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
memsetGeneric(a, 0xff)
}
}
Loading