Remove FastMath

hotspot has intrinsics for almost everything we use it for
This commit is contained in:
Zoë Gidiere
2023-10-26 10:25:39 -06:00
parent 805f99f57a
commit 9292d3de17
153 changed files with 517 additions and 771 deletions

View File

@@ -7,5 +7,5 @@ dependencies {
api("com.dfsek.tectonic", "common", Versions.Libraries.tectonic)
api("com.github.ben-manes.caffeine:caffeine:3.1.0")
implementation("net.jafama", "jafama", Versions.Libraries.Internal.jafama)
}

View File

@@ -7,7 +7,6 @@
package com.dfsek.terra.api.util;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
@@ -44,7 +43,7 @@ public class ConstantRange implements Range {
@Override
public Range intersects(Range other) {
try {
return new ConstantRange(FastMath.max(this.getMin(), other.getMin()), FastMath.min(this.getMax(), other.getMax()));
return new ConstantRange(Math.max(this.getMin(), other.getMin()), Math.min(this.getMax(), other.getMax()));
} catch(IllegalArgumentException e) {
return null;
}

View File

@@ -7,8 +7,6 @@
package com.dfsek.terra.api.util;
import net.jafama.FastMath;
import java.util.List;
@@ -16,11 +14,58 @@ import java.util.List;
* Utility class for mathematical functions.
*/
public final class MathUtil {
private static final int SIN_BITS, SIN_MASK, SIN_COUNT;
private static final double radFull, radToIndex;
private static final double degFull, degToIndex;
private static final double[] sin, cos;
static {
SIN_BITS = 12;
SIN_MASK = ~(-1 << SIN_BITS);
SIN_COUNT = SIN_MASK + 1;
radFull = Math.PI * 2.0;
degFull = 360.0;
radToIndex = SIN_COUNT / radFull;
degToIndex = SIN_COUNT / degFull;
sin = new double[SIN_COUNT];
cos = new double[SIN_COUNT];
for (int i = 0; i < SIN_COUNT; i++) {
sin[i] = Math.sin((i + 0.5f) / SIN_COUNT * radFull);
cos[i] = Math.cos((i + 0.5f) / SIN_COUNT * radFull);
}
// Four cardinal directions (credits: Nate)
for (int i = 0; i < 360; i += 90) {
sin[(int) (i * degToIndex) & SIN_MASK] = Math.sin(i * Math.PI / 180.0);
cos[(int) (i * degToIndex) & SIN_MASK] = Math.cos(i * Math.PI / 180.0);
}
}
/**
* Epsilon for fuzzy floating point comparisons.
*/
public static final double EPSILON = 1.0E-5;
public static double sin(double rad) {
return sin[(int) (rad * radToIndex) & SIN_MASK];
}
public static double cos(double rad) {
return cos[(int) (rad * radToIndex) & SIN_MASK];
}
public static double invSqrt(double x) {
double xhalf = 0.5d * x;
long i = Double.doubleToLongBits(x);
i = 0x5fe6ec85e7de30daL - (i >> 1);
x = Double.longBitsToDouble(i);
x *= (1.5d - xhalf * x * x);
return x;
}
/**
* Gets the standard deviation of an array of doubles.
*
@@ -39,10 +84,10 @@ public final class MathUtil {
double mean = sum / length;
for(Number num : numArray) {
standardDeviation += FastMath.pow2(num.doubleValue() - mean);
standardDeviation += Math.pow(num.doubleValue() - mean, 2);
}
return FastMath.sqrt(standardDeviation / length);
return Math.sqrt(standardDeviation / length);
}
public static long hashToLong(String s) {
@@ -65,11 +110,11 @@ public final class MathUtil {
* @return Whether these values are equal
*/
public static boolean equals(double a, double b) {
return a == b || FastMath.abs(a - b) < EPSILON;
return a == b || Math.abs(a - b) < EPSILON;
}
public static int normalizeIndex(double val, int size) {
return FastMath.max(FastMath.min(FastMath.floorToInt(((val + 1D) / 2D) * size), size - 1), 0);
return Math.max(Math.min((int)Math.floor(((val + 1D) / 2D) * size), size - 1), 0);
}
public static long squash(int first, int last) {
@@ -84,11 +129,11 @@ public final class MathUtil {
* @return Clamped value
*/
public static double clamp(double in) {
return FastMath.min(FastMath.max(in, -1), 1);
return Math.min(Math.max(in, -1), 1);
}
public static int clamp(int min, int i, int max) {
return FastMath.max(FastMath.min(i, max), min);
return Math.max(Math.min(i, max), min);
}
/**
@@ -115,7 +160,7 @@ public final class MathUtil {
q = p - 0.5;
if(FastMath.abs(q) <= .425) {
if(Math.abs(q) <= .425) {
r = .180625 - q * q;
val =
q * (((((((r * 2509.0809287301226727 +
@@ -134,7 +179,7 @@ public final class MathUtil {
r = p;
}
r = FastMath.sqrt(-FastMath.log(r));
r = Math.sqrt(-Math.log(r));
if(r <= 5) {
r -= 1.6;
@@ -173,4 +218,37 @@ public final class MathUtil {
return mu + sigma * val;
}
/**
* Murmur64 hashing function
*
* @param h Input value
*
* @return Hashed value
*/
public static long murmur64(long h) {
h ^= h >>> 33;
h *= 0xff51afd7ed558ccdL;
h ^= h >>> 33;
h *= 0xc4ceb9fe1a85ec53L;
h ^= h >>> 33;
return h;
}
public static double lerp(double a, double b, double t) {
return a + t * (b - a);
}
public static double cubicLerp(double a, double b, double c, double d, double t) {
double p = (d - c) - (a - b);
return t * t * t * p + t * t * ((a - b) - p) + t * (c - a) + b;
}
public static double interpHermite(double t) {
return t * t * (3 - 2 * t);
}
public static double interpQuintic(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
}

View File

@@ -7,9 +7,6 @@
package com.dfsek.terra.api.util;
import net.jafama.FastMath;
public enum Rotation {
CW_90(90),
@@ -23,7 +20,7 @@ public enum Rotation {
}
public static Rotation fromDegrees(int deg) {
return switch(FastMath.floorMod(deg, 360)) {
return switch(Math.floorMod(deg, 360)) {
case 0 -> Rotation.NONE;
case 90 -> Rotation.CW_90;
case 180 -> Rotation.CW_180;

View File

@@ -51,25 +51,25 @@ public class ProbabilityCollection<E> implements Collection<E> {
@SuppressWarnings("unchecked")
public E get(NoiseSampler n, double x, double y, double z, long seed) {
if(array.length == 0) return null;
return (E) array[MathUtil.normalizeIndex(n.noise(seed, x, y, z), array.length)];
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, x, y, z), array.length)];
}
@SuppressWarnings("unchecked")
public E get(NoiseSampler n, Vector3Int vector3Int, long seed) {
if(array.length == 0) return null;
return (E) array[MathUtil.normalizeIndex(n.noise(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()), array.length)];
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()), array.length)];
}
@SuppressWarnings("unchecked")
public E get(NoiseSampler n, Vector3 vector3Int, long seed) {
if(array.length == 0) return null;
return (E) array[MathUtil.normalizeIndex(n.noise(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()), array.length)];
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()), array.length)];
}
@SuppressWarnings("unchecked")
public E get(NoiseSampler n, double x, double z, long seed) {
if(array.length == 0) return null;
return (E) array[MathUtil.normalizeIndex(n.noise(seed, x, z), array.length)];
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, x, z), array.length)];
}
@SuppressWarnings("unchecked")

View File

@@ -7,8 +7,6 @@
package com.dfsek.terra.api.util.vector;
import net.jafama.FastMath;
import com.dfsek.terra.api.util.MathUtil;
@@ -41,7 +39,7 @@ public class Vector2 {
* @return length
*/
public double length() {
return FastMath.sqrt(lengthSquared());
return Math.sqrt(lengthSquared());
}
/**
@@ -61,7 +59,7 @@ public class Vector2 {
* @return Distance between vectors
*/
public double distance(Vector2 other) {
return FastMath.sqrt(distanceSquared(other));
return Math.sqrt(distanceSquared(other));
}
/**
@@ -102,11 +100,11 @@ public class Vector2 {
public int getBlockX() {
return FastMath.floorToInt(x);
return (int) Math.floor(x);
}
public int getBlockZ() {
return FastMath.floorToInt(z);
return (int) Math.floor(z);
}
@Override
@@ -166,7 +164,7 @@ public class Vector2 {
* @return length
*/
public double length() {
return FastMath.sqrt(lengthSquared());
return Math.sqrt(lengthSquared());
}
/**

View File

@@ -7,7 +7,6 @@
package com.dfsek.terra.api.util.vector;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.util.MathUtil;
@@ -33,11 +32,11 @@ public class Vector3 {
}
public double length() {
return FastMath.sqrt(lengthSquared());
return Math.sqrt(lengthSquared());
}
public double inverseLength() {
return FastMath.invSqrtQuick(lengthSquared());
return MathUtil.invSqrt(lengthSquared());
}
/**
@@ -52,7 +51,7 @@ public class Vector3 {
* @return the distance
*/
public double distance(@NotNull Vector3 o) {
return FastMath.sqrt(FastMath.pow2(x - o.getX()) + FastMath.pow2(y - o.getY()) + FastMath.pow2(z - o.getZ()));
return Math.sqrt(Math.pow(x - o.getX(), 2) + Math.pow(y - o.getY(), 2) + Math.pow(z - o.getZ(), 2));
}
/**
@@ -63,7 +62,7 @@ public class Vector3 {
* @return the distance
*/
public double distanceSquared(@NotNull Vector3 o) {
return FastMath.pow2(x - o.getX()) + FastMath.pow2(y - o.getY()) + FastMath.pow2(z - o.getZ());
return Math.pow(x - o.getX(), 2) + Math.pow(y - o.getY(), 2) + Math.pow(z - o.getZ(), 2);
}
/**
@@ -94,15 +93,15 @@ public class Vector3 {
public int getBlockX() {
return FastMath.floorToInt(x);
return (int) Math.floor(x);
}
public int getBlockY() {
return FastMath.floorToInt(y);
return (int) Math.floor(y);
}
public int getBlockZ() {
return FastMath.floorToInt(z);
return (int) Math.floor(z);
}
/**
@@ -201,11 +200,11 @@ public class Vector3 {
}
public double length() {
return FastMath.sqrt(lengthSquared());
return Math.sqrt(lengthSquared());
}
public double inverseLength() {
return FastMath.invSqrtQuick(lengthSquared());
return MathUtil.invSqrt(lengthSquared());
}
public Mutable normalize() {
@@ -323,8 +322,8 @@ public class Vector3 {
double x = getX(), y = getY(), z = getZ();
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double cosTheta = MathUtil.cos(angle);
double sinTheta = MathUtil.sin(angle);
double dotProduct = this.dot(axis);
double xPrime = x2 * dotProduct * (1d - cosTheta)
@@ -355,8 +354,8 @@ public class Vector3 {
*/
@NotNull
public Mutable rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double angleCos = MathUtil.cos(angle);
double angleSin = MathUtil.sin(angle);
double y = angleCos * getY() - angleSin * getZ();
double z = angleSin * getY() + angleCos * getZ();
@@ -378,8 +377,8 @@ public class Vector3 {
*/
@NotNull
public Mutable rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double angleCos = MathUtil.cos(angle);
double angleSin = MathUtil.sin(angle);
double x = angleCos * getX() + angleSin * getZ();
double z = -angleSin * getX() + angleCos * getZ();
@@ -401,8 +400,8 @@ public class Vector3 {
*/
@NotNull
public Mutable rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double angleCos = MathUtil.cos(angle);
double angleSin = MathUtil.sin(angle);
double x = angleCos * getX() - angleSin * getY();
double y = angleSin * getX() + angleCos * getY();
@@ -420,15 +419,15 @@ public class Vector3 {
}
public int getBlockX() {
return FastMath.floorToInt(x);
return (int) Math.floor(x);
}
public int getBlockY() {
return FastMath.floorToInt(y);
return (int) Math.floor(y);
}
public int getBlockZ() {
return FastMath.floorToInt(z);
return (int) Math.floor(z);
}
}
}