Yatopia/patches/server/0060-Lithium-CompactSineLUT.patch
Simon Gardling e01705826c
Upstream (#484)
* Updated Upstream and Sidestream(s) (Paper/Purpur/Empirecraft)

Upstream/An Sidestream has released updates that appears to apply and compile correctly
This update has NOT been tested by YatopiaMC and as with ANY update, please do your own testing.

Paper Changes:
2a67a9e51 Fix missing CraftMetaBook#toBuilder override
48aa06106 Add more Wandering Trader API (#5020)
8cec462a5 [CI-SKIP] Remove Astei from the MIT list (#5577)
21fbc3196 [CI-SKIP] Add Other textarea to issue templates (#5562)
453e983e4 fix isProxyOnlineMode coverage, don't lookup UUIDs in offline mode (Fixes #2011)
81cc4f928 Send empty commands if tab completion is disabled (Closes #5519)
9ed3e470d Keep moveToWorld with old method signature
99a66a583 [Auto] Updated Upstream (CraftBukkit)
976c6d425 [CI-SKIP] [Auto] Rebuild Patches
51deec726 Drop unneeded portal patch (#5566)
07a18c457 [Auto] Updated Upstream (CraftBukkit)
6733d7875 [Auto] Updated Upstream (Bukkit)
9958447cb [Auto] Updated Upstream (Bukkit)
840e72091 [CI-SKIP] [Auto] Rebuild Patches
a33232d4a Add beacon activation and deactivation events (#5121)

Purpur Changes:
ebc0765 Updated Upstream (Paper)
5bc5dfd Fix #318 - Stonecutter does not damage when sneaking
8366582 Resolve #286 - Add config and API for item immunity to lightning
a86555b Updated Upstream (Paper)
8b2607c Config for only sending advancements to affected players (#312)
7ee3393 Add config for the piston push limit (#311)
a30e5ac Add config for void damage dealt
786382b Updated Upstream (Paper)
60d8411 [ci-skip] fix patch name
b64f536 Config for changing the blocks that turn into paths (#303)
1d3f710 Updated Upstream (Paper)
3af86ad Oopsie
4b0342a Gamemode extra permissions
17c8573 [CI-SKIP] Don't pass ping to demo command output
c3481c0 Initialize the credits command output in config
a176fe6 Add credits command

Empirecraft Changes:
b2879a24 Updated Paper

* Updated Upstream and Sidestream(s) (Paper/Tuinity/Airplane/Purpur/Empirecraft)

Upstream/An Sidestream has released updates that appears to apply and compile correctly
This update has NOT been tested by YatopiaMC and as with ANY update, please do your own testing.

Paper Changes:
b3cc88799 Add the ability to clear entities from block storage (#5598)
f50171f3b Add missing rarity method on ItemStack (#5594)
aeb6e7dd2 Updated Upstream (Bukkit/CraftBukkit/Spigot) (#5601)
b0a4f353b fix cancelling block falling causing client desync (fixes #5386) (#5419)
8ed45920c Don't annotate type parameters using JetBrains annotations (#5600)
d7625d926 Add new methods from PlayerMoveEvent to EntityMoveEvent (#5591)
29785297b Allow for Component suggestion tooltips in AsyncTabCompleteEvent (#5504)
0aea6c2ff Use JsonSerializationContext#serialize instead of recursion for AdventureComponents - fixes #5580 and #5371
8fcef3c1f Return after sending empty commands (#5586)

Tuinity Changes:
3350246 Updated Upstream (Paper)
f773caf Fix rare ticket level recursion crash
0fa8a0e Starlight Handle concurrent chunk generation and lighting better

Airplane Changes:
fad74c2 Fix JB annotations for gradle (credit jpenilla)
d02f034 Updated Upstream (Tuinity)

Purpur Changes:
adfe5d3 Updated Upstream (Paper)
7a728f8 Fix #325 - Fix SPIGOT-6278
f5c3dae Updated Upstream (Paper)
a80e5b6 Dont create item meta when checking if meta properties exist
90339c5 Add missing netherite from Material#isArmor
bedac18 Updated Upstream (Paper, Tuinity, & Airplane)
3b1531a [ci-skip] Add wrapper validation step to actions
73ab4e6 Update the "changing the blocks that turn into paths" defaults to reflect the vanilla defaults. (#322)
a24b31b Updated Upstream (Paper)

Empirecraft Changes:
87ceb204 Updated Paper
a3497a7e Fix some patches
2ed9e7fa Updated Paper
d5a47a3d Updated Paper

* Updated Upstream and Sidestream(s) (Tuinity)

Upstream/An Sidestream has released updates that appears to apply and compile correctly
This update has NOT been tested by YatopiaMC and as with ANY update, please do your own testing.

Tuinity Changes:
1382473 Null check chunk in block changes for Starlight
2021-05-10 23:16:50 -04:00

139 lines
6.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: JellySquid <jellysquid+atwork@protonmail.com>
Date: Fri, 5 Feb 2021 00:16:30 -0600
Subject: [PATCH] Lithium: CompactSineLUT
Original code by JellySquid, licensed under GNU Lesser General Public License v3.0
you can find the original code on https://github.com/CaffeineMC/lithium-fabric/ (Yarn mappings)
diff --git a/src/main/java/net/minecraft/util/MathHelper.java b/src/main/java/net/minecraft/util/MathHelper.java
index e9e7fcf2b63febe2a7d055826fabb86bc13a5cf3..b95115aca72ba0cf6451096ddbd8b50a8f3bb5c6 100644
--- a/src/main/java/net/minecraft/util/MathHelper.java
+++ b/src/main/java/net/minecraft/util/MathHelper.java
@@ -5,6 +5,7 @@ import java.util.UUID;
import java.util.function.IntPredicate;
import net.minecraft.SystemUtils;
import net.minecraft.core.BaseBlockPosition;
+import org.yatopiamc.yatopia.server.math.CompactSineLUT;
public class MathHelper {
@@ -15,6 +16,7 @@ public class MathHelper {
}
});
+ public static float[] getSinTable() { return b; } // Yatopia - OBFHELPER
private static final Random c = new Random();
private static final int[] d = new int[]{0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
private static final double e = Double.longBitsToDouble(4805340802404319232L);
@@ -22,11 +24,11 @@ public class MathHelper {
private static final double[] g = new double[257];
public static float sin(float f) {
- return MathHelper.b[(int) (f * 10430.378F) & '\uffff'];
+ return CompactSineLUT.sin(f); // Yatopia - CompactSineLUT
}
public static float cos(float f) {
- return MathHelper.b[(int) (f * 10430.378F + 16384.0F) & '\uffff'];
+ return CompactSineLUT.cos(f); // Yatopia - CompactSineLUT
}
public static float c(float f) {
diff --git a/src/main/java/org/yatopiamc/yatopia/server/math/CompactSineLUT.java b/src/main/java/org/yatopiamc/yatopia/server/math/CompactSineLUT.java
new file mode 100644
index 0000000000000000000000000000000000000000..1bb8ba3836af872bc2e58898f16a0b91036b9b02
--- /dev/null
+++ b/src/main/java/org/yatopiamc/yatopia/server/math/CompactSineLUT.java
@@ -0,0 +1,90 @@
+package org.yatopiamc.yatopia.server.math;
+
+import net.minecraft.util.MathHelper;
+
+/**
+ * A replacement for the sine angle lookup table used in {@link MathHelper}, both reducing the size of LUT and improving
+ * the access patterns for common paired sin/cos operations.
+ *
+ * sin(-x) = -sin(x)
+ * ... to eliminate negative angles from the LUT.
+ *
+ * sin(x) = sin(pi/2 - x)
+ * ... to eliminate supplementary angles from the LUT.
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * Unlike other "fast math" implementations, the values returned by this class are *bit-for-bit identical* with those
+ * from {@link MathHelper}. 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 = MathHelper.getSinTable();
+ // 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.38) & 0xFFFF);
+ }
+
+ // [VanillaCopy] MathHelper#cos(float)
+ public static float cos(float f) {
+ return lookup((int) (f * 10430.38 + 16384.0) & 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);
+ }
+}