GF8: один байт
Вы узнаете
GoldenFloat в один байт, 1 + 3 + 4, смещение 3, и каковы его наибольшее и наименьшее значения.
gf8.t27 держит один байт: 1 бит знака, 3 бита порядка, 4 бита мантиссы и смещение 3, то есть 2^(3 - 1) - 1. PHI_DISTANCE равно 0.132 — это дальше всех из семи ширин, что перечисляет phi_ratio.t27, ведь 3 / 4 = 0.75 перелетает 1 / phi. Наибольшее значение, по max_value, равно (1 + 15/16) * 2^(7 - 3) = 31, а наименьшее, по min_positive, 1/16 * 2^-2 = 1/64. Комментарии над обеими функциями говорят 15.5 и 0.0625; код считает 31 и 1/64. MEMORY_RATIO_VS_FP32 равно 0.25: четыре значения GF8 помещаются там, где одно FP32.
Попробуйте
Посчитайте max_value и min_positive из gf8.t27 вручную и сравните с комментариями над ними. Затем декодируйте код 0b01000000, который тест gf8_phi_distance ждёт для phi, и скажите, какое значение в нём.

GF8: 8 bits as the spec lays them out, read from gf8.t27. Lesson 9 of the GoldenFloat course.
specs/numeric/gf8.t27
// SPDX-License-Identifier: Apache-2.0
// t27/specs/numeric/gf8.t27
// GoldenFloat8 — 8-bit φ-structured floating point
// NUMERIC-STANDARD-001 — Agent 3 (P1)
module GF8 {
// Import base format family
use numeric::goldenfloat_family;
use numeric::phi_ratio;
// ═════════════════════════════════════════════════════════════════
// 1. Format Definition
// ═════════════════════════════════════════════════════════════════════════
// GF8 bit layout: [S|EEE|MMMM]
// S: 1 bit (sign)
// E: 3 bits (exponent)
// M: 4 bits (mantissa)
const BITS : u8 = 8;
const SIGN_BITS : u8 = 1;
const EXP_BITS : u8 = 3;
const MANT_BITS : u8 = 4;
// Bias for exponent (2^(3-1) - 1 = 3)
const EXP_BIAS : u8 = 3;
// φ-ratio: exp/mant = 3/4 = 0.75
// φ-distance: math::constants::PHI_DISTANCE
const PHI_DISTANCE : f64 = 0.132;
// ═════════════════════════════════════════════════════════════════
// 2. GoldenFloat8 Type
// ═════════════════════════════════════════════════════════════════════════
struct GF8 {
raw : u8, // 8-bit raw value
}
// ═════════════════════════════════════════════════════════════════
// 3. Encoding/Decoding
// ═════════════════════════════════════════════════════════════════════════
// Encode f32 to GF8
fn encode(value: f32) -> GF8 {
if (value == 0.0) {
return GF8{ raw = 0 };
}
const sign = if (value < 0.0) { 1 } else { 0 };
const abs_val = if (value < 0.0) { -value } else { value };
// Extract exponent (unbiased)
const exp_unbiased = floor_log2(abs_val) as i8;
const exp_biased = (exp_unbiased + EXP_BIAS as i8) as u8;
// Clamp exponent
const exp_clamped = clamp(exp_biased, 0, (1 << EXP_BITS) - 1);
// Extract mantissa (4 bits)
const mant = extract_mantissa(abs_val, exp_unbiased, MANT_BITS);
return GF8{
raw = (sign << 7) | (exp_clamped << MANT_BITS) | mant
};
}
// Test: gf8_phi_distance invariant
test gf8_phi_distance
given phi = 1.618033988749894848 // math::sacred_physics::PHI
when gf8_phi = GF8::encode(phi)
then gf8_phi.raw == 0b01000000 // |S|EEE (phi = math::constants::PHI_DISTANCE)
// Decode GF8 to f32
fn decode(gf: GF8) -> f32 {
const sign = (gf.raw >> 7) as u8;
const exp_biased = ((gf.raw >> MANT_BITS) & 0x07) as u8;
const mant = (gf.raw & 0x0F) as u8;
// Zero
if (exp_biased == 0 && mant == 0) {
return 0.0;
}
// Exponent (with special case for subnormals)
const exp_unbiased = if (exp_biased == 0) {
-EXP_BIAS as i8 + 1
} else {
(exp_biased as i8) - EXP_BIAS as i8
};
// Mantissa (with implicit 1 for normalized, 0 for subnormal)
const mant_normalized = if (exp_biased == 0) {
(mant as f32) / 16.0
} else {
1.0 + (mant as f32) / 16.0
};
const value = mant_normalized * pow(2.0, exp_unbiased as f32);
if (sign != 0) {
return -value;
}
return value;
}
// ═════════════════════════════════════════════════════════════════
// 4. Format Properties
// ═════════════════════════════════════════════════════════════════════════
fn max_value() -> f32 {
// Max normalized: mant=1.9375, exp=3 → 15.5
const mant_max = 1.0 + 15.0 / 16.0;
const exp_max = (1 << EXP_BITS) - 1 - EXP_BIAS;
return mant_max * pow(2.0, exp_max as f32);
}
fn min_positive() -> f32 {
// Min subnormal: mant=1/16, exp=-2 → 0.0625
const mant_min = 1.0 / 16.0;
const exp_min = -EXP_BIAS as i8 + 1;
return mant_min * pow(2.0, exp_min as f32);
}
fn epsilon() -> f32 {
// Smallest representable difference at 1.0
return 1.0 / 16.0; // 0.0625
}
// ═════════════════════════════════════════════════════════════════
// 5. Validation
// ═════════════════════════════════════════════════════════════════════════
fn validate_format() -> bool {
const fmt = goldenfloat_family::get_format_by_name("GF8");
return (fmt != null) &&
(fmt.?.bits == BITS) &&
(fmt.?.exp_bits == EXP_BITS) &&
(fmt.?.mant_bits == MANT_BITS);
}
// ═════════════════════════════════════════════════════════════════
// 6. Use Cases
// ═════════════════════════════════════════════════════════════════════════
// GF8 is optimal for:
// - High compression (75% smaller than FP32)
// - Weight quantization for lightweight models
// - Activation caching
// - Intermediate feature maps
// Memory: 8 bits = 1 byte (4x FP32 in same space)
const MEMORY_RATIO_VS_FP32 : f32 = 8.0 / 32.0; // 0.25
// ═════════════════════════════════════════════════════════════════
// 7. Helper Functions
// ═════════════════════════════════════════════════════════════════════════
fn floor_log2(x: f32) -> i8 {
if (x <= 0.0) { return -128; }
let mut exp : i8 = 0;
while (x >= 2.0) {
x = x / 2.0;
exp = exp + 1;
}
while (x < 1.0) {
x = x * 2.0;
exp = exp - 1;
}
return exp;
}
fn extract_mantissa(value: f32, exp: i8, mant_bits: u8) -> u8 {
const normalized = value / pow(2.0, exp as f32);
const frac = normalized - 1.0;
const max_mant = (1 << mant_bits) - 1;
return (frac * (max_mant as f32 + 1.0)) as u8;
}
fn clamp(x: u8, min: u8, max: u8) -> u8 {
if (x < min) { return min; }
if (x > max) { return max; }
return x;
}
fn pow(base: f32, exp: f32) -> f32 {
// Efficient power function for GF8
// Integer exponent: binary exponentiation
// Fractional exponent: use logarithm approximation
if (base <= 0.0 || exp == 0.0) {
if (exp == 0.0) {
return 1.0;
}
if (base == 0.0 && exp > 0.0) {
return 0.0;
}
return 0.0 / 0.0; // NaN for negative base with non-integer exp
}
// Check if exponent is (approximately) integer
const is_integer = exp == floor(exp);
if (is_integer) {
// Binary exponentiation for integer exponents
let exp_int = exp as i32;
let mut result = 1.0;
let mut base_acc = base;
let mut e = exp_int;
if (e < 0) {
e = -e;
base_acc = 1.0 / base_acc;
}
while (e > 0) {
if (e % 2 == 1) {
result = result * base_acc;
}
base_acc = base_acc * base_acc;
e = e / 2;
}
return result;
}
// Fractional exponent: x^y = exp(y * ln(x))
const ln_val = ln_approx(base);
return exp_approx(exp * ln_val);
}
// Natural logarithm approximation
fn ln_approx(x: f32) -> f32 {
if (x <= 0.0) {
return 0.0 / 0.0; // NaN
}
if (x == 1.0) {
return 0.0;
}
// Series: ln(x) = 2 * ((x-1)/(x+1) + 1/3*((x-1)/(x+1))^3 + ...)
const t = (x - 1.0) / (x + 1.0);
const t2 = t * t;
const t3 = t2 * t;
const t5 = t3 * t2;
const t7 = t5 * t2;
return 2.0 * (t + t3 / 3.0 + t5 / 5.0 + t7 / 7.0);
}
// Exponential approximation
fn exp_approx(x: f32) -> f32 {
if (x == 0.0) {
return 1.0;
}
// Taylor series: e^x = 1 + x + x^2/2! + x^3/3! + ...
let mut result = 1.0;
let mut term = 1.0;
let mut exp_x = x;
// Scale down for large inputs to maintain accuracy
if (exp_x > 5.0 || exp_x < -5.0) {
const k = floor(exp_x / 5.0) as i32;
exp_x = exp_x - (k as f32) * 5.0;
}
for (i in 1..=8) {
term = term * exp_x / (i as f32);
result = result + term;
}
// Scale back if needed
if (x > 5.0 || x < -5.0) {
const k = floor(x / 5.0) as i32;
if (k > 0) {
for (i in 0..k) {
result = result * exp_approx(5.0);
}
} else if (k < 0) {
for (i in k..0) {
result = result / exp_approx(5.0);
}
}
}
return result;
}
// Floor function
fn floor(x: f32) -> f32 {
let xi = x as i32;
if (x >= 0.0 || x == xi as f32) {
return xi as f32;
}
return (xi - 1) as f32;
}
// ═══════════════════════════════════════════════════════════════════════════════════════════════════════
// TDD-Inside-Spec: Tests and Invariants for GF8
// ═══════════════════════════════════════════════════════════════════════════════════════════════════════
test gf8_decode_zero
given gf = GF8{ raw = 0 }
when value = decode(gf)
then value == 0.0
test gf8_encode_zero_roundtrip
given original = 0.0
and encoded = encode(original)
and decoded = decode(encoded)
then decoded == original
test gf8_decode_positive_value
given gf = GF8{ raw = 0b01000000 }
when value = decode(gf)
then value > 0.0
test gf8_decode_negative_value
given gf = GF8{ raw = 0b10000000 }
when value = decode(gf)
then value < 0.0
test gf8_bits_sum_correct
given total = SIGN_BITS + EXP_BITS + MANT_BITS
then total == BITS
test gf8_max_value_positive
given max_val = max_value()
then max_val > 0.0
test gf8_min_positive_greater_than_zero
given min_pos = min_positive()
then min_pos > 0.0
test gf8_epsilon_positive
given eps = epsilon()
then eps > 0.0
test gf8_memory_ratio_vs_fp32
given ratio = MEMORY_RATIO_VS_FP32
then ratio == 0.25
test gf8_validate_format_success
given valid = validate_format()
then valid == true
invariant gf8_bits_constant
assert BITS == 8
invariant gf8_sign_bits_is_one
assert SIGN_BITS == 1
invariant gf8_exp_bits_is_three
assert EXP_BITS == 3
invariant gf8_mant_bits_is_four
assert MANT_BITS == 4
invariant gf8_max_ge_min_positive
assert max_value() >= min_positive()
invariant gf8_phi_distance_within_tolerance
assert PHI_DISTANCE < 0.14
invariant gf8_exp_bias_positive
assert EXP_BIAS > 0
test gf8_pow_zero_exponent_returns_one
given result = pow(2.0, 0.0)
then abs(result - 1.0) < 1e-6
test gf8_pow_one_exponent_returns_base
given result = pow(5.0, 1.0)
then abs(result - 5.0) < 1e-6
test gf8_pow_positive_integer_exponent
given result = pow(2.0, 5.0)
and expected = 32.0
then abs(result - expected) < 1e-5
test gf8_pow_negative_integer_exponent
given result = pow(2.0, -3.0)
and expected = 0.125
then abs(result - expected) < 1e-5
test gf8_pow_fractional_exponent
given result = pow(4.0, 0.5)
and expected = 2.0
then abs(result - expected) < 1e-4
test gf8_pow_phi_squared
given phi = 1.6180339887498948 as f32
and result = pow(phi, 2.0)
and expected = phi * phi
then abs(result - expected) < 1e-5
test gf8_pow_zero_base_positive_exponent
given result = pow(0.0, 5.0)
then result == 0.0
test gf8_pow_one_base_any_exponent
given result1 = pow(1.0, 10.0)
and result2 = pow(1.0, -5.0)
then abs(result1 - 1.0) < 1e-6 and abs(result2 - 1.0) < 1e-6
test gf8_ln_approx_of_one
given result = ln_approx(1.0)
then abs(result) < 1e-6
test gf8_ln_approx_of_e
given e = 2.718281828459045 as f32
and result = ln_approx(e)
then abs(result - 1.0) < 0.01
test gf8_ln_approx_of_e_squared
given e = 2.718281828459045 as f32
and result = ln_approx(e * e)
then abs(result - 2.0) < 0.02
test gf8_ln_approx_negative_returns_nan
given result = ln_approx(-1.0)
then result != result // NaN check
test gf8_exp_approx_zero
given result = exp_approx(0.0)
then abs(result - 1.0) < 1e-6
test gf8_exp_approx_one
given e = 2.718281828459045 as f32
and result = exp_approx(1.0)
then abs(result - e) < 0.01
test gf8_exp_approx_negative
given result = exp_approx(-1.0)
and expected = 1.0 / 2.718281828459045 as f32
then abs(result - expected) < 0.01
test gf8_floor_positive
given result = floor(3.7)
then abs(result - 3.0) < 1e-6
test gf8_floor_negative
given result = floor(-3.2)
then abs(result - (-4.0)) < 1e-6
test gf8_floor_integer
given result = floor(5.0)
then abs(result - 5.0) < 1e-6
invariant gf8_pow_zero_exponent_identity
// For every positive x; checked at three points, one on each side of 1.
given x1 = 0.5
and x2 = 2.5
and x3 = 7.0
assert abs(pow(x1, 0.0) - 1.0) < 1e-6 and abs(pow(x2, 0.0) - 1.0) < 1e-6 and abs(pow(x3, 0.0) - 1.0) < 1e-6
invariant gf8_pow_one_exponent_identity
// For every valid x; checked at three points.
given x1 = 0.5
and x2 = 2.5
and x3 = 7.0
assert abs(pow(x1, 1.0) - x1) < 1e-5 and abs(pow(x2, 1.0) - x2) < 1e-5 and abs(pow(x3, 1.0) - x3) < 1e-5
invariant gf8_pow_multiply_exponents
given a = 2.0
and b = 3.0
assert abs(pow(pow(a, 2.0), b) - pow(a, 2.0 * b)) < 1e-5
invariant gf8_ln_exp_inversion
given x = 2.0
and y = ln_approx(x)
then abs(exp_approx(y) - x) < 0.01
invariant gf8_exp_ln_inversion
given x = 1.5
and y = exp_approx(x)
then abs(ln_approx(y) - x) < 0.01
invariant gf8_floor_returns_integer
// floor returns a whole number, and the floor of a whole number is itself.
given r1 = floor(3.7)
and r2 = floor(-3.2)
assert abs(floor(r1) - r1) < 1e-6 and abs(floor(r2) - r2) < 1e-6
invariant gf8_floor_monotonic
given x1 = 2.5
and x2 = 3.5
assert floor(x1) <= floor(x2)
bench gf8_pow_integer_exponent
measure: nanoseconds to compute pow(2.0, 10.0)
target: < 500ns
bench gf8_pow_fractional_exponent
measure: nanoseconds to compute pow(4.0, 0.5)
target: < 1000ns
bench gf8_ln_latency
measure: nanoseconds to compute ln_approx(2.0)
target: < 300ns
bench gf8_exp_latency
measure: nanoseconds to compute exp_approx(1.0)
target: < 500ns
bench gf8_floor_latency
measure: nanoseconds to compute floor(3.7)
target: < 50ns
bench gf8_encode_latency
measure: nanoseconds to encode(1.0)
target: < 100ns
bench gf8_decode_latency
measure: nanoseconds to decode(GF8{raw = 64})
target: < 50ns
// Bench: GF8 weight quantization (NN weights from N(0, 0.1))
bench gf8_weight_quantize
measure: nanoseconds to encode(0.1) and decode(GF8{raw = encode(0.1).raw})
target: < 200ns
// Invariant: GF8 phi_distance
invariant gf8_phi_distance
assert abs(PHI_DISTANCE - 0.132) < 0.001
// Rationale: exp/mant = 3/4 = 0.75, phi_distance = |0.75 - 0.618| = 0.132
}
Все уроки
Модуль 1 · Правило и его числа
Одно правило делит каждую ширину, отношение, к которому оно стремится, и числа Люка за тройкой 3.
Модуль 2 · Почему phi, почему три
Почему деление идёт по phi, почему основание три и как спека проверяет, что GF16 хранит phi.
Модуль 3 · Малые ступени: от GF4 до GF8
GF4, GF6 и GF8 — меньше всего битов, и округление до целых битов стоит здесь дороже всего.
Модуль 4 · От десяти до четырнадцати битов
GF10, GF12 и GF14 и то, как расстояние до 1 / phi меняется с ростом слова.
Модуль 5 · GF16 в работе
Основной 16-битный формат, скалярное произведение из двух слагаемых в GF-T16, затем GF20 и GF24.
Модуль 6 · От GF32 до GF64
GF32 рядом с IEEE single, GF48 без пары в IEEE, GF64 рядом с IEEE double.
Модуль 7 · От GF96 до GF256
GF96, GF128 и GF256, где спеки держат раскладку инвариантами.
Модуль 8 · Самые широкие ступени, затем триты
GF512 и GF1024, две самые широкие ступени, затем GF-T8, где порядок уходит в триты.
Модуль 9 · Ещё триты, затем декодирование
GF-T16 и GF-T32, затем почему фиксированные поля декодируются параллельно, а posit — нет.