From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Fri, 31 Jan 2025 21:50:46 +0300 Subject: [PATCH] Math Optimizations diff --git a/com/mojang/math/OctahedralGroup.java b/com/mojang/math/OctahedralGroup.java index 11902e7427761746ee098fea3276a34fef0096ba..3ba23fa243f7af712a41316066ca554f1c23b495 100644 --- a/com/mojang/math/OctahedralGroup.java +++ b/com/mojang/math/OctahedralGroup.java @@ -112,6 +112,7 @@ public enum OctahedralGroup implements StringRepresentable { this.transformation = new Matrix3f().scaling(invertX ? -1.0F : 1.0F, invertY ? -1.0F : 1.0F, invertZ ? -1.0F : 1.0F); this.transformation.mul(permutation.transformation()); this.initializeRotationDirections(); // Paper - Avoid Lazy Initialization for Enum Fields + this.rotate(Direction.UP); // DivineMC - Math Optimizations } private BooleanList packInversions() { diff --git a/com/mojang/math/Transformation.java b/com/mojang/math/Transformation.java index aa755b8b7f8bc5910322e0c5b520f603da06a85a..e781dea43279aa77cc40a7afd2281c32cc8347a9 100644 --- a/com/mojang/math/Transformation.java +++ b/com/mojang/math/Transformation.java @@ -51,6 +51,7 @@ public final class Transformation { } else { this.matrix = matrix; } + ensureDecomposed(); // DivineMC - Math Optimizations } public Transformation(@Nullable Vector3f translation, @Nullable Quaternionf leftRotation, @Nullable Vector3f scale, @Nullable Quaternionf rightRotation) { @@ -60,6 +61,7 @@ public final class Transformation { this.scale = scale != null ? scale : new Vector3f(1.0F, 1.0F, 1.0F); this.rightRotation = rightRotation != null ? rightRotation : new Quaternionf(); this.decomposed = true; + ensureDecomposed(); // DivineMC - Math Optimizations } public static Transformation identity() { diff --git a/net/minecraft/util/Mth.java b/net/minecraft/util/Mth.java index ae1ab070a93b46a0790eed3feda1d09f5fbe9b25..3befc00c10ce8f29d3ee1ea493c2b220df5eaaea 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); // DivineMC - Math Optimizations } public static int floor(double value) { - int i = (int)value; - return value < i ? i - 1 : i; + return (int) Math.floor(value); // DivineMC - Math Optimizations } public static long lfloor(double value) { - long l = (long)value; - return value < l ? l - 1L : l; + return (long) Math.floor(value); // DivineMC - Math Optimizations } 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); // DivineMC - Math Optimizations } public static int ceil(double value) { - int i = (int)value; - return value > i ? i + 1 : i; + return (int) Math.ceil(value); // DivineMC - Math Optimizations } 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)); // DivineMC - Math Optimizations } public static int floorDiv(int dividend, int divisor) { @@ -162,14 +149,26 @@ public class Mth { return Math.floorMod(x, y); } - public static float positiveModulo(float numerator, float denominator) { + public static float positiveModuloForAnyDenominator(float numerator, float denominator) { // DivineMC - Math Optimizations return (numerator % denominator + denominator) % denominator; } - public static double positiveModulo(double numerator, double denominator) { + public static double positiveModuloForAnyDenominator(double numerator, double denominator) { // DivineMC - Math Optimizations return (numerator % denominator + denominator) % denominator; } + // DivineMC start - Math Optimizations + public static float positiveModuloForPositiveIntegerDenominator(float numerator, float denominator) { + var modulo = numerator % denominator; + return modulo < 0 ? modulo + denominator : modulo; + } + + public static double positiveModuloForPositiveIntegerDenominator(double numerator, double denominator) { + var modulo = numerator % denominator; + return modulo < 0 ? modulo + denominator : modulo; + } + // DivineMC end - Math Optimizations + public static boolean isMultipleOf(int number, int multiple) { return number % multiple == 0; } diff --git a/net/minecraft/world/level/biome/BiomeManager.java b/net/minecraft/world/level/biome/BiomeManager.java index 73962e79a0f3d892e3155443a1b84508b0f4042e..db400d7b25e454b4a1ac8d09a590c3c7d2504052 100644 --- a/net/minecraft/world/level/biome/BiomeManager.java +++ b/net/minecraft/world/level/biome/BiomeManager.java @@ -14,6 +14,7 @@ public class BiomeManager { private static final int ZOOM_MASK = 3; private final BiomeManager.NoiseBiomeSource noiseBiomeSource; private final long biomeZoomSeed; + private static final double maxOffset = 0.4500000001D; // DivineMC - Math Optimizations public BiomeManager(BiomeManager.NoiseBiomeSource noiseBiomeSource, long biomeZoomSeed) { this.noiseBiomeSource = noiseBiomeSource; @@ -29,39 +30,65 @@ public class BiomeManager { } public Holder getBiome(BlockPos pos) { - int i = pos.getX() - 2; - int i1 = pos.getY() - 2; - int i2 = pos.getZ() - 2; - int i3 = i >> 2; - int i4 = i1 >> 2; - int i5 = i2 >> 2; - double d = (i & 3) / 4.0; - double d1 = (i1 & 3) / 4.0; - double d2 = (i2 & 3) / 4.0; - int i6 = 0; - double d3 = Double.POSITIVE_INFINITY; + // DivineMC start - Math Optimizations + int xMinus2 = pos.getX() - 2; + int yMinus2 = pos.getY() - 2; + int zMinus2 = pos.getZ() - 2; + int x = xMinus2 >> 2; + int y = yMinus2 >> 2; + int z = zMinus2 >> 2; + double quartX = (double) (xMinus2 & 3) / 4.0; + double quartY = (double) (yMinus2 & 3) / 4.0; + double quartZ = (double) (zMinus2 & 3) / 4.0; + int smallestX = 0; + double smallestDist = Double.POSITIVE_INFINITY; + for (int biomeX = 0; biomeX < 8; ++biomeX) { + boolean everyOtherQuad = (biomeX & 4) == 0; + boolean everyOtherPair = (biomeX & 2) == 0; + boolean everyOther = (biomeX & 1) == 0; + double quartXX = everyOtherQuad ? quartX : quartX - 1.0; + double quartYY = everyOtherPair ? quartY : quartY - 1.0; + double quartZZ = everyOther ? quartZ : quartZ - 1.0; - for (int i7 = 0; i7 < 8; i7++) { - boolean flag = (i7 & 4) == 0; - boolean flag1 = (i7 & 2) == 0; - boolean flag2 = (i7 & 1) == 0; - int i8 = flag ? i3 : i3 + 1; - int i9 = flag1 ? i4 : i4 + 1; - int i10 = flag2 ? i5 : i5 + 1; - double d4 = flag ? d : d - 1.0; - double d5 = flag1 ? d1 : d1 - 1.0; - double d6 = flag2 ? d2 : d2 - 1.0; - double fiddledDistance = getFiddledDistance(this.biomeZoomSeed, i8, i9, i10, d4, d5, d6); - if (d3 > fiddledDistance) { - i6 = i7; - d3 = fiddledDistance; + double maxQuartYY = 0.0, maxQuartZZ = 0.0; + if (biomeX != 0) { + maxQuartYY = Mth.square(Math.max(quartYY + maxOffset, Math.abs(quartYY - maxOffset))); + maxQuartZZ = Mth.square(Math.max(quartZZ + maxOffset, Math.abs(quartZZ - maxOffset))); + double maxQuartXX = Mth.square(Math.max(quartXX + maxOffset, Math.abs(quartXX - maxOffset))); + if (smallestDist < maxQuartXX + maxQuartYY + maxQuartZZ) continue; } - } + int xx = everyOtherQuad ? x : x + 1; + int yy = everyOtherPair ? y : y + 1; + int zz = everyOther ? z : z + 1; + + long seed = LinearCongruentialGenerator.next(this.biomeZoomSeed, xx); + seed = LinearCongruentialGenerator.next(seed, yy); + seed = LinearCongruentialGenerator.next(seed, zz); + seed = LinearCongruentialGenerator.next(seed, xx); + seed = LinearCongruentialGenerator.next(seed, yy); + seed = LinearCongruentialGenerator.next(seed, zz); + double offsetX = getFiddle(seed); + double sqrX = Mth.square(quartXX + offsetX); + if (biomeX != 0 && smallestDist < sqrX + maxQuartYY + maxQuartZZ) continue; + seed = LinearCongruentialGenerator.next(seed, this.biomeZoomSeed); + double offsetY = getFiddle(seed); + double sqrY = Mth.square(quartYY + offsetY); + if (biomeX != 0 && smallestDist < sqrX + sqrY + maxQuartZZ) continue; + seed = LinearCongruentialGenerator.next(seed, this.biomeZoomSeed); + double offsetZ = getFiddle(seed); + double biomeDist = sqrX + sqrY + Mth.square(quartZZ + offsetZ); - int i7x = (i6 & 4) == 0 ? i3 : i3 + 1; - int i11 = (i6 & 2) == 0 ? i4 : i4 + 1; - int i12 = (i6 & 1) == 0 ? i5 : i5 + 1; - return this.noiseBiomeSource.getNoiseBiome(i7x, i11, i12); + if (smallestDist > biomeDist) { + smallestX = biomeX; + smallestDist = biomeDist; + } + } + return this.noiseBiomeSource.getNoiseBiome( + (smallestX & 4) == 0 ? x : x + 1, + (smallestX & 2) == 0 ? y : y + 1, + (smallestX & 1) == 0 ? z : z + 1 + ); + // DivineMC end - Math Optimizations } public Holder getNoiseBiomeAtPosition(double x, double y, double z) { diff --git a/net/minecraft/world/level/levelgen/blending/Blender.java b/net/minecraft/world/level/levelgen/blending/Blender.java index 01e5b29d6e9a5c53c0e23b61ed0c1d7be1a0fe08..d80df05e40f3941ade5ed320e12f8dcf47e6b247 100644 --- a/net/minecraft/world/level/levelgen/blending/Blender.java +++ b/net/minecraft/world/level/levelgen/blending/Blender.java @@ -144,7 +144,7 @@ public class Blender { private static double heightToOffset(double height) { double d = 1.0; double d1 = height + 0.5; - double d2 = Mth.positiveModulo(d1, 8.0); + double d2 = Mth.positiveModuloForPositiveIntegerDenominator(d1, 8.0); // DivineMC - Math optimizations return 1.0 * (32.0 * (d1 - 128.0) - 3.0 * (d1 - 120.0) * d2 + 3.0 * d2 * d2) / (128.0 * (32.0 - 3.0 * d2)); } diff --git a/net/minecraft/world/phys/AABB.java b/net/minecraft/world/phys/AABB.java index c9c6e4e460ad8435f12761704bb9b0284d6aa708..54807bb4b4189ceaded1f78a1a9ab85ce40ab2b1 100644 --- a/net/minecraft/world/phys/AABB.java +++ b/net/minecraft/world/phys/AABB.java @@ -189,13 +189,15 @@ public class AABB { } public AABB intersect(AABB other) { - double max = Math.max(this.minX, other.minX); - double max1 = Math.max(this.minY, other.minY); - double max2 = Math.max(this.minZ, other.minZ); - double min = Math.min(this.maxX, other.maxX); - double min1 = Math.min(this.maxY, other.maxY); - double min2 = Math.min(this.maxZ, other.maxZ); - return new AABB(max, max1, max2, min, min1, min2); + // DivineMC start - Math Optimizations + return new AABB( + this.minX > other.minX ? this.minX : other.minX, + this.minY > other.minY ? this.minY : other.minY, + this.minZ > other.minZ ? this.minZ : other.minZ, + this.maxX < other.maxX ? this.maxX : other.maxX, + this.maxY < other.maxY ? this.maxY : other.maxY, + this.maxZ < other.maxZ ? this.maxZ : other.maxZ + ); } public AABB minmax(AABB other) { @@ -227,16 +229,37 @@ public class AABB { } public boolean intersects(AABB other) { - return this.intersects(other.minX, other.minY, other.minZ, other.maxX, other.maxY, other.maxZ); + // DivineMC start - Math Optimizations + return this.minX < other.maxX && + this.maxX > other.minX && + this.minY < other.maxY && + this.maxY > other.minY && + this.minZ < other.maxZ && + this.maxZ > other.minZ; + // DivineMC end - Math Optimizations } public boolean intersects(double x1, double y1, double z1, double x2, double y2, double z2) { - return this.minX < x2 && this.maxX > x1 && this.minY < y2 && this.maxY > y1 && this.minZ < z2 && this.maxZ > z1; + // DivineMC start - Math Optimizations + return this.minX < x2 && + this.maxX > x1 && + this.minY < y2 && + this.maxY > y1 && + this.minZ < z2 && + this.maxZ > z1; + // DivineMC end - Math Optimizations } public boolean intersects(Vec3 min, Vec3 max) { return this.intersects( - Math.min(min.x, max.x), Math.min(min.y, max.y), Math.min(min.z, max.z), Math.max(min.x, max.x), Math.max(min.y, max.y), Math.max(min.z, max.z) + // DivineMC start - Math Optimizations + min.x < max.x ? min.x : max.x, + min.y < max.y ? min.y : max.y, + min.z < max.z ? min.z : max.z, + min.x > max.x ? min.x : max.x, + min.y > max.y ? min.y : max.y, + min.z > max.z ? min.z : max.z + // DivineMC end - Math Optimizations ); }