//
// Copyright (C) 2025-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <cstdint>
#include <limits>
#include <ostream>

namespace vpux {
namespace type {
/**
 * @brief Class to represent the f4e2m1 type.
 */
class float4_e2m1 {
public:
    float4_e2m1() = default;
    float4_e2m1(float value);

    template <typename I>
    explicit float4_e2m1(I value): float4_e2m1(static_cast<float>(value)) {
    }

    template <typename T>
    bool operator==(const T& other) const;
    template <typename T>
    bool operator!=(const T& other) const {
        return !(*this == other);
    }

    template <typename T>
    bool operator<(const T& other) const;
    template <typename T>
    bool operator<=(const T& other) const;
    template <typename T>
    bool operator>(const T& other) const;
    template <typename T>
    bool operator>=(const T& other) const;
    template <typename T>
    float4_e2m1 operator+(const T& other) const;
    template <typename T>
    float4_e2m1 operator+=(const T& other);
    template <typename T>
    float4_e2m1 operator-(const T& other) const;
    template <typename T>
    float4_e2m1 operator-=(const T& other);
    template <typename T>
    float4_e2m1 operator*(const T& other) const;
    template <typename T>
    float4_e2m1 operator*=(const T& other);
    template <typename T>
    float4_e2m1 operator/(const T& other) const;
    template <typename T>
    float4_e2m1 operator/=(const T& other);

    operator float() const;

    static constexpr float4_e2m1 from_bits(uint8_t bits) {
        return float4_e2m1(bits, true);
    }

    uint8_t to_bits() const;

    friend std::ostream& operator<<(std::ostream& out, const float4_e2m1& obj) {
        out << static_cast<float>(obj);
        return out;
    }

private:
    constexpr float4_e2m1(uint8_t x, bool): m_value{x} {
    }

    uint8_t m_value;
};

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4756)
#endif
template <typename T>
bool float4_e2m1::operator==(const T& other) const {
    return (static_cast<float>(*this) == static_cast<float>(other));
}

template <typename T>
bool float4_e2m1::operator<(const T& other) const {
    return (static_cast<float>(*this) < static_cast<float>(other));
}

template <typename T>
bool float4_e2m1::operator<=(const T& other) const {
    return (static_cast<float>(*this) <= static_cast<float>(other));
}

template <typename T>
bool float4_e2m1::operator>(const T& other) const {
    return (static_cast<float>(*this) > static_cast<float>(other));
}

template <typename T>
bool float4_e2m1::operator>=(const T& other) const {
    return (static_cast<float>(*this) >= static_cast<float>(other));
}

template <typename T>
float4_e2m1 float4_e2m1::operator+(const T& other) const {
    return {static_cast<float>(*this) + static_cast<float>(other)};
}

template <typename T>
float4_e2m1 float4_e2m1::operator+=(const T& other) {
    return *this = *this + other;
}

template <typename T>
float4_e2m1 float4_e2m1::operator-(const T& other) const {
    return {static_cast<float>(*this) - static_cast<float>(other)};
}

template <typename T>
float4_e2m1 float4_e2m1::operator-=(const T& other) {
    return *this = *this - other;
}

template <typename T>
float4_e2m1 float4_e2m1::operator*(const T& other) const {
    return {static_cast<float>(*this) * static_cast<float>(other)};
}

template <typename T>
float4_e2m1 float4_e2m1::operator*=(const T& other) {
    return *this = *this * other;
}

template <typename T>
float4_e2m1 float4_e2m1::operator/(const T& other) const {
    return {static_cast<float>(*this) / static_cast<float>(other)};
}

template <typename T>
float4_e2m1 float4_e2m1::operator/=(const T& other) {
    return *this = *this / other;
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
}  // namespace type
}  // namespace vpux

namespace std {
template <>
class numeric_limits<vpux::type::float4_e2m1> {
public:
    static constexpr bool is_specialized = true;
    static constexpr vpux::type::float4_e2m1 min() noexcept {
        return vpux::type::float4_e2m1::from_bits(0b0010);  // minimum positive normalized value
    }
    static constexpr vpux::type::float4_e2m1 max() noexcept {
        return vpux::type::float4_e2m1::from_bits(0b0111);
    }
    static constexpr vpux::type::float4_e2m1 lowest() noexcept {
        return vpux::type::float4_e2m1::from_bits(0b1111);
    }
    static constexpr int digits = 2;
    static constexpr int digits10 = 0;

    static constexpr bool is_signed = true;
    static constexpr bool is_integer = false;
    static constexpr bool is_exact = false;

    static constexpr int radix = 2;

    static constexpr vpux::type::float4_e2m1 epsilon() noexcept {
        return vpux::type::float4_e2m1::from_bits(0b0001);
    }
    static constexpr vpux::type::float4_e2m1 round_error() noexcept {
        return vpux::type::float4_e2m1::from_bits(0b0001);
    }

    static constexpr int min_exponent = 1;
    static constexpr int min_exponent10 = 0;
    static constexpr int max_exponent = 3;
    static constexpr int max_exponent10 = 0;

    static constexpr bool has_infinity = false;
    static constexpr bool has_quiet_NaN = false;
    static constexpr bool has_signaling_NaN = false;

    static constexpr float_denorm_style has_denorm = denorm_present;
    static constexpr bool has_denorm_loss = false;

    static constexpr vpux::type::float4_e2m1 infinity() noexcept {
        return vpux::type::float4_e2m1::from_bits(0);  // no infinity
    }
    static constexpr vpux::type::float4_e2m1 quiet_NaN() noexcept {
        return vpux::type::float4_e2m1::from_bits(0);  // no quiet NaN
    }
    static constexpr vpux::type::float4_e2m1 signaling_NaN() noexcept {
        return vpux::type::float4_e2m1::from_bits(0);  // no signaling NaN
    }
    static constexpr vpux::type::float4_e2m1 denorm_min() noexcept {
        return vpux::type::float4_e2m1::from_bits(0b0001);  // minimum positive denormalized value
    }
    static constexpr bool is_iec559 = false;
    static constexpr bool is_bounded = true;
    static constexpr bool is_modulo = false;
    static constexpr bool traps = false;
    static constexpr bool tinyness_before = false;
    static constexpr float_round_style round_style = round_to_nearest;
};
}  // namespace std
