CVBasic icon indicating copy to clipboard operation
CVBasic copied to clipboard

Better divisions/products

Open artrag opened this issue 1 year ago • 4 comments

The compiler could improve the generated code for math by supporting shift operations when you multiply/divide by powers of two. Version 0.70 use generic mul/div functions even when those optimization is possible

artrag avatar Oct 12 '24 18:10 artrag

@artrag Are you sure? https://github.com/nanochess/CVBasic/blob/a4464d255b7c6c3068fe52440db9fe68604a0fb7/cpuz80.c#L468 https://github.com/nanochess/CVBasic/blob/a4464d255b7c6c3068fe52440db9fe68604a0fb7/cpuz80.c#L631

polluks avatar Oct 14 '24 06:10 polluks

yes, I get this for frame/16

; 		call TCOPY($3000 + (12+(FRAME/16 and 3))*32)
LD HL,(frame)
LD DE,16
CALL _div16
LD A,L
AND 3
LD L,A
LD H,0
LD DE,12
ADD HL,DE
ADD HL,HL
ADD HL,HL
ADD HL,HL
ADD HL,HL
ADD HL,HL
LD DE,12288
ADD HL,DE
CALL TCOPY

artrag avatar Oct 14 '24 17:10 artrag

In attach the complete ASM file where the basic code is in the comments output.asm.txt

artrag avatar Oct 14 '24 17:10 artrag

A better conversion would have been ; call TCOPY($3000 + (12+(FRAME/16 and 3))*32) LD HL,(frame) ADD HL,HL ADD HL,HL ADD HL,HL ADD HL,HL LD A,H AND 3 LD L,A LD H,0 LD DE,12 ADD HL,DE ADD HL,HL ADD HL,HL ADD HL,HL ADD HL,HL ADD HL,HL LD DE,12288 ADD HL,DE CALL TCOPY

artrag avatar Oct 14 '24 17:10 artrag

An even better conversion, using the fact that the result of the division is AND 3, could have been:

; call TCOPY($3000 + (12+(FRAME/16 and 3))*32) LD A,(frame) RRCA RRCA RRCA RRCA AND 3 ADD A,12 LD L,A LD H,0 ADD HL,HL ADD HL,HL ADD HL,HL ADD HL,HL ADD HL,HL LD DE,12288 ADD HL,DE CALL TCOPY

artrag avatar Oct 15 '24 16:10 artrag