code wiki / (root) / nx_f32prim_test.nx

nx_f32prim_test.nx source

↩ module page · 34 lines · 1454 B

1// nx_f32prim_test.nx -- sanity-check the f32 primitives the SDPA organ newly uses (exp, sqrt, lt-on-negs). 2// license_tier: ORIGINAL 3import "nx_syscalls.nx" 4import "nx_f32.nx" 5import "nx_f32_div.nx" 6import "nx_f32_cvt.nx" 7import "nx_f32_exp.nx" 8 9func pt_close(a: i64, e: i64, tol: i64) -> i64 { 10 if (nx_f32_sub(a, e) & 0x7FFFFFFF) < tol { return 1 } 11 return 0 12} 13 14func main() -> i64 { 15 let tol: i64 = nx_f32_div(nx_i32_to_f32(5), nx_i32_to_f32(100)) // 0.05 16 // exp(0) == 1 17 if pt_close(nx_f32_exp(0), nx_i32_to_f32(1), tol) != 1 { return 1 } 18 // exp(1) ~= 2.71828 19 let e1: i64 = nx_f32_div(nx_i32_to_f32(271828), nx_i32_to_f32(100000)) 20 if pt_close(nx_f32_exp(nx_i32_to_f32(1)), e1, tol) != 1 { return 2 } 21 // exp(-1) ~= 0.3679 22 let em1: i64 = nx_f32_div(nx_i32_to_f32(3679), nx_i32_to_f32(10000)) 23 if pt_close(nx_f32_exp(nx_f32_neg(nx_i32_to_f32(1))), em1, tol) != 1 { return 3 } 24 // sqrt(4) == 2 ; sqrt(2) ~= 1.4142 25 if pt_close(nx_f32_sqrt(nx_i32_to_f32(4)), nx_i32_to_f32(2), tol) != 1 { return 4 } 26 let s2: i64 = nx_f32_div(nx_i32_to_f32(14142), nx_i32_to_f32(10000)) 27 if pt_close(nx_f32_sqrt(nx_i32_to_f32(2)), s2, tol) != 1 { return 5 } 28 // lt on negatives: -2 < -1 true ; -1 < -2 false 29 let neg2: i64 = nx_f32_neg(nx_i32_to_f32(2)) 30 let neg1: i64 = nx_f32_neg(nx_i32_to_f32(1)) 31 if nx_f32_lt(neg2, neg1) != 1 { return 6 } 32 if nx_f32_lt(neg1, neg2) != 0 { return 7 } 33 return 0 34}