/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Test arithmetic operations.
 */
public class FloatMath {

    static void convTest() {
        System.out.println("FloatMath.convTest");

        float f;
        double d;
        int i;
        long l;

        /* float --> int */
        f = 1234.5678f;
        i = (int) f;
        Main.assertTrue(i == 1234);

        f = -1234.5678f;
        i = (int) f;
        Main.assertTrue(i == -1234);

        /* float --> long */
        f = 1238.5678f;
        l = (long) f;
        Main.assertTrue(l == 1238);

        f = -1238.5678f;
        l = (long) f;
        Main.assertTrue(l == -1238);

        /* float --> double */
        f = 1238.5678f;
        d = (double) f;
        Main.assertTrue(d > 1238.567 && d < 1238.568);

        /* double --> int */
        d = 1234.5678;
        i = (int) d;
        Main.assertTrue(i == 1234);

        d = -1234.5678;
        i = (int) d;
        Main.assertTrue(i == -1234);

        /* double --> long */
        d = 5678956789.0123;
        l = (long) d;
        Main.assertTrue(l == 5678956789L);

        d = -5678956789.0123;
        l = (long) d;
        Main.assertTrue(l == -5678956789L);

        /* double --> float */
        d = 1238.5678;
        f = (float) d;
        Main.assertTrue(f > 1238.567 && f < 1238.568);

        /* int --> long */
        i = 7654;
        l = (long) i;
        Main.assertTrue(l == 7654L);

        i = -7654;
        l = (long) i;
        Main.assertTrue(l == -7654L);

        /* int --> float */
        i = 1234;
        f = (float) i;
        Main.assertTrue(f > 1233.9f && f < 1234.1f);

        i = -1234;
        f = (float) i;
        Main.assertTrue(f < -1233.9f && f > -1234.1f);

        /* int --> double */
        i = 1238;
        d = (double) i;
        Main.assertTrue(d > 1237.9f && d < 1238.1f);

        i = -1238;
        d = (double) i;
        Main.assertTrue(d < -1237.9f && d > -1238.1f);

        /* long --> int (with truncation) */
        l = 5678956789L;
        i = (int) l;
        Main.assertTrue(i == 1383989493);

        l = -5678956789L;
        i = (int) l;
        Main.assertTrue(i == -1383989493);

        /* long --> float */
        l = 5678956789L;
        f = (float) l;
        Main.assertTrue(f > 5.6789564E9 && f < 5.6789566E9);

        l = -5678956789L;
        f = (float) l;
        Main.assertTrue(f < -5.6789564E9 && f > -5.6789566E9);

        /* long --> double */
        l = 6678956789L;
        d = (double) l;
        Main.assertTrue(d > 6.6789567E9 && d < 6.6789568E9);

        l = -6678956789L;
        d = (double) l;
        Main.assertTrue(d < -6.6789567E9 && d > -6.6789568E9);
    }

    /*
     * We pass in the arguments and return the results so the compiler
     * doesn't do the math for us.
     */
    static float[] floatOperTest(float x, float y) {
        System.out.println("FloatMath.floatOperTest");

        float[] results = new float[10];
        float tmp;

        /* this seems to generate "op-float" instructions */
        results[0] = x + y;
        results[1] = x - y;
        results[2] = x * y;
        results[3] = x / y;
        results[4] = x % -y;

        /* this seems to generate "op-float/2addr" instructions */
        tmp = x;
        tmp += y;
        results[5] = tmp;
        tmp = x;
        tmp -= y;
        results[6] = tmp;
        tmp = x;
        tmp *= y;
        results[7] = tmp;
        tmp = x;
        tmp /= y;
        results[8] = tmp;
        tmp = x;
        tmp %= -y;
        results[9] = tmp;

        return results;
    }
    static void floatOperCheck(float[] results) {
        Main.assertTrue(results[0] > 69996.99f && results[0] < 69997.01f);
        Main.assertTrue(results[1] > 70002.99f && results[1] < 70003.01f);
        Main.assertTrue(results[2] > -210000.01f && results[2] < -209999.99f);
        Main.assertTrue(results[3] > -23333.34f && results[3] < -23333.32f);
        Main.assertTrue(results[4] > 0.999f && results[4] < 1.001f);
        Main.assertTrue(results[5] > 69996.99f && results[5] < 69997.01f);
        Main.assertTrue(results[6] > 70002.99f && results[6] < 70003.01f);
        Main.assertTrue(results[7] > -210000.01f && results[7] < -209999.99f);
        Main.assertTrue(results[8] > -23333.34f && results[8] < -23333.32f);
        Main.assertTrue(results[9] > 0.999f && results[9] < 1.001f);
    }

    /*
     * We pass in the arguments and return the results so the compiler
     * doesn't do the math for us.
     */
    static double[] doubleOperTest(double x, double y) {
        System.out.println("FloatMath.doubleOperTest");

        double[] results = new double[10];
        double tmp;

        /* this seems to generate "op-double" instructions */
        results[0] = x + y;
        results[1] = x - y;
        results[2] = x * y;
        results[3] = x / y;
        results[4] = x % -y;

        /* this seems to generate "op-double/2addr" instructions */
        tmp = x;
        tmp += y;
        results[5] = tmp;
        tmp = x;
        tmp -= y;
        results[6] = tmp;
        tmp = x;
        tmp *= y;
        results[7] = tmp;
        tmp = x;
        tmp /= y;
        results[8] = tmp;
        tmp = x;
        tmp %= -y;
        results[9] = tmp;

        return results;
    }
    static void doubleOperCheck(double[] results) {
        Main.assertTrue(results[0] > 69996.99 && results[0] < 69997.01);
        Main.assertTrue(results[1] > 70002.99 && results[1] < 70003.01);
        Main.assertTrue(results[2] > -210000.01 && results[2] < -209999.99);
        Main.assertTrue(results[3] > -23333.34 && results[3] < -23333.32);
        Main.assertTrue(results[4] > 0.999 && results[4] < 1.001);
        Main.assertTrue(results[5] > 69996.99 && results[5] < 69997.01);
        Main.assertTrue(results[6] > 70002.99 && results[6] < 70003.01);
        Main.assertTrue(results[7] > -210000.01 && results[7] < -209999.99);
        Main.assertTrue(results[8] > -23333.34 && results[8] < -23333.32);
        Main.assertTrue(results[9] > 0.999 && results[9] < 1.001);
    }

    /*
     * Try to cause some unary operations.
     */
    static float unopTest(float f) {
        f = -f;
        return f;
    }

    static int[] convI(long l, float f, double d, float zero) {
        int[] results = new int[6];
        results[0] = (int) l;
        results[1] = (int) f;
        results[2] = (int) d;
        results[3] = (int) (1.0f / zero);       // +inf
        results[4] = (int) (-1.0f / zero);      // -inf
        results[5] = (int) ((1.0f / zero) / (1.0f / zero)); // NaN
        return results;
    }
    static void checkConvI(int[] results) {
        System.out.println("FloatMath.checkConvI");
        Main.assertTrue(results[0] == 0x44332211);
        Main.assertTrue(results[1] == 123);
        Main.assertTrue(results[2] == -3);
        Main.assertTrue(results[3] == 0x7fffffff);
        Main.assertTrue(results[4] == 0x80000000);
        Main.assertTrue(results[5] == 0);
    }

    static long[] convL(int i, float f, double d, double zero) {
        long[] results = new long[6];
        results[0] = (long) i;
        results[1] = (long) f;
        results[2] = (long) d;
        results[3] = (long) (1.0 / zero);       // +inf
        results[4] = (long) (-1.0 / zero);      // -inf
        results[5] = (long) ((1.0 / zero) / (1.0 / zero));  // NaN
        return results;
    }
    static void checkConvL(long[] results) {
        System.out.println("FloatMath.checkConvL");
        Main.assertTrue(results[0] == 0xFFFFFFFF88776655L);
        Main.assertTrue(results[1] == 123);
        Main.assertTrue(results[2] == -3);
        Main.assertTrue(results[3] == 0x7fffffffffffffffL);
        Main.assertTrue(results[4] == 0x8000000000000000L);
        Main.assertTrue(results[5] == 0);
    }

    static float[] convF(int i, long l, double d) {
        float[] results = new float[3];
        results[0] = (float) i;
        results[1] = (float) l;
        results[2] = (float) d;
        return results;
    }
    static void checkConvF(float[] results) {
        System.out.println("FloatMath.checkConvF");
        Main.assertTrue(results[0] == -2.0054409E9f);
        Main.assertTrue(results[1] == -8.613303E18f);
        Main.assertTrue(results[2] == -3.1415927f);
    }

    static double[] convD(int i, long l, float f) {
        double[] results = new double[3];
        results[0] = (double) i;
        results[1] = (double) l;
        results[2] = (double) f;
        return results;
    }
    static void checkConvD(double[] results) {
        System.out.println("FloatMath.checkConvD");
        Main.assertTrue(results[0] == -2.005440939E9);
        Main.assertTrue(results[1] == -8.6133032459203287E18);
        Main.assertTrue(results[2] == 123.45600128173828);
    }

    static void checkConsts() {
        System.out.println("FloatMath.checkConsts");

        float f = 10.0f;        // const/special
        Main.assertTrue(f > 9.9 && f < 10.1);

        double d = 10.0;        // const-wide/special
        Main.assertTrue(d > 9.9 && d < 10.1);
    }

    /*
     * Determine if two floating point numbers are approximately equal.
     *
     * (Assumes that floating point is generally working, so we can't use
     * this for the first set of tests.)
     */
    static boolean approxEqual(float a, float b, float maxDelta) {
        if (a > b)
            return (a - b) < maxDelta;
        else
            return (b - a) < maxDelta;
    }
    static boolean approxEqual(double a, double b, double maxDelta) {
        if (a > b)
            return (a - b) < maxDelta;
        else
            return (b - a) < maxDelta;
    }

    /*
     * Test some java.lang.Math functions.
     *
     * The method arguments are positive values.
     */
    static void jlmTests(float ff, double dd) {
        System.out.println("FloatMath.jlmTests");

        Main.assertTrue(approxEqual(Math.abs(ff), ff, 0.001f));
        Main.assertTrue(approxEqual(Math.abs(-ff), ff, 0.001f));
        Main.assertTrue(approxEqual(Math.min(ff, -5.0f), -5.0f, 0.001f));
        Main.assertTrue(approxEqual(Math.max(ff, -5.0f), ff, 0.001f));

        Main.assertTrue(approxEqual(Math.abs(dd), dd, 0.001));
        Main.assertTrue(approxEqual(Math.abs(-dd), dd, 0.001));
        Main.assertTrue(approxEqual(Math.min(dd, -5.0), -5.0, 0.001));
        Main.assertTrue(approxEqual(Math.max(dd, -5.0), dd, 0.001));

        double sq = Math.sqrt(dd);
        Main.assertTrue(approxEqual(sq*sq, dd, 0.001));

        Main.assertTrue(approxEqual(0.5403023058681398, Math.cos(1.0), 0.00000001));
        Main.assertTrue(approxEqual(0.8414709848078965, Math.sin(1.0), 0.00000001));
    }

    public static void run() {
        convTest();

        float[] floatResults;
        double[] doubleResults;
        int[] intResults;
        long[] longResults;

        floatResults = floatOperTest(70000.0f, -3.0f);
        floatOperCheck(floatResults);
        doubleResults = doubleOperTest(70000.0, -3.0);
        doubleOperCheck(doubleResults);

        intResults = convI(0x8877665544332211L, 123.456f, -3.1415926535, 0.0f);
        checkConvI(intResults);
        longResults = convL(0x88776655, 123.456f, -3.1415926535, 0.0);
        checkConvL(longResults);
        floatResults = convF(0x88776655, 0x8877665544332211L, -3.1415926535);
        checkConvF(floatResults);
        doubleResults = convD(0x88776655, 0x8877665544332211L, 123.456f);
        checkConvD(doubleResults);

        unopTest(123.456f);

        checkConsts();

        jlmTests(3.14159f, 123456.78987654321);
    }
}
