Skip to content

Commit 979d902

Browse files
bmkesslerrandall77
authored andcommitted
math/bits: define Div to panic when y<=hi
Div panics when y<=hi because either the quotient overflows the size of the output or division by zero occurs when y==0. This provides a uniform behavior for all implementations. Fixes #28316 Change-Id: If23aeb10e0709ee1a60b7d614afc9103d674a980 Reviewed-on: https://go-review.googlesource.com/c/149517 Reviewed-by: Robert Griesemer <[email protected]>
1 parent 319787a commit 979d902

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/math/bits/bits.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ func Mul64(x, y uint64) (hi, lo uint64) {
454454
// Div returns the quotient and remainder of (hi, lo) divided by y:
455455
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
456456
// half in parameter hi and the lower half in parameter lo.
457-
// hi must be < y otherwise the behavior is undefined (the quotient
458-
// won't fit into quo).
457+
// Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).
459458
func Div(hi, lo, y uint) (quo, rem uint) {
460459
if UintSize == 32 {
461460
q, r := Div32(uint32(hi), uint32(lo), uint32(y))
@@ -468,8 +467,7 @@ func Div(hi, lo, y uint) (quo, rem uint) {
468467
// Div32 returns the quotient and remainder of (hi, lo) divided by y:
469468
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
470469
// half in parameter hi and the lower half in parameter lo.
471-
// hi must be < y otherwise the behavior is undefined (the quotient
472-
// won't fit into quo).
470+
// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
473471
func Div32(hi, lo, y uint32) (quo, rem uint32) {
474472
if y != 0 && y <= hi {
475473
panic(overflowError)
@@ -482,8 +480,7 @@ func Div32(hi, lo, y uint32) (quo, rem uint32) {
482480
// Div64 returns the quotient and remainder of (hi, lo) divided by y:
483481
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
484482
// half in parameter hi and the lower half in parameter lo.
485-
// hi must be < y otherwise the behavior is undefined (the quotient
486-
// won't fit into quo).
483+
// Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
487484
func Div64(hi, lo, y uint64) (quo, rem uint64) {
488485
const (
489486
two32 = 1 << 32

0 commit comments

Comments
 (0)