Files
Strata/engine/include/strata/quant/fp16.hpp
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

53 lines
2.0 KiB
C++

// include/strata/quant/fp16.hpp - IEEE half <-> float, scalar and exact (own-stack phase 2).
//
// The same values as F16C's VCVTPH2PS / VCVTPS2PH and ggml's table for every finite input (fp16 -> fp32 is exact; fp32 -> fp16 rounds to nearest even; a NaN
// comes out as 0x7E00). quant_parity checks both over every fp16 value and 4M random floats.
#pragma once
#include <cmath>
#include <cstdint>
#include <cstring>
namespace strata::quant {
inline float f32_from_bits(uint32_t w) {
float f;
std::memcpy(&f, &w, sizeof f);
return f;
}
inline uint32_t f32_to_bits(float f) {
uint32_t w;
std::memcpy(&w, &f, sizeof w);
return w;
}
inline float fp16_to_fp32(uint16_t h) {
const uint32_t w = (uint32_t) h << 16;
const uint32_t sign = w & 0x80000000u;
const uint32_t two_w = w + w;
const float exp_scale = f32_from_bits(0x7800000u); // 2^-112
const float normalized = f32_from_bits((two_w >> 4) + (0xE0u << 23)) * exp_scale;
const float denormalized = f32_from_bits((two_w >> 17) | (126u << 23)) - 0.5f;
const uint32_t result = sign | (two_w < (1u << 27) ? f32_to_bits(denormalized) : f32_to_bits(normalized));
return f32_from_bits(result);
}
inline uint16_t fp32_to_fp16(float f) {
const float scale_to_inf = f32_from_bits(0x77800000u); // 2^112
const float scale_to_zero = f32_from_bits(0x08800000u); // 2^-110
float base = (std::fabs(f) * scale_to_inf) * scale_to_zero;
const uint32_t w = f32_to_bits(f);
const uint32_t shl1_w = w + w;
const uint32_t sign = w & 0x80000000u;
uint32_t bias = shl1_w & 0xFF000000u;
if (bias < 0x71000000u) bias = 0x71000000u;
base = f32_from_bits((bias >> 1) + 0x07800000u) + base;
const uint32_t bits = f32_to_bits(base);
const uint32_t exp_bits = (bits >> 13) & 0x00007C00u;
const uint32_t mantissa_bits = bits & 0x00000FFFu;
const uint32_t nonsign = exp_bits + mantissa_bits;
return (uint16_t) ((sign >> 16) | (shl1_w > 0xFF000000u ? 0x7E00u : nonsign));
}
} // namespace strata::quant