Senyai Senyai - 1 year ago 109
C++ Question

Strange uint32_t to float array conversion

I have the following code snippet:

#include <cstdio>
#include <cstdint>

static const size_t ARR_SIZE = 129;

int main()
uint32_t value = 2570980487;

uint32_t arr[ARR_SIZE];
for (int x = 0; x < ARR_SIZE; ++x)
arr[x] = value;

float arr_dst[ARR_SIZE];
for (int x = 0; x < ARR_SIZE; ++x)
arr_dst[x] = static_cast<float>(arr[x]);

printf("%s\n", arr_dst[ARR_SIZE - 1] == arr_dst[ARR_SIZE - 2] ? "OK" : "WTF??!!");

printf("magic = %0.10f\n", arr_dst[ARR_SIZE-2]);
printf("magic = %0.10f\n", arr_dst[ARR_SIZE-1]);
return 0;

If I compile it under MS Visual Studio 2015 I can see that the output is:

magic = 2570980352.0000000000
magic = 2570980608.0000000000

So the last arr_dst element is different from the previous one, yet these two values were obtained by converting the same value, which populates the arr array!
Is it a bug?

I noticed that if I modify the conversion loop in the following manner, I get the "OK" result:

for (int x = 0; x < ARR_SIZE; ++x)
if (x == 0)
x = 0;
arr_dst[x] = static_cast<float>(arr[x]);

So this probably is some issue with vectorizing optimisation.

This behavior does not reproduce on gcc 4.8. Any ideas?

Answer Source

I did an investigation on a PowerPC imeplementation (Freescale MCP7450) as they are far better documented than any voodoo Intel comes up with.

As it turns out the floating point unit, FPU, and vector unit may have different rounding for floating point operations. The FPU can be configured to use one of four rounding modes; round to nearest (default), truncate, towards positive infinity and towards negative infinity. The vector unit however is only able to round to nearest, with a few select instructions having specific rounding rules. The internal precision of the FPU is 106-bit. The vector unit fulfills IEEE-754 but the documentation does not state much more.

Looking at your result the conversion 2570980608 is closer to the original integer, suggesting the FPU has better internal precision than the vector unit OR different rounding modes.

I´m not so sure about the x86 though as they have nearly zero documentation about their processors open for the public.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download