9
0
mirror of https://github.com/BX-Team/DivineMC.git synced 2026-01-06 15:41:52 +00:00

23/50 patches (lithium)

This commit is contained in:
NONPLAYT
2024-06-17 15:39:59 +03:00
parent d2fa8755c0
commit c9f37aacfe
10 changed files with 37 additions and 39 deletions

View File

@@ -0,0 +1,41 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Fri, 26 Jan 2024 17:42:42 +0300
Subject: [PATCH] vmp: skip entity move if movement is zero
Original code by RelativityMC, licensed under MIT
You can find the original code on https://github.com/RelativityMC/VMP-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 2892d3ad489f0fe2a1b11ef0eb7f8b5290f841a4..50a818fc10a717ddeaf35cb2c4455510bfd9cbad 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -323,6 +323,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
public float yRotO;
public float xRotO;
private AABB bb;
+ private boolean boundingBoxChanged = false; // DivineMC - vmp: skip entity move if movement is zero
public boolean onGround;
public boolean horizontalCollision;
public boolean verticalCollision;
@@ -1090,6 +1091,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
}
public void move(MoverType movementType, Vec3 movement) {
+ // DivineMC start - vmp: skip entity move if movement is zero
+ if (!boundingBoxChanged && movement.equals(Vec3.ZERO)) {
+ boundingBoxChanged = false;
+ return;
+ }
+ // DivineMC end
final Vec3 originalMovement = movement; // Paper - Expose pre-collision velocity
if (this.noPhysics) {
this.setPos(this.getX() + movement.x, this.getY() + movement.y, this.getZ() + movement.z);
@@ -4045,6 +4052,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
}
public final void setBoundingBox(AABB boundingBox) {
+ if (!this.bb.equals(boundingBox)) boundingBoxChanged = true; // DivineMC - vmp: skip entity move if movement is zero
// CraftBukkit start - block invalid bounding boxes
double minX = boundingBox.minX,
minY = boundingBox.minY,

View File

@@ -0,0 +1,29 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sun, 14 Jan 2024 14:50:10 +0300
Subject: [PATCH] vmp: use linked map for entity trackers for faster iteration
Original code by RelativityMC, licensed under MIT
You can find the original code on https://github.com/RelativityMC/VMP-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index 686f3d8c91e1ffc0c7ffe1cd9bcf5df5503cb938..da56e8d58f8b0391689f697807a4873fc759a26b 100644
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -12,6 +12,7 @@ import com.mojang.datafixers.DataFixer;
import com.mojang.logging.LogUtils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap; // DivineMC - vmp: use linked map for entity trackers for faster iteration
import it.unimi.dsi.fastutil.longs.Long2ByteMap;
import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
@@ -229,7 +230,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
this.toDrop = new LongOpenHashSet();
this.tickingGenerated = new AtomicInteger();
this.playerMap = new PlayerMap();
- this.entityMap = new Int2ObjectOpenHashMap();
+ this.entityMap = new Int2ObjectLinkedOpenHashMap<>(); // DivineMC - vmp: use linked map for entity trackers for faster iteration
this.chunkTypeCache = new Long2ByteOpenHashMap();
this.chunkSaveCooldowns = new Long2LongOpenHashMap();
// Paper - rewrite chunk system

View File

@@ -0,0 +1,52 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 8 Apr 2023 23:45:11 +0300
Subject: [PATCH] lithium: ai.raid
This patch is based on the following mixin:
"me/jellysquid/mods/lithium/mixin/ai/raid/RaidMixin.java"
By: Angeline <jellysquid3@users.noreply.github.com>
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/world/entity/raid/Raid.java b/src/main/java/net/minecraft/world/entity/raid/Raid.java
index dcbef04bbaab988096bf416163264833e84d1967..f3e3013c0b02224de86ea4f3dd945fd8a26090e1 100644
--- a/src/main/java/net/minecraft/world/entity/raid/Raid.java
+++ b/src/main/java/net/minecraft/world/entity/raid/Raid.java
@@ -107,6 +107,7 @@ public class Raid {
private Raid.RaidStatus status;
private int celebrationTicks;
private Optional<BlockPos> waveSpawnPos;
+ private boolean isBarDirty; // DivineMC - lithium: ai.raid
// Paper start
private static final String PDC_NBT_KEY = "BukkitValues";
private static final org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry PDC_TYPE_REGISTRY = new org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry();
@@ -292,6 +293,12 @@ public class Raid {
public void tick() {
if (!this.isStopped()) {
+ // DivineMC start - lithium: ai.raid
+ if (this.isBarDirty) {
+ this.updateBossbarInternal();
+ this.isBarDirty = false;
+ }
+ // DivineMC end
if (this.status == Raid.RaidStatus.ONGOING) {
boolean flag = this.active;
@@ -660,9 +667,15 @@ public class Raid {
}
+ // DivineMC start - lithium: ai.raid
public void updateBossbar() {
+ this.isBarDirty = true;
+ }
+
+ private void updateBossbarInternal() {
this.raidEvent.setProgress(Mth.clamp(this.getHealthOfLivingRaiders() / this.totalHealth, 0.0F, 1.0F));
}
+ // DivineMC end
public float getHealthOfLivingRaiders() {
float f = 0.0F;

View File

@@ -0,0 +1,29 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 8 Apr 2023 01:28:01 +0300
Subject: [PATCH] lithium: collections.gamerules
Original code by CaffeineMC, licensed under LGPL v3
You can find the original code on https://github.com/CaffeineMC/lithium-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/world/level/GameRules.java b/src/main/java/net/minecraft/world/level/GameRules.java
index 89e327bc3a45879fe68887c7aadb077f31a770eb..bb0fcf15fa49d6b352b11d4cffe4e2c8b0fe5c30 100644
--- a/src/main/java/net/minecraft/world/level/GameRules.java
+++ b/src/main/java/net/minecraft/world/level/GameRules.java
@@ -30,6 +30,7 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import org.slf4j.Logger;
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
public class GameRules {
@@ -149,7 +150,7 @@ public class GameRules {
}
private GameRules(Map<GameRules.Key<?>, GameRules.Value<?>> rules) {
- this.rules = rules;
+ this.rules = new Object2ObjectOpenHashMap<>(rules); // DivineMC - lithium: collections.gamerules
// Paper start - Perf: Use array for gamerule storage
int arraySize = rules.keySet().stream().mapToInt(key -> key.gameRuleIndex).max().orElse(-1) + 1;

View File

@@ -0,0 +1,32 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 13 Jan 2024 20:12:23 +0300
Subject: [PATCH] lithium: collections.entity_by_type
Original code by CaffeineMC, licensed under LGPL v3
You can find the original code on https://github.com/CaffeineMC/lithium-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java b/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
index 038710ba934a9a57815dfe9f414b98223b848385..f631aa8bc724d0fc899783967417c3e01aac3c9c 100644
--- a/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
+++ b/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
@@ -3,7 +3,6 @@ package net.minecraft.util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Collections;
@@ -12,9 +11,10 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.Util;
+import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; // DivineMC
public class ClassInstanceMultiMap<T> extends AbstractCollection<T> {
- private final Map<Class<?>, List<T>> byClass = Maps.newHashMap();
+ private final Map<Class<?>, List<T>> byClass = new Reference2ReferenceOpenHashMap<>(); // DivineMC
private final Class<T> baseClass;
private final List<T> allInstances = Lists.newArrayList();

View File

@@ -0,0 +1,28 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 13 Jan 2024 20:37:54 +0300
Subject: [PATCH] lithium: entity.fast_elytra_check + entity.fast_hand_swing
Original code by CaffeineMC, licensed under LGPL v3
You can find the original code on https://github.com/CaffeineMC/lithium-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 8c2dd45a146395168e92f2304082a7f6e53edec6..6aaee520213e576f131db430bd4d6df1b34ba197 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -2641,6 +2641,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
}
protected void updateSwingTime() {
+ if (!this.swinging && this.swingTime == 0) return; // DivineMC - lithium: entity.fast_hand_swing
int i = this.getCurrentSwingDuration();
if (this.swinging) {
@@ -3641,6 +3642,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
}
private void updateFallFlying() {
+ if (!this.isFallFlying()) return; // DivineMC - lithium: entity.fast_elytra_check
boolean flag = this.getSharedFlag(7);
if (flag && !this.onGround() && !this.isPassenger() && !this.hasEffect(MobEffects.LEVITATION)) {

View File

@@ -0,0 +1,75 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Wed, 17 Apr 2024 02:08:02 +0300
Subject: [PATCH] lithium: precompute shape arrays
Original code by CaffeineMC, licensed under LGPL v3
You can find the original code on https://github.com/CaffeineMC/lithium-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/core/Direction.java b/src/main/java/net/minecraft/core/Direction.java
index 03c45ee77276462818a6f774b5945b25924aa3f0..7c56228639027d0bfcf8901a5f3c996c6b1faa8c 100644
--- a/src/main/java/net/minecraft/core/Direction.java
+++ b/src/main/java/net/minecraft/core/Direction.java
@@ -46,7 +46,7 @@ public enum Direction implements StringRepresentable {
private final Direction.Axis axis;
private final Direction.AxisDirection axisDirection;
private final Vec3i normal;
- private static final Direction[] VALUES = values();
+ public static final Direction[] VALUES = values(); // DivineMC - lithium: precompute shape arrays
private static final Direction[] BY_3D_DATA = Arrays.stream(VALUES)
.sorted(Comparator.comparingInt(direction -> direction.data3d))
.toArray(Direction[]::new);
diff --git a/src/main/java/net/minecraft/world/phys/shapes/CubePointRange.java b/src/main/java/net/minecraft/world/phys/shapes/CubePointRange.java
index ad02cdb00360165f6405eb3044bd8320f01a7ef1..61f6f612470076c7b6930dcb63911a2ac6d45be1 100644
--- a/src/main/java/net/minecraft/world/phys/shapes/CubePointRange.java
+++ b/src/main/java/net/minecraft/world/phys/shapes/CubePointRange.java
@@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.doubles.AbstractDoubleList;
public class CubePointRange extends AbstractDoubleList {
private final int parts;
+ private double scale; // DivineMC - lithium: precompute shape arrays
public CubePointRange(int sectionCount) {
if (sectionCount <= 0) {
@@ -11,10 +12,11 @@ public class CubePointRange extends AbstractDoubleList {
} else {
this.parts = sectionCount;
}
+ this.scale = 1.0D / sectionCount; // DivineMC - lithium: precompute shape arrays
}
public double getDouble(int i) {
- return (double)i / (double)this.parts;
+ return i * this.scale; // DivineMC - lithium: precompute shape arrays
}
public int size() {
diff --git a/src/main/java/net/minecraft/world/phys/shapes/CubeVoxelShape.java b/src/main/java/net/minecraft/world/phys/shapes/CubeVoxelShape.java
index d812949c7329ae2696b38dc792fa011ba87decb9..98e218c65d489822f334c06fddd1ab608662597b 100644
--- a/src/main/java/net/minecraft/world/phys/shapes/CubeVoxelShape.java
+++ b/src/main/java/net/minecraft/world/phys/shapes/CubeVoxelShape.java
@@ -5,13 +5,23 @@ import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
public final class CubeVoxelShape extends VoxelShape {
+ private DoubleList[] list; // DivineMC - lithium: precompute shape arrays
+
protected CubeVoxelShape(DiscreteVoxelShape voxels) {
super(voxels);
}
@Override
public DoubleList getCoords(Direction.Axis axis) {
- return new CubePointRange(this.shape.getSize(axis));
+ // DivineMC start - lithium: precompute shape arrays
+ if (this.list == null) {
+ this.list = new DoubleList[Direction.Axis.VALUES.length];
+ for (Direction.Axis existingAxis : Direction.Axis.VALUES) {
+ this.list[existingAxis.ordinal()] = new CubePointRange(this.shape.getSize(axis));
+ }
+ }
+ return this.list[axis.ordinal()];
+ // DivineMC end
}
@Override

View File

@@ -0,0 +1,39 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Tue, 16 Jan 2024 20:35:06 +0300
Subject: [PATCH] vmp: spawn_density_cap
Original code by RelativityMC, licensed under MIT
You can find the original code on https://github.com/RelativityMC/VMP-fabric (Yarn mappings)
diff --git a/src/main/java/net/minecraft/world/level/LocalMobCapCalculator.java b/src/main/java/net/minecraft/world/level/LocalMobCapCalculator.java
index 2039b16e5e9bc0797b3f31081d221bb8b34a4dc7..4239fa40788fb92211cc52ed5e06236621195018 100644
--- a/src/main/java/net/minecraft/world/level/LocalMobCapCalculator.java
+++ b/src/main/java/net/minecraft/world/level/LocalMobCapCalculator.java
@@ -3,8 +3,6 @@ package net.minecraft.world.level;
import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
-import it.unimi.dsi.fastutil.objects.Object2IntMap;
-import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.server.level.ChunkMap;
@@ -42,14 +40,14 @@ public class LocalMobCapCalculator {
}
static class MobCounts {
- private final Object2IntMap<MobCategory> counts = new Object2IntOpenHashMap<>(MobCategory.values().length);
+ private final int[] spawnGroupDensities = new int[MobCategory.values().length]; // DivineMC - vmp: spawn_density_cap
public void add(MobCategory spawnGroup) {
- this.counts.computeInt(spawnGroup, (group, density) -> density == null ? 1 : density + 1);
+ this.spawnGroupDensities[spawnGroup.ordinal()]++; // DivineMC - vmp: spawn_density_cap
}
public boolean canSpawn(MobCategory spawnGroup) {
- return this.counts.getOrDefault(spawnGroup, 0) < spawnGroup.getMaxInstancesPerChunk();
+ return this.spawnGroupDensities[spawnGroup.ordinal()] < spawnGroup.getMaxInstancesPerChunk(); // DivineMC - vmp: spawn_density_cap
}
}
}

View File

@@ -0,0 +1,43 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 25 May 2024 17:13:35 +0300
Subject: [PATCH] lithium: cached_hashcode
diff --git a/src/main/java/net/minecraft/world/level/block/Block.java b/src/main/java/net/minecraft/world/level/block/Block.java
index 6d012ca724f1373bcf9e8d86d22194143f56d52b..daad0d67428bb49d7f0b37bec430ceb0d30564cf 100644
--- a/src/main/java/net/minecraft/world/level/block/Block.java
+++ b/src/main/java/net/minecraft/world/level/block/Block.java
@@ -633,11 +633,19 @@ public class Block extends BlockBehaviour implements ItemLike {
private final BlockState first;
private final BlockState second;
private final Direction direction;
+ private final int hash; // DivineMC - lithium: cached_hashcode
public BlockStatePairKey(BlockState self, BlockState other, Direction facing) {
this.first = self;
this.second = other;
this.direction = facing;
+
+ // DivineMC start - lithium: cached_hashcode
+ int i = this.first.hashCode();
+ i = 31 * i + this.second.hashCode();
+ i = 31 * i + this.direction.hashCode();
+ this.hash = i;
+ // DivineMC end
}
public boolean equals(Object object) {
@@ -653,11 +661,7 @@ public class Block extends BlockBehaviour implements ItemLike {
}
public int hashCode() {
- int i = this.first.hashCode();
-
- i = 31 * i + this.second.hashCode();
- i = 31 * i + this.direction.hashCode();
- return i;
+ return this.hash; // DivineMC - lithium: cached_hashcode
}
}
}

View File

@@ -0,0 +1,127 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 25 May 2024 18:39:17 +0300
Subject: [PATCH] lithium: math.sine_lut
diff --git a/src/main/java/net/minecraft/util/Mth.java b/src/main/java/net/minecraft/util/Mth.java
index f298cdfcf1539e467f57f9f7789de3cf2ca54665..f83a544be66206ddd52f11524e84e821eb15343c 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 -> { // DivineMC - lithium: math.sine_lut
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 space.bxteam.divinemc.util.lithium.CompactSineLUT.sin(value); // DivineMC - lithium: math.sine_lut
}
public static float cos(float value) {
- return SIN[(int)(value * 10430.378F + 16384.0F) & 65535];
+ return space.bxteam.divinemc.util.lithium.CompactSineLUT.cos(value); // DivineMC - lithium: math.sine_lut
}
public static float sqrt(float value) {
diff --git a/src/main/java/space/bxteam/divinemc/util/lithium/CompactSineLUT.java b/src/main/java/space/bxteam/divinemc/util/lithium/CompactSineLUT.java
new file mode 100644
index 0000000000000000000000000000000000000000..79a49c3e99ab069172f2fd85a942474f0c872fc9
--- /dev/null
+++ b/src/main/java/space/bxteam/divinemc/util/lithium/CompactSineLUT.java
@@ -0,0 +1,88 @@
+package space.bxteam.divinemc.util.lithium;
+
+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) <a href="https://gitlab.com/coderbot16/i73/-/tree/master/i73-trig/src">implementation</a> in Rust
+ * @author jellysquid3 Additional optimizations, port to Java
+ */
+public class CompactSineLUT {
+ private static final int[] SIN_INT = new int[16384 + 1];
+ private static final float SIN_MIDPOINT;
+
+ static {
+ // Copy the sine table, covering to raw int bits
+ for (int i = 0; i < SIN_INT.length; i++) {
+ SIN_INT[i] = Float.floatToRawIntBits(Mth.SIN[i]);
+ }
+
+ SIN_MIDPOINT = Mth.SIN[Mth.SIN.length / 2];
+
+ // Test that the lookup table is correct during runtime
+ for (int i = 0; i < Mth.SIN.length; i++) {
+ float expected = Mth.SIN[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] Mth#sin(float)
+ public static float sin(float f) {
+ return lookup((int) (f * 10430.378f) & 0xFFFF);
+ }
+
+ // [VanillaCopy] Mth#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 SIN_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(SIN_INT[pos] ^ neg);
+ }
+}