# Thread: Fast normalization - slightly off topic

1. ## Fast normalization - slightly off topic

Is there a faster way to normalize a vector than just?:

len = sqrt(vect.x^2 + vect.y^2 + vect.z^2);
vect.x /= len;
vect.y /= len;
vect.z /= len;

I remember a poster earlier mentioning some faster methods of finding the length of a vector without using sqrt.

2. ## Re: Fast normalization - slightly off topic

This would be faster:

len = 1.0 / sqrt(vect.x^2 + vect.y^2 + vect.z^2);

vect.x *= len;
vect.y *= len;
vect.z *= len;

Multiplies are significantly faster than divides. Of course, the slowest part of the above code is the square root. Nvidia has some fastmath routines on their site that has a faster square root in it. There are also other "tricks" like Nvidia's floating around the web. They're not as accurate or as universal as just calling sqrt(), so be aware of this.

-- Zeno

3. ## Re: Fast normalization - slightly off topic

void __forceinline __fastcall normalizeASM(float* v)
{
static float f=0;
static const float one=1.0f;
__asm{
mov eax,dword ptr[v]
fld dword ptr[eax]
fmul dword ptr[eax]
fstp f
fld dword ptr[eax+4]
fmul dword ptr[eax+4]
fstp f
fld dword ptr[eax+8]
fmul dword ptr[eax+8]
fsqrt
fstp f
fld one
fdiv f
fstp f
fld dword ptr[eax]
fmul f
fstp dword ptr[eax]
fld dword ptr[eax+4]
fmul f
fstp dword ptr[eax+4]
fld dword ptr[eax+8]
fmul f
fstp dword ptr[eax+8]
}
}

thats the one i wrote.. could be faster without the f, but i dont know how to store from the floatreg into a stdregister..
(means fstp ebx, not allowed.. ) anyone?

4. ## Re: Fast normalization - slightly off topic

If you use 3dnow or SSE you can do a fast a quite accurate 1/sqrt approximation in only two or so clockcycles.

5. ## Re: Fast normalization - slightly off topic

Thanks for all the info, guys! So, Humus, how would you implement such an optimization? In asm?

6. ## Re: Fast normalization - slightly off topic

Humus -

I'm sure a lot of people on here would LOVE to get a 2 cycle sqrt approx (I would ). Do you know where we could find it?

Davepermen -

Have you done any benchmarking of your algorithm vs. regular sqrtf()? Could you post the results?

Thanks,
--Zeno

7. ## Re: Fast normalization - slightly off topic

Yeah, I'd do it in asm. It would be a quite short function. I haven't done any 3dnow stuff for some time, so don't have the instructions name in my head (was something like PFI2D I think ... or something other weird). But the 3dnow docs are freely available on AMD homepage and the SSE eqvivalent is free for download from Intel too.

[This message has been edited by Humus (edited 02-12-2001).]

8. ## Re: Fast normalization - slightly off topic

Cool. So now all I need to do is download tens of megabytes of Intel and AMD processor specs, study them, learn assembly, and maybe come up with a mathematical trick for approximating a square root (if such instructions aren't built-in). That shouldn't take long .

-- Zeno

9. ## Re: Fast normalization - slightly off topic

They are built-in. I could smash up a small piece of code later tonight when I get time ...

10. ## Re: Fast normalization - slightly off topic

Please have a look at:
http://www.opengl.org/discussion_boa...ML/000211.html

(you may have to reconstruct the link)

Regards

LG

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•