|
| 1 | +/** |
| 2 | + * @file bfloat16.hpp |
| 3 | + * @brief BFloat16 (Brain Floating Point) type definition |
| 4 | + * @author David Sanftenberg |
| 5 | + * @date 2025-10-19 |
| 6 | + * |
| 7 | + * Implements the BFloat16 format: 16-bit floating point with 1 sign bit, |
| 8 | + * 8 exponent bits, and 7 mantissa bits. This format is compatible with |
| 9 | + * FP32's exponent range but has reduced precision, making it suitable for |
| 10 | + * deep learning and scientific computing where memory bandwidth is critical. |
| 11 | + * |
| 12 | + * Memory layout (big-endian bit ordering): |
| 13 | + * [15]: Sign bit |
| 14 | + * [14:7]: Exponent (8 bits, same as FP32) |
| 15 | + * [6:0]: Mantissa (7 bits, truncated from FP32's 23 bits) |
| 16 | + */ |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <cstring> |
| 22 | +#include <limits> |
| 23 | +#include <ostream> |
| 24 | + |
| 25 | +namespace costa { |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief BFloat16 (Brain Floating Point) 16-bit floating point type |
| 29 | + * |
| 30 | + * This class provides a compact 16-bit floating point representation with |
| 31 | + * the same exponent range as FP32 but reduced mantissa precision. It is |
| 32 | + * designed for use in neural networks and matrix operations where memory |
| 33 | + * bandwidth is more critical than precision. |
| 34 | + * |
| 35 | + * Key properties: |
| 36 | + * - Size: 16 bits (2 bytes) |
| 37 | + * - Range: Same as FP32 (~1e-38 to ~3e38) |
| 38 | + * - Precision: ~3 decimal digits (vs ~7 for FP32) |
| 39 | + * - Conversion to/from FP32: Simple bit truncation/extension |
| 40 | + */ |
| 41 | +class bfloat16 { |
| 42 | +private: |
| 43 | + uint16_t data_; ///< Raw 16-bit storage |
| 44 | + |
| 45 | +public: |
| 46 | + /** |
| 47 | + * @brief Default constructor (initializes to zero) |
| 48 | + */ |
| 49 | + constexpr bfloat16() : data_(0) {} |
| 50 | + |
| 51 | + /** |
| 52 | + * @brief Construct from raw uint16_t bits |
| 53 | + * @param raw Raw 16-bit representation |
| 54 | + */ |
| 55 | + explicit constexpr bfloat16(uint16_t raw) : data_(raw) {} |
| 56 | + |
| 57 | + /** |
| 58 | + * @brief Construct from int (convenience for literals like 0, 1) |
| 59 | + * @param value Integer value to convert |
| 60 | + */ |
| 61 | + bfloat16(int value) : bfloat16(static_cast<float>(value)) {} |
| 62 | + |
| 63 | + /** |
| 64 | + * @brief Construct from float (FP32) |
| 65 | + * @param value FP32 value to convert |
| 66 | + * |
| 67 | + * Conversion truncates the lower 16 bits of the FP32 mantissa. |
| 68 | + * This is a simple right-shift operation on the bit representation. |
| 69 | + * |
| 70 | + * Note: Non-explicit to allow implicit conversion from float for convenience |
| 71 | + */ |
| 72 | + bfloat16(float value) { |
| 73 | + uint32_t fp32_bits; |
| 74 | + std::memcpy(&fp32_bits, &value, sizeof(float)); |
| 75 | + |
| 76 | + // BF16 is the upper 16 bits of FP32 |
| 77 | + // Simple truncation (round-to-nearest-even would be more accurate but slower) |
| 78 | + data_ = static_cast<uint16_t>(fp32_bits >> 16); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Convert to float (FP32) |
| 83 | + * @return FP32 representation |
| 84 | + * |
| 85 | + * Conversion extends the BF16 by appending 16 zero bits to the mantissa. |
| 86 | + * This is a simple left-shift operation. |
| 87 | + */ |
| 88 | + explicit operator float() const { |
| 89 | + // Extend BF16 to FP32 by shifting left and zero-padding |
| 90 | + uint32_t fp32_bits = static_cast<uint32_t>(data_) << 16; |
| 91 | + |
| 92 | + float result; |
| 93 | + std::memcpy(&result, &fp32_bits, sizeof(float)); |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @brief Get raw 16-bit representation |
| 99 | + * @return Raw bits as uint16_t |
| 100 | + */ |
| 101 | + constexpr uint16_t raw() const { return data_; } |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Set raw 16-bit representation |
| 105 | + * @param raw Raw bits to set |
| 106 | + */ |
| 107 | + constexpr void set_raw(uint16_t raw) { data_ = raw; } |
| 108 | + |
| 109 | + // Comparison operators |
| 110 | + bool operator==(const bfloat16& other) const { |
| 111 | + return data_ == other.data_; |
| 112 | + } |
| 113 | + |
| 114 | + bool operator!=(const bfloat16& other) const { |
| 115 | + return data_ != other.data_; |
| 116 | + } |
| 117 | + |
| 118 | + bool operator<(const bfloat16& other) const { |
| 119 | + return static_cast<float>(*this) < static_cast<float>(other); |
| 120 | + } |
| 121 | + |
| 122 | + bool operator>(const bfloat16& other) const { |
| 123 | + return static_cast<float>(*this) > static_cast<float>(other); |
| 124 | + } |
| 125 | + |
| 126 | + bool operator<=(const bfloat16& other) const { |
| 127 | + return static_cast<float>(*this) <= static_cast<float>(other); |
| 128 | + } |
| 129 | + |
| 130 | + bool operator>=(const bfloat16& other) const { |
| 131 | + return static_cast<float>(*this) >= static_cast<float>(other); |
| 132 | + } |
| 133 | + |
| 134 | + // Arithmetic operators (implemented via conversion to FP32) |
| 135 | + bfloat16 operator+(const bfloat16& other) const { |
| 136 | + return bfloat16(static_cast<float>(*this) + static_cast<float>(other)); |
| 137 | + } |
| 138 | + |
| 139 | + bfloat16 operator-(const bfloat16& other) const { |
| 140 | + return bfloat16(static_cast<float>(*this) - static_cast<float>(other)); |
| 141 | + } |
| 142 | + |
| 143 | + bfloat16 operator*(const bfloat16& other) const { |
| 144 | + return bfloat16(static_cast<float>(*this) * static_cast<float>(other)); |
| 145 | + } |
| 146 | + |
| 147 | + bfloat16 operator/(const bfloat16& other) const { |
| 148 | + return bfloat16(static_cast<float>(*this) / static_cast<float>(other)); |
| 149 | + } |
| 150 | + |
| 151 | + bfloat16& operator+=(const bfloat16& other) { |
| 152 | + *this = *this + other; |
| 153 | + return *this; |
| 154 | + } |
| 155 | + |
| 156 | + bfloat16& operator-=(const bfloat16& other) { |
| 157 | + *this = *this - other; |
| 158 | + return *this; |
| 159 | + } |
| 160 | + |
| 161 | + bfloat16& operator*=(const bfloat16& other) { |
| 162 | + *this = *this * other; |
| 163 | + return *this; |
| 164 | + } |
| 165 | + |
| 166 | + bfloat16& operator/=(const bfloat16& other) { |
| 167 | + *this = *this / other; |
| 168 | + return *this; |
| 169 | + } |
| 170 | + |
| 171 | + // Unary operators |
| 172 | + bfloat16 operator-() const { |
| 173 | + return bfloat16(-static_cast<float>(*this)); |
| 174 | + } |
| 175 | + |
| 176 | + // Stream output |
| 177 | + friend std::ostream& operator<<(std::ostream& os, const bfloat16& bf) { |
| 178 | + os << static_cast<float>(bf); |
| 179 | + return os; |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +// Mathematical functions for bfloat16 |
| 184 | +inline float abs(const bfloat16& x) { |
| 185 | + return std::abs(static_cast<float>(x)); |
| 186 | +} |
| 187 | + |
| 188 | +} // namespace costa |
| 189 | + |
| 190 | +// Specialization of std::numeric_limits for bfloat16 |
| 191 | +namespace std { |
| 192 | + template<> |
| 193 | + class numeric_limits<costa::bfloat16> { |
| 194 | + public: |
| 195 | + static constexpr bool is_specialized = true; |
| 196 | + static constexpr bool is_signed = true; |
| 197 | + static constexpr bool is_integer = false; |
| 198 | + static constexpr bool is_exact = false; |
| 199 | + static constexpr bool has_infinity = true; |
| 200 | + static constexpr bool has_quiet_NaN = true; |
| 201 | + static constexpr bool has_signaling_NaN = true; |
| 202 | + static constexpr float_denorm_style has_denorm = denorm_present; |
| 203 | + static constexpr bool has_denorm_loss = false; |
| 204 | + static constexpr float_round_style round_style = round_to_nearest; |
| 205 | + static constexpr bool is_iec559 = false; |
| 206 | + static constexpr bool is_bounded = true; |
| 207 | + static constexpr bool is_modulo = false; |
| 208 | + static constexpr int digits = 8; // Mantissa bits + 1 (implicit) |
| 209 | + static constexpr int digits10 = 2; // Decimal digits of precision |
| 210 | + static constexpr int max_digits10 = 4; // Max decimal digits for round-trip |
| 211 | + static constexpr int radix = 2; |
| 212 | + static constexpr int min_exponent = -125; |
| 213 | + static constexpr int min_exponent10 = -37; |
| 214 | + static constexpr int max_exponent = 128; |
| 215 | + static constexpr int max_exponent10 = 38; |
| 216 | + static constexpr bool traps = false; |
| 217 | + static constexpr bool tinyness_before = false; |
| 218 | + |
| 219 | + static constexpr costa::bfloat16 min() noexcept { |
| 220 | + return costa::bfloat16(static_cast<uint16_t>(0x0080)); // Smallest normalized positive value |
| 221 | + } |
| 222 | + |
| 223 | + static constexpr costa::bfloat16 lowest() noexcept { |
| 224 | + return costa::bfloat16(static_cast<uint16_t>(0xFF7F)); // Most negative finite value |
| 225 | + } |
| 226 | + |
| 227 | + static constexpr costa::bfloat16 max() noexcept { |
| 228 | + return costa::bfloat16(static_cast<uint16_t>(0x7F7F)); // Largest finite value |
| 229 | + } |
| 230 | + |
| 231 | + static constexpr costa::bfloat16 epsilon() noexcept { |
| 232 | + return costa::bfloat16(static_cast<uint16_t>(0x3C00)); // 2^-7 (smallest x where 1+x != 1) |
| 233 | + } |
| 234 | + |
| 235 | + static constexpr costa::bfloat16 round_error() noexcept { |
| 236 | + return costa::bfloat16(static_cast<uint16_t>(0x3F00)); // 0.5 in BF16 |
| 237 | + } |
| 238 | + |
| 239 | + static constexpr costa::bfloat16 infinity() noexcept { |
| 240 | + return costa::bfloat16(static_cast<uint16_t>(0x7F80)); // +Infinity |
| 241 | + } |
| 242 | + |
| 243 | + static constexpr costa::bfloat16 quiet_NaN() noexcept { |
| 244 | + return costa::bfloat16(static_cast<uint16_t>(0x7FC0)); // Quiet NaN |
| 245 | + } |
| 246 | + |
| 247 | + static constexpr costa::bfloat16 signaling_NaN() noexcept { |
| 248 | + return costa::bfloat16(static_cast<uint16_t>(0x7F80 | 1)); // Signaling NaN |
| 249 | + } |
| 250 | + |
| 251 | + static constexpr costa::bfloat16 denorm_min() noexcept { |
| 252 | + return costa::bfloat16(static_cast<uint16_t>(0x0001)); // Smallest denormalized positive value |
| 253 | + } |
| 254 | + }; |
| 255 | +} // namespace std |
0 commit comments