use super::generic;

/// Floor (f16)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg(f16_enabled)]
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn floorf16(x: f16) -> f16 {
    return generic::floor_status(x).val;
}

/// Floor (f64)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn floor(x: f64) -> f64 {
    select_implementation! {
        name: floor,
        use_arch: all(target_arch = "wasm32", intrinsics_enabled),
        use_arch_required: x86_no_sse2,
        args: x,
    }

    return generic::floor_status(x).val;
}

/// Floor (f32)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn floorf(x: f32) -> f32 {
    select_implementation! {
        name: floorf,
        use_arch: all(target_arch = "wasm32", intrinsics_enabled),
        args: x,
    }

    return generic::floor_status(x).val;
}

/// Floor (f128)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg(f128_enabled)]
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn floorf128(x: f128) -> f128 {
    return generic::floor_status(x).val;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::support::{Float, FpResult, Hex, Status};

    macro_rules! cases {
        ($f:ty) => {
            [
                // roundtrip
                (0.0, 0.0, Status::OK),
                (-0.0, -0.0, Status::OK),
                (1.0, 1.0, Status::OK),
                (-1.0, -1.0, Status::OK),
                (<$f>::INFINITY, <$f>::INFINITY, Status::OK),
                (<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
                // with rounding
                (0.1, 0.0, Status::INEXACT),
                (-0.1, -1.0, Status::INEXACT),
                (0.5, 0.0, Status::INEXACT),
                (-0.5, -1.0, Status::INEXACT),
                (0.9, 0.0, Status::INEXACT),
                (-0.9, -1.0, Status::INEXACT),
                (1.1, 1.0, Status::INEXACT),
                (-1.1, -2.0, Status::INEXACT),
                (1.5, 1.0, Status::INEXACT),
                (-1.5, -2.0, Status::INEXACT),
                (1.9, 1.0, Status::INEXACT),
                (-1.9, -2.0, Status::INEXACT),
            ]
        };
    }

    #[track_caller]
    fn check<F: Float>(f: fn(F) -> F, cases: &[(F, F, Status)]) {
        for &(x, exp_res, exp_stat) in cases {
            let FpResult { val, status } = generic::floor_status(x);
            assert_biteq!(val, exp_res, "generic::floor_status({x:?}) ({})", Hex(x));
            assert_eq!(
                status,
                exp_stat,
                "{x:?} {} -> {exp_res:?} {}",
                Hex(x),
                Hex(exp_res)
            );
            let val = f(x);
            assert_biteq!(val, exp_res, "floor({x:?}) ({})", Hex(x));
        }
    }

    #[test]
    #[cfg(f16_enabled)]
    fn check_f16() {
        check::<f16>(floorf16, &cases!(f16));
        check::<f16>(
            floorf16,
            &[
                (hf16!("0x1p10"), hf16!("0x1p10"), Status::OK),
                (hf16!("-0x1p10"), hf16!("-0x1p10"), Status::OK),
            ],
        );
    }

    #[test]
    fn check_f32() {
        check::<f32>(floorf, &cases!(f32));
        check::<f32>(
            floorf,
            &[
                (hf32!("0x1p23"), hf32!("0x1p23"), Status::OK),
                (hf32!("-0x1p23"), hf32!("-0x1p23"), Status::OK),
            ],
        );
    }

    #[test]
    fn check_f64() {
        check::<f64>(floor, &cases!(f64));
        check::<f64>(
            floor,
            &[
                (hf64!("0x1p52"), hf64!("0x1p52"), Status::OK),
                (hf64!("-0x1p52"), hf64!("-0x1p52"), Status::OK),
            ],
        );
    }

    #[test]
    #[cfg(f128_enabled)]
    fn check_f128() {
        check::<f128>(floorf128, &cases!(f128));
        check::<f128>(
            floorf128,
            &[
                (hf128!("0x1p112"), hf128!("0x1p112"), Status::OK),
                (hf128!("-0x1p112"), hf128!("-0x1p112"), Status::OK),
            ],
        );
    }
}
