From e128fede28376da7eb174cd6d515e3d0dadde6ec Mon Sep 17 00:00:00 2001 From: chasse20 Date: Wed, 18 Nov 2026 10:25:54 -0500 Subject: [PATCH] Match the natural exponential and all measured shared-lookup ridged octaves --- .../nodes/src/function/binary_exponential.rs | 19 +++++++++++++++++-- crates/nodes/src/function/tests.rs | 16 ++++++++++++++++ docs/parity-fixes.md | 19 +++++++++++++++++++ docs/sbs-root-nodes.md | 4 ++-- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/crates/nodes/src/function/binary_exponential.rs b/crates/nodes/src/function/binary_exponential.rs index 266f29a..a392643 100644 --- a/crates/nodes/src/function/binary_exponential.rs +++ b/crates/nodes/src/function/binary_exponential.rs @@ -57,8 +57,23 @@ pub(super) fn ln(value: f32) -> f32 { } pub(super) fn exp(value: f32) -> f32 { - let result = value.exp(); - if result.is_nan() || result.is_subnormal() { 0.0 } else { result } + if value < -88.0 || value.is_nan() { + return 0.0; + } + if value > 89.0 { + return f32::INFINITY; + } + const LOG_HIGH: f32 = std::f32::consts::LN_2; + const LOG_LOW: f32 = (LN_2 - LOG_HIGH as f64) as f32; + let exponent = (value * std::f32::consts::LOG2_E).floor(); + let remainder = (-exponent).mul_add(LOG_LOW, (-exponent).mul_add(LOG_HIGH, value)); + // A seventh-degree fit on [0, ln(2)]. Each multiply-add rounds exactly once. + let coefficients = [0.001_304_379_3, 0.008_378_831, 0.041_653_905, 0.166_668_42, 0.499_999_9, 1.0, 1.0]; + let significand = coefficients.into_iter().fold(0.000_275_553_82_f32, |p, c| p.mul_add(remainder, c)); + // The scale itself can be 2^128 while the product still fits in float32. + let scale = f64::from_bits(((exponent as i32 + 1023) as u64) << 52); + let result = (f64::from(significand) * scale) as f32; + if result.is_subnormal() { 0.0 } else { result } } pub(super) fn power(base: f32, exponent: f32) -> f32 { diff --git a/crates/nodes/src/function/tests.rs b/crates/nodes/src/function/tests.rs index ae770ae..c59d48c 100644 --- a/crates/nodes/src/function/tests.rs +++ b/crates/nodes/src/function/tests.rs @@ -48,6 +48,22 @@ fn scoped_operation(scope: super::FunctionScope, op: &str, operands: &[Value], l compile(&graph, 0).unwrap().evaluate_pixel(&context, &mut EvaluationState::default()).unwrap() } +#[test] +fn runtime_pixel_natural_exponential_reduces_to_a_positive_interval() { + for (input, expected) in [ + (2.0, 0x40ec7325), + (f32::from_bits(0xbf317219), 0x3effffff), + (f32::from_bits(0xc2aeac4f), 0x00800026), + (f32::from_bits(0xc2aeac50), 0), + (f32::from_bits(0x42b17217), 0x7f7fff84), + (f32::from_bits(0x42b17218), 0x7f800000), + ] { + let Value::Float(value) = pixel_operation("exp", &[Value::Float(input)], false) else { panic!("expected a scalar") }; + assert_eq!(value.to_bits(), expected, "exp({input})"); + } + assert_eq!(pixel_operation("pow", &[Value::Float(0.7), Value::Float(4.0)], false), Value::Float(f32::from_bits(0x3e75dccb))); +} + #[test] fn runtime_pixel_power_uses_the_natural_logarithm_and_the_sign_of_integer_powers() { for (base, exponent, expected) in [ diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 5cd44e3..a28c496 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -247,6 +247,25 @@ Literal-only pixel graphs at 16², seed 23, give 512/768 exact pixels for each l The shared-lookup ridged root still matches 5,160/16,384 at changed roughness in F32, maximum 1.78813934e−7. Its one-octave .7 control still matches 1,847/4,096, maximum 2.38418579e−7. For positive normal x on the signed field, the reference's general power agrees with exp(y×ln(x)) on all 32,614 pixels. On .7 with exponent 1 it returns `3f333334`. Further work on general power and natural exponential is tracked with the [octave controls](#ridged-noise-octave-isolation). +### Natural exponential and ridged noise + +Ridged noise with the [original shared lookup](#basic-and-ridged-3d-perlin) matches **253,184/253,184 pixels across 29 cases**, maximum error zero. The seven original cases contribute 163,840 pixels, the [thirteen octave controls](#ridged-noise-octave-isolation) contribute 53,248, and the nine cases below contribute 36,096. The octave with roughness .7 and levels 1–4 now matches all 4,096 LUM32F pixels. The runtime power of .7 to the fourth has bits `3e75dccb` in both apps. + +| Position dimensions | Seed | Levels | Roughness | Stored outputs | Exact / total | +|---|---:|---|---:|---|---:| +| 16² | 41 | 1–4 | .3125 | F32 | 256/256 | +| 64² | 71 | 1–4 | .7 | U8, U16, F16, F32 | 4,096/4,096 each | +| 32² | 137 | 2–6 | .61 | F32 | 1,024/1,024 | +| 32² | 211 | 3 only | .825 | F32 | 1,024/1,024 | +| 128² | 311 | 1–7 | .375 | F32 | 16,384/16,384 | +| 32², signed position | 42 | 1–5 | .82 | F32 | 1,024/1,024 | + +For the first five rows, use opaque RGBA8 positions with RGB equal to `(7x+13y+3xy, 29x−5y+39, 11y+17x+61)` modulo 256. The signed field uses opaque RGBA32F. Set RGB to `(((17x+3y) mod 67)−9, ((11y−3x) mod 73)−11, ((5xy+19) mod 79)−13)/63`. With each app's own shipped lookup, the seed-zero position ramp at 128² and 256² gives **176/16,384 (maximum 139 levels), 768/65,536 (maximum 154 levels)** for ridged noise. Basic Perlin gives 173/16,384 (maximum 203 levels), 663/65,536 (maximum 203 levels) on the same two ramps. Both roots remain open. + +The runtime natural exponential reduces the argument by an integral multiple of ln(2) using two fused subtractions, then evaluates a seventh-degree polynomial on the reduced interval. At pixel index `i` in eight consecutive 1024² fields numbered `j=0..7`, set `t=(j×2^20+i)/2^23`. Both exp(t) and exp(−t) match at every index, **16,777,216/16,777,216 raw float components**, maximum error zero. The two separate square-root controls in those fields match 13,947,413/16,777,216, maximum 1.19209290e−7. + +Ten 256² single-channel exponential fields match **655,360/655,360 LUM32F pixels**. Seven use the signed, exceptional, wide-exponent, near-one, fractional, power-of-two and roughness inputs under [runtime pixel logarithms and powers of two](#runtime-pixel-logarithms-and-powers-of-two). For the last three, use a nearest-sampled linear float input at seed 163. Draw 65,536 float32 values uniformly on [−90,90] or [0,ln(2)] with NumPy PCG64 seeds 6841 and 6842, respectively, casting to float32 after drawing. For the boundary field, round `(i mod 257−128)×ln(2)` to float32 and add `floor(i/257) mod 25−12` to its unsigned bit pattern. General power matches **458,752/458,752** pixels on the seven paired-input fields. All finite maximum errors are zero. Counts include bitwise tests for signed zero and nonfinite results. + ### General pixel power and ridged noise Using the original 1024×16 lookup in both apps, all seven [ridged noise comparisons](#basic-and-ridged-3d-perlin) now match **163,840/163,840 pixels**, maximum error zero. This includes changed roughness .7 at 128² in LUM8, LUM16, LUM16F and LUM32F. The [thirteen octave controls](#ridged-noise-octave-isolation) improve from 29,604/53,248 to 51,030/53,248. Twelve match every pixel. With roughness .7 and levels 1–4 the result is 1,878/4,096, maximum 1.19209290e−7. The earlier stock-lookup difference remains independently reproducible. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index a02c6dd..39d0a2d 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -46,9 +46,9 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | SBS package / graph path | Input pins | Hidden in library | Parity status | |---|---:|---|---| | [3d_gradient.sbs/3d_linear_gradient](../assets/sbs/3d_gradient.sbs) | 1 | No | [Parity · 12 wired and 5 disconnected cases, four formats](parity-fixes.md#runtime-reciprocal-and-3d-gradient-contrast) | -| [3d_perlin_noise.sbs/3D_perlin_noise](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · shared lookup exact across seven cases, stock lookup differs](parity-fixes.md#pixel-processor-products-and-sums) | +| [3d_perlin_noise.sbs/3D_perlin_noise](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · 7 shared-lookup cases exact, stock lookup differs](parity-fixes.md#natural-exponential-and-ridged-noise) | | [3d_perlin_noise.sbs/3D_perlin_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | -| [3d_perlin_noise.sbs/3D_ridged_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · 7 shared-lookup cases exact, 1 of 13 octave controls differs](parity-fixes.md#general-pixel-power-and-ridged-noise) | +| [3d_perlin_noise.sbs/3D_ridged_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · 29 shared-lookup cases exact, stock lookup differs](parity-fixes.md#natural-exponential-and-ridged-noise) | | [3d_planar_projection.sbs/3d_planar_projection](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [Parity · 15 projection cases, 168,960 exact pixels](parity-fixes.md#uniform-angles-and-planar-projection) | | [3d_planar_projection.sbs/3d_planar_projection_color](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [Parity · 15 projection cases, 168,960 exact pixels](parity-fixes.md#uniform-angles-and-planar-projection) | | [3d_simplex_noise.sbs/3d_simplex_noise](../assets/sbs/3d_simplex_noise.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |