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 8102825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:20:29+00:00 2026-06-05T23:20:29+00:00

There is a following function that is supposed to make a comparison between 2

  • 0

There is a following function that is supposed to make a comparison between 2 floating point values, but faster than regular comparison in some specific cases (e.g. on Cortex-A8)

int isGreater(float* f1, float* f2)
{
    int i1, i2, t1, t2;

    i1 = *(int*)f1; // reading float as integer
    i2 = *(int*)f2; // reading float as integer

    t1 = i1 >> 31;
    i1 = (i1 ^ t1) + (t1 & 0x80000001);

    t2 = i2 >> 31;
    i2 = (i2 ^ t2) + (t2 & 0x80000001);

    return i1 > i2;
}

Can someone explain how does it exactly work?

  • 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-06-05T23:20:30+00:00Added an answer on June 5, 2026 at 11:20 pm

    This code exploits the structure of the IEEE 754 format for floating point numbers. The structure itself was specifically designed for such operations in order to make comparison operations fast.

    Each single-precision IEEE 754 number has three parts (in order from MSB to LSB):

    • sign bit
    • exponent part (8 bits)
    • significand of the mantissa (23 bits)

    f1 is greater than f2 if:

    • f1 is positive and f2 is negative
    • f1 and f2 are both positive but f1 has greater exponent than f2
    • f1 and f2 are both positive and have the same exponents but f1 has larger significand than f2
    • the opposite of the previous two if f1 and f2 are negative

    One could just compare both floating point numbers as integers if they were in two’s complement representation. Unfortunately IEEE 754 doesn’t use two’s complement to represent negative numbers and that’s why this code performs the conversion in order to be able to just compare the numbers as signed integers.

    Here is a step by step commentary on what each line of code does:

    i1 = *(int*)f1; // reading float as integer
    i2 = *(int*)f2; // reading float as integer
    

    This one uses the fact that on most 32-bit systems sizeof(int) == sizeof(float) to read the floating point numbers into regular signed integer variables.

    t1 = i1 >> 31;
    

    This one extracts the sign bit of f1. If f1 is negative its MSB would be 1 and hence i1 would be negative. Shifting it 31 bits to the right preserves the sign and hence if i1 was negative t1 would have all bits set to 1 (equal to -1). If f1 was positive its sign bit would be 0 and in the end t1 would equal 0.

    i1 = (i1 ^ t1) + (t1 & 0x80000001);
    

    If the sign bit was 1 this line would perform conversion to two’s complement representation if f1 was negative.

    Here is how it works: if f1 was positive, then t1 is 0 and (i1 ^ t1) would just be i1 and (t1 & 0x80000001) would be 0 and in the end i1 would just retain its original value. If f1 was negative then t1 would have all bits set to 1 and the first expression on the RHS would be the bit inversion of i1 and the second expression would equal 0x80000001. This way i1 would be converted to its bit inversion and 1 would be added. But this would lead to a positive number since the MSB would be cleared and that’s why 0x80000000 is also added to keep the number negative.

    t2 = i2 >> 31;
    i2 = (i2 ^ t2) + (t2 & 0x80000001);
    

    Perform the same as above for f2.

    return i1 > i2;
    

    Just compare the two resulting signed integers. Most CPUs have dedicated instructions to perform signed comparison in hardware.

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

Sidebar

Related Questions

Is there a platform function that will do the following? convertBase :: (Num a,
Will there be a difference between the following two ways of calling a function
In the following function, there is the line: var username=getCookie(username); Here's the whole function:
Is there a way to get the following function declaration? public bool Foo<T>() where
In the jquery.cycle.js file, there is the following code: $.fn.cycle.transitions.scrollRight = function($cont, $slides, opts)
I have the following piece of code from a book. There is this function
I currently use the following function to register a dll that handles context menu
In Excel, I have the following formula =(MIN(H69,H52,H35,H18)*(1/H18))*10 that is supposed to return the
Suppose I want to make a function that recursively parses a variadic argument list,
Let's say there's following directory structure: root | +--projects | | | +-test |

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.