chewxy / math32

A float32 version of Go's math package
BSD 2-Clause "Simplified" License
61 stars 22 forks source link

Broken assembly implementations #42

Closed soypat closed 2 months ago

soypat commented 2 years ago

Due to recent developments, some assembly implementations have been discovered to be broken.

An idea for 64bit assembly implementations

Disclaimer: I'm unsure if casting float64s is desirable. This is just a shot-in-the-dark idea.

Instead of us hosting the assembly implementation for 64 bit architectures we could provide an additional boolean build flag similar to haveArchFunc, but indicating that the Go standard library has the 64 bit implementation, thus we can just use a wrapper func style implementation.

This may be clearer if I illustrate with an example Today The Sqrt implementation is as follows:

func Sqrt(x float32) float32 {
    if haveArchSqrt {
        return archSqrt(x)
    }
    return sqrt(x)
}

I propose adding a new constant haveStdlibSqrt and a new file sqrt_stdlib.go. The above code changes as follows

func Sqrt(x float32) float32 {
    if haveArchSqrt {
        return archSqrt(x)
    }
    if haveStdlibSqrt {
        return float32(math.Sqrt(float32(x)))
    }
    return sqrt(x)
}

The new file sqrt_stdlib.go has the following contents.

//go:build !noasm && (arm64 || risc64)
package math32

import "math"

const haveStdlibSqrt = true // set to false in _noasm and _asm files
soypat commented 2 months ago

Fixed in v1.11.0