Skip to content

Commit 1d9fa82

Browse files
committed
Cheapen most end-of-block checks in the hot path
1 parent 9f5a6db commit 1d9fa82

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/ce/lz4.src

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,17 @@ lz4_decompress_block_internal:
9595
; Set BC to the literal length, which is known to be non-zero
9696
ld c, b
9797
ld b, 0
98-
; Mask the backref field, affecting the zero flag, and clear carry
99-
and a, $0F
100-
.L.copy_literal:
10198
; Copy the literals
10299
ldir
103-
; Zero flag is currently set according to the backref field
104-
; If the backref field is non-zero, this cannot be end of block
100+
; Mask the backref field
101+
and a, $0F
102+
; If the backref field is non-zero, this cannot be end of block
105103
jr nz, .L.backref
106-
; Check for end of block (carry is currently clear)
107-
lea bc, iy + 0
108-
sbc hl, bc
109-
ret nc
110-
add hl, bc
111-
inc.s bc ; BCU = 0
104+
; Check for end of block quickly
105+
ld a, l
106+
sub a, iyl
107+
jr z, .L.maybe_end_of_block
108+
xor a, a
112109
.L.backref:
113110
; Read backref offset into BC
114111
ld c, (hl)
@@ -157,7 +154,7 @@ lz4_decompress_block_internal:
157154
jr .L.backref_loop
158155

159156
.L.literal_long:
160-
; Save the backref field
157+
; Save the backref field and zero flag
161158
push af
162159
; Start with literal length of 15
163160
ld b, 0
@@ -193,8 +190,18 @@ lz4_decompress_block_internal:
193190
ldir
194191
; Copy the amount of the last length byte, plus 1
195192
ld c, a
196-
; Restore the backref field
193+
ldir
194+
; Restore the backref field and zero flag
197195
pop af
198-
; Affect the zero flag based on the backref field and clear carry
196+
; If the backref field is non-zero, this cannot be end of block
197+
jr nz, .L.backref
198+
; Clear carry
199199
or a, a
200-
jr .L.copy_literal
200+
.L.maybe_end_of_block:
201+
; Full check for end of block (carry is clear and A = 0)
202+
lea bc, iy + 0
203+
sbc hl, bc
204+
add hl, bc
205+
inc.s bc ; BCU = 0
206+
jr nz, .L.backref
207+
ret

0 commit comments

Comments
 (0)