Difference between revisions of "Z80 Routines:Math:Logarithm"
From WikiTI
(created, includes log of base 2) |
|||
(One intermediate revision by one other user not shown) | |||
Line 2: | Line 2: | ||
[[Category:Z80 Routines|Logarithm]] | [[Category:Z80 Routines|Logarithm]] | ||
− | + | = Introduction = | |
− | = Integer Log of base 2 == | + | == Integer Log of base 2 == |
<nowiki> | <nowiki> | ||
Line 10: | Line 10: | ||
; output: a = log2(hl) (rounded down and from -1 to 15) (8-bit integer signed) | ; output: a = log2(hl) (rounded down and from -1 to 15) (8-bit integer signed) | ||
log2: | log2: | ||
− | ld a, | + | ld a,16 |
− | + | scf | |
− | + | ||
− | + | ||
− | + | ||
log2loop: | log2loop: | ||
− | + | adc hl,hl | |
− | jr | + | dec a |
− | + | jr nc,log2loop | |
− | + | ||
− | + | ||
ret | ret | ||
</nowiki> | </nowiki> |
Latest revision as of 12:32, 17 June 2010
Contents
Introduction
Integer Log of base 2
; input: hl (16-bit integer unsigned) ; output: a = log2(hl) (rounded down and from -1 to 15) (8-bit integer signed) log2: ld a,16 scf log2loop: adc hl,hl dec a jr nc,log2loop ret
Integer Log of base 10
Since log10=log2(hl)/log2(10). We can multiply by 1/log2(10).
Integer Log of base B
The same trick as above.
;unfinished logB: ; input: hl = number ; b = base ; output: a = log hl base b ret