Closed ktye closed 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
^^^^ ^^^^ ^^^^ ^^^^ ^^
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
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}
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}
DiscoDoug, ktye: v:(+/&\s=v)_v