Raw memory from C, bits, a wrapping hash, SIMD, C layout, and the OS through write.
low_level.zg
# Low level: raw memory from C, bits, a wrapping hash, SIMD, C layout, and the OS through `write`.# a block of C's functions (owner, 2026-10-02); one alone may be a line, `extern.C fn exit ['code': int32];`extern.C{fnwrite['fd':int32,'buf':ptr[uint8],'len':uint64]->int64;fnmalloc['bytes':uint64]->ptr.mut[uint8];fnfree['p':ptr.mut[uint8]];}layout.CstructHeader{magic:uint32,flags:uint8,length:uint64}# FNV-1a: the wrap *is* the algorithm, so `x~`, never a checkfnfnv1a[lent'bytes':array[uint8]]->uint64{letmut'h':uint64=14695981039346656037;for'b'inlend'bytes'{change'h'=('h'xorto.uint64['b'])times.wrap1099511628211;}return'h';}# count the 1 bits, one shift at a timefnpopcount['x':uint32]->uint32{letmut'n':uint32='x';letmut'count':uint32=0;while'n'>0{change'count'='count'plus('n'and.bit1);change'n'='n'shr1;}return'count';}# 16 bytes at once: is every byte ASCII (below 128)?fnall_ascii[lent'bytes':array[uint8]]->bool{let'v':simd[uint8,16]=simd.load[lend'bytes',pos1];returnsimd.all['v'<simd.fill[value128]];}MAIN{# 8 bytes nobody owns: C's memory, written and read through a pointerno_checklet'raw':=malloc[8];for'i'from0to7by1{no_checkptr.write[ptr.at['raw',by'i'],value65plus'i'];# 'A' 'B' 'C' …}no_checklet'third':=ptr.read[ptr.at['raw',by2]];no_checkfree['raw'];let'text':array[uint8]=[104,101,108,108,111,44,32,108,111,119,32,108,101,118,101,108];let'msg':="straight to fd 1 via write(2)";no_checklet'n':=write[1,lend'msg',str.bytes[lend'msg']];std::print.useful[newln,"third raw byte: ",'third',newln,"fnv1a: ",fnv1a[lend'text'],newln,"popcount(0xF0F0): ",popcount[61680],newln,"all ascii: ",all_ascii[lend'text'],newln,"Header: ",size.of[Header]," bytes, aligned to ",align.of[Header],newln];}
Output
straight to fd 1 via write(2)
third raw byte: 67
fnv1a: 6181572250411736725
popcount(0xF0F0): 8
all ascii: true
Header: 16 bytes, aligned to 8