Last active
October 20, 2017 07:03
-
-
Save myui/9ea4a9941932a7d942d6e7d5d6c619cf to your computer and use it in GitHub Desktop.
FastInverseSquareRootTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FastInverseSquareRootTest { | |
private static FastInverseSquareRootTest conductTestHalfPrecision() { | |
float result = 0F; | |
long startTime, endTime, midTime; | |
startTime = System.nanoTime(); | |
for (float x = 1F; x < 4_000_000F; x += 0.25F) { | |
result += 1F / (float) Math.sqrt(x); | |
} | |
midTime = System.nanoTime(); | |
for (float x = 1F; x < 4_000_000F; x += 0.25F) { | |
result += fastInverseSquareRoot(x); | |
} | |
endTime = System.nanoTime(); | |
if (result == 0) | |
System.out.println("Wow!"); | |
return new FastInverseSquareRootTest(midTime - startTime, endTime - midTime); | |
} | |
private static FastInverseSquareRootTest conductTestDoublePrecision() { | |
double result = 0d; | |
long startTime, endTime, midTime; | |
startTime = System.nanoTime(); | |
for (double x = 1d; x < 4_000_000d; x += 0.25d) { | |
result += 1d / Math.sqrt(x); | |
} | |
midTime = System.nanoTime(); | |
for (double x = 1d; x < 4_000_000d; x += 0.25D) { | |
result += fastInverseSquareRoot(x); | |
} | |
endTime = System.nanoTime(); | |
if (result == 0) | |
System.out.println("Wow!"); | |
return new FastInverseSquareRootTest(midTime - startTime, endTime - midTime); | |
} | |
/** | |
* https://en.wikipedia.org/wiki/Fast_inverse_square_root | |
*/ | |
@Deprecated | |
private static float fastInverseSquareRoot(final float x) { | |
final float hx = 0.5f * x; | |
int i = 0x5f375a86 - (Float.floatToRawIntBits(x) >>> 1); | |
float y = Float.intBitsToFloat(i); | |
y *= (1.5f - hx * y * y); // pass 1 | |
y *= (1.5f - hx * y * y); // pass 2 | |
y *= (1.5f - hx * y * y); // pass 3 | |
//y *= (1.5f - hx * y * y); // pass 4 | |
// more pass for more accuracy | |
return y; | |
} | |
/** | |
* https://en.wikipedia.org/wiki/Fast_inverse_square_root | |
*/ | |
private static double fastInverseSquareRoot(final double x) { | |
final double hx = 0.5d * x; | |
long i = 0x5fe6eb50c7b537a9L - (Double.doubleToRawLongBits(x) >>> 1); | |
double y = Double.longBitsToDouble(i); | |
y *= (1.5d - hx * y * y); // pass 1 | |
y *= (1.5d - hx * y * y); // pass 2 | |
y *= (1.5d - hx * y * y); // pass 3 | |
y *= (1.5d - hx * y * y); // pass 4 | |
// more pass for more accuracy | |
return y; | |
} | |
public static void main(String[] args) throws Exception { | |
for (int i = 0; i < 7; i++) { | |
System.out.println("-- round #" + (i + 1)); | |
System.out.println("-- Half Precision"); | |
System.out.print(conductTestHalfPrecision().toString()); | |
System.out.println("-- Double Precision"); | |
System.out.println(conductTestDoublePrecision().toString()); | |
} | |
} | |
private long controlDiff; | |
private long experimentalDiff; | |
private double percentError; | |
public FastInverseSquareRootTest(long controlDiff, long experimentalDiff) { | |
this.experimentalDiff = experimentalDiff; | |
this.controlDiff = controlDiff; | |
this.percentError = 100D * (experimentalDiff - controlDiff) / controlDiff; | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(String.format("1F / Math.sqrt() took %d nanoseconds.%n", controlDiff)); | |
sb.append( | |
String.format("Fast Inverse Square Root took %d nanoseconds.%n", experimentalDiff)); | |
sb.append( | |
String.format("Fast Inverse Square Root was %f percent %s than 1F / Math.sqrt()%n", | |
Math.abs(percentError), percentError > 0D ? "slower" : "faster")); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
changed
result = fastInverseSquareRoot(x);
=>result += fastInverseSquareRoot(x);
4 passes
2 passes