9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-20 15:29:30 +00:00

Use platform math functions

This commit is contained in:
Martijn Muijsers
2023-08-08 20:49:05 +02:00
parent de70588630
commit 562d6e3dd1
123 changed files with 205 additions and 18 deletions

View File

@@ -42,8 +42,9 @@ Of course, this fork would not exist without the years-long work of all the cont
Additional thanks and friendly greetings go out to the following forks and other projects, for their code, shared knowledge or generous support:
* [Airplane](https://github.com/TECHNOVE/Airplane)
* [Purpur](https://github.com/PurpurMC/Purpur)
* [Mirai](https://github.com/etil2jz/Mirai)
* [Leaf](https://github.com/Winds-Studio/Leaf)
* [Mirai](https://github.com/etil2jz/Mirai)
* [Kaiiju](https://github.com/KaiijuMC/Kaiiju)
* [KeYi](https://github.com/MC-Multithreading-Lab/KeYi-MT)
* [EmpireCraft](https://github.com/starlis/empirecraft)
* [Slice](https://github.com/Cryptite/Slice)
@@ -52,7 +53,6 @@ Additional thanks and friendly greetings go out to the following forks and other
* [VMP](https://github.com/RelativityMC/VMP-fabric)
* [C2ME](https://github.com/RelativityMC/C2ME-fabric)
* [MultiPaper](https://github.com/MultiPaper/MultiPaper)
* [Patina](https://github.com/PatinaMC/Patina)
* MCMT ([Fabric](https://github.com/himekifee/MCMTFabric), [Forge](https://github.com/jediminer543/JMT-MCMT))
## License

View File

@@ -0,0 +1,187 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Tue, 8 Aug 2023 20:43:20 +0200
Subject: [PATCH] Use platform math functions
License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
Gale - https://galemc.org
This patch is based on the following patch:
"Use Math.floor instead of fastfloor"
By: Xymb <xymb@endcrystal.me>
As part of: Kaiiju (https://github.com/KaiijuMC/Kaiiju)
Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
* Comparison of floor methods used in Paper *
Measure shown is floored number of milliseconds
(nanoseconds integer-divided by 1_000_000
taken to get the floor of 1000 randomly chosen doubles
(all in the range of [-Integer.MAX_VALUE + 10, Integer.MAX_VALUE - 10])
100_000 times (making it 100_000_000 floor operations total)
and adding it to a total.
We are testing the following methods:
* net.minecraft.util.Mth.floor
* java.lang.Math.floor
* java.lang.StrictMath.floor
* org.apache.commons.math3.util.FastMath.floor
* org.apache.commons.math3.util.FastMath.floor, but with a hot start (see comment in code)
* io.papermc.paper.util.MCUtil.fastFloor
The tests performed clearly show that Math.floor is the fastest.
This is most likely due to java.lang.Math's usage of the @IntrinsicCandidate
annotation, which allows the JVM to use a more optimized implementation at runtime.
However, in the case that there is no intrinsic replacement for Math.floor,
it defers to StrictMath.floor, which relies on a number of native methods, and is
still much faster than the existing Minecraft utility functions.
Therefore, using Math.floor instead of these functions is better regardless.
In Apache Commons Math 4, FastMath.floor has also been removed in favor of Math.floor.
The versions used:
* Windows 10 Home 21H2 19044.3086
* OpenJDK Runtime Environment Temurin-19.0.2+7 (build 19.0.2+7)
* Paper a3c760e6af1e8c7244ef75c6da6e6df278a79e14 on Minecraft 1.20.1
* Apache Commons Math 3.6.1
Results:
Total is of type int Total is of type double
----------------------------------------------------------------------------------
Mth.floor 2113 (double) Mth.floor 2658
(int) Math.floor 130 Math.floor 194
(int) StrictMath.floor 835 StrictMath.floor 381
(int) FastMath.floor 412 FastMath.floor 376
(int) FastMath.floor with hot start 359 FastMath.floor with hot start 321
MCUtil.fastFloor 2284 (double) MCUtil.fastFloor 2469
Code is below:
```java
package somepackage;
import io.papermc.paper.util.MCUtil;
import net.minecraft.util.Mth;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// IF FastMath.floor with a hot start:
// FastMath.floor(37485.5);
var random = new Random(4889338);
int size = 1000;
var values = new double[size];
double bound = Integer.MAX_VALUE - 10;
for (int i = 0; i < size; i++) {
values[i] = random.nextDouble(bound * 2) - bound;
}
int repeats = 100_000;
// int total = 0;
// OR
// double total = 0;
long start = System.nanoTime();
for (int repeat = 0; repeat < repeats; repeat++) {
for (int index = 0; index < size; index++) {
total += insert_function_being_tested_here(values[index]);
}
}
long diff = System.nanoTime() - start;
System.out.println(total);
System.out.println(diff / 1_000_000L);
}
}
```
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
index cb4379268b191d331c71be44642baac381ffaaf6..517ea24ed254eaa9c13439b8b83e8391ea5e10ff 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -164,13 +164,11 @@ public final class MCUtil {
}
public static int fastFloor(double x) {
- int truncated = (int)x;
- return x < (double)truncated ? truncated - 1 : truncated;
+ return (int) Math.floor(x); // Gale - use platform math functions
}
public static int fastFloor(float x) {
- int truncated = (int)x;
- return x < (double)truncated ? truncated - 1 : truncated;
+ return (int) Math.floor(x); // Gale - use platform math functions
}
public static float normalizeYaw(float f) {
@@ -231,11 +229,11 @@ public final class MCUtil {
}
public static int getChunkCoordinate(final double coordinate) {
- return MCUtil.fastFloor(coordinate) >> 4;
+ return ((int) Math.floor(coordinate)) >> 4; // Gale - use platform math functions
}
public static int getBlockCoordinate(final double coordinate) {
- return MCUtil.fastFloor(coordinate);
+ return (int) Math.floor(coordinate); // Gale - use platform math functions
}
public static long getBlockKey(final int x, final int y, final int z) {
diff --git a/src/main/java/net/minecraft/util/Mth.java b/src/main/java/net/minecraft/util/Mth.java
index 9084754103e948a02e68ee82336047e48e741438..7327f3bb1c1f764ae1da88af27b1e9fb03fb89d9 100644
--- a/src/main/java/net/minecraft/util/Mth.java
+++ b/src/main/java/net/minecraft/util/Mth.java
@@ -51,13 +51,11 @@ public class Mth {
}
public static int floor(float value) {
- int i = (int)value;
- return value < (float)i ? i - 1 : i;
+ return (int) Math.floor(value); // Gale - use platform math functions
}
public static int floor(double value) {
- int i = (int)value;
- return value < (double)i ? i - 1 : i;
+ return (int) Math.floor(value); // Gale - use platform math functions
}
public static long lfloor(double value) {
@@ -74,13 +72,11 @@ public class Mth {
}
public static int ceil(float value) {
- int i = (int)value;
- return value > (float)i ? i + 1 : i;
+ return (int) Math.ceil(value); // Gale - use platform math functions
}
public static int ceil(double value) {
- int i = (int)value;
- return value > (double)i ? i + 1 : i;
+ return (int) Math.ceil(value); // Gale - use platform math functions
}
public static int clamp(int value, int min, int max) {
@@ -112,15 +108,7 @@ public class Mth {
}
public static double absMax(double a, double b) {
- if (a < 0.0D) {
- a = -a;
- }
-
- if (b < 0.0D) {
- b = -b;
- }
-
- return Math.max(a, b);
+ return Math.max(Math.abs(a), Math.abs(b)); // Gale - use platform math functions
}
public static int floorDiv(int dividend, int divisor) {

View File

@@ -37,7 +37,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java b/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
index f4f3f3a19d3cadaef1ae1a47daa68251a983dcf2..04f8e7709c52654241ff5f9403e2d58e42b47550 100644
index 7f174bb89bf4d700a5ae1b65d8abd4f5b1e7b5ed..7420f51ad52d233330fc9313fc08611917876788 100644
--- a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
@@ -27,8 +27,15 @@ public class ShapelessRecipe implements CraftingRecipe {

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/world/entity/monster/EnderMan.java b/src/main/java/net/minecraft/world/entity/monster/EnderMan.java
index 39eb9301626b191958ce42daa34b1ff3241cea80..926098ec96fb1b6ea58403921ab49a21b7a51e76 100644
index b62457313a1e30aad0c5313d608667b5d3811455..52ae9e64b7d78b76214c6c4c8616a8d091bf3bc9 100644
--- a/src/main/java/net/minecraft/world/entity/monster/EnderMan.java
+++ b/src/main/java/net/minecraft/world/entity/monster/EnderMan.java
@@ -326,11 +326,17 @@ public class EnderMan extends Monster implements NeutralMob {

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java b/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java
index a7575b5ef56af6f53448d391abb4956e130148ca..58c3b8e622941108e46bb1b4e9eb9497d6553ab4 100644
index 58422f00c7d64dbd1cf6d7211c9838875cbe7778..57bd0b2d01c92f08cc41db59b38641a82653af0e 100644
--- a/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java
+++ b/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java
@@ -75,9 +75,18 @@ public class TargetingConditions {

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
index 6670e657e08e130f7e0368f418379fd1ece00cdf..1717844256fe6479e3d7125db3937354578d17d0 100644
index d934d07ad761319f338d4386536f68fde211c041..e215cb22a226fa61d7eb9a8f69fe2a644d4394f7 100644
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
@@ -181,6 +181,7 @@ public class ServerEntity {

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/world/level/NaturalSpawner.java b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
index eebc251467ddc141b2ae0408941740f6b01d11f2..e0faab041aa2cca41f56fc065d04ca572a36a329 100644
index 599efa2ed24e0d4944324c86ca23c38d8b111277..135c62a90b1cf924812fa4c8c224057eed67a109 100644
--- a/src/main/java/net/minecraft/world/level/NaturalSpawner.java
+++ b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
@@ -415,13 +415,14 @@ public final class NaturalSpawner {

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index 31963a0931bcd3465e46b9c875e89ac56aead62d..caa765c6264e14cc66973267f94074c7056fe122 100644
index 67d778ef115fc1e09fc8fa9c21d17613a11ca17f..eb12a3c0aefb3d6a42f08439db1ca51e3db65241 100644
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -1549,8 +1549,30 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 431bc32a61934f9bf4d268df1f70194c3e48ee93..93255c7388c1a2f281f508f8ca34571bf565accd 100644
index be9ac3b5b1107f588f6c00e7768a883691a8adcb..32ec0620e9d8c900df5ce78c1ff5f306bff969d2 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -507,17 +507,37 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {

View File

@@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 93255c7388c1a2f281f508f8ca34571bf565accd..c27af8fc81f09abbb99b2b9cd990477298b210ed 100644
index 32ec0620e9d8c900df5ce78c1ff5f306bff969d2..14ce63f8fbac2214f1653a5388043de0420d7223 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -433,6 +433,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {

View File

@@ -22,7 +22,7 @@ you to easily disable books, should you want to preemptively remove this
functionality before additional exploits are found.
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 8b0441f672f1821e31860d3318c09c309b276135..9645952c84412adf5b98c2c189e6a9dc8612c8c8 100644
index 1b66c30ad7b4db4a77aa4da66ce7ed6843bdb370..47c6c3b3d0ac90771b872a921dab06832c0c18c2 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -184,6 +184,8 @@ import net.minecraft.world.phys.Vec3;

View File

@@ -22,10 +22,10 @@ data is already available in the blockPosition struct, so we use that
instead of re-doing the casting.
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
index cb4379268b191d331c71be44642baac381ffaaf6..77aa75c9b1c31b47513a9d06b0930f9524a3d6d1 100644
index 517ea24ed254eaa9c13439b8b83e8391ea5e10ff..7b3713c8c0840190292a96b7e8b9be620c83274d 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -211,7 +211,7 @@ public final class MCUtil {
@@ -209,7 +209,7 @@ public final class MCUtil {
}
public static long getCoordinateKey(final Entity entity) {
@@ -35,7 +35,7 @@ index cb4379268b191d331c71be44642baac381ffaaf6..77aa75c9b1c31b47513a9d06b0930f95
public static long getCoordinateKey(final ChunkPos pair) {
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 24e43afed47993d25df37a1f0e7d90d77205cb0d..c6d41c3650d24ebfbb57b75a1d76a12aa6a22faf 100644
index 6754e0636e5b11f431717e9e77310ac00f8b33b4..1e43237008827538773d3299778d6778bee4049b 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -306,7 +306,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {

View File

@@ -13,7 +13,7 @@ As part of: JettPack (https://gitlab.com/Titaniumtown/JettPack)
Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index c6d41c3650d24ebfbb57b75a1d76a12aa6a22faf..bd82f2374dc991dea21b83ae44d7f5708dc4e357 100644
index 1e43237008827538773d3299778d6778bee4049b..ef85df48cfac8af3c15df0d686eea73c1346423c 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -305,7 +305,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {

View File

@@ -13,7 +13,7 @@ As part of: Slice (https://github.com/Cryptite/Slice)
Licensed under: MIT (https://opensource.org/licenses/MIT)
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 5690f551281a8d0cc3927cb4d860a403141b7239..4f654e5cc39ca3fccb0bff1c1f859a17e7d17229 100644
index 38e24fbc55d36d1e5529e3ff0315b2408cdb99a9..0b22993d2f6bdc6a096626ae76dac2433293fa14 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -890,7 +890,13 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {

Some files were not shown because too many files have changed in this diff Show More