mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-21 16:09:19 +00:00
* uwu * reorder * Change author * Sync upstream * Lithium: equipment tracking * Faster CraftServer#getworlds list creation Co-Authored-By: Kobe ⑧ <102713261+HaHaWTH@users.noreply.github.com>
135 lines
6.1 KiB
Diff
135 lines
6.1 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Taiyou06 <kaandindar21@gmail.com>
|
|
Date: Thu, 7 Nov 2024 23:51:51 +0100
|
|
Subject: [PATCH] Lithium: CompactSineLUT
|
|
|
|
This patch is based on the following mixin:
|
|
"net/caffeinemc/mods/lithium/mixin/math/sine_lut/MthMixin.java"
|
|
By: 2No2Name <2No2Name@web.de>
|
|
As part of: Lithium (https://github.com/CaffeineMC/lithium-fabric)
|
|
Licensed under: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
|
|
|
|
diff --git a/src/main/java/net/minecraft/util/Mth.java b/src/main/java/net/minecraft/util/Mth.java
|
|
index cb8cde3c1b65329f92b7c78e529e128f5a408fd6..4d1576b9920c1a0826b03565fe660c33bc285ef6 100644
|
|
--- a/src/main/java/net/minecraft/util/Mth.java
|
|
+++ b/src/main/java/net/minecraft/util/Mth.java
|
|
@@ -29,7 +29,7 @@ public class Mth {
|
|
public static final Vector3f Y_AXIS = new Vector3f(0.0F, 1.0F, 0.0F);
|
|
public static final Vector3f X_AXIS = new Vector3f(1.0F, 0.0F, 0.0F);
|
|
public static final Vector3f Z_AXIS = new Vector3f(0.0F, 0.0F, 1.0F);
|
|
- private static final float[] SIN = Util.make(new float[65536], sineTable -> {
|
|
+ public static final float[] SIN = Util.make(new float[65536], sineTable -> { // Leaf - Lithium - private -> public
|
|
for (int ix = 0; ix < sineTable.length; ix++) {
|
|
sineTable[ix] = (float)Math.sin((double)ix * Math.PI * 2.0 / 65536.0);
|
|
}
|
|
@@ -46,11 +46,11 @@ public class Mth {
|
|
private static final double[] COS_TAB = new double[257];
|
|
|
|
public static float sin(float value) {
|
|
- return SIN[(int)(value * 10430.378F) & 65535];
|
|
+ return org.dreeam.leaf.util.math.CompactSineLUT.sin(value); // Leaf - Lithium - CompactSineLUT
|
|
}
|
|
|
|
public static float cos(float value) {
|
|
- return SIN[(int)(value * 10430.378F + 16384.0F) & 65535];
|
|
+ return org.dreeam.leaf.util.math.CompactSineLUT.cos(value); // Leaf - Lithium - CompactSineLUT
|
|
}
|
|
|
|
public static float sqrt(float value) {
|
|
diff --git a/src/main/java/org/dreeam/leaf/util/math/CompactSineLUT.java b/src/main/java/org/dreeam/leaf/util/math/CompactSineLUT.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..ef19bc097a5125afcac77497d4ab51d3fe1692c0
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/util/math/CompactSineLUT.java
|
|
@@ -0,0 +1,90 @@
|
|
+package org.dreeam.leaf.util.math;
|
|
+
|
|
+import net.minecraft.util.Mth;
|
|
+
|
|
+/**
|
|
+ * A replacement for the sine angle lookup table used in {@link Mth}, both reducing the size of LUT and improving
|
|
+ * the access patterns for common paired sin/cos operations.
|
|
+ * <p>
|
|
+ * sin(-x) = -sin(x)
|
|
+ * ... to eliminate negative angles from the LUT.
|
|
+ * <p>
|
|
+ * sin(x) = sin(pi/2 - x)
|
|
+ * ... to eliminate supplementary angles from the LUT.
|
|
+ * <p>
|
|
+ * Using these identities allows us to reduce the LUT from 64K entries (256 KB) to just 16K entries (64 KB), enabling
|
|
+ * it to better fit into the CPU's caches at the expense of some cycles on the fast path. The implementation has been
|
|
+ * tightly optimized to avoid branching where possible and to use very quick integer operations.
|
|
+ * <p>
|
|
+ * Generally speaking, reducing the size of a lookup table is always a good optimization, but since we need to spend
|
|
+ * extra CPU cycles trying to maintain parity with vanilla, there is the potential risk that this implementation ends
|
|
+ * up being slower than vanilla when the lookup table is able to be kept in cache memory.
|
|
+ * <p>
|
|
+ * Unlike other "fast math" implementations, the values returned by this class are *bit-for-bit identical* with those
|
|
+ * from {@link Mth}. Validation is performed during runtime to ensure that the table is correct.
|
|
+ *
|
|
+ * @author coderbot16 Author of the original (and very clever) implementation in Rust:
|
|
+ * https://gitlab.com/coderbot16/i73/-/tree/master/i73-trig/src
|
|
+ * @author jellysquid3 Additional optimizations, port to Java
|
|
+ */
|
|
+public class CompactSineLUT {
|
|
+ private static final int[] SINE_TABLE_INT = new int[16384 + 1];
|
|
+ private static final float SINE_TABLE_MIDPOINT;
|
|
+
|
|
+ static {
|
|
+ final float[] SINE_TABLE = Mth.SIN;
|
|
+ // Copy the sine table, covering to raw int bits
|
|
+ for (int i = 0; i < SINE_TABLE_INT.length; i++) {
|
|
+ SINE_TABLE_INT[i] = Float.floatToRawIntBits(SINE_TABLE[i]);
|
|
+ }
|
|
+
|
|
+ SINE_TABLE_MIDPOINT = SINE_TABLE[SINE_TABLE.length / 2];
|
|
+
|
|
+ // Test that the lookup table is correct during runtime
|
|
+ for (int i = 0; i < SINE_TABLE.length; i++) {
|
|
+ float expected = SINE_TABLE[i];
|
|
+ float value = lookup(i);
|
|
+
|
|
+ if (expected != value) {
|
|
+ throw new IllegalArgumentException(String.format("LUT error at index %d (expected: %s, found: %s)", i, expected, value));
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ // [VanillaCopy] MathHelper#sin(float)
|
|
+ public static float sin(float f) {
|
|
+ return lookup((int) (f * 10430.378f) & 0xFFFF);
|
|
+ }
|
|
+
|
|
+ // [VanillaCopy] MathHelper#cos(float)
|
|
+ public static float cos(float f) {
|
|
+ return lookup((int) (f * 10430.378f + 16384.0f) & 0xFFFF);
|
|
+ }
|
|
+
|
|
+ private static float lookup(int index) {
|
|
+ // A special case... Is there some way to eliminate this?
|
|
+ if (index == 32768) {
|
|
+ return SINE_TABLE_MIDPOINT;
|
|
+ }
|
|
+
|
|
+ // Trigonometric identity: sin(-x) = -sin(x)
|
|
+ // Given a domain of 0 <= x <= 2*pi, just negate the value if x > pi.
|
|
+ // This allows the sin table size to be halved.
|
|
+ int neg = (index & 0x8000) << 16;
|
|
+
|
|
+ // All bits set if (pi/2 <= x), none set otherwise
|
|
+ // Extracts the 15th bit from 'half'
|
|
+ int mask = (index << 17) >> 31;
|
|
+
|
|
+ // Trigonometric identity: sin(x) = sin(pi/2 - x)
|
|
+ int pos = (0x8001 & mask) + (index ^ mask);
|
|
+
|
|
+ // Wrap the position in the table. Moving this down to immediately before the array access
|
|
+ // seems to help the Hotspot compiler optimize the bit math better.
|
|
+ pos &= 0x7fff;
|
|
+
|
|
+ // Fetch the corresponding value from the LUT and invert the sign bit as needed
|
|
+ // This directly manipulate the sign bit on the float bits to simplify logic
|
|
+ return Float.intBitsToFloat(SINE_TABLE_INT[pos] ^ neg);
|
|
+ }
|
|
+}
|