CPCWiki forum

General Category => Programming => Topic started by: Arnaud on 13:41, 23 August 21

Title: Asm how optimize if elseif code structure
Post by: Arnaud on 13:41, 23 August 21
Hi,
here an extract of my code, it's a simple if elseif structure.

The tests take more time than the computation itself, is the better way to make these comparisons ?

Thanks.

loopTilemap:
    ld   a, (hl)     ;; [2] A = (HL) tile to compare

    ;; Select tilemap type
    or   a             ;; [1] 00 = Land Tile
    jp   z, replaceTilemap  ;; [2/3]

    cp   #40         ;; [2] 40 = Forest Tile
    jp   z, replaceTilemap  ;; [2/3] 

    cp   #05         ;; [2] 05 = Swamp Tile
    jp   z, replaceTilemap  ;; [2/3]

    cp   #79         ;; [2] 79 = Flag Tile
    jp   z, changeTilemap  ;; [2/3] 

    cp   #151        ;; [2] 151 = Fire Camp
    jp   z, changeTilemap  ;; [2/3] 

    jp   nextTileMap ;; [3]
Title: Re: Asm how optimize if elseif code structure
Post by: roudoudou on 13:50, 23 August 21
take a look at   https://wikiti.brandonw.net/index.php?title=Z80_Optimization#Look_up_Table
Title: Re: Asm how optimize if elseif code structure
Post by: Fessor on 17:44, 23 August 21
Quote from: Arnaud on 13:41, 23 August 21Hi,
here an extract of my code, it's a simple if elseif structure.

The tests take more time than the computation itself, is the better way to make these comparisons ?

Thanks.
why not putting the tilenumbers in a table and compare a to them via cpi/cpir?
Title: Re: Asm how optimize if elseif code structure
Post by: zhulien on 15:34, 26 August 21
Quote from: roudoudou on 13:50, 23 August 21
take a look at   https://wikiti.brandonw.net/index.php?title=Z80_Optimization#Look_up_Table


fantasitc!  I always disliked the constant chain of CPs as the last one always takes longer to compare than the first.  now there are several options between consistency in speed, super fast, tight memory etc...


i also was thinking about jump tables, and of course if you have only a couple of CPs then the benefits are lost, but if you have dozens, it definitely looks like a good option.

Powered by SMFPacks Menu Editor Mod