Java: 32-bit fp implementation for Math.sqrt ()

The standard Math.sqrt() method seems pretty fast in Java already, but it has the inherent drawback that it will always include 64-bit operations, which do not slow down when working with 32-bit float values. Is it possible to do better with a custom method that uses float as a parameter, performs only 32-bit operations and returns a float as a result?

I have seen:

Fast sqrt in java due to precision

and this did a little more than reinforcing the notion that Math.sqrt () is usually difficult to measure. I also saw:

http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi

which showed me a bunch of interesting C ++ / ASM hacks that I'm just too unfamiliar to directly port to Java. Although sqrt14 might be interesting as part of a JNI call.,.

I also looked at Apache Commons FastMath, but it looks like this library defaults to the Math.sqrt () standard, so there is no help. And there Yeppp !:

http://www.yeppp.info/

but I have not worried about it yet.

+6
source share
2 answers

You have nothing to speed up sqrt for 32-bit values. HotSpot JVM does this automatically for you.

The JIT compiler is smart enough to recognize the f2d -> Math.sqrt() -> d2f and replace it with the faster sqrtss CPU command instead of sqrtsd . Source

Test:

 @State(Scope.Benchmark) public class Sqrt { double d = Math.random(); float f = (float) d; @Benchmark public double sqrtD() { return Math.sqrt(d); } @Benchmark public float sqrtF() { return (float) Math.sqrt(f); } } 

And the results:

 Benchmark Mode Cnt Score Error Units Sqrt.sqrtD thrpt 5 145501,072 ± 2211,666 ops/ms Sqrt.sqrtF thrpt 5 223657,110 ± 2268,735 ops/ms 
+5
source

As you know, JNI:

just write the minimal wrapper for double sqrt(double) and float sqrt(float) from the C math.h standard library and compare performance.

Hint: you won’t feel the difference if you don’t make a lot of square root, and then the performance advantage when using SIMD instructions to execute multiple sqrts at the same time will most likely dominate the effects. You will need to get an array of floating point values ​​from Java, which can be quite complicated if you use standard Java libraries.

0
source

Source: https://habr.com/ru/post/988905/


All Articles