mathgl icon indicating copy to clipboard operation
mathgl copied to clipboard

Consider finding (or writing if need be) a float32 native math library

Open UserAB1236872 opened this issue 10 years ago • 8 comments

After writing some benchmarks today (not for this package, just dicking around with benchmarking fast inverse square root compared to Go's stdlib and such), it's become clear that the rampant type conversions are a significant performance hit. "Significant" is in relative terms, but code of the form:

float32(1.0 / math.Sqrt(float64(n)))

takes about 15.5 ns/op compared to 3.5 ns/op for float64-native. I suspected this, hence why this library generally takes a "convert as infrequently as possible" approach. However, I would consider this significant enough to be worth worrying about.

While Go's standard library is written partially in assembly, I don't think it should be too difficult to rewrite (I think most of the assembly conversions would amount to "swap a letter in the instruction names to do 32-bit operations instead"). However, I'd like to look into existing options if they exist before undertaking this.

I did find this, but the repository was deleted for some ominous reason.

UserAB1236872 avatar Aug 03 '14 08:08 UserAB1236872

Couldnt we just download all the files from the original math package and do a fancy find and replace for float64->float32 then fix the 2-3 other mistakes it caused ?

ghost avatar May 08 '15 19:05 ghost

It's largely in assembly, so it's not quite that simple.

On Fri, May 8, 2015 at 12:45 PM, Olivier Gagnon [email protected] wrote:

Couldnt we just download all the files from the original math package and do a fancy find and replace for float64->float32 then fix the 2-3 other mistakes it caused ?

— Reply to this email directly or view it on GitHub https://github.com/go-gl/mathgl/issues/28#issuecomment-100338003.

UserAB1236872 avatar May 08 '15 20:05 UserAB1236872

Granted, we could possibly do a find+replace on the assembly instructions too, but we'd have to do a lot of testing to ensure we didn't bork something.

On Fri, May 8, 2015 at 1:11 PM, Jeff Juozapaitis [email protected] wrote:

It's largely in assembly, so it's not quite that simple.

On Fri, May 8, 2015 at 12:45 PM, Olivier Gagnon [email protected] wrote:

Couldnt we just download all the files from the original math package and do a fancy find and replace for float64->float32 then fix the 2-3 other mistakes it caused ?

— Reply to this email directly or view it on GitHub https://github.com/go-gl/mathgl/issues/28#issuecomment-100338003.

UserAB1236872 avatar May 08 '15 20:05 UserAB1236872

Yeah im working on it

ghost avatar May 08 '15 20:05 ghost

Benchmark32Sqrt 1000000000           2.52 ns/op
Benchmark64Sqrt 300000000            4.98 ns/op
ok      math32  4.786s

First results look promising

ghost avatar May 08 '15 22:05 ghost

https://github.com/luxengine/math WIP

ghost avatar May 13 '15 14:05 ghost

No seriously are we getting anywhere on this.

ghost avatar Jul 18 '15 05:07 ghost

If you (or someone) submits a PR, I'd be happy to accept it. It should be a relatively straightforward patch to the generator code to sub your library for native match for the f32 version. I don't have time at the moment due to PhD stuff.

UserAB1236872 avatar Jul 20 '15 04:07 UserAB1236872