code wiki / (root) / nx_f32_sincos_test.nx

nx_f32_sincos_test.nx source

↩ module page · 50 lines · 1653 B

1// nx_f32_sincos_test.nx -- smoke for nx_f32_sincos.nx. 2 3import "nx_syscalls.nx" 4import "nx_tier.nx" 5import "nx_f32.nx" 6import "nx_f32_cvt.nx" 7import "nx_f32_sincos.nx" 8 9func _ulp_diff_pos(a: i64, b: i64) -> i64 { 10 if a >= b { return a - b } 11 return b - a 12} 13 14func main() -> i64 { 15 // sin(0) = 0 exact 16 if nx_f32_sin(0x00000000) != 0x00000000 { return 10 } 17 18 // cos(0) = 1.0 exact 19 if nx_f32_cos(0x00000000) != 0x3F800000 { return 11 } 20 21 // sin(pi/2) ~= 1.0 = 0x3F800000 22 let r1: i64 = nx_f32_sin(0x3FC90FDB) 23 if _ulp_diff_pos(r1, 0x3F800000) > 16384 { return 20 } 24 25 // cos(pi/2) ~= 0.0 (might be small but nonzero due to truncation) 26 // We accept abs(result) < 1e-3 (= ~8388 ULPs near 0) 27 let r2: i64 = nx_f32_cos(0x3FC90FDB) 28 let abs_r2: i64 = r2 & 0x7FFFFFFF 29 // 1e-3 in f32 = 0x3A83126F 30 if abs_r2 > 0x3A83126F { return 21 } 31 32 // sin(pi/4) ~= sqrt(2)/2 ~= 0.7071068 = 0x3F3504F3 33 let r3: i64 = nx_f32_sin(0x3F490FDB) 34 if _ulp_diff_pos(r3, 0x3F3504F3) > 16384 { return 30 } 35 36 // cos(pi/4) ~= 0.7071068 = 0x3F3504F3 (same value, different formula) 37 let r4: i64 = nx_f32_cos(0x3F490FDB) 38 if _ulp_diff_pos(r4, 0x3F3504F3) > 16384 { return 31 } 39 40 // sin(pi/6) ~= 0.5 = 0x3F000000. pi/6 = 0.523598775 = 0x3F060A92 41 let r5: i64 = nx_f32_sin(0x3F060A92) 42 if _ulp_diff_pos(r5, 0x3F000000) > 16384 { return 40 } 43 44 // Special cases 45 if nx_f32_is_nan(nx_f32_sin(0x7F800000)) != 1 { return 50 } // sin(+inf) = NaN 46 if nx_f32_is_nan(nx_f32_cos(0x7F800000)) != 1 { return 51 } 47 if nx_f32_is_nan(nx_f32_sin(0x7FC00000)) != 1 { return 52 } // sin(NaN) = NaN 48 49 return 0 50}