Better Math

This commit is contained in:
kirillsaint 2023-08-21 15:29:49 +06:00
parent 88e28374ef
commit c9abb0273e
9 changed files with 212 additions and 1 deletions

View File

@ -0,0 +1,12 @@
package net.silentclient.client.math;
public class JavaMath {
public static float sin(float radians) {
return (float) Math.sin((double) radians);
}
public static float cos(float radians) {
return (float) Math.cos((double) radians);
}
}

View File

@ -0,0 +1,29 @@
package net.silentclient.client.math;
public class LibGDXMath {
public static final float BF_PI = 3.1415927F;
public static final float BF_degreesToRadians = 0.017453292F;
private static final float[] BF_table = new float[16384];
public static float sin(float radians) {
return LibGDXMath.BF_table[(int) (radians * 2607.5945F) & 16383];
}
public static float cos(float radians) {
return LibGDXMath.BF_table[(int) ((radians + 1.5707964F) * 2607.5945F) & 16383];
}
static {
int i;
for (i = 0; i < 16384; ++i) {
LibGDXMath.BF_table[i] = (float) Math.sin((double) (((float) i + 0.5F) / 16384.0F * 6.2831855F));
}
for (i = 0; i < 360; i += 90) {
LibGDXMath.BF_table[(int) ((float) i * 45.511112F) & 16383] = (float) Math.sin((double) ((float) i * 0.017453292F));
}
}
}

View File

@ -0,0 +1,12 @@
package net.silentclient.client.math;
public class RandomMath {
public static float sin(float radians) {
return (float) Math.random() * radians;
}
public static float cos(float radians) {
return (float) Math.random() * radians;
}
}

View File

@ -0,0 +1,27 @@
package net.silentclient.client.math;
public class RivensFullMath {
private static final float BF_SIN_TO_COS = 1.5707964F;
private static final int BF_SIN_BITS = 12;
private static final int BF_SIN_MASK = ~(-1 << RivensFullMath.BF_SIN_BITS);
private static final int BF_SIN_COUNT = RivensFullMath.BF_SIN_MASK + 1;
private static final float BF_radFull = 6.2831855F;
private static final float BF_radToIndex = (float) RivensFullMath.BF_SIN_COUNT / RivensFullMath.BF_radFull;
private static final float[] BF_sinFull = new float[RivensFullMath.BF_SIN_COUNT];
public static float sin(float rad) {
return RivensFullMath.BF_sinFull[(int) (rad * RivensFullMath.BF_radToIndex) & RivensFullMath.BF_SIN_MASK];
}
public static float cos(float rad) {
return sin(rad + RivensFullMath.BF_SIN_TO_COS);
}
static {
for (int i = 0; i < RivensFullMath.BF_SIN_COUNT; ++i) {
RivensFullMath.BF_sinFull[i] = (float) Math.sin(((double) i + (double) Math.min(1, i % (RivensFullMath.BF_SIN_COUNT / 4)) * 0.5D) / (double) RivensFullMath.BF_SIN_COUNT * (double) RivensFullMath.BF_radFull);
}
}
}

View File

@ -0,0 +1,33 @@
package net.silentclient.client.math;
public class RivensHalfMath {
private static final float BF_SIN_TO_COS = 1.5707964F;
private static final int BF_SIN_BITS = 12;
private static final int BF_SIN_MASK = ~(-1 << RivensHalfMath.BF_SIN_BITS);
private static final int BF_SIN_MASK2 = RivensHalfMath.BF_SIN_MASK >> 1;
private static final int BF_SIN_COUNT = RivensHalfMath.BF_SIN_MASK + 1;
private static final int BF_SIN_COUNT2 = RivensHalfMath.BF_SIN_MASK2 + 1;
private static final float BF_radFull = 6.2831855F;
private static final float BF_radToIndex = (float) RivensHalfMath.BF_SIN_COUNT / RivensHalfMath.BF_radFull;
private static final float[] BF_sinHalf = new float[RivensHalfMath.BF_SIN_COUNT2];
public static float sin(float rad) {
int index1 = (int) (rad * RivensHalfMath.BF_radToIndex) & RivensHalfMath.BF_SIN_MASK;
int index2 = index1 & RivensHalfMath.BF_SIN_MASK2;
int mul = index1 == index2 ? 1 : -1;
return RivensHalfMath.BF_sinHalf[index2] * (float) mul;
}
public static float cos(float rad) {
return sin(rad + RivensHalfMath.BF_SIN_TO_COS);
}
static {
for (int i = 0; i < RivensHalfMath.BF_SIN_COUNT2; ++i) {
RivensHalfMath.BF_sinHalf[i] = (float) Math.sin(((double) i + (double) Math.min(1, i % (RivensHalfMath.BF_SIN_COUNT / 4)) * 0.5D) / (double) RivensHalfMath.BF_SIN_COUNT * (double) RivensHalfMath.BF_radFull);
}
}
}

View File

@ -0,0 +1,37 @@
package net.silentclient.client.math;
public class RivensMath {
private static final int BF_SIN_BITS = 12;
private static final int BF_SIN_MASK = ~(-1 << RivensMath.BF_SIN_BITS);
private static final int BF_SIN_COUNT = RivensMath.BF_SIN_MASK + 1;
private static final float BF_radFull = 6.2831855F;
private static final float BF_radToIndex = (float) RivensMath.BF_SIN_COUNT / RivensMath.BF_radFull;
private static final float BF_degFull = 360.0F;
private static final float BF_degToIndex = (float) RivensMath.BF_SIN_COUNT / RivensMath.BF_degFull;
private static final float[] BF_sin = new float[RivensMath.BF_SIN_COUNT];
private static final float[] BF_cos = new float[RivensMath.BF_SIN_COUNT];
public static float sin(float rad) {
return RivensMath.BF_sin[(int) (rad * RivensMath.BF_radToIndex) & RivensMath.BF_SIN_MASK];
}
public static float cos(float rad) {
return RivensMath.BF_cos[(int) (rad * RivensMath.BF_radToIndex) & RivensMath.BF_SIN_MASK];
}
static {
int i;
for (i = 0; i < RivensMath.BF_SIN_COUNT; ++i) {
RivensMath.BF_sin[i] = (float) Math.sin((double) (((float) i + 0.5F) / (float) RivensMath.BF_SIN_COUNT * RivensMath.BF_radFull));
RivensMath.BF_cos[i] = (float) Math.cos((double) (((float) i + 0.5F) / (float) RivensMath.BF_SIN_COUNT * RivensMath.BF_radFull));
}
for (i = 0; i < 360; i += 90) {
RivensMath.BF_sin[(int) ((float) i * RivensMath.BF_degToIndex) & RivensMath.BF_SIN_MASK] = (float) Math.sin((double) i * 3.141592653589793D / 180.0D);
RivensMath.BF_cos[(int) ((float) i * RivensMath.BF_degToIndex) & RivensMath.BF_SIN_MASK] = (float) Math.cos((double) i * 3.141592653589793D / 180.0D);
}
}
}

View File

@ -0,0 +1,33 @@
package net.silentclient.client.math;
public class TaylorMath {
private static final float BF_SIN_TO_COS = 1.5707964F;
public static float sin(float rad) {
double x = (double) rad;
double x2 = x * x;
double x3 = x2 * x;
double x5 = x2 * x3;
double x7 = x2 * x5;
double x9 = x2 * x7;
double x11 = x2 * x9;
double x13 = x2 * x11;
double x15 = x2 * x13;
double x17 = x2 * x15;
double val = x - x3 * 0.16666666666666666D;
val += x5 * 0.008333333333333333D;
val -= x7 * 1.984126984126984E-4D;
val += x9 * 2.7557319223985893E-6D;
val -= x11 * 2.505210838544172E-8D;
val += x13 * 1.6059043836821613E-10D;
val -= x15 * 7.647163731819816E-13D;
val += x17 * 2.8114572543455206E-15D;
return (float) val;
}
public static float cos(float rad) {
return sin(rad + TaylorMath.BF_SIN_TO_COS);
}
}

View File

@ -0,0 +1,27 @@
package net.silentclient.client.mixin.mixins;
import net.minecraft.util.MathHelper;
import net.silentclient.client.math.RivensHalfMath;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
@Mixin(MathHelper.class)
public class MathHelperMixin {
/**
* @author kirillsaint
* @reason custom math (faster)
*/
@Overwrite
public static float sin(float param) {
return RivensHalfMath.sin(param);
}
/**
* @author kirillsaint
* @reason custom math (faster)
*/
@Overwrite
public static float cos(float param) {
return RivensHalfMath.cos(param);
}
}

View File

@ -114,6 +114,7 @@
"accessors.optifine.CustomColorsAccessor",
"accessors.optifine.ConfigAccessor",
"mixins.NetworkPlayerInfoMixin",
"accessors.NetworkPlayerInfoAccessor"
"accessors.NetworkPlayerInfoAccessor",
"mixins.MathHelperMixin"
]
}