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

Check if the current CPU has AVX2 support #5

Merged
merged 1 commit into from
Jul 12, 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
7 changes: 7 additions & 0 deletions detect_axv2_always.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build amd64.v3

package and

func hasAVX2() bool {
return true
}
7 changes: 7 additions & 0 deletions detect_axv2_never.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !amd64

package and

func hasAVX2() bool {
return false
}
9 changes: 9 additions & 0 deletions detect_axv2_runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build amd64 && !amd64.v3

package and

import "golang.org/x/sys/cpu"

func hasAVX2() bool {
return cpu.X86.HasAVX2
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/bwesterb/go-and

go 1.22.0

require golang.org/x/sys v0.22.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
6 changes: 5 additions & 1 deletion lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func And(dst, a, b []byte) {
panic("lengths of a, b and dst must be equal")
}

and(dst, a, b)
if hasAVX2() {
and(dst, a, b)
return
}
andGeneric(dst, a, b)
}

func andGeneric(dst, a, b []byte) {
Expand Down
Loading