Skip to content

Commit

Permalink
estimate the square size correctly for small square sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-forbes committed May 22, 2022
1 parent 0355cda commit 38158e1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
11 changes: 7 additions & 4 deletions x/payment/types/payfordata.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ func powerOf2MountainRange(l, k uint64) []uint64 {
// NextHighestPowerOf2 returns the next lowest power of 2 unless the input is a power
// of two, in which case it returns the input
func NextHighestPowerOf2(v uint64) uint64 {
if v == 1 {
return 1
}
// keep track of the value to check if its the same later
i := v

// find the next highest power using bit mashing
v--
Expand All @@ -198,7 +197,11 @@ func NextHighestPowerOf2(v uint64) uint64 {
v |= v >> 32
v++

// return the next highest power
// force the value to the next highest power of two if its the same
if v == i {
return 2 * v
}

return v
}

Expand Down
33 changes: 33 additions & 0 deletions x/payment/types/payfordata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ func TestNextLowestPowerOf2(t *testing.T) {
}
}

func TestNextHighestPowerOf2(t *testing.T) {
type test struct {
input uint64
expected uint64
}
tests := []test{
{
input: 2,
expected: 4,
},
{
input: 11,
expected: 16,
},
{
input: 511,
expected: 512,
},
{
input: 1,
expected: 2,
},
{
input: 0,
expected: 0,
},
}
for _, tt := range tests {
res := NextHighestPowerOf2(tt.input)
assert.Equal(t, tt.expected, res)
}
}

func TestPowerOf2(t *testing.T) {
type test struct {
input uint64
Expand Down

0 comments on commit 38158e1

Please sign in to comment.