Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4f7fe4

Browse files
authored
bits: Remove special cases and go 1.8 support (#187)
Reimplement findMSBSetNonZero64 with bits.Len64, which is a more direct reflection of its semantics than bits.LeadingZeros64. findMSBSetNonZero64 and findLSBSetNonZero64 were called "NonZero" because their precondition in C++ was that the argument was non-zero. Change the zero handling of these functions to be more uniform since it's never used and we don't care. In a future commit, these functions can be inlined completely. Fixes #179.
1 parent e8fe6a7 commit c4f7fe4

File tree

3 files changed

+8
-71
lines changed

3 files changed

+8
-71
lines changed

s2/bits_go19.go renamed to s2/bits.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//go:build go1.9
16-
// +build go1.9
17-
1815
package s2
1916

20-
// This file is for the bit manipulation code post-Go 1.9.
17+
// This file is for bit manipulation code. It will soon be deleted.
2118

2219
import "math/bits"
2320

2421
// findMSBSetNonZero64 returns the index (between 0 and 63) of the most
25-
// significant set bit. Passing zero to this function return zero.
22+
// significant set bit. Passing zero to this function returns -1.
2623
func findMSBSetNonZero64(x uint64) int {
27-
if x == 0 {
28-
return 0
29-
}
30-
return 63 - bits.LeadingZeros64(x)
24+
return bits.Len64(x) - 1
3125
}
3226

3327
// findLSBSetNonZero64 returns the index (between 0 and 63) of the least
34-
// significant set bit. Passing zero to this function return zero.
28+
// significant set bit. Passing zero to this function returns 64.
3529
func findLSBSetNonZero64(x uint64) int {
36-
if x == 0 {
37-
return 0
38-
}
3930
return bits.TrailingZeros64(x)
4031
}

s2/bits_go18.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

s2/bits_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func TestFindMSBSetNonZero64(t *testing.T) {
4141
t.Errorf("findMSBSetNonZero64(1) = %v, want 0", got)
4242
}
4343

44-
if got := findMSBSetNonZero64(0); got != 0 {
45-
t.Errorf("findMSBSetNonZero64(0) = %v, want 0", got)
44+
if got := findMSBSetNonZero64(0); got != -1 {
45+
t.Errorf("findMSBSetNonZero64(0) = %v, want -1", got)
4646
}
4747
}
4848

@@ -65,7 +65,7 @@ func TestFindLSBSetNonZero64(t *testing.T) {
6565
testSome <<= 1
6666
}
6767

68-
if got := findLSBSetNonZero64(0); got != 0 {
69-
t.Errorf("findLSBSetNonZero64(0) = %v, want 0", got)
68+
if got := findLSBSetNonZero64(0); got != 64 {
69+
t.Errorf("findLSBSetNonZero64(0) = %v, want 64", got)
7070
}
7171
}

0 commit comments

Comments
 (0)