From 25fca052f5b55c8d3fc4aef1e71dc7d0ab751cdd Mon Sep 17 00:00:00 2001 From: Zachary Levy Date: Sun, 29 Mar 2026 20:29:24 -0700 Subject: [PATCH] Switch from custom LN_2 constant to math.LN2 --- .zed/tasks.json | 5 +++++ levmath/levmath.odin | 9 ++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.zed/tasks.json b/.zed/tasks.json index 63f392a..d49b77d 100644 --- a/.zed/tasks.json +++ b/.zed/tasks.json @@ -22,6 +22,11 @@ "command": "odin test levsync -out=out/debug/test_levsync", "cwd": "$ZED_WORKTREE_ROOT" }, + { + "label": "Test levmath", + "command": "odin test levmath -out=out/debug/test_levmath", + "cwd": "$ZED_WORKTREE_ROOT" + }, // --------------------------------------------------------------------------------------------------------------------- // ----- LMDB Examples ------------------------ // --------------------------------------------------------------------------------------------------------------------- diff --git a/levmath/levmath.odin b/levmath/levmath.odin index cbd6de3..b9ddaa6 100644 --- a/levmath/levmath.odin +++ b/levmath/levmath.odin @@ -34,14 +34,13 @@ import "core:testing" // Neural Computation 11, 853–862 (1999). // J. Rade, FastExp.h (2021), https://gist.github.com/jrade/293a73f89dfef51da6522428c857802d fast_exp :: #force_inline proc "contextless" (x: $FLOAT) -> FLOAT where intrinsics.type_is_float(FLOAT) { - LN_2 :: 0.6931471805599453 CORRECTION :: 0.04367744890362246 when FLOAT == f16 { MANTISSA_BITS :: 10 EXPONENT_BIAS :: 15 SHIFT :: f16(1 << MANTISSA_BITS) - SCALE :: SHIFT / LN_2 + SCALE :: SHIFT / math.LN2 BIAS :: SHIFT * f16(EXPONENT_BIAS - CORRECTION) MAX_Y :: SHIFT * (2 * EXPONENT_BIAS + 1) @@ -52,7 +51,7 @@ fast_exp :: #force_inline proc "contextless" (x: $FLOAT) -> FLOAT where intrinsi MANTISSA_BITS :: 23 EXPONENT_BIAS :: 127 SHIFT :: f32(1 << MANTISSA_BITS) - SCALE :: SHIFT / LN_2 + SCALE :: SHIFT / math.LN2 BIAS :: SHIFT * f32(EXPONENT_BIAS - CORRECTION) MAX_Y :: SHIFT * (2 * EXPONENT_BIAS + 1) @@ -63,7 +62,7 @@ fast_exp :: #force_inline proc "contextless" (x: $FLOAT) -> FLOAT where intrinsi MANTISSA_BITS :: 52 EXPONENT_BIAS :: 1023 SHIFT :: f64(1 << MANTISSA_BITS) - SCALE :: SHIFT / LN_2 + SCALE :: SHIFT / math.LN2 BIAS :: SHIFT * f64(EXPONENT_BIAS - CORRECTION) MAX_Y :: SHIFT * (2 * EXPONENT_BIAS + 1) @@ -174,4 +173,4 @@ test_fast_exp_saturation :: proc(t: ^testing.T) { ov64 := fast_exp(f64(1e5)) testing.expectf(t, math.is_inf_f32(ov32), "f32 overflow: got %v, want +inf", ov32) testing.expectf(t, math.is_inf_f64(ov64), "f64 overflow: got %v, want +inf", ov64) -} \ No newline at end of file +}