Skip to content

Commit

Permalink
Merge pull request #5 from Jille/detect-cpu
Browse files Browse the repository at this point in the history
Check if the current CPU has AVX2 support
  • Loading branch information
bwesterb authored Jul 12, 2024
2 parents 51bf02e + 3fb8e60 commit bc14f02
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
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

0 comments on commit bc14f02

Please sign in to comment.