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

perf: use bits.Len64 instead of bsr #129

Merged
merged 1 commit into from
Apr 13, 2022
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
19 changes: 0 additions & 19 deletions lang/mcache/bsr_amd64.go

This file was deleted.

27 changes: 0 additions & 27 deletions lang/mcache/bsr_amd64.s

This file was deleted.

27 changes: 0 additions & 27 deletions lang/mcache/bsr_other.go

This file was deleted.

6 changes: 6 additions & 0 deletions lang/mcache/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package mcache

import "math/bits"

func bsr(x int) int {
return bits.Len(uint(x)) - 1
}

func isPowerOfTwo(x int) bool {
return (x & (-x)) == x
}
10 changes: 10 additions & 0 deletions lang/mcache/bsr_test.go → lang/mcache/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ func TestBsr(t *testing.T) {
assert.Equal(t, bsr((1<<10)-1), 9)
assert.Equal(t, bsr((1<<30)+(1<<19)+(1<<16)+(1<<1)), 30)
}

func BenchmarkBsr(b *testing.B) {
num := (1 << 30) + (1 << 19) + (1 << 16) + (1 << 1)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = bsr(num + i)
}
}