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
source share