Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Fast normalization - slightly off topic

  1. #1
    Member Regular Contributor
    Join Date
    Oct 2000
    Location
    USA
    Posts
    322

    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. #2
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    San Diego, CA, USA
    Posts
    769

    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. #3
    Senior Member OpenGL Pro
    Join Date
    Feb 2001
    Location
    Switzerland
    Posts
    1,840

    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]
    fadd f
    fstp f
    fld dword ptr[eax+8]
    fmul dword ptr[eax+8]
    fadd f
    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?
    http://davepermen.net - if i could stay true to my heart, i would feel totally free

  4. #4
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    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. #5
    Member Regular Contributor
    Join Date
    Oct 2000
    Location
    USA
    Posts
    322

    Re: Fast normalization - slightly off topic

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

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    San Diego, CA, USA
    Posts
    769

    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. #7
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    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. #8
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    San Diego, CA, USA
    Posts
    769

    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. #9
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    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. #10
    Junior Member Regular Contributor
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    200

    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
  •