mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
160 lines
5.4 KiB
Diff
160 lines
5.4 KiB
Diff
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/net/minecraft/util/Mth.java b/net/minecraft/util/Mth.java
|
|
index ab3a221c115992d0f4ea921aa92cf0976b815ff4..75da3011058918e1da6936522f19a2ccdb843d73 100644
|
|
--- a/net/minecraft/util/Mth.java
|
|
+++ b/net/minecraft/util/Mth.java
|
|
@@ -58,18 +58,15 @@ public class Mth {
|
|
}
|
|
|
|
public static int floor(float value) {
|
|
- int i = (int)value;
|
|
- return value < 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 < i ? i - 1 : i;
|
|
+ return (int) Math.floor(value); // Gale - use platform math functions
|
|
}
|
|
|
|
public static long lfloor(double value) {
|
|
- long l = (long)value;
|
|
- return value < l ? l - 1L : l;
|
|
+ return (long) Math.floor(value); // Gale - use platform math functions
|
|
}
|
|
|
|
public static float abs(float value) {
|
|
@@ -81,13 +78,11 @@ public class Mth {
|
|
}
|
|
|
|
public static int ceil(float value) {
|
|
- int i = (int)value;
|
|
- return value > 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 > i ? i + 1 : i;
|
|
+ return (int) Math.ceil(value); // Gale - use platform math functions
|
|
}
|
|
|
|
public static int clamp(int value, int min, int max) {
|
|
@@ -123,15 +118,7 @@ public class Mth {
|
|
}
|
|
|
|
public static double absMax(double x, double y) {
|
|
- if (x < 0.0) {
|
|
- x = -x;
|
|
- }
|
|
-
|
|
- if (y < 0.0) {
|
|
- y = -y;
|
|
- }
|
|
-
|
|
- return Math.max(x, y);
|
|
+ return Math.max(Math.abs(x), Math.abs(y)); // Gale - use platform math functions
|
|
}
|
|
|
|
public static int floorDiv(int dividend, int divisor) {
|