-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
goos: linux goarch: amd64 pkg: github.com/bwesterb/go-and cpu: 13th Gen Intel(R) Core(TM) i9-13900 BenchmarkPopcnt-32 34485 32383 ns/op 30879.98 MB/s BenchmarkPopcntGeneric-32 17019 70787 ns/op 14126.98 MB/s
- Loading branch information
Showing
11 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build amd64.v2 | ||
|
||
package and | ||
|
||
func hasPopcnt() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build !amd64 | ||
|
||
package and | ||
|
||
func hasPopcnt() bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build amd64 && !amd64.v2 | ||
|
||
package and | ||
|
||
import "golang.org/x/sys/cpu" | ||
|
||
func hasPopcnt() bool { | ||
return cpu.X86.HasPOPCNT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package and | ||
|
||
import ( | ||
"math/rand/v2" | ||
"testing" | ||
) | ||
|
||
func testPopcntAgainstGeneric(t *testing.T, size int) { | ||
a := make([]byte, size) | ||
rng := rand.New(rand.NewPCG(0, 0)) | ||
for i := range a { | ||
a[i] = uint8(rng.UintN(256)) | ||
} | ||
got := Popcnt(a) | ||
want := popcntGeneric(a) | ||
if got != want { | ||
t.Fatalf("Popcnt produced a different result from popcntGeneric at length %d: %d; want %d", size, got, want) | ||
} | ||
} | ||
|
||
func TestPopcntAgainstGeneric(t *testing.T) { | ||
for i := 0; i < 20; i++ { | ||
size := 1 << i | ||
testPopcntAgainstGeneric(t, size) | ||
for j := 0; j < 10; j++ { | ||
testPopcntAgainstGeneric(t, size+rand.IntN(100)) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkPopcnt(b *testing.B) { | ||
b.StopTimer() | ||
size := 1000000 | ||
a := make([]byte, size) | ||
b.SetBytes(int64(size)) | ||
b.StartTimer() | ||
for i := 0; i < b.N; i++ { | ||
_ = Popcnt(a) | ||
} | ||
} | ||
|
||
func BenchmarkPopcntGeneric(b *testing.B) { | ||
b.StopTimer() | ||
size := 1000000 | ||
a := make([]byte, size) | ||
b.SetBytes(int64(size)) | ||
b.StartTimer() | ||
for i := 0; i < b.N; i++ { | ||
_ = popcntGeneric(a) | ||
} | ||
} |