code wiki / _hdl_build / nx_negdiv_probe.nx
nx_negdiv_probe.nx source
↩ module page · 19 lines · 956 B
1// nx_negdiv_probe.nx -- ONE-NUMBER PROBE: NishiLang `/` and `%` semantics on negatives.
2// C (and libwebp) truncate toward zero: (-7)/2 = -3, (-7)%2 = -1. If nx_cc emits floor
3// semantics ((-7)/2 = -4) every VP8L clamp-half predictor drifts by 1 vs the oracle.
4// license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6
7func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{pw("-\x00" as *u8);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
9
10func main() -> i64 {
11 var a: i64 = 0 - 7
12 pw("(-7)/2=\x00" as *u8); pn(a / 2)
13 pw(" (-7)%2=\x00" as *u8); pn(a % 2)
14 var c: i64 = 0 - 1
15 pw(" (-1)/2=\x00" as *u8); pn(c / 2)
16 pw("\n\x00" as *u8)
17 sys_exit(0)
18 return 0
19}