Skip to content

Commit 6a0629b

Browse files
calc84maniacmateoconlechuga
authored andcommitted
optimize util_block_to_cluster and util_ceil_byte_size_to_cluster_size
1 parent 49584f4 commit 6a0629b

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

src/fatdrvce/fatdrvce.asm

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,17 +2198,16 @@ util_block_to_cluster:
21982198
or a,a
21992199
sbc hl,bc
22002200
sbc a,(yfat.data_region + 3)
2201-
ld de,(yfat.blocks_per_cluster - 2)
2202-
ld d,0
2203-
ld e,a
2201+
; AC:UHL = A:UHL << (8 - log2(blocks_per_cluster))
2202+
ld c,a
2203+
ld a,(yfat.blocks_per_cluster)
22042204
.loop:
22052205
add hl,hl
2206-
ex de,hl
2207-
adc hl,hl
2208-
ex de,hl
2206+
rl c
2207+
rla
22092208
jr nc,.loop
2210-
ld a,d
2211-
push de
2209+
; A:UHL = (AC:UHL >> 8) + 2
2210+
push bc
22122211
push hl
22132212
inc sp
22142213
pop hl
@@ -2551,23 +2550,28 @@ util_ceil_byte_size_to_cluster_size:
25512550
; destroys:
25522551
; - A, BC, flags
25532552

2554-
; First, get ceiling block size
2555-
call util_ceil_byte_size_to_block_size
2556-
2557-
; Now, get ceiling cluster size by adding (blocks_per_cluster-1) and dividing by blocks_per_cluster
2558-
ld c,(yfat.blocks_per_cluster)
2559-
ld a,c
2560-
dec c
2561-
; Note: ceiling block size is at most $800000 so this addition cannot overflow
2562-
add hl,bc
2553+
; BC:UHL = A:UHL << (7 - log2(blocks_per_cluster))
2554+
ld b,(yfat.blocks_per_cluster)
2555+
or a,a
2556+
jr .enter
25632557
.loop:
25642558
add hl,hl
25652559
rla
2560+
.enter:
2561+
rl b
25662562
jr nc,.loop
2567-
push af
2568-
inc sp
2563+
ld c,a
2564+
; check if HL != 0
2565+
ld a,h
2566+
or a,l
2567+
; UHL = BC:UHL >> 16
2568+
push bc
25692569
push hl
25702570
inc sp
2571+
inc sp
25712572
pop hl
25722573
inc sp
2574+
; round up if any shifted-out bits were non-zero
2575+
ret z
2576+
inc hl
25732577
ret

0 commit comments

Comments
 (0)