golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.59k stars 17.48k forks source link

proposal: math: add math_pure_go flag to avoid assembly code #63270

Open 0x1cc opened 11 months ago

0x1cc commented 11 months ago

Introduction

In the current Go math package, when the architecture has a Floating-Point Unit (FPU), it accelerates floating-point computations using assembly code tailored to take advantage of the architecture's FPU. While this approach offers excellent performance, it creates compatibility issues with softfloat. To support softfloat, we can use -gcflags=all=-d=softfloat, which can convert floating-point calculations to softfloat within the compiler, but it doesn't impact any assembly code in the math package. (The related issue can be found here: [link to GitHub issue #62470].)

To address this issue, we propose introducing a new build tag that disables the math package from utilizing FPU-related assembly code for performance optimization, thereby providing support for softfloat.

Proposed Solution

To support softfloat in the Go math package, we propose adding a new build tag, let's call it math_pure_go, that users can apply to disable the use of FPU-specific assembly code in the math package. We can use the math_pure_go build tag to disable the assembly in math package. With the build tag -tags=math_pure_go and -gcflags=all=-d=softfloat, we can support softfloat for all platforms.

go build -gcflags=all=-d=softfloat -tags=math_pure_go

Benefit

0x1cc commented 11 months ago

The implementation for softfloat support in the math package can be found in this pull request: GitHub Pull Request #63271.

mvdan commented 11 months ago

What about the existing purego tag? https://github.com/golang/go/issues/23172 cc @dsnet

bcmills commented 11 months ago

While this approach offers excellent performance, it creates compatibility issues with softfloat.

We already have GOMIPS=softfloat, GO386=softfloat, etc. for specifying the use of softfloat implementations on specific platforms — it seems like a mistake to add yet another (orthogonal) way to specify that.

If softfloat support is really needed on WASM platforms, adding a GOWASM=softfloat seems like it would be a cleaner approach.

FiloSottile commented 6 months ago

This should be fixable without a proposal by just adding purego support per #23172. It has other advantages, such as supporting other compilers. TinyGo is currently having to alias symbols in the math package:

https://github.com/tinygo-org/tinygo/blob/731532cd2b6353b60b443343b51296ec0fafae09/compiler/alias.go#L27-L31

leongross commented 1 month ago

Are there still ongoing efforts on this topic? PR #23172 still seems to use the earlier proposed math_pure_go which is different to pure_go. What are the blockers here exactly? I would be glad to assist since I'm working on tinygo as well and would profit from this implementation.

ianlancetaylor commented 1 month ago

I don't see a problem with using the existing purego build tag to disable assembler code in the math package. I don't think there any blockers, but somebody has to write and test the patch.