Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:ClickHouse ClickHouse BFloat16

From Leeroopedia
Revision as of 14:36, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/ClickHouse_ClickHouse_BFloat16.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Numeric_Types, Data_Types
Last Updated 2026-02-08 00:00 GMT

Overview

A manual implementation of the BFloat16 (Brain Float16) 16-bit floating point data type optimized for AI and machine learning workloads.

Description

This code implements `BFloat16`, a 16-bit floating point type that shares the same 8 exponent bits as `Float32` but with reduced mantissa precision. The implementation provides a complete C++ class with constructors, arithmetic operators, comparison operators, and conversion functions. Unlike IEEE Float16 (half precision), `BFloat16` prioritizes range over precision, making it ideal for AI applications. The implementation is designed to work around limitations in current compiler support for `__bf16` and the upcoming C++23 `std::bfloat16_t`.

Usage

Use this when you need a 16-bit floating point type for AI/ML workloads, vector search, or running quantized models where the range of the data type is more important than precision. `BFloat16` has the advantage that you can extract it from the most significant two bytes of a `Float32` representation, enabling efficient conversions.

Code Reference

Source Location

Signature

class BFloat16
{
private:
    UInt16 x = 0;

public:
    constexpr BFloat16() = default;
    explicit constexpr BFloat16(const Float32 & other);
    template <typename T> explicit constexpr BFloat16(const T & other);

    static constexpr BFloat16 fromBits(UInt16 bits) noexcept;

    explicit constexpr operator Float32() const;
    template <typename T> explicit constexpr operator T() const;

    constexpr bool isFinite() const;
    constexpr bool isNaN() const;
    constexpr bool signBit() const;
    constexpr BFloat16 abs() const;

    constexpr BFloat16 operator+(const BFloat16 & other) const;
    constexpr BFloat16 operator-(const BFloat16 & other) const;
    constexpr BFloat16 operator*(const BFloat16 & other) const;
    constexpr BFloat16 operator/(const BFloat16 & other) const;

    constexpr const UInt16 & raw() const;
};

Import

#include <base/BFloat16.h>

I/O Contract

Inputs

Name Type Required Description
value Float32 or numeric type Yes The value to convert to `BFloat16`
bits UInt16 No Raw 16-bit representation (for `fromBits`)

Outputs

Name Type Description
BFloat16 BFloat16 A 16-bit brain floating point value
Float32 Float32 Conversion back to 32-bit float
raw bits UInt16 The raw 16-bit representation

Usage Examples

// Create from float32
BFloat16 bf = BFloat16(3.14159f);

// Arithmetic operations
BFloat16 a = BFloat16(2.0f);
BFloat16 b = BFloat16(3.0f);
BFloat16 sum = a + b;       // 5.0
BFloat16 product = a * b;   // 6.0

// Conversion back to float32
float result = static_cast<Float32>(sum);

// Create from raw bits
BFloat16 from_bits = BFloat16::fromBits(0x4000); // 2.0

// Check properties
if (bf.isFinite() && !bf.isNaN()) {
    // Process valid value
}

// Mixed type operations
BFloat16 c = BFloat16(1.5f);
auto d = c + 2.0;  // Promotes to Float32 or Float64

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment