|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <cmath> |
| 8 | +#include <iostream> |
| 9 | +#include <limits> |
| 10 | +#include <memory> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include "openvino/core/core_visibility.hpp" |
| 15 | + |
| 16 | +namespace ov { |
| 17 | + |
| 18 | +/** |
| 19 | + * @brief Class to represent the f8e8m0 type. |
| 20 | + */ |
| 21 | +class OPENVINO_API float8_e8m0 { |
| 22 | +public: |
| 23 | + float8_e8m0() = default; |
| 24 | + float8_e8m0(const float value); |
| 25 | + |
| 26 | + template <typename I> |
| 27 | + explicit float8_e8m0(I value) : float8_e8m0{static_cast<float>(value)} {} |
| 28 | + |
| 29 | + template <typename T> |
| 30 | + bool operator==(const T& other) const; |
| 31 | + template <typename T> |
| 32 | + bool operator!=(const T& other) const { |
| 33 | + return !(*this == other); |
| 34 | + } |
| 35 | + |
| 36 | + template <typename T> |
| 37 | + bool operator<(const T& other) const; |
| 38 | + template <typename T> |
| 39 | + bool operator<=(const T& other) const; |
| 40 | + template <typename T> |
| 41 | + bool operator>(const T& other) const; |
| 42 | + template <typename T> |
| 43 | + bool operator>=(const T& other) const; |
| 44 | + template <typename T> |
| 45 | + float8_e8m0 operator+(const T& other) const; |
| 46 | + template <typename T> |
| 47 | + float8_e8m0 operator+=(const T& other); |
| 48 | + template <typename T> |
| 49 | + float8_e8m0 operator-(const T& other) const; |
| 50 | + template <typename T> |
| 51 | + float8_e8m0 operator-=(const T& other); |
| 52 | + template <typename T> |
| 53 | + float8_e8m0 operator*(const T& other) const; |
| 54 | + template <typename T> |
| 55 | + float8_e8m0 operator*=(const T& other); |
| 56 | + template <typename T> |
| 57 | + float8_e8m0 operator/(const T& other) const; |
| 58 | + template <typename T> |
| 59 | + float8_e8m0 operator/=(const T& other); |
| 60 | + |
| 61 | + operator float() const; |
| 62 | + |
| 63 | + static constexpr float8_e8m0 from_bits(uint8_t bits) { |
| 64 | + return float8_e8m0(bits, true); |
| 65 | + } |
| 66 | + |
| 67 | + uint8_t to_bits() const; |
| 68 | + |
| 69 | + friend std::ostream& operator<<(std::ostream& out, const float8_e8m0& obj) { |
| 70 | + out << static_cast<float>(obj); |
| 71 | + return out; |
| 72 | + } |
| 73 | + |
| 74 | +private: |
| 75 | + constexpr float8_e8m0(const uint8_t x, bool) : m_value{x} {} |
| 76 | + |
| 77 | + uint8_t m_value{}; |
| 78 | +}; |
| 79 | + |
| 80 | +#if defined(_MSC_VER) |
| 81 | +# pragma warning(push) |
| 82 | +# pragma warning(disable : 4756) |
| 83 | +#endif |
| 84 | +template <typename T> |
| 85 | +bool float8_e8m0::operator==(const T& other) const { |
| 86 | +#if defined(__GNUC__) |
| 87 | +# pragma GCC diagnostic push |
| 88 | +# pragma GCC diagnostic ignored "-Wfloat-equal" |
| 89 | +#endif |
| 90 | + return (static_cast<float>(*this) == static_cast<float>(other)); |
| 91 | +#if defined(__GNUC__) |
| 92 | +# pragma GCC diagnostic pop |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +template <typename T> |
| 97 | +bool float8_e8m0::operator<(const T& other) const { |
| 98 | + return (static_cast<float>(*this) < static_cast<float>(other)); |
| 99 | +} |
| 100 | + |
| 101 | +template <typename T> |
| 102 | +bool float8_e8m0::operator<=(const T& other) const { |
| 103 | + return (static_cast<float>(*this) <= static_cast<float>(other)); |
| 104 | +} |
| 105 | + |
| 106 | +template <typename T> |
| 107 | +bool float8_e8m0::operator>(const T& other) const { |
| 108 | + return (static_cast<float>(*this) > static_cast<float>(other)); |
| 109 | +} |
| 110 | + |
| 111 | +template <typename T> |
| 112 | +bool float8_e8m0::operator>=(const T& other) const { |
| 113 | + return (static_cast<float>(*this) >= static_cast<float>(other)); |
| 114 | +} |
| 115 | + |
| 116 | +template <typename T> |
| 117 | +float8_e8m0 float8_e8m0::operator+(const T& other) const { |
| 118 | + return {static_cast<float>(*this) + static_cast<float>(other)}; |
| 119 | +} |
| 120 | + |
| 121 | +template <typename T> |
| 122 | +float8_e8m0 float8_e8m0::operator+=(const T& other) { |
| 123 | + return *this = *this + other; |
| 124 | +} |
| 125 | + |
| 126 | +template <typename T> |
| 127 | +float8_e8m0 float8_e8m0::operator-(const T& other) const { |
| 128 | + return {static_cast<float>(*this) - static_cast<float>(other)}; |
| 129 | +} |
| 130 | + |
| 131 | +template <typename T> |
| 132 | +float8_e8m0 float8_e8m0::operator-=(const T& other) { |
| 133 | + return *this = *this - other; |
| 134 | +} |
| 135 | + |
| 136 | +template <typename T> |
| 137 | +float8_e8m0 float8_e8m0::operator*(const T& other) const { |
| 138 | + return {static_cast<float>(*this) * static_cast<float>(other)}; |
| 139 | +} |
| 140 | + |
| 141 | +template <typename T> |
| 142 | +float8_e8m0 float8_e8m0::operator*=(const T& other) { |
| 143 | + return *this = *this * other; |
| 144 | +} |
| 145 | + |
| 146 | +template <typename T> |
| 147 | +float8_e8m0 float8_e8m0::operator/(const T& other) const { |
| 148 | + return {static_cast<float>(*this) / static_cast<float>(other)}; |
| 149 | +} |
| 150 | + |
| 151 | +template <typename T> |
| 152 | +float8_e8m0 float8_e8m0::operator/=(const T& other) { |
| 153 | + return *this = *this / other; |
| 154 | +} |
| 155 | +#if defined(_MSC_VER) |
| 156 | +# pragma warning(pop) |
| 157 | +#endif |
| 158 | +} // namespace ov |
| 159 | + |
| 160 | +namespace std { |
| 161 | +template <> |
| 162 | +class numeric_limits<ov::float8_e8m0> { |
| 163 | +public: |
| 164 | + static constexpr bool is_specialized = true; |
| 165 | + static constexpr ov::float8_e8m0 min() noexcept { |
| 166 | + return ov::float8_e8m0::from_bits(0b00000000u); |
| 167 | + } |
| 168 | + static constexpr ov::float8_e8m0 max() noexcept { |
| 169 | + return ov::float8_e8m0::from_bits(0b11111110u); |
| 170 | + } |
| 171 | + static constexpr ov::float8_e8m0 lowest() noexcept { |
| 172 | + return ov::float8_e8m0::from_bits(0b00000000u); |
| 173 | + } |
| 174 | + static constexpr int digits = 1; |
| 175 | + static constexpr int digits10 = 0; |
| 176 | + |
| 177 | + static constexpr bool is_signed = false; |
| 178 | + static constexpr bool is_integer = false; |
| 179 | + static constexpr bool is_exact = false; |
| 180 | + |
| 181 | + static constexpr int radix = 2; |
| 182 | + |
| 183 | + static constexpr ov::float8_e8m0 epsilon() noexcept { |
| 184 | + return ov::float8_e8m0::from_bits(0b00000001u); |
| 185 | + } |
| 186 | + static constexpr ov::float8_e8m0 round_error() noexcept { |
| 187 | + return ov::float8_e8m0::from_bits(0b01111110u); |
| 188 | + } |
| 189 | + |
| 190 | + static constexpr int min_exponent = -126; |
| 191 | + static constexpr int min_exponent10 = -38; |
| 192 | + static constexpr int max_exponent = 128; |
| 193 | + static constexpr int max_exponent10 = 38; |
| 194 | + |
| 195 | + static constexpr bool has_infinity = false; |
| 196 | + static constexpr bool has_quiet_NaN = true; |
| 197 | + static constexpr bool has_signaling_NaN = false; |
| 198 | + |
| 199 | + static constexpr float_denorm_style has_denorm = denorm_absent; |
| 200 | + static constexpr bool has_denorm_loss = false; |
| 201 | + |
| 202 | + static constexpr ov::float8_e8m0 infinity() noexcept { |
| 203 | + return ov::float8_e8m0::from_bits(0); // no infinity |
| 204 | + } |
| 205 | + static constexpr ov::float8_e8m0 quiet_NaN() noexcept { |
| 206 | + return ov::float8_e8m0::from_bits(0b11111111); |
| 207 | + } |
| 208 | + static constexpr ov::float8_e8m0 signaling_NaN() noexcept { |
| 209 | + return ov::float8_e8m0::from_bits(0); // no signaling NaN |
| 210 | + } |
| 211 | + static constexpr bool is_iec559 = false; |
| 212 | + static constexpr bool is_bounded = false; |
| 213 | + static constexpr bool is_modulo = false; |
| 214 | + static constexpr bool traps = false; |
| 215 | + static constexpr bool tinyness_before = false; |
| 216 | + static constexpr float_round_style round_style = round_to_nearest; |
| 217 | +}; |
| 218 | +} // namespace std |
0 commit comments