Skip to content

Commit

Permalink
tierByVoltage optimizations (GregTechCEu#2617)
Browse files Browse the repository at this point in the history
Co-authored-by: M-W-K <[email protected]>
Co-authored-by: kross <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2024
1 parent 8a062da commit c3a02b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/main/java/gregtech/api/GTValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class GTValues {
* The Voltage Tiers. Use this Array instead of the old named Voltage Variables
*/
public static final long[] V = { 8, 32, 128, 512, 2048, 8192, 32768, 131072, 524288, 2097152, 8388608, 33554432,
134217728, 536870912, Integer.MAX_VALUE };
134217728, 536870912, 2147483648L };

/**
* The Voltage Tiers divided by 2.
Expand All @@ -78,10 +78,10 @@ public class GTValues {
* The Voltage Tiers extended all the way to max Long value for overclocking
*/
public static final long[] VOC = { 8, 32, 128, 512, 2048, 8192, 32768, 131072, 524288, 2097152, 8388608, 33554432,
134217728, 536870912, Integer.MAX_VALUE, 8589934592L, 34359738368L, 137438953472L, 549755813888L,
2199023255552L,
8796093022208L, 35184372088832L, 140737488355328L, 562949953421312L, 2251799813685248L, 9007199254740992L,
36028797018963968L, 144115188075855872L, 576460752303423488L, 2305843009213693952L, Long.MAX_VALUE };
134217728, 536870912, 2147483648L, 8589934592L, 34359738368L, 137438953472L, 549755813888L,
2199023255552L, 8796093022208L, 35184372088832L, 140737488355328L, 562949953421312L, 2251799813685248L,
9007199254740992L, 36028797018963968L, 144115188075855872L, 576460752303423488L, 2305843009213693952L,
Long.MAX_VALUE };

public static final int ULV = 0;
public static final int LV = 1;
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@
import java.util.function.Function;
import java.util.function.Predicate;

import static gregtech.api.GTValues.V;
import static gregtech.api.GTValues.VOC;

public class GTUtility {

public static <T> String[] mapToString(T[] array, Function<T, String> mapper) {
Expand Down Expand Up @@ -257,7 +254,10 @@ public static int nearestLesser(@NotNull long[] array, long value) {
* tier that can handle it, {@code MAX} is returned.
*/
public static byte getTierByVoltage(long voltage) {
return (byte) Math.min(GTValues.MAX, nearestLesser(V, voltage) + 1);
if (voltage >= Integer.MAX_VALUE) {
return GTValues.MAX;
}
return getOCTierByVoltage(voltage);
}

/**
Expand All @@ -266,7 +266,10 @@ public static byte getTierByVoltage(long voltage) {
* tier that can handle it, {@code MAX_TRUE} is returned.
*/
public static byte getOCTierByVoltage(long voltage) {
return (byte) Math.min(GTValues.MAX_TRUE, nearestLesser(VOC, voltage) + 1);
if (voltage <= GTValues.V[GTValues.ULV]) {
return GTValues.ULV;
}
return (byte) ((62 - Long.numberOfLeadingZeros(voltage - 1)) >> 1);
}

/**
Expand All @@ -276,7 +279,14 @@ public static byte getOCTierByVoltage(long voltage) {
* {@code ULV} if there's no tier below
*/
public static byte getFloorTierByVoltage(long voltage) {
return (byte) Math.max(GTValues.ULV, nearestLesserOrEqual(VOC, voltage));
if (voltage < GTValues.V[GTValues.LV]) {
return GTValues.ULV;
}
if (voltage == GTValues.VOC[GTValues.MAX_TRUE]) {
return GTValues.MAX_TRUE;
}

return (byte) ((60 - Long.numberOfLeadingZeros(voltage)) >> 1);
}

@SuppressWarnings("deprecation")
Expand Down

0 comments on commit c3a02b5

Please sign in to comment.