CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: some q3 code

  1. #1
    Join Date
    Feb 2007
    Posts
    186

    some q3 code

    I know this has been looked at before a lot, but I was wondering what a line in this q3 code does. This code is very confusing partly because of that crazy number they invent.

    Code:
    float Q_rsqrt( float number )
    {
    	long i;
    	float x2, y;
    
    	y  = number;
    	i  = * ( long * ) &y;			//what does this do?			
    	i  = 0x5f3759df - ( i >> 1 );               
    	y  = * ( float * ) &i;
    	y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
    
    	return y;
    }
    What exactly does casting the address of a float do?

  2. #2
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    Re: some q3 code

    Well .. i = * ( long * ) &y
    &y - gives the address of y variable
    ( long * ) &y - is casting that float address to an address of type long
    * ( long * ) &y - gets the value using our new pointer of type long
    Bogdan

    If someone helped you then please Rate his post and mark the thread as Resolved
    Please improve your messages appearance by using tags [ code] Place your code here [ /code]

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: some q3 code

    It is a bad way of doing the following

    Code:
    union FloatBits
    {
        float Float;
        int    Bits;
    };
    
    {
       float f = 123.456;
       FloatBits fb;
       fb.Float = f;
       int i = fb.Bits;
    }
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: some q3 code

    A bit OT, but here's a link to an article on the origin of the function in question: Origin of Quake3's Fast InvSqrt(). Doesn't go too much into detail, but you might find it interesting. It also links to an article on the math behind the function if you're curious.
    Insert entertaining phrase here

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: some q3 code

    Interesting article.

    Quote Originally Posted by TheCPUWizard
    It is a bad way of doing the following
    Isn't it also bad to write to one member, and then read from another member, of the same union?
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: some q3 code

    Isn't it also bad to write to one member, and then read from another member, of the same union?
    Not if you understand the implications

    There are many analogies I am thinking of, but each one is sure to embarass/offend someone in this international community.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Feb 2007
    Posts
    186

    Re: some q3 code

    ah ok thanks. I guess it just confused me

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured