简体   繁体   中英

Using integer and shifting to imitate floating point operations

my question is scoping efficiency in a C++-Framework I'm currently developing. The context: I have two arrays equal in size. One of them contains point coordinates from a detected point cloud, the other one the corresponding color-values. The problem arose that the color values indices do not exactly match the point coordinates so the color array needs to be scaled.

To keep the efficiency of the system, I was advised to use the following "hack" to avoid floating point operations in order to let the shifted_row_index run slower than x in order to scale the color array index. Unfortunately the advisor is not available at the moment, and I don't understand why this solutions works in this case.

It would be nice of you, if someone could explain it to me. Thanks in advance!

int shifted_row_index = (RGB_OFFSET_X << 8);
for (int x = 0, xmax = g_depthMD.XRes(); x < xmax; x += raster_width)
{
     shifted_row_index += RGB_SCALE_FACTOR_X;
     int rowindex = shifted_row_index >> 8;
     rowindex = std::max(std::min(rowindex, (int)(xmax - 1)), 0);
     color_pointer[0] = pImageRow[rowindex].nRed;
     //....
}

The only "bit hack" operations I see are the left and right shift operations. I see no reason for you to use them as any reasonable compiler will optimize the code:

int shifted_row_index = (RGB_OFFSET_X * 64);

And

int rowindex = shifted_row_index / 64;

To be just as efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM