Skip to content

Commit c00647b

Browse files
committed
cmd/compile: set bits.OnesCount's limits to [0, 64]
Change-Id: I2f60de836f58ef91baae856f44d8f73c190326f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/656158 Reviewed-by: David Chase <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Jorropo <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]>
1 parent 554a3c5 commit c00647b

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/cmd/compile/internal/ssa/prove.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,13 +1645,13 @@ func initLimit(v *Value) limit {
16451645
lim = lim.signedMinMax(math.MinInt32, math.MaxInt32)
16461646

16471647
// math/bits intrinsics
1648-
case OpCtz64, OpBitLen64:
1648+
case OpCtz64, OpBitLen64, OpPopCount64:
16491649
lim = lim.unsignedMax(64)
1650-
case OpCtz32, OpBitLen32:
1650+
case OpCtz32, OpBitLen32, OpPopCount32:
16511651
lim = lim.unsignedMax(32)
1652-
case OpCtz16, OpBitLen16:
1652+
case OpCtz16, OpBitLen16, OpPopCount16:
16531653
lim = lim.unsignedMax(16)
1654-
case OpCtz8, OpBitLen8:
1654+
case OpCtz8, OpBitLen8, OpPopCount8:
16551655
lim = lim.unsignedMax(8)
16561656

16571657
// bool to uint8 conversion

test/prove_popcount.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// errorcheck -0 -d=ssa/prove/debug=1
2+
3+
//go:build amd64.v3 || arm64
4+
5+
// Copyright 2025 The Go Authors. All rights reserved.
6+
// Use of this source code is governed by a BSD-style
7+
// license that can be found in the LICENSE file.
8+
9+
// FIXME(@Jorropo): this file exists because I havn't yet bothered to
10+
// make prove work on the pure go function call fallback.
11+
// My idea was to wait until CL 637936 is merged, then we can always emit
12+
// the PopCount SSA operation and translate them to pure function calls
13+
// in late-opt.
14+
15+
package main
16+
17+
import "math/bits"
18+
19+
func onesCountsBounds(x uint64, ensureAllBranchesCouldHappen func() bool) int {
20+
z := bits.OnesCount64(x)
21+
if ensureAllBranchesCouldHappen() && z > 64 { // ERROR "Disproved Less64$"
22+
return 42
23+
}
24+
if ensureAllBranchesCouldHappen() && z <= 64 { // ERROR "Proved Leq64$"
25+
return 4242
26+
}
27+
if ensureAllBranchesCouldHappen() && z < 0 { // ERROR "Disproved Less64$"
28+
return 424242
29+
}
30+
if ensureAllBranchesCouldHappen() && z >= 0 { // ERROR "Proved Leq64$"
31+
return 42424242
32+
}
33+
return z
34+
}
35+
36+
func main() {
37+
}

0 commit comments

Comments
 (0)