Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:02:14+00:00 2026-05-31T11:02:14+00:00

I found the following code in a book Mathematics for Game Programmers by Christopher

  • 0

I found the following code in a book “Mathematics for Game Programmers” by Christopher Ttremblay. it looks like c++, and he does make a bunch of references to performance comparisons to the stllib the code given is as thus:

float Exp2(float X)
{
    float Result, Square, IntPow;
    if (X < 0) {
        const unsigned long IntVal = *(unsigned long *)&X & 0x7FFFFFFF;
        const unsigned long Int = (IntVal >> 23) - 127;
        if ((long)Int > 0) {
            *(unsigned long *)&IntPow = ((((IntVal & 0x007FFFFF) |
                0x00800000) >> (23 - Int)) + 127 + 1) << 23;
            *(unsigned long *)&X = (((IntVal << Int) & 0x007FFFFF)
                | 0x3F800000);
            X = 2.0f - X;
        } else {
            IntPow = 2.0f;
            X++;
        }
        Result = X0CoEff + Square * X1CoEff;
        Square *= X; // The 2 last lines are repeated for every coeff.
        Result += Square * XiCoEff;
        ...
        return Result / IntPow;
    } else {
        const unsigned long IntVal = *(unsigned long *)&X;
        const unsigned long Int = (IntVal >> 23) - 127;
        if ((long)Int > 0) {
            *(unsigned long *)&IntPow = ((((IntVal & 0x007FFFFF) |
                0x00800000) >> (23 - Int)) + 127) << 23;
            *(unsigned long *)&X = (((IntVal << Int) & 0x007FFFFF)
                | 0x3F800000);
            X—;
        } else
            IntPow = 1.0f;
        Square = X;
        Result = X0CoEff + Square * X1CoEff;
        Square *= X; // The 2 last lines are repeated for every coeff.
        Result += Square * XiCoEff;
        ...
        return Result * IntPow;
    }
}

float log2(float X)
{
    float Result, Square;
    Result = (float)((*(unsigned long *)&X) >> 23) - 127 + x0CoEff;
    *(unsigned long *)&X = (*(unsigned long *)&X & 0x007FFFFF) | 0x3F800000;
    Square = X;
    Result += Square * XiCoEff;
    Square *= X; // The 2 last lines are repeated for every coeff.
    ...
    return Result;
}

the thing is I never actually learned bit operations (which I think is what is going on here, and if not I still never dealt with hex in my coding assignments). if someone could go through and maybe help by either commenting, or translating this to higher level c/c++ for understanding.

the book explains that these are supposed to be optimized versions of the functions compared to stllib, but as you can see the code is not very well commented (these lines where copied character for character)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:02:15+00:00Added an answer on May 31, 2026 at 11:02 am

    You don’t really need to understand what they’re doing. If you want, you can google bitshift operators in C and understand what the operators are doing, but that probably won’t help you here to understand the code. Really, the code just contains very tricky ways to do thing very very fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading a book on templates and found the following piece of code:
At a website , I found the following code to make a jQuery plugin:
I found the following code in the book Accelerated C++ (Chapter 6.1.1), but I
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
I found the following code somewhere, but I am not understanding the code properly.
I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m
The following code can be found in the NHibernate.Id.GuidCombGenerator class. The algorithm creates sequential
According to what I have found so far, I can use the following code:
The following javascript code concatenates the matches found by a regular expression. The regular
I was just reviewing some old code and found the following (inside foo.asp): Const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.