nx_fx_f64_const.nx source
↩ module page · 24 lines · 1343 B
1// nx_fx_f64_const.nx -- LN39 fixture: `const NAME: f64 = EXPR` folds at compile time to the SAME bits the
2// program computes at run time with the hardware ops on the same literals. Exit 0 iff every check holds,
3// else a bitmask (bit k = check k). license_tier: ORIGINAL
4const PI: f64 = 3.141592653589793
5const HALF_PI: f64 = PI / 2.0
6const TWO_PI: f64 = 2.0 * PI
7const NEG_1P5: f64 = -1.5
8const K: i64 = 3
9const KF: f64 = K * 2.0
10const E9: f64 = 1e9
11const MIX: f64 = (PI - 1.0) / (K + 1)
12func fx_id(x: f64) -> f64 { return x }
13func main() -> i64 {
14 var bad: i64 = 0
15 let pi: f64 = fx_id(3.141592653589793)
16 if HALF_PI != pi / fx_id(2.0) { bad = bad + 1 } // check 0: folded division
17 if TWO_PI != fx_id(2.0) * pi { bad = bad + 2 } // check 1: folded product
18 if NEG_1P5 != fx_id(0.0) - fx_id(1.5) { bad = bad + 4 } // check 2: unary minus
19 if KF != fx_id(6.0) { bad = bad + 8 } // check 3: int const converts exactly
20 if E9 != fx_id(1000000000.0) { bad = bad + 16 } // check 4: exponent literal in a const
21 if MIX != (pi - fx_id(1.0)) / fx_id(4.0) { bad = bad + 32 } // check 5: parens, mixed int and f64
22 if PI != pi { bad = bad + 64 } // check 6: a plain literal const
23 return bad
24}