Skip to content

math: consider removing Dim assembly (and possibly Max/Min too) #21913

@mundaym

Description

@mundaym

I've just sent CL 64194 that makes dim (the generic implementation of Dim) simple enough to be easily inlineable. I've deleted the s390x assembly since I don't think it is worth having any more. I could have made it better but I think inlining the code would be more of a win. If this CL is accepted dim will look something like this:

func dim(x, y float64) float64 {
	x -= y
	switch {
	case x > 0:
		return x
	case x <= 0:
		return 0
	}
	return NaN()
}

It would be really nice if we could delete the assembly for the other platforms so that we can get rid of the assembly stubs and Dim itself can be made inlineable.

Min and Max could also be reworked to make them inlineable (and, theoretically, faster in the usual case), using code like this:

func Max(x, y float64) float64 {
        switch {
        case y > x:
                return y
        case x > y:
                return x
        case x == y:
                // return +0 in preference to -0
                return Float64frombits(Float64bits(x) & Float64bits(y))
        }
        if inf := Float64frombits(uvinf); x == inf || y == inf {
                return inf
        }
        return Float64frombits(uvnan)
}

Note that we can't use the IsInf, Inf and NaN functions because they increase the complexity of the code the compiler sees and push max over the inlining threshold.

It is less clear cut that this is better than the assembly on all platforms (arm64 seems to have an FMAX instruction for example) and my benchmarking results were more mixed, so I haven't made this change.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions