Skip to content

Optimize multiply high routines#800

Merged
mateoconlechuga merged 4 commits intomasterfrom
opt_imulhu
Apr 25, 2026
Merged

Optimize multiply high routines#800
mateoconlechuga merged 4 commits intomasterfrom
opt_imulhu

Conversation

@ZERICO2005
Copy link
Copy Markdown
Contributor

@ZERICO2005 ZERICO2005 commented Apr 24, 2026

  • Optimizes __imulhu (-5 bytes)
  • Removed redundant RCF in __i48mulu (-1 byte)
  • Optimize __lmulhu (-4 bytes)
  • Optimize __llmulhu (-2 bytes, and inner loop 1F faster)

Also, __frimulu with the rounding code removed appears to implement a working __imulhu too. You can use this branch and edit toolchain/test/standalone/mulhu/crt_wrap.s to test it https://github.com/ZERICO2005/toolchain/tree/imulhu_test_rig

__imulhu:
; __frimulu:
	push	af
	push	de
	push	iy
	push	bc
	push	hl
	ld	d,l
	ld	e,c
	mlt	de
	ld	a,d
	ld	d,h
	ld	e,b
	ld	b,h
	ld	h,e
	mlt	hl
	mlt	bc
	mlt	de
	add	a,c
	ld	c,a
	ld	a,b
	adc	a,0h
	ld	b,a
	add.s	hl,bc
	jr	nc,_L0
	inc	d
_L0:
	ld	iy,0h
	add	iy,sp
	ld	b,(iy+2)
	ld	c,(iy+5)
	mlt	bc
	ld	a,h
	add	a,e
	ld	e,a
	jr	nc,_L1
	inc	d
	jr	nz,_L1
	inc	bc
_L1:
	ld	h,(iy+2)
	ld	l,(iy+3)
	mlt	hl

	; ld	a,l	;approximation
	; add	a,80h
	; ld	l,a
	; ld	a,h
	; adc	a,0h
	; ld	h,a

	add.s	hl,de
	jr	nc,_L2
	inc	bc
_L2:
	ld	d,(iy+0)
	ld	e,(iy+5)
	mlt	de
	add.s	hl,de
	jr	nc,_L3
	inc	bc
_L3:
	ld	a,h
	ld	d,(iy+2)
	ld	e,(iy+4)
	ld	h,(iy+1)
	ld	l,(iy+5)
	mlt	de
	mlt	hl
	add.s	hl,de
	jr	nc,_L4
	inc	b
_L4:
	add	a,l
	ld	l,a
	ld	a,h
	adc	a,c
	ld	h,a
	jr	nc,_L5
	inc	b
_L5:
	ex	de,hl
	push	bc
	dec	sp
	pop	hl
	inc	sp
	ld	h,d
	ld	l,e
	pop	bc
	pop	bc
	pop	iy
	pop	de
	pop	af
	ret

@mateoconlechuga mateoconlechuga merged commit 5643016 into master Apr 25, 2026
9 checks passed
@mateoconlechuga mateoconlechuga deleted the opt_imulhu branch April 25, 2026 00:03
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.

2 participants