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

  • Home
  • SEARCH
  • 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 6720851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:15:38+00:00 2026-05-26T09:15:38+00:00

The function below calculates absolute value of 32-bit floating point value: __forceinline static float

  • 0

The function below calculates absolute value of 32-bit floating point value:

__forceinline static float Abs(float x)
{
    union {
        float x;
        int a;
    } u;
    //u.x = x;
    u.a &= 0x7FFFFFFF;
    return u.x;
}

union u declared in the function holds variable x, which is different from the x which is passed as parameter in the function. Is there any way to create a union with argument to the function – x?

Any reason the function above with uncommented line be executing longer than this one?

__forceinline float fastAbs(float a)
{
    int b= *((int *)&a) & 0x7FFFFFFF;
    return *((float *)(&b));
}

I’m trying to figure out best way to take Abs of floating point value in as little count of read/writes to memory as possible.

  • 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-26T09:15:39+00:00Added an answer on May 26, 2026 at 9:15 am

    Looking at the disassembly of the code compiled in release mode the difference is quite clear!
    I removed the inline and used two virtual function to allow the compiler to not optimize too much and let us show the differences.

    This is the first function.

    013D1002  in          al,dx  
                union {
                    float x;
                    int a;
                } u;
                u.x = x;
    013D1003  fld         dword ptr [x]   // Loads a float on top of the FPU STACK.
    013D1006  fstp        dword ptr [x]   // Pops a Float Number from the top of the FPU Stack into the destination address.
                u.a &= 0x7FFFFFFF;
    013D1009  and         dword ptr [x],7FFFFFFFh  // Execute a 32 bit binary and operation with the specified address.
                return u.x;
    013D1010  fld         dword ptr [x]  // Loads the result on top of the FPU stack.
            }
    

    This is the second function.

    013D1020  push        ebp                       // Standard function entry... i'm using a virtual function here to show the difference.
    013D1021  mov         ebp,esp
                int b= *((int *)&a) & 0x7FFFFFFF;
    013D1023  mov         eax,dword ptr [a]         // Load into eax our parameter.
    013D1026  and         eax,7FFFFFFFh             // Execute 32 bit binary and between our register and our constant.
    013D102B  mov         dword ptr [a],eax         // Move the register value into our destination variable
                return *((float *)(&b));
    013D102E  fld         dword ptr [a]             // Loads the result on top of the FPU stack.
    

    The number of floating point operations and the usage of FPU stack in the first case is greater.
    The functions are executing exactly what you asked, so no surprise.
    So i expect the second function to be faster.

    Now… removing the virtual and inlining things are a little different, is hard to write the disassembly code here because of course the compiler does a good job, but i repeat, if values are not constants, the compiler will use more floating point operation in the first function.
    Of course, integer operations are faster than floating point operations.

    Are you sure that directly using math.h abs function is slower than your method?
    If correctly inlined, abs function will just do this!

    00D71016  fabs  
    

    Micro-optimizations like this are hard to see in long code, but if your function is called in a long chain of floating point operations, fabs will work better since values will be already in FPU stack or in SSE registers! abs would be faster and better optimized by the compiler.

    You cannot measure the performances of optimizations running a loop in a piece of code, you must see how the compiler mix all together in the real code.

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

Sidebar

Related Questions

the function calculates an insurance premium based on a set value for each month.
I have created a function (see below) that calculates a 7.5% sales tax. Now
Added a bit more assembly on top of the function and below it to
The function below prints a color raster image to a PCL-5 printer. The function
The function below is logging the 0, z and the 1 ok... but its
The function below takes a python file handle, reads in packed binary data from
My function below, will take the values from my custom meta fields (after a
My function below calls a partial view after a user enters a filter-by string
I am using the function below inside a thread to receive the byte[] via
I am using the function below to open the user's default web browser. Public

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.