[A83] Re: HL / 12 (fast)


[Prev][Next][Index][Thread]

[A83] Re: HL / 12 (fast)




Using 16.16 fixed point math you could do hl*1556h.

e.g. 5000h*1556h=06AAE000h and 5000h/12d=06AAh

1556h is in fact an approximation of 10000h/12. However it isn't precise
enough to calculate the remainder. You'll have to use 16.24 for that.

; de = hl / 12
; f,b,c,d,e,h,l destroyed
; 474-480 tstates w/o ret

    ld     b,h
    ld     c,l
    ld     de,0
    add    hl,hl
    rl     e
    rl     d
    add    hl,hl
    rl     e
    rl     d
    add    hl,bc
    jr     nc,$+1
    inc    de
    add    hl,hl
    rl     e
    rl     d
    add    hl,hl
    rl     e
    rl     d
    add    hl,bc
    jr     nc,$+1
    inc    de
    add    hl,hl
    rl     e
    rl     d
    add    hl,hl
    rl     e
    rl     d
    add    hl,bc
    jr     nc,$+1
    inc    de
    add    hl,hl
    rl     e
    rl     d
    add    hl,hl
    rl     e
    rl     d
    add    hl,bc
    jr     nc,$+1
    inc    de
    add    hl,hl
    rl     e
    rl     d
    add    hl,hl
    rl     e
    rl     d
    add    hl,bc
    jr     nc,$+1
    inc    de
    add    hl,hl
    rl     e
    rl     d
    add    hl,bc
    jr     nc,$+1
    inc    de
    add    hl,hl
    rl     e
    rl     d

> From: "Harper Maddox" <gte172i@prism.gatech.edu>
>
> This is such a simple algorithm, i've probably written it a hundred
> times.  If you guys want a challenge, how about you write a divide hl by
12
> algorithm.  I use that a lot for collision detection with my projects that
> use 12x12 pixel tiles.
>
> harper




Follow-Ups: References: