package gregtech.api.util;

// Minimal stand-in for gregtech.api.util.GTUtility: only the helpers OverclockCalculator and
// ParallelHelper use, copied from GT5-Unofficial 5.09.54.265 GTUtility.java (line numbers in comments).
// run.sh renames this file to GTUtility.java in the scratch build dir.
public class GTUtility {

    public static final long[] V = new long[] { 8L, 32L, 128L, 512L, 2048L, 8192L, 32_768L, 131_072L, 524_288L,
        2_097_152L, 8_388_608L, 33_554_432L, 134_217_728L, 536_870_912L, Integer.MAX_VALUE - 7, 8_589_934_592L };

    // :230-237
    public static int safeInt(long number, int margin) {
        return number > Integer.MAX_VALUE - margin ? Integer.MAX_VALUE - margin : (int) number;
    }

    // :443-453
    public static byte getTier(long l) {
        if (l > V[14]) return 15;
        if (l <= V[0]) return 0;
        int log2L = 64 - Long.numberOfLeadingZeros(l - 1);
        return (byte) ((log2L - 2) / 2);
    }

    // :3418-3432
    public static long clamp(long val, long lo, long hi) {
        return Math.min(hi, Math.max(val, lo));
    }

    public static int clamp(int val, int lo, int hi) {
        return Math.min(hi, Math.max(val, lo));
    }

    public static double clamp(double val, double lo, double hi) {
        return Math.min(hi, Math.max(val, lo));
    }

    // :3570-3618
    public static double powInt(double base, int exp) {
        if (exp > 0) return powBySquaring(base, exp);
        if (exp < 0) return 1.0 / powBySquaring(base, -exp);
        return 1.0;
    }

    private static double powBySquaring(double base, int exp) {
        if (base == 2) return exp > 1023 ? Double.POSITIVE_INFINITY : Double.longBitsToDouble(exp + 1023L << 52);
        if (base == 4) return exp > 511 ? Double.POSITIVE_INFINITY : Double.longBitsToDouble(exp * 2L + 1023L << 52);
        double result = 1.0;
        while (exp > 0) {
            if ((exp & 1) == 1) result *= base;
            base *= base;
            exp >>= 1;
        }
        return result;
    }

    public static double powInt(double base, long exp) {
        if (exp > 0) return powBySquaring(base, exp);
        if (exp < 0) return 1.0 / powBySquaring(base, -exp);
        return 1.0;
    }

    private static double powBySquaring(double base, long exp) {
        if (base == 2) return exp > 1023 ? Double.POSITIVE_INFINITY : Double.longBitsToDouble(exp + 1023L << 52);
        if (base == 4) return exp > 511 ? Double.POSITIVE_INFINITY : Double.longBitsToDouble(exp * 2L + 1023L << 52);
        double result = 1.0;
        while (exp > 0) {
            if ((exp & 1) == 1) result *= base;
            base *= base;
            exp >>= 1;
        }
        return result;
    }

    // :3623-3682
    public static int log2(int a) {
        if (a <= 1) return 0;
        return 31 - Integer.numberOfLeadingZeros(a);
    }

    public static int log4(int a) {
        if (a <= 1) return 0;
        return 31 - Integer.numberOfLeadingZeros(a) >> 1;
    }

    public static int log4ceil(int a) {
        if (a <= 1) return 0;
        return 33 - Integer.numberOfLeadingZeros(a - 1) >> 1;
    }

    public static long log2(long a) {
        if (a <= 1) return 0;
        return 63 - Long.numberOfLeadingZeros(a);
    }

    public static long log2ceil(long a) {
        if (a <= 1) return 0;
        return 64 - Long.numberOfLeadingZeros(a - 1);
    }

    public static long log4(long a) {
        if (a <= 1) return 0;
        return 63 - Long.numberOfLeadingZeros(a) >> 1;
    }

    public static long log4ceil(long a) {
        if (a <= 1) return 0;
        return 65 - Long.numberOfLeadingZeros(a - 1) >> 1;
    }
}
