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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:55:47+00:00 2026-05-25T01:55:47+00:00

I’m currently converting a program that was originally intended for OpenCL to C++ and

  • 0

I’m currently converting a program that was originally intended for OpenCL to C++ and I’m having a bit of trouble with one particular part of it.

One of the expressions commonly used in said program involves taking a 32 bit float, converting it to an integer (i.e. not actually rounding it to an int, but interpreting the same data as an int – think reinterpret_cast), performing some bit twiddling magic on it and then converting it back to a float (once again, not actual conversion, but reinterpretation of the same data). While this works well in OpenCL, with C++ and gcc this violates strict aliasing rules, breaking the program if optimization is enabled and, depending on the architecture, may involve an expensive load-hit-store since float and integer registers are separated.

I’ve been able to avoid most of these expressions efficiently, but there is one I’m not sure about whether it could be done faster. Basically, the intention is to clear a number of bits from the right of a float; the OpenCL code does this similar to this:

float ClearFloatBits(float Value, int NumberOfBits) {
    return __int_as_float((__float_as_int(Value) >> NumberOfBits) << NumberOfBits);
}

Since this is essentially rounding down from a specified (binary) digit, my C++ version now looks like this:

float ClearFloatBits(float Value, int NumberOfBits) {
    float Factor = pow(2.0f, 23 - NumberOfBits);

    return ((int)(Value*Factor))/Factor;
}

Where the pow and the division are of course replaced by a LUT lookup and a respective multiplication, here omitted for better readability.

Is there a better way to do this? What bugs me in particular is the (int) conversion to round down, which I guess is the most expensive part. It is guaranteed that the float passed to the function is a number between 1.0 (inclusive) and 2.0 (exclusive), if that helps.

Thanks in advance

  • 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-25T01:55:47+00:00Added an answer on May 25, 2026 at 1:55 am

    Use the union hack instead:

    float ClearFloatBits(float Value, int NumberOfBits) {
       union { unsigned int int_val; float flt_val; } union_hack;
       union_hack.flt_val = Value;
       (union_hack.int_val >>= NumberOfBits) <<= NumberOfBits;
       return union_hack.flt_val;
    }
    

    Strictly speaking, this is undefined behavior. Per both the C and C++ standards, it is illegal to write the result of writing to one member of a union and then reading from another member without first writing to that other member is undefined.

    However, this usage of unions is so widespread and so ancient that no compiler writer that I know of obeys the standard. In practice, the behavior is very well defined and is exactly what you would expect. That said, this hack might not work if ported to some very strange architecture machine that uses a very strictly conforming compiler.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT

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.