ktye / i

interpret
100 stars 16 forks source link

wb leb128 #41

Closed ktye closed 1 year ago

ktye commented 1 year ago
/in                   leb128
t:(0x5e0a000000000000;0xde14                 /2654
   0xffffff7f00000000;0xffffffff07           /2147483647
   0x000000000000e03f;0x80808080808080f03f   /4602678819172646912
   0x010000000000f87f;0x81808080808080fcff00 /9221120237041090561
   0x000000000000f07f;0x80808080808080f8ff00 /9218868437227405312
   0xffffffffffffffff;0x7f                   /-1                 (false)
   0x00f0ffffffffffff;0x8060                 /-4096
   0x000000000000f0ff;0x8080808080808078     /-4503599627370496
   0xffffffff000000ff;0xffffffff8f8080807f)  /-72057589742960641 (false)

leb8:{r:!0;B:1v:,/+(8#2)\\256/256+|x;while[B&(8<#v)&+/v;b:-7#v;s:*b;v:-7_v;r,:$[((0~&/v)|0x00~s)&(+/v)|s;1;B:0],b];_2//'-8^r}

g:leb8't i:2*!2\#t
e:t 1+i
 \g~'e
ktye commented 1 year ago

literal:

(module
  (global $a i64 (i64.const -72057589742960641))
 )

encode with:

$wabt/wat2wasm x.wat
$wabt/wasm-objdump.exe -dxs x.wasm

result:

000000a: 017e 0042 ffff ffff 8f80 8080 7f0b
                   ^^^^ ^^^^ ^^^^ ^^^^ ^^
ktye commented 1 year ago

this should work:

leb8:{
 v:,/+(8#2)\\256/256+|x  /binary 1 0 1..
 s:*v                    /sign
 v:(+/&\(~;:)[s]v)_v     /cut leading run (sign extension)
 v:(((7*1+7\#v)-#v)#s),v /sign extend to multiples of 7
 v:-7^v                  /split
 v:(,(0,*v)),1,'1_v      /add leading 1 in each but first 
 |_2//'v}                /reencode to string

t:{ \(y~x;x:leb8 x;x)}

/ 64bit little-end;    encode                 /integer
t[0x5e0a000000000000;0xde14                 ] /2654
t[0xffffff7f00000000;0xffffffff07           ] /2147483647
t[0x000000000000e03f;0x80808080808080f03f   ] /4602678819172646912
t[0x010000000000f87f;0x81808080808080fcff00 ] /9221120237041090561
t[0x000000000000f07f;0x80808080808080f8ff00 ] /9218868437227405312
t[0xffffffffffffffff;,0x7f                  ] /-1
t[0x00f0ffffffffffff;0x8060                 ] /-4096
t[0x000000000000f0ff;0x8080808080808078     ] /-4503599627370496
t[0xffffffff000000ff;0xffffffff8f8080807f   ] /-72057589742960641

chars are signed int8, binary encode/decode is 2// and 2\\, 256/ is modulo, 7\ divides by 7 and _int converts to char

ktye commented 1 year ago
leb8:{
 v:,/+(8#2)\\256/256+|x  /binary 1 0 1..
 s:*v                    /sign
 v:(+/&\s=v)_v           /cut leading run (sign extension)
 v:(((7*1+7\#v)-#v)#s),v /sign extend to multiples of 7
 v:-7^v                  /split
 v:(1&!#v),'v            /add leading 1 in each but first 
 |_2//'v}                /reencode to string

or short

leb8:{s:*x:,/+(8#2)\\256/256+|x;|_2//'(1&!#x),'x:-7^(((7*1+7\#x)-#x)#s),x:(+/&\s=x)_x}
ktye commented 1 year ago

chrispn: (x?~s)_

leb8:{s:*x:,/+(8#2)\\256/256+|x;|_2//'(1&!#x),'x:-7^(((7*1+7\#x)-#x)#s),x:(x?~s)_x}
ktye commented 1 year ago

DiscoDoug, ktye: v:(+/&\s=v)_v