; Returns the square root of E, rounded to the nearest integer: ;=============================================================== 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,sqrtEskp1 ;4 12|15 48+3x inc d ;-- -- -- sub c ;-- -- -- sqrtEskp1: djnz sqrtELoop ;2 13|8 47 cp d ;1 4 4 jr c,sqrtEskp2 ;3 12|11 12|11 inc d ;-- -- -- sqrtEskp2: 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. ;=============================================================== end