nx_f16_test.nx source
↩ module page · 88 lines · 4275 B
1// nx_f16_test.nx -- smoke + IEEE 754 conformance for nx_f16.nx.
2//
3// Tests cover:
4// A) Verdict + classification gates
5// B) Sign manipulation (neg, abs)
6// C) IEEE 754 equality (NaN != NaN, +0 == -0)
7// D) Multiplication on canonical bit patterns:
8// 1.0 * 1.0 = 1.0 (0x3C00 * 0x3C00 = 0x3C00)
9// 2.0 * 0.5 = 1.0 (0x4000 * 0x3800 = 0x3C00)
10// -1.0 * 1.0 = -1.0 (0xBC00 * 0x3C00 = 0xBC00)
11// -1.0 * -1.0 = 1.0 (0xBC00 * 0xBC00 = 0x3C00)
12// 0.0 * any = 0.0 (zero propagation)
13// 0.0 * inf = NaN (IEEE 754 spec)
14// inf * 1.0 = inf
15// NaN * 1.0 = NaN
16
17import "nx_syscalls.nx"
18import "nx_tier.nx"
19import "nx_f16.nx"
20
21func main() -> i64 {
22 // ----- A) Classification gate -----
23 var vi: nx_int = 0
24 while vi < NX_F16_CLS_N {
25 if nx_f16_cls_is_valid(vi) != 1 { return 5 + vi }
26 vi = vi + 1
27 }
28
29 // Classifications of canonical values.
30 if nx_f16_classify(0x0000) != NX_F16_CLS_ZERO { return 10 } // +0
31 if nx_f16_classify(0x8000) != NX_F16_CLS_ZERO { return 11 } // -0
32 if nx_f16_classify(0x3C00) != NX_F16_CLS_NORMAL { return 12 } // 1.0
33 if nx_f16_classify(0xBC00) != NX_F16_CLS_NORMAL { return 13 } // -1.0
34 if nx_f16_classify(0x4000) != NX_F16_CLS_NORMAL { return 14 } // 2.0
35 if nx_f16_classify(0x7C00) != NX_F16_CLS_INF { return 15 } // +inf
36 if nx_f16_classify(0xFC00) != NX_F16_CLS_INF { return 16 } // -inf
37 if nx_f16_classify(0x7E00) != NX_F16_CLS_NAN { return 17 } // qNaN
38 if nx_f16_classify(0x0001) != NX_F16_CLS_SUBNORMAL { return 18 }
39
40 // ----- B) Sign manipulation -----
41 if nx_f16_neg(0x3C00) != 0xBC00 { return 30 } // 1.0 -> -1.0
42 if nx_f16_neg(0xBC00) != 0x3C00 { return 31 } // -1.0 -> 1.0
43 if nx_f16_neg(0x0000) != 0x8000 { return 32 } // +0 -> -0
44 if nx_f16_abs(0xBC00) != 0x3C00 { return 33 } // |-1.0| = 1.0
45 if nx_f16_abs(0x3C00) != 0x3C00 { return 34 } // |1.0| = 1.0
46
47 // ----- C) IEEE 754 equality -----
48 if nx_f16_eq(0x3C00, 0x3C00) != 1 { return 40 } // 1.0 == 1.0
49 if nx_f16_eq(0x3C00, 0xBC00) != 0 { return 41 } // 1.0 != -1.0
50 if nx_f16_eq(0x0000, 0x8000) != 1 { return 42 } // +0 == -0
51 if nx_f16_eq(0x7E00, 0x7E00) != 0 { return 43 } // NaN != NaN
52 if nx_f16_eq(0x7E00, 0x3C00) != 0 { return 44 } // NaN != 1.0
53 if nx_f16_eq(0x7C00, 0x7C00) != 1 { return 45 } // +inf == +inf
54 if nx_f16_eq(0x7C00, 0xFC00) != 0 { return 46 } // +inf != -inf
55
56 // ----- D) Multiplication: canonical bit patterns -----
57 if nx_f16_mul(0x3C00, 0x3C00) != 0x3C00 { return 60 } // 1*1 = 1
58 if nx_f16_mul(0x4000, 0x3800) != 0x3C00 { return 61 } // 2*0.5 = 1
59 if nx_f16_mul(0x3800, 0x3800) != 0x3400 { return 62 } // 0.5*0.5 = 0.25 (0x3400)
60 if nx_f16_mul(0xBC00, 0x3C00) != 0xBC00 { return 63 } // -1*1 = -1
61 if nx_f16_mul(0xBC00, 0xBC00) != 0x3C00 { return 64 } // -1*-1 = 1
62 if nx_f16_mul(0x4000, 0x4000) != 0x4400 { return 65 } // 2*2 = 4 (0x4400)
63
64 // Zero propagation
65 if nx_f16_mul(0x0000, 0x3C00) != 0x0000 { return 70 } // 0 * 1 = 0
66 if nx_f16_mul(0x3C00, 0x0000) != 0x0000 { return 71 } // 1 * 0 = 0
67 if nx_f16_mul(0x0000, 0x0000) != 0x0000 { return 72 } // 0 * 0 = 0
68 if nx_f16_mul(0x8000, 0x3C00) != 0x8000 { return 73 } // -0 * 1 = -0
69 if nx_f16_mul(0x8000, 0x8000) != 0x0000 { return 74 } // -0 * -0 = +0
70
71 // 0 * inf = NaN (IEEE 754)
72 if nx_f16_is_nan(nx_f16_mul(0x0000, 0x7C00)) != 1 { return 80 }
73 if nx_f16_is_nan(nx_f16_mul(0x7C00, 0x0000)) != 1 { return 81 }
74 if nx_f16_is_nan(nx_f16_mul(0x0000, 0xFC00)) != 1 { return 82 }
75
76 // inf * non-zero non-NaN = signed inf
77 if nx_f16_mul(0x7C00, 0x3C00) != 0x7C00 { return 90 } // +inf * 1 = +inf
78 if nx_f16_mul(0xFC00, 0x3C00) != 0xFC00 { return 91 } // -inf * 1 = -inf
79 if nx_f16_mul(0x7C00, 0xBC00) != 0xFC00 { return 92 } // +inf * -1 = -inf
80 if nx_f16_mul(0xFC00, 0xFC00) != 0x7C00 { return 93 } // -inf * -inf = +inf
81
82 // NaN propagation
83 if nx_f16_is_nan(nx_f16_mul(0x7E00, 0x3C00)) != 1 { return 100 }
84 if nx_f16_is_nan(nx_f16_mul(0x3C00, 0x7E00)) != 1 { return 101 }
85 if nx_f16_is_nan(nx_f16_mul(0x7E00, 0x7E00)) != 1 { return 102 }
86
87 return 0
88}