A mad fast SquareRoot method

Started by
30 comments, last by ageny6 20 years, 5 months ago
quote:Original post by Yann L
Those are two totally different things. The later one converts a IEEE float number into an integer number. The former one converts the raw bit representation of the floating point number into a 32 bit integer. It still stays a floating point number, but allows you to perform integer only operations (the bitshift) on it. Carmack''s trick is a hack, that works on the way a raw floating point number is stored in memory (signbit/exponent/mantissa). Google for a detailed explanation, it''s pretty well covered on the web.



This trick really rulez ! Thank you for have explained it to me )

then it is the same as

__asm{

lea ebx, x // it work ?!
mov i,dword ptr [ebx]

}







!o)
!o)
Advertisement
quote:Original post by Chuck3d
then it is the same as

__asm{

lea ebx, x // it work ?!
mov i,dword ptr [ebx]

}

Actually, the statement int i = *(int*)&x is a NO-OP in ASM. Other than C/C++, ASM isn''t typed. C uses the above mentioned statement to access operators on a type they weren''t designed for. That''s why you need this C-sytle reinterpret cast (as it would be in C++).

You could write Carmacks function in ASM something like this (very unoptimized):
fld dword ptr [x]        // st(0) = [x]fmul [0.5_constant]      // st(0) = 0.5f*xmov eax, [x]             // eax = *(int*)&xmov ebx, 05f3759dfh      // ebx = magic numbershr eax, 1               // eax >>= 1sub ebx, eax             // ebx = magic number - eaxmov [x], ebx             // *(float*)&ifld dword ptr [x]        // st(0) = new_x, st(1) = 0.5f*old_x// etc... 

This topic is closed to new replies.

Advertisement