// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class SqrtSpeed {
    private final static int N = 10000000;
    private final static double MAX = 1000.0;
    private final static double SCALE = MAX/N;
    public final static double PREC = 0.000001;
    // newtons method
    public static double newton(double x) {
        double lower = x * (1 - PREC);
        double upper = x * (1 + PREC);
        double res = 0.05*x;
        do {
            res = 0.5*(res + x/res);
        } while(res*res < lower || res*res > upper);
        return res;
    }
    // table lookup (only valid for values 0.0 - 1000.0)
    private final static int TABSIZ = 1000000;
    private final static double DOWN = MAX/TABSIZ;
    private final static double UP = TABSIZ/MAX;
    private static double[] tab;
    static {
        tab = new double[TABSIZ];
        for(int i = 0; i < TABSIZ; i++) {
            tab[i] = Math.sqrt(i * DOWN);
        }
    }
    public static double lookup(double x) {
        return tab[(int)(x * UP)];
    }
    // taylor series - scaled
    public static double taylor_scaled_help(double x) {
        double lower = x * (1 - PREC);
        double upper = x * (1 + PREC);
        double res = 0;
        double c = 1;
        double f = 1;
        double xa = 1;
        int i = 0;
        do {
            res += c / f * xa;
            c *= (0.5 - i);
            f *= (i + 1);
            xa *= (x - 1);
            i++;
        } while(res*res < lower || res*res > upper);
        return res;
    }
    public static double taylor_scaled(double x) {
        if(x < 2) {
            return taylor_scaled_help(x);
        } else {
            return 2*taylor_scaled(0.25*x);
        }
    }
    // taylor series - scaled precalc (only valid for values 0.0 - 1000.0)
    private final static int MAXCOEF = 1000;
    private static double[] cf;
    static {
        cf = new double[MAXCOEF];
        double c = 1;
        double f = 1;
        for(int i = 0; i < MAXCOEF; i++) {
            cf[i] = c / f;
            c *= (0.5 - i);
            f *= (i + 1);
        }
    }
    private static double taylor_scaled_precalc_help(double x) {
        double lower = x * (1 - PREC);
        double upper = x * (1 + PREC);
        double res = 0;
        double xa = 1;
        int i = 0;
        do {
            res += cf[i] * xa;
            xa *= (x - 1);
            i++;
        } while(res*res < lower || res*res > upper);
        return res;
    }
    public static double taylor_scaled_precalc(double x) {
        if(x < 2) {
            return taylor_scaled_precalc_help(x);
        } else {
            return 2*taylor_scaled_precalc(0.25*x);
        }
    }
    // taylor series - multi point precalc (only valid for values 0.0 - 1000.0)
    private final static int MAXPOINT2 = 35;
    private final static int MAXCOEF2 = 1000;
    private static double[][] cf2;
    static {
        cf2 = new double[MAXPOINT2][MAXCOEF2];
        for(int j = 1; j <= MAXPOINT2; j++) {
            double c = j;
            double f = 1;
            for(int i = 0; i < MAXCOEF2; i++) {
                cf2[j-1][i] = c / f;
                c *= (0.5 - i)/(j*j);
                f *= (i + 1);
            }
        }
    }
    public static double taylor_multipoint_precalc(double x) {
        int a = 1;
        while(a*a < x) a++;
        double lower = x * (1 - PREC);
        double upper = x * (1 + PREC);
        double res = 0;
        double xa = 1;
        int i = 0;
        do {
            res += cf2[a-1][i] * xa;
            xa *= (x - a*a);
            i++;
        } while(res*res < lower || res*res > upper);
        return res;
    }
    // newtons method on inverse
    public static double newton_inverse(double x) {
        if(x < PREC) return 0;
        double lower = 1 - PREC;
        double upper = 1 + PREC;
        double res = 0.025;
        do {
            res = 0.5*res*(3 - x*res*res);
        } while(res*res*x < lower || res*res*x > upper);
        return res*x;
    }
    // estimate based on exponent bits
    private static double[] bitguess;
    private static double[] invbitguess;
    static {
        bitguess = new double[2048];
        invbitguess = new double[2048];
        for(int i = 0; i < 1024; i++) {
            bitguess[1023 + i] = 1.5*Math.pow(2, i/2.0);
            invbitguess[1023 + i] = 1/bitguess[1023 + i];
        }
        for(int i = 0; i < 1024; i++) {
            bitguess[1023 - i] = 1.5*Math.pow(2, -i/2.0);
            invbitguess[1023 - i] = 1/bitguess[1023 + i];
        }
    }
    private static double guess(double x) {
        return bitguess[(int)((Double.doubleToLongBits(x) & 0x7ff0000000000000L) >> 52)];
    }
    private static double invguess(double x) {
        return invbitguess[(int)((Double.doubleToLongBits(x) & 0x7ff0000000000000L) >> 52)];
    }
    // newtons method using bit guess
    public static double newton_bitguess(double x) {
        if(x < PREC) return 0;
        double lower = x * (1 - PREC);
        double upper = x * (1 + PREC);
        double res = guess(x);
        do {
            res = 0.5*(res + x/res);
        } while(res*res < lower || res*res > upper);
        return res;
    }
    // newtons method on inverse using bit guess
    public static double newton_inverse_bitguess(double x) {
        if(x < PREC) return 0;
        if(x<2) return 0.1*newton_inverse_bitguess(100*x);
        double lower = 1 - PREC;
        double upper = 1 + PREC;
        double res = invguess(x);
        do {
            res = 0.5*res*(3 - x*res*res);
        } while(res*res*x < lower || res*res*x > upper);
        return res*x;
    }
    // newtons method using bit guess and fixed iterations
    public static double newton_bitguess_fixed(double x) {
        if(x < PREC) return 0;
        double res = guess(x);
        res = 0.5*(res + x/res);
        res = 0.5*(res + x/res);
        res = 0.5*(res + x/res);
        res = 0.5*(res + x/res);
        return res;
    }
    // newtons method on inverse using bit guess and fixed iterations
    public static double newton_inverse_bitguess_fixed(double x) {
        if(x < PREC) return 0;
        if(x<2) return 0.1*newton_inverse_bitguess_fixed(100*x);
        double res = invguess(x);
        res = 0.5*res*(3 - x*res*res);
        res = 0.5*res*(3 - x*res*res);
        res = 0.5*res*(3 - x*res*res);
        res = 0.5*res*(3 - x*res*res);
        res = 0.5*res*(3 - x*res*res);
        return res*x;
    }
    // correctness test
    private static void test(double x) {
        System.out.println(Math.sqrt(x));
        System.out.println(newton(x));
        System.out.println(lookup(x));
        System.out.println(taylor_scaled(x));
        System.out.println(taylor_scaled_precalc(x));
        System.out.println(taylor_multipoint_precalc(x));
        System.out.println(newton_inverse(x));
        System.out.println(newton_bitguess(x));
        System.out.println(newton_inverse_bitguess(x));
        System.out.println(newton_bitguess_fixed(x));
        System.out.println(newton_inverse_bitguess_fixed(x));
        System.out.println("------------------");
    }
    // speed test
    public static void main(String[] args) {
        test(0.5);
        test(10);
        test(123.456789);
        double x;
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = Math.sqrt(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Math.sqrt: " + (t2-t1));
        long t3 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = newton(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t4 = System.currentTimeMillis();
        System.out.println("Newton: " + (t4-t3));
        long t5 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = lookup(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t6 = System.currentTimeMillis();
        System.out.println("Table lookup: " + (t6-t5));
        long t7 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = taylor_scaled(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t8 = System.currentTimeMillis();
        System.out.println("Taylor scaled: " + (t8-t7));
        long t9 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = taylor_scaled_precalc(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t10 = System.currentTimeMillis();
        System.out.println("Taylor scaled precalculated: " + (t10-t9));
        long t11 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = taylor_multipoint_precalc(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t12 = System.currentTimeMillis();
        System.out.println("Taylor multi point precalculated: " + (t12-t11));
        long t13 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = newton_inverse(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t14 = System.currentTimeMillis();
        System.out.println("Newton inverse: " + (t14-t13));
        long t15 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = newton_bitguess(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t16 = System.currentTimeMillis();
        System.out.println("Newton bit guess: " + (t16-t15));
        long t17 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = newton_inverse_bitguess(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t18 = System.currentTimeMillis();
        System.out.println("Newton inverse bit guess: " + (t18-t17));
        long t19 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = newton_bitguess_fixed(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t20 = System.currentTimeMillis();
        System.out.println("Newton bit guess fixed iterations: " + (t20-t19));
        long t21 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            x = newton_inverse_bitguess_fixed(SCALE * i);
            if(x>SCALE*i+1) throw new RuntimeException("Oops");
        }
        long t22 = System.currentTimeMillis();
        System.out.println("Newton inverse bit guess fixed iterations: " + (t22-t21));
    }
}
