-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleb128.fs
63 lines (52 loc) · 1.29 KB
/
leb128.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
: check-clear-byte-msb { n1 -- n2 msb }
n1 %01111111 and
n1 %10000000 and 0<>
;
: consume-uleb128-base { c-addr -- i u }
0 0 ( result i )
BEGIN
dup chars c-addr + c@ check-clear-byte-msb ( result i byte should-continue )
>r
over 7 * lshift
rot + swap
1+
r>
WHILE
REPEAT
;
: consume-uleb128 { c-addr -- c-addr2 u }
c-addr consume-uleb128-base
chars c-addr + swap
;
: consume-leb128 { c-addr -- c-addr2 s }
c-addr consume-uleb128-base
dup chars c-addr + -rot
7 * 1-
-1 swap lshift ( c-addr2 result mask )
\ sign-extend the 7*i-bit number to 64 bit
\ if any bit in the mask is set, set all of them
2dup and 0<> IF
or
ELSE
drop
ENDIF
;
\ CREATE EXAMPLE1 1 ALLOT \ 624485 = $98765
\ $E5 EXAMPLE1 c!
\ $8E EXAMPLE1 1 chars + c!
\ $26 EXAMPLE1 2 chars + c!
\ CREATE EXAMPLE1 1 ALLOT \ 234
\ $EA EXAMPLE1 c!
\ $01 EXAMPLE1 1 chars + c!
\ CREATE EXAMPLE1 1 ALLOT \ 7
\ $07 EXAMPLE1 c!
\ CREATE EXAMPLE1 1 ALLOT \ 234
\ $0 EXAMPLE1 c!
\ CREATE EXAMPLE1 1 ALLOT \ -123456 = $FFFFFFFFFFFE1DC0
\ $C0 EXAMPLE1 c!
\ $BB EXAMPLE1 1 chars + c!
\ $78 EXAMPLE1 2 chars + c!
\ EXAMPLE1 consume-uleb128 . drop cr
\ EXAMPLE1 consume-leb128 . drop cr
\ \ EXAMPLE1 1 chars + .
\ bye