Skip to content

cmd/compile: optimize conditional subtractions #76056

@FiloSottile

Description

@FiloSottile

Here are two ways of doing the same thing, reducing a value in [0, 2q) mod q:

const q = 8380417 // 2²³ - 2¹³ + 1

// fieldElement is an integer modulo q, an element of ℤ_q. It is always reduced.
type fieldElement uint32

// fieldReduceOnce reduces a value a < 2q.
func fieldReduceOnce(a uint32) fieldElement {
	x, b := bits.Sub32(a, q, 0)
	return fieldElement(x + b*q)
}

// fieldReduceOnce reduces a value a < 2q.
func fieldReduceOnce2(a uint32) fieldElement {
	x, b := bits.Sub32(a, q, 0)
	return fieldElement(subtle.ConstantTimeSelect(int(b), int(x), int(a)))
}

They both generate awful code, when it could be just

        MOVL    AX, CX
        SUBL    $8380417, CX
        CMOVCC  CX, AX

Could the compiler pattern match one of those (or some other form I can use) and produce the more efficient code?

More generally, it might also be useful to detect using the carry/borrow from a math/bits function with subtle.ConstantTimeSelect in cases that are more complex than a conditional subtraction.

/cc @golang/compiler

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions