From 21a087fba239ca87e01eaa8df62e00cc26a201c2 Mon Sep 17 00:00:00 2001 From: kstvr32 <109012629+kstvr32@users.noreply.github.com> Date: Sat, 14 Sep 2024 08:31:55 -0400 Subject: Optimize GTUtility.getTier call (#3165) Co-authored-by: kstvr32 Co-authored-by: Martin Robertz --- src/main/java/gregtech/api/util/GTUtility.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/main/java') 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) { -- cgit