Skip to content

[fatdrvce] converted division by a power of two into a bitshift#791

Draft
ZERICO2005 wants to merge 6 commits intomasterfrom
fatdrvce_use_shift
Draft

[fatdrvce] converted division by a power of two into a bitshift#791
ZERICO2005 wants to merge 6 commits intomasterfrom
fatdrvce_use_shift

Conversation

@ZERICO2005
Copy link
Copy Markdown
Contributor

@ZERICO2005 ZERICO2005 commented Apr 18, 2026

TL:DR turns 32bit division into a bitshift

Resolves #757

Comment thread src/fatdrvce/fatdrvce.asm Outdated
Comment on lines 2522 to 2541
compare_auhl_zero
ret z
ld e,a
push hl,de
xor a,a
ld bc,512
push bc
call ti._lremu
compare_hl_zero
pop bc,de,hl
; test if the low 9 bits are non-zero
inc l
dec l
jr nz,.round_up
bit 0,h
.round_up:
push af
xor a,a
call ti._ldivu
push hl
pop bc
ld l,9
call ti._lshru
push bc
pop hl
pop af
ld a,0
ret z
; round up
inc hl
Copy link
Copy Markdown
Member

@runer112 runer112 Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	; if A:UHL == 0, abort
	compare_hl_zero
	jr	nz, .dec_hl
	or	a, a
	ret	z

	; A:UHL--
	dec	a
.dec_hl:
	dec	hl

	; A:UHL /= 512
	push	hl
	pop	bc
	ld	l, 9
	call	ti._lshru
	push	bc
	pop	hl

	; A:UHL++
	inc	hl

	ret

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. That trick works since we have already filtered out the A:UHL == 0 case

Comment thread src/fatdrvce/fatdrvce.asm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

division/remainder can be replaced with bitshifts in fatdrvce

4 participants