code wiki / _hdl_build / nx_simd_sse_shuf_gate.nx
nx_simd_sse_shuf_gate.nx source
↩ module page · 109 lines · 4341 B
1// nx_simd_sse_shuf_gate.nx -- SOVEREIGN 128-bit pshufd run-KAT (rung R2+R3:
2// 3-OPERAND assembler + packed dword shuffle), on REAL x86 silicon, NO gcc/
3// qemu/binutils. AUTHOR=ORGAN, no-false-green.
4//
5// Proves (a) the assembler now parses a THIRD operand (the prerequisite for
6// pshufd AND all VEX/EVEX 256/512-bit ops), and (b) pshufd routes lanes by its
7// imm control byte. src=[11,22,33,44] (4x u32):
8// POS: pshufd $2 -> dst lane0 = src[2] = 33 -> exit(33).
9// NEG: pshufd $0 -> dst lane0 = src[0] = 11 -> exit(11).
10// GREEN iff pos==33 AND neg==11 AND pos!=neg (distinct => the imm genuinely
11// selects the lane, and op0=imm/op1=src/op2=dst threaded correctly). Completes
12// the sovereign 128-bit integer SIMD surface. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14
15const ASM_TOOL: *u8 = "_offc/nxasm_x86_main.elf"
16const SV_LOG: *u8 = "knowledge/status/simd_vec_kat.log"
17
18func gw(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
19func gwn(fd: i64, v: i64) -> i64 {
20 let bb: *u8 = sys_mmap(28); var m: i64 = v
21 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }
22 let t: *u8 = sys_mmap(28); var k: i64 = 0
23 if m == 0 { t[0] = 48; k = 1 }
24 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
25 var i: i64 = 0
26 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
27 sys_write(fd, bb, k); return 0
28}
29
30func g_run(path: *u8, argv: *i64) -> i64 {
31 let envp: *i64 = sys_mmap(8 * 4) as *i64
32 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
33 envp[1] = 0
34 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
35 let pid: i64 = sys_fork()
36 if pid == 0 {
37 if dn >= 0 { sys_dup3(dn, 1, 0) }
38 if dn >= 0 { sys_dup3(dn, 2, 0) }
39 sys_execve(path, argv, envp)
40 sys_exit(127)
41 }
42 let st: *i64 = sys_mmap(16) as *i64
43 sys_wait4(pid, st, 0)
44 if dn >= 0 { sys_close(dn) }
45 let sig: i64 = st[0] & 0x7f
46 if sig != 0 { return 128 + sig }
47 return (st[0] >> 8) & 0xff
48}
49
50func asm_and_run(spath: *u8, elfpath: *u8) -> i64 {
51 let aa: *i64 = sys_mmap(8 * 4) as *i64
52 aa[0] = ASM_TOOL as i64
53 aa[1] = spath as i64
54 aa[2] = elfpath as i64
55 aa[3] = 0
56 let rc_a: i64 = g_run(ASM_TOOL, aa)
57 if rc_a != 0 { return 0 - 200 - rc_a }
58 let rr: *i64 = sys_mmap(8 * 4) as *i64
59 rr[0] = elfpath as i64
60 rr[1] = 0
61 return g_run(elfpath, rr)
62}
63
64func write_shuf_s(path: *u8, sel: i64) -> i64 {
65 let fd: i64 = sys_openat_wr(path, 0x1a4)
66 if fd < 0 { return 0 - 1 }
67 gw(fd, ".text\n" as *u8)
68 gw(fd, "_start:\n" as *u8)
69 gw(fd, "leaq vecS(%rip), %rdi\n" as *u8)
70 gw(fd, "movdqu (%rdi), %xmm0\n" as *u8)
71 gw(fd, "pshufd $" as *u8); gwn(fd, sel); gw(fd, ", %xmm0, %xmm1\n" as *u8)
72 gw(fd, "leaq outbuf(%rip), %rdx\n" as *u8)
73 gw(fd, "movdqu %xmm1, (%rdx)\n" as *u8)
74 gw(fd, "movl (%rdx), %edi\n" as *u8)
75 gw(fd, "movabsq $60, %rax\n" as *u8)
76 gw(fd, "syscall\n" as *u8)
77 gw(fd, ".section .rodata\n" as *u8)
78 gw(fd, "vecS:\n" as *u8)
79 gw(fd, ".byte 11,0,0,0,22,0,0,0,33,0,0,0,44,0,0,0\n" as *u8)
80 gw(fd, ".lcomm outbuf, 16\n" as *u8)
81 sys_close(fd)
82 return 0
83}
84
85func g_emit(fd: i64, pos: i64, neg: i64, ok: i64) -> i64 {
86 gw(fd, "SIMD_VEC_KAT rung=R2R3-3operand+pshufd authored=organ width=128 reg=xmm forms=pshufd(66-0F-70-ib) sovereign(nx_cc->nxasm_x86,no-gcc/qemu/binutils) silicon=real src=[11,22,33,44] pos_sel2=33? " as *u8); gwn(fd, pos)
87 gw(fd, " neg_sel0=11? " as *u8); gwn(fd, neg)
88 gw(fd, " distinct=" as *u8); if pos != neg { gwn(fd, 1) } else { gwn(fd, 0) }
89 if ok == 1 { gw(fd, " verdict=GREEN\n" as *u8) } else { gw(fd, " verdict=RED reason=exit-mismatch-or-3operand-gap\n" as *u8) }
90 return 0
91}
92
93func main() -> i64 {
94 write_shuf_s("/tmp/_sse_shuf_pos.s" as *u8, 2)
95 let pos: i64 = asm_and_run("/tmp/_sse_shuf_pos.s" as *u8, "/tmp/_sse_shuf_pos.elf" as *u8)
96 write_shuf_s("/tmp/_sse_shuf_neg.s" as *u8, 0)
97 let neg: i64 = asm_and_run("/tmp/_sse_shuf_neg.s" as *u8, "/tmp/_sse_shuf_neg.elf" as *u8)
98
99 var ok: i64 = 1
100 if pos != 33 { ok = 0 }
101 if neg != 11 { ok = 0 }
102 if pos == neg { ok = 0 }
103
104 g_emit(1, pos, neg, ok)
105 let lf: i64 = sys_openat_append(SV_LOG, 420)
106 if lf >= 0 { g_emit(lf, pos, neg, ok); sys_close(lf) }
107 if ok == 1 { return 0 }
108 return 1
109}