Skip to content

math/bits: define Div to panic when hi>=y #28316

@randall77

Description

@randall77

math/bits.Div currently makes the behavior undefined if hi >= y.

func Div(hi, lo, y uint) (quo, rem uint)

We should instead define it to panic in that case.

The compare-and-branch should be cheap relative to the divide itself.

This came up because the current code returns a (weird?) sentinel value in this case. When intrinsifying the code, the raw amd64 opcode will instead cause an arithmetic fault. Both behaviors fit in the "undefined" category, so they are technically spec conforming, but it's really weird for behavior to change when optimization is turned on. Since we'd need a test anyway to unify the behaviors, might as well make the test result panic.

We should resolve this one way or the other for 1.12, before the API goes public.

See #28273 #24813

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions