code wiki / _hdl_build / _f64_probe.nx
_f64_probe.nx source
↩ module page · 50 lines · 2504 B
1// _f64_probe.nx -- compiler-behavior probe BEFORE authoring nx_f64 (verify-runtime-not-docs law).
2// Probes the exact primitives the f64 core depends on:
3// P1 64-bit hex literal with bit 63 set (0xFFF0000000000000 = -inf bit pattern = i64 -4503599627370496)
4// P2 1 << 63 (sign-bit construction)
5// P3 arithmetic >> on a negative raw, masked (exp-field unpack idiom)
6// P4 26/27-bit split multiply partials (the 106-bit product decomposition)
7// P5 (1 << 53) - 1 mask construction
8// Judge by printed markers, never $?.
9import "nx_syscalls.nx"
10
11func f6p_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
12func f6p_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }; let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48; k = 1 }; while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 }; var i: i64 = 0; while i < k { bb[i] = t[k-1-i]; i = i + 1 }; sys_write(1, bb, k); return 0 }
13func f6p_line(tag: *u8, v: i64) -> i64 { f6p_puts(tag); f6p_putn(v); f6p_puts("\n" as *u8); return 0 }
14
15func main() -> i64 {
16 // P1: hex literal with sign bit. -inf bits = 0xFFF0000000000000.
17 // As signed i64 that is -(2^52) = -4503599627370496.
18 let ninf: i64 = 0xFFF0000000000000
19 f6p_line("P1-hex-ninf=" as *u8, ninf)
20
21 // P2: sign-bit via shift. 1<<63 = i64 min = -9223372036854775808.
22 let sb: i64 = 1 << 63
23 f6p_line("P2-shl63=" as *u8, sb)
24
25 // P3: exp-field unpack from a NEGATIVE raw (sign=1). (ninf >> 52) & 0x7FF must be 2047.
26 let ef: i64 = (ninf >> 52) & 0x7FF
27 f6p_line("P3-expfield=" as *u8, ef)
28 // sign unpack: (ninf >> 63) & 1 must be 1.
29 let sg: i64 = (ninf >> 63) & 1
30 f6p_line("P3-sign=" as *u8, sg)
31
32 // P4: split multiply. ma = mb = 2^53 - 1 (max 53-bit significand).
33 let m53: i64 = (1 << 53) - 1
34 let a0: i64 = m53 & ((1 << 27) - 1) // low 27 bits
35 let a1: i64 = m53 >> 27 // high 26 bits
36 let pp_hh: i64 = a1 * a1 // < 2^52
37 let pp_hl: i64 = a1 * a0 // < 2^53
38 let pp_ll: i64 = a0 * a0 // < 2^54
39 f6p_line("P4-a0=" as *u8, a0)
40 f6p_line("P4-a1=" as *u8, a1)
41 f6p_line("P4-hh=" as *u8, pp_hh)
42 f6p_line("P4-hl=" as *u8, pp_hl)
43 f6p_line("P4-ll=" as *u8, pp_ll)
44
45 // P5: mask construction.
46 f6p_line("P5-m53=" as *u8, m53)
47
48 f6p_puts("F64-PROBE-DONE\n" as *u8)
49 return 0
50}