aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorkstvr32 <109012629+kstvr32@users.noreply.github.com>2024-09-14 08:31:55 -0400
committerGitHub <noreply@github.com>2024-09-14 14:31:55 +0200
commit21a087fba239ca87e01eaa8df62e00cc26a201c2 (patch)
treee1644d1e1882be8b8683475c74d79ec95645ed41 /src/main
parentdce066c6e3dc5fc07d575aac82160f366e3e1df3 (diff)
downloadGT5-Unofficial-21a087fba239ca87e01eaa8df62e00cc26a201c2.tar.gz
GT5-Unofficial-21a087fba239ca87e01eaa8df62e00cc26a201c2.tar.bz2
GT5-Unofficial-21a087fba239ca87e01eaa8df62e00cc26a201c2.zip
Optimize GTUtility.getTier call (#3165)
Co-authored-by: kstvr32 <kstvr32@gmail.com> Co-authored-by: Martin Robertz <dream-master@gmx.net>
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/gregtech/api/util/GTUtility.java12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/main/java/gregtech/api/util/GTUtility.java b/src/main/java/gregtech/api/util/GTUtility.java
index 8eeb71753e..ede680ba62 100644
--- a/src/main/java/gregtech/api/util/GTUtility.java
+++ b/src/main/java/gregtech/api/util/GTUtility.java
@@ -500,9 +500,15 @@ public class GTUtility {
}
public static byte getTier(long l) {
- byte i = -1;
- while (++i < V.length) if (l <= V[i]) return i;
- return (byte) (V.length - 1);
+ if (l > V[14]) return 15;
+ if (l <= V[0]) return 0;
+
+ // numberOfLeadingZeros is implemented in hardware by x86 LZCNT
+ // and is extremely efficient (takes only a couple of hardware cycles)
+ // (64 - numberOfLeadingZeros(l - 1)) = ceil(log_2(l))
+ int log2L = 64 - Long.numberOfLeadingZeros(l - 1);
+
+ return (byte) ((log2L - 2) / 2);
}
public static long getAmperageForTier(long voltage, byte tier) {