I was toying around with some math routines while I was away and I was curious about the square root algorithms. Are the designed to return the square root rounded down, up, or just rounded? If it is rounded down and you want to round it to the nearest integer answer, here is a code I made a while ago (it isn't even close to what Axe needs, but it should only be taken as an example):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
;=============================================================== sqrtE: ;=============================================================== ;Input: ; E is the value to find the square root of ;Outputs: ; A is E-D^2 ; B is 0 ; D is the rounded result ; E is not changed ; HL is not changed ;Destroys: ; C ; xor a ;1 4 4 ld d,a ;1 4 4 ld c,a ;1 4 4 ld b,4 ;2 7 7 sqrtELoop: rlc d ;2 8 32 ld c,d ;1 4 16 scf ;1 4 16 rl c ;2 8 32 rlc e ;2 8 32 rla ;1 4 16 rlc e ;2 8 32 rla ;1 4 16 cp c ;1 4 16 jr c,$+4 ;4 12|15 48+3x inc d ;-- -- -- sub c ;-- -- -- djnz sqrtELoop ;2 13|8 47 cp d ;1 4 4 jr c,$+3 ;3 12|11 12|11 inc d ;-- -- -- ret ;1 10 10 ;=============================================================== ;Size : 29 bytes ;Speed : 347+3x cycles plus 1 if rounded down ; x is the number of set bits in the result. ;===============================================================
The only reason that I mention this is that I know a lot of graphical algorithms would have better results if the square root was returned in rounded form as opposed to just rounded up or down.
Sorry if this was already covered and I missed it
Spoiler for Hidden :
EDIT: Wow, I just did something I didn't think was even possible.
I found a good use for a forward djnz . >.> Hehe, I use forward djnz in many-- if not most-- of my programs... It is one of the most useful tricks I use and is kind of my signature touch
I use it to save time and memory a lot, especially in instances like this:
1 2 3 4 5 6 7 8 9 10 11
ld b,a or a \ jr nz,Next1 ;code Next1: djnz Next2 ;code Next2: djnz Next3 ;code ;...et cetera
* Xeda112358 loves it