(lz) Housekeeping

This commit is contained in:
neauoire 2023-11-18 19:58:57 -08:00
parent dabc6723aa
commit f134a4a1c0
1 changed files with 46 additions and 49 deletions

View File

@ -46,7 +46,8 @@
BRK BRK
@<append-byte> ( byte -- ) @<append-byte> ( byte -- )
.output-ptr LDZ2 INC2k .output-ptr STZ2 STA .output-ptr LDZ2 INC2k .output-ptr STZ2
STA
JMP2r JMP2r
@uxn_lz_compress ( input* length* -- ) @uxn_lz_compress ( input* length* -- )
@ -66,8 +67,7 @@
DUP2 .dict-len LDZ2 SUB2 .dict STZ2 DUP2 .dict-len LDZ2 SUB2 .dict STZ2
&for1 ( for ; dict_len; dict++, dict_len-- ) &for1 ( for ; dict_len; dict++, dict_len-- )
.dict-len LDZ2 #0000 EQU2 ?&end-for1 .dict-len LDZ2 #0000 EQU2 ?&end-for1
( Find common prefix length with the string ) ( Find common prefix length with the string ) #0000
#0000
&for2 ( for i = 0;; i++ ) &for2 ( for i = 0;; i++ )
( | If we reach the end of the string, it's the best possible match. ) ( | If we reach the end of the string, it's the best possible match. )
DUP2 [ LIT2 &string-len $2 ] NEQ2 ?{ DUP2 [ LIT2 &string-len $2 ] NEQ2 ?{
@ -76,24 +76,22 @@
POP2 !&done-search } POP2 !&done-search }
( | in[i] != dict[i % dict_len] break; ) ( | in[i] != dict[i % dict_len] break; )
( a ) ADD2k LDA STH ( a ) ADD2k LDA STH
( b ) DUP2 .dict-len LDZ2 DIV2k MUL2 SUB2 .dict LDZ2 ADD2 LDA STHr ( b ) DUP2 .dict-len LDZ2 DIV2k MUL2 SUB2 .dict LDZ2 ADD2
NEQ ?&end-for2 ( res ) LDA STHr NEQ ?&end-for2
INC2 ORAk ?&for2 INC2 ORAk ?&for2
&end-for2 &end-for2 ( | i > match_len )
( | i > match_len )
DUP2 .match-len LDZ2 LTH2 ?{ DUP2 .match-len LDZ2 LTH2 ?{
DUP2 .match-len STZ2 DUP2 .match-len STZ2
.dict LDZ2 .dict-best STZ2 } .dict LDZ2 .dict-best STZ2 }
POP2 POP2 .dict LDZ2 INC2 .dict STZ2
.dict LDZ2 INC2 .dict STZ2 .dict-len LDZ2 #0001 SUB2 .dict-len STZ2
.dict-len LDZ2 #0001 SUB2 .dict-len STZ2 !&for1 !&for1
&end-for1 &end-for1
&done-search ( -- ) &done-search ( -- )
( CPY ) .match-len LDZ2 #0003 GTH2 ?op-cpy ( CPY ) .match-len LDZ2 #0003 GTH2 ?op-cpy
( LIT ) !op-lit ( LIT ) !op-lit
&end &end POP2 POP2 JMP2r
POP2 POP2
JMP2r
( (
@|opcodes ) @|opcodes )
@ -107,8 +105,8 @@
.match-ctl LDZ2 NIP #80 ORA <append-byte> .match-ctl LDZ2 NIP #80 ORA <append-byte>
!&cpy-resume !&cpy-resume
&cpy2 ( -- ) &cpy2 ( -- )
.match-ctl LDZ2 .match-ctl LDZ2 SWP #c0 ORA <append-byte>
SWP #c0 ORA <append-byte> <append-byte> <append-byte>
&cpy-resume ( -- ) &cpy-resume ( -- )
( | *output_ptr++ = in - dict_best - 1; ) ( | *output_ptr++ = in - dict_best - 1; )
DUP2 .dict-best LDZ2 SUB2 #0001 SUB2 NIP <append-byte> DUP2 .dict-best LDZ2 SUB2 #0001 SUB2 NIP <append-byte>
@ -123,15 +121,14 @@
( | start a new literal ) ( | start a new literal )
( | Store this address, and later use it to increment the literal size. ) ( | Store this address, and later use it to increment the literal size. )
( | combine = output_ptr++; ) ( | combine = output_ptr++; )
.output-ptr LDZ2 .output-ptr LDZ2 INC2k .output-ptr STZ2
INC2k .output-ptr STZ2
.combine STZ2 .combine STZ2
( | *combine = 0; ) ( | *combine = 0; )
#00 .combine LDZ2 LDA2 STA #00 .combine LDZ2 LDA2 STA
( | *output_ptr++ = *in++; ) ( | *output_ptr++ = *in++; )
LDAk <append-byte> LDAk <append-byte>
INC2 !uxn_lz_compress/w INC2 !uxn_lz_compress/w
&combine ( -- ) &combine ( -- )
( | if ++*combine == 127 ) ( | if ++*combine == 127 )
.combine LDZ2 LDA INC DUP #7f NEQ ?{ POP #00 } .combine LDZ2 LDA INC DUP #7f NEQ ?{ POP #00 }
.combine LDZ2 STA .combine LDZ2 STA