nx_f32_log.nx
buildroot/runtime/nx_f32_log.nx
about
nx_f32_log.nx -- IEEE 754 binary32 natural-log log(x) bits-up.
L6 of the bits-up numeric tower. Composes L4 mul/add/sub/div/cvt
to produce real-valued log(x) without libm.
Algorithm (clean-room from cited refs):
1. Special cases per IEEE 754:
log(NaN) = NaN
log(+0) = -inf
log(-0) = -inf
log(neg) = NaN
log(+inf) = +inf
log(1.0) = +0 (exact)
2. Range reduction: x = 2^k * m, m in [1, 2).
k = f32_exp_field(x) - 127
m = same bits with exp field set to 127 (interpret as 1.frac)
Then log(x) = k * ln(2) + log(m).
3. Polynomial for log(m) on m in [1, 2):
u = (m - 1) / (m + 1), u in [0, 1/3]
log(m) = 2 * atanh(u)
~= 2 * u * (1 + u^2/3 + u^4/5 + u^6/7 + u^8/9)
The atanh form converges rapidly on this range (much better
than direct Taylor of log(1+t) which is slow near t=1).
4. Combine: log(x) = k * ln(2) + 2*u*P(u^2)
where P(u^2) = 1 + u^2/3 + u^4/5 + u^6/7 + u^8/9 (Horner).
References absorbed clean-room (no code borrowed):
Hart 1968, "Computer Approximations"
Cody+Waite 1980, "Software Manual for Elementary Functions"
Muller 2016, "Elementary Functions"
v1 accuracy: ~50-500 ULPs on f32 (atanh polynomial truncation +
accumulator rounding). Adequate for ML loss + entropy math;
v2 Remez tightening for sub-100 ULPs.
genealogy_id: standard_log_range_reduction + atanh_form_polynomial
dependencies 5 imports · 5 importers
imports: nx_syscalls.nxnx_tier.nxnx_f32.nxnx_f32_div.nxnx_f32_cvt.nx
imported by: nx_autograd_tensor.nxnx_f32_log_test.nxnx_tgrad_core.nxnx_vcodec_neural_entropy_gate.nxnx_vcodec_stdseq_gate.nx
structs
| none |
consts
| 50 | const NX_F32_LOG_ONE: i64 = 0x3F800000 // 1.0 |
| 51 | const NX_F32_LOG_TWO: i64 = 0x40000000 // 2.0 |
| 52 | const NX_F32_LOG_LN2: i64 = 0x3F317218 // ln(2) = 0.6931472 |
| 53 | const NX_F32_LOG_NEG_INF: i64 = 0xFF800000 // -inf |
| 55 | const NX_F32_LOG_INV_3: i64 = 0x3EAAAAAB // 1/3 |
| 56 | const NX_F32_LOG_INV_5: i64 = 0x3E4CCCCD // 1/5 |
| 57 | const NX_F32_LOG_INV_7: i64 = 0x3E124925 // 1/7 ~= 0.14285715 |
| 58 | const NX_F32_LOG_INV_9: i64 = 0x3DE38E39 // 1/9 ~= 0.11111111 |
functions
| 60 | func nx_f32_log(x: i64) -> i64 |