1- # zig-golden-float
1+ # GoldenFloat
22
33[ ![ Zig] ( https://img.shields.io/badge/Zig-0.15+-F7A41D?logo=zig&logoColor=white )] ( https://ziglang.org/ )
4- [ ![ License] ( MIT )] ( LICENSE )
5- [ ![ Golden Ratio] ( https://img.shields.io/badge/φ-1.618033988-gold )] ( https://en.wikipedia.org/wiki/Golden_ratio )
6- [ ![ Trinity] ( https://img.shields.io/badge/Trinity-S³AI-purple )] ( https://github.com/gHashTag/trinity )
4+ [ ![ CI] ( https://github.com/gHashTag/zig-golden-float/actions/workflows/test-bindings.yml/badge.svg )] ( https://github.com/gHashTag/zig-golden-float/actions/workflows/test-bindings.yml )
5+ [ ![ License] ( https://img.shields.io/badge/License-MIT-blue.svg )] ( LICENSE )
6+ [ ![ Release] ( https://img.shields.io/github/v/release/gHashTag/zig-golden-float?label=release )] ( https://github.com/gHashTag/zig-golden-float/releases/latest )
7+ [ ![ Golden Ratio] ( https://img.shields.io/badge/%CF%86-1.618033988-gold )] ( https://en.wikipedia.org/wiki/Golden_ratio )
78
8- > ** Numerical core of the Trinity S³AI ecosystem ** — GoldenFloat16 (GF16), IEEE-754 fp16/bf16 codecs , ternary arithmetic, VSA, IGLA architecture , and φ-optimized FMA — all built on the golden ratio φ .
9+ > 16-bit floating point in base-φ with multi-format support, φ-optimized FMA , ternary arithmetic, VSA hypervectors , and unified JIT — the numerical core of the [ Trinity ] ( https://github.com/gHashTag/trinity ) ecosystem .
910
10- ## What is GoldenFloat?
11-
12- GoldenFloat is a family of floating-point formats where the mantissa/exponent ratio approximates φ (golden ratio = 1.618...). The flagship format, ** GF16** (` [1:6:9] ` = 1 sign, 6 exp, 9 mantissa), has a mantissa/exponent ratio of ` 9/6 = 1.5 ` , which deviates from φ by exactly ` α_φ = 0.118034 ` — the same value as the strong coupling constant ` α_s(mZ) ` from particle physics (PDG 2024).
13-
14- This three-way closure — ` {GF16 format, α_s coupling, LR_init} = α_φ ` — is the mathematical foundation of the IGLA-GF16 neural architecture.
11+ ---
1512
1613## Formats
1714
18- | Format | Layout | Bias | Range | φ-distance |
19- | --------| --------| ------| -------| ------------|
20- | ** GF16** | ` [s:1][e:6][m:9] ` | 31 | ±2.0×10⁹ | 0.049 (best) |
21- | ** fp16** | ` [s:1][e:5][m:10] ` | 15 | ±65504 | 0.118 |
22- | ** bf16** | ` [s:1][e:8][m:7] ` | 127 | ±3.4×10³⁸ | 0.525 |
23- | ** GF8** | ` [s:1][e:3][m:4] ` | 3 | ±4.24 | 0.132 |
24- | ** GFTernary** | ` {−1, 0, +1} ` | — | ±1 | 0.000 |
25- | ** fp32** | IEEE 754 binary32 | 127 | ±3.4×10³⁸ | 0.000 (baseline) |
15+ | Format | Layout | Bias | Range | Notes |
16+ | --------| --------| ------| -------| -------|
17+ | ** GF16** | ` [s:1][e:6][m:9] ` | 31 | ~ ±65504 | Golden ratio base, no subnormals |
18+ | ** fp16** | IEEE 754 binary16 | 15 | ±65504 | Full subnormal support |
19+ | ** bf16** | IEEE 754 brain16 | 127 | ~ ±3.4e38 | Canonical ` (bits +\| 0x7FFF) >> 16 ` encoder |
20+ | ** GF8** | ` [s:1][e:4][m:3] ` | 7 | ~ ±4.24 | Saturates outside φ³ |
21+ | ** GFTernary** | ` {-1, 0, +1} ` | — | ±1 | ±0.5 threshold, 100% sparse |
2622
27- All encode/decode functions use canonical IEEE-754 round-to-nearest-even semantics .
23+ All formats use ** round-to-nearest-even** via ` quantizeValue() ` dispatch .
2824
2925## Quick Start
3026
@@ -33,150 +29,119 @@ zig fetch --save https://github.com/gHashTag/zig-golden-float/archive/refs/tags/
3329```
3430
3531``` zig
36- const golden = @import("golden-float ");
32+ const gf = @import("golden_float ");
3733
38- // GF16 format
39- const gf = golden.formats.GF16.fromF32(3.14159);
40- const back = gf.toF32();
41-
42- // Quantize to any format
43- const q = golden.formats.quantizeValue(0.5, .gf16);
34+ const x = gf.GF16.fromF32(3.14);
35+ const y = gf.GF16.fromF32(2.71);
36+ const z = x.add(y);
37+ std.debug.print("{d}\n", .{z.toF32()}); // 5.85...
38+ ```
4439
45- // φ-optimized FMA
46- const result = golden.formats.phiFma(a, b, c);
40+ ## Architecture
4741
48- // Trinity constants
49- const phi = golden.trinity.PHI;
50- const alpha_phi = golden.trinity.ALPHA_PHI;
42+ ```
43+ src/
44+ ├── formats/ GF16, GF8, fp16, bf16, GFTernary codecs
45+ ├── math/ constants, transcendental (sin, cos, exp, log)
46+ ├── ternary/ HybridBigInt, packed trit storage
47+ ├── vsa/ core, HRR, 10K-dim hypervectors, FPGA bind
48+ ├── vm/ stack interpreter, ARM64 & x86_64 JIT
49+ ├── c_abi.zig FFI layer → libgoldenfloat.{so,dylib,dll}
50+ └── root.zig public API
5151```
5252
53- ## C-ABI
53+ ## Language Bindings
5454
55- Build the shared library:
55+ | Language | Path | Status |
56+ | ----------| ------| --------|
57+ | ** Zig** | ` src/ ` | Native |
58+ | ** C/C++** | ` src/c/gf16.h ` + ` cpp/ ` | C-ABI + header-only wrapper |
59+ | ** Rust** | ` rust/goldenfloat-sys/ ` | FFI crate |
60+ | ** Python** | ` python/goldenfloat/ ` | ctypes bridge |
61+ | ** Go** | ` go/goldenfloat/ ` | cgo wrapper |
5662
57- ``` bash
58- zig build shared # produces libgoldenfloat.{so,dylib,dll}
59- ```
63+ ### Building & Testing
6064
61- ``` c
62- #include < gf16.h>
65+ ``` bash
66+ # Build shared library (required for bindings)
67+ zig build shared
6368
64- gf16_t a = gf16_from_f32(3 .14f );
65- gf16_t b = gf16_from_f32(2 .71f );
66- gf16_t sum = gf16_add(a, b);
67- float result = gf16_to_f32(sum);
68- ```
69+ # Run Zig tests
70+ zig build test
6971
70- ## Language Bindings
72+ # Test all bindings
73+ ./scripts/test_bindings.sh
7174
72- | Language | Package | Status |
73- | ---------- | --------- | -------- |
74- | C/C++ | ` src/c/gf16.h ` | Stable |
75- | Rust | ` rust/goldenfloat-sys ` | Stable |
76- | Python | ` python /goldenfloat` | ctypes bridge |
77- | Go | ` go/goldenfloat ` | cgo bridge |
75+ # Individual bindings
76+ cd rust/goldenfloat-sys && cargo test
77+ cd python && python -m goldenfloat.tests.test_gf16
78+ cd cpp && cmake -S . -B build && cmake --build build && ./build/test_gf16
79+ cd go /goldenfloat && go test -v ./...
80+ ```
7881
79- ## Architecture
82+ ## φ-Optimized FMA
8083
81- ```
82- src/
83- ├── formats/
84- │ ├── golden_float16.zig GF16 core, FMA, φ-ops
85- │ ├── formats_root.zig Unified quantize/encode/decode (GF16/fp16/bf16/ternary)
86- │ └── gf8.zig GF8 format + verification tests
87- ├── math/
88- │ ├── constants.zig φ, e, π sacred constants
89- │ └── transcendental.zig sin, cos, exp, log (from .tri specs)
90- ├── trinity_constants.zig IGLA architecture dimensions (Fibonacci-derived)
91- ├── phi_attention.zig φ-Sparse Attention with Fibonacci CA-mask
92- ├── trinity_init.zig Trinity Weight Initialization (4 physics sectors)
93- ├── phi_schedule.zig φ-LR Schedule (warmup + φ-decay)
94- ├── jepa_t.zig JEPA-T Predictor (latent-space prediction)
95- ├── ternary/
96- │ ├── hybrid.zig HybridBigInt engine
97- │ └── packed_trit.zig Packed trit storage
98- ├── vsa/
99- │ ├── core.zig VSA bind/bundle/similarity
100- │ ├── hrr.zig Holographic Reduced Representations
101- │ ├── 10k_vsa.zig 10K-dimensional hypervectors
102- │ └── concurrency.zig Lock-free VSA data structures
103- ├── vm/
104- │ ├── vm.zig Stack-based VM interpreter
105- │ ├── jit_unified.zig Unified JIT (ARM64 + x86_64)
106- │ └── opcodes.zig Opcode definitions
107- └── c_abi.zig C-ABI shared library exports
108-
109- benches/
110- ├── bench_007b_extended_range.rs φ-distance extended range benchmark
111- ├── bench_008_fashion_mnist.zig Fashion-MNIST MLP quantization
112- ├── bench_009_transformer_attention.zig Transformer attention φ-patterns
113- └── igla_gf16_bench.zig IGLA architecture verification proofs
84+ ``` c
85+ // Standard
86+ gf16_fma (a, b, c); // a×b + c
87+ gf16_fms(a, b, c); // a×b - c
88+ gf16_fnma(a, b, c); // -(a×b) + c
89+
90+ // φ-weighted
91+ gf16_phi_fma(a, b, c); // (a×b)×φ + c×φ⁻¹
92+ gf16_phi_dot(n, a, b); // φ-weighted dot product
11493```
11594
11695## IGLA-GF16 Architecture
11796
118- IGLA (Intelligent Golden-ratio Language Architecture) is a 16MB language model where every hyperparameter derives from Trinity φ-algebra :
97+ Neural network architecture built on φ-math :
11998
120- ```
121- d_model = 144 (Fib(12))
122- n_heads = 8 (Fib(6))
123- d_head = 18 (144/8)
124- d_ffn = 233 (Fib(13) ≈ 144×φ)
125- n_layers = 7 (16MB budget)
126- TOTAL ≈ 15.8MB in GF16
127- ```
128-
129- | Module | File | Description |
130- | --------| ------| -------------|
131- | 1. Trinity Constants | ` trinity_constants.zig ` | φ, α_φ, Fibonacci sequence, model dims |
132- | 3. φ-Sparse Attention | ` phi_attention.zig ` | Fibonacci distance mask, φ-scale factor |
133- | 4. Trinity Weight Init | ` trinity_init.zig ` | 4 physics sectors (gauge/higgs/lepton/cosmology) |
134- | 5. φ-LR Schedule | ` phi_schedule.zig ` | Warmup over Fib(7)=21 steps, φ-decay |
135- | 6. JEPA-T Predictor | ` jepa_t.zig ` | Encoder 6 + Predictor 3 = φ-split |
136- | 7. Benchmarks | ` igla_gf16_bench.zig ` | 5 whitepaper proofs |
99+ | Module | Description |
100+ |--------|-------------|
101+ | Trinity Constants | φ, α_φ, Fibonacci dimensions |
102+ | φ-Sparse Attention | Fibonacci distance mask `{1,2,3,5,8,13,21,34,55,89,144}` — 2.15% sparsity |
103+ | Trinity Weight Init | 4 physics sectors: gauge / higgs / lepton / cosmology |
104+ | φ-LR Schedule | Warmup Fib(7)=21 steps, φ-decay |
105+ | JEPA-T Predictor | Encoder 6 + Predictor 3 layers, φ-split |
137106
138107## Benchmarks
139108
140- Benchmarks run on 10,000 samples across 6 distributions. Results stored in ` .trinity/results/ ` .
109+ | Metric | Result |
110+ |--------|--------|
111+ | GF16 accuracy vs fp32 (σ=1.0) | > 99.99% |
112+ | GF16 vs bf16 MSE ratio (uniform ±100) | 16.2× better |
113+ | GF16 sparsity at [-10,10] | 0% (no saturation) |
114+ | GFTernary sparsity (He init σ=0.05) | 100% |
115+ | Pearson r(φ-distance, MSE) | −0.42 |
141116
142- ** BENCH-007b ** — Extended range [ -10, 10 ] (10,000 samples):
117+ Full results in `.trinity/results/` and benches under `benches/`.
143118
144- | Format | MSE | MaxAbsErr | InRange |
145- | --------| -----| -----------| ---------|
146- | fp32 | 0.000000 | 0.000000 | Yes |
147- | GF16 | 0.000520 | 0.072897 | Yes |
148- | bf16 | 0.000410 | 0.062475 | Yes |
149- | fp16 | 0.000006 | 0.007809 | Yes |
150- | GF8 | 6.390662 | 5.763932 | ** CLIP** |
119+ ## C-ABI
151120
152- ** BENCH-010** — Key findings:
153- - H1 CONFIRMED: bf16/gf16 diverge on Uniform [ -100, +100]
154- - H2 FAILED: σ=0.1 collision was genuine bf16 encoder bug (now fixed)
155- - GF16 outperforms bf16 by 16× on all distributions
121+ ```c
122+ #include "gf16.h"
156123
157- ## Build
124+ gf16_t a = gf16_from_f32(3.14f);
125+ gf16_t b = gf16_from_f32(2.71f);
126+ gf16_t c = gf16_add(a, b);
127+ printf("%.6f\n", gf16_to_f32(c));
158128
159- ``` bash
160- zig build # build library module
161- zig build test # run all tests
162- zig build shared # build C-ABI shared library
163- zig build c-abi-test # test C-ABI layer
164- zig build gen # generate code from .tri specs
129+ double phi = goldenfloat_phi(); // 1.6180339887...
130+ double trinity = goldenfloat_trinity(); // φ² + φ⁻² = 3
165131```
166132
167133## Ecosystem
168134
169- Core dependency for:
170135- [ zig-sacred-geometry] ( https://github.com/gHashTag/zig-sacred-geometry )
171136- [ zig-physics] ( https://github.com/gHashTag/zig-physics )
172137- [ zig-hdc] ( https://github.com/gHashTag/zig-hdc )
173138- [ trinity-training] ( https://github.com/gHashTag/trinity-training )
174139- [ trinity] ( https://github.com/gHashTag/trinity )
175140
176- ## Changelog
141+ ## Version
177142
178- See [ CHANGELOG.md] ( CHANGELOG.md ) for release history.
143+ ** 2.0.0 ** — see [ CHANGELOG.md] ( CHANGELOG.md ) for release history.
179144
180145## License
181146
182- MIT (c) 2026 gHashTag
147+ [ MIT] ( LICENSE ) © gHashTag
0 commit comments