pub const SAFE_DIV: f64 = 1e-300;
pub const SAFE_LOG: f64 = 1e-300;
pub const SAFE_SQRT: f64 = 0.0;
pub const UNDERFLOW_GUARD: f64 = -700.0;
pub const SSA_FLOOR: f64 = 1e-30;
pub const SAFE_DIV_F32: f32 = 1e-38;
pub const SAFE_LOG_F32: f32 = 1e-38;
pub const UNDERFLOW_GUARD_F32: f32 = -87.0;
pub const SSA_FLOOR_F32: f32 = 1e-15;
pub const WGSL_PREAMBLE: &str = "\
// eps:: precision guards (f64) — groundSpring V76 absorption
const EPS_SAFE_DIV: f64 = f64(1e-300);
const EPS_SAFE_LOG: f64 = f64(1e-300);
const EPS_SAFE_SQRT: f64 = f64(0.0);
const EPS_UNDERFLOW_GUARD: f64 = f64(-700.0);
const EPS_SSA_FLOOR: f64 = f64(1e-30);
";
pub const WGSL_PREAMBLE_F32: &str = "\
// eps:: precision guards (f32) — groundSpring V76 absorption
const EPS_SAFE_DIV: f32 = 1e-38;
const EPS_SAFE_LOG: f32 = 1e-38;
const EPS_SAFE_SQRT: f32 = 0.0;
const EPS_UNDERFLOW_GUARD: f32 = -87.0;
const EPS_SSA_FLOOR: f32 = 1e-15;
";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn safe_div_prevents_nan() {
let a = 1.0_f64;
let b = 0.0_f64;
let result = a / b.max(SAFE_DIV);
assert!(result.is_finite());
}
#[test]
fn safe_log_prevents_neg_inf() {
let x = 0.0_f64;
let result = x.max(SAFE_LOG).ln();
assert!(result.is_finite());
}
#[test]
fn underflow_guard_prevents_denorm() {
let x = -800.0_f64;
let result = x.max(UNDERFLOW_GUARD).exp();
assert!(result >= 0.0);
}
#[test]
fn wgsl_preamble_contains_all_constants() {
assert!(WGSL_PREAMBLE.contains("EPS_SAFE_DIV"));
assert!(WGSL_PREAMBLE.contains("EPS_SAFE_LOG"));
assert!(WGSL_PREAMBLE.contains("EPS_SAFE_SQRT"));
assert!(WGSL_PREAMBLE.contains("EPS_UNDERFLOW_GUARD"));
assert!(WGSL_PREAMBLE.contains("EPS_SSA_FLOOR"));
}
#[test]
fn f32_preamble_uses_f32_values() {
assert!(WGSL_PREAMBLE_F32.contains("f32"));
assert!(!WGSL_PREAMBLE_F32.contains("f64"));
}
}