3

My program reads into 4 bytes an IEEE 754 floating point number from a file. I need to portable convert those bytes to my C compilers float type. In other words I need a function with the prototype float IEEE_754_to_float(uint8_t raw_value[4]) for my C program.

7
  • 1
    There isn't going to be a 100% portable way due to endianness. But I bet you can get away with just a simple union.
    – Mysticial
    Sep 3, 2012 at 5:00
  • @Mysticial, I was wondering about that. I know that there is a 100% portable way to accomplish this for big endian integer numbers. Why should floating point be any different? Sep 3, 2012 at 5:01
  • Well, for one, you can't shift floating-point numbers. So you can't really "construct" a floating-point value byte-by-byte using just floating-point operations.
    – Mysticial
    Sep 3, 2012 at 5:05
  • @Mysticial, but a floating point number is just a number to the power of another number, and a sign bit. Isn't it possible to just do sign_bit * powf(x, y)? Sep 3, 2012 at 5:14
  • 1
    You could do it that way. Probably not the best idea if performance matters. (You'd also have to check for infinities and NaN...) But the other issue is the endian of the input bytes. So I can't see any way other than to use preprocessor to check the endian and then choose the appropriate implementation.
    – Mysticial
    Sep 3, 2012 at 5:16

3 Answers 3

5

If your implementation can guarantee correct endianness:

float raw2ieee(uint8_t *raw)
{
    // either
    union {
        uint8_t bytes[4];
        float fp;
    } un;
    memcpy(un.bytes, raw, 4);
    return un.fp;

    // or, as seen in the fast inverse square root:
    return *(float *)raw;
}
2

If the endianness is the same, then like so:

float f;
memcpy(&f, raw_value, sizeof f);
return f;

If not, say:

float f;
char * p = (char *)&f;

And now populate the bytes p[0]... manually as needed.

1

Here is a solution that portable transforms an IEEE_754 number to one's C compiler's float value. This code works but the loop to get the value of the fraction is is ugly, and can be done better. As well, this code does not handle special cases like infinity, and not a number.

float IEEE_754_to_float(const uint8_t raw[4]) {
        int sign = (raw[0] >> 7) ? -1 : 1;

        int8_t exponent = (raw[0] << 1) + (raw[1] >> 7) - 126;

        uint32_t fraction_bits = ((raw[1] & 0x7F) << 16) + (raw[2] << 8) + raw[3];

        float fraction = 0.5f;
        for (uint8_t ii = 0; ii < 24; ++ii)
                fraction += ldexpf((fraction_bits >> (23 - ii)) & 1, -(ii + 1));

        float significand = sign * fraction;

        return ldexpf(significand, exponent);
}
1
  • There is no need to process each fraction bit separately. Simply define float fraction = 1<<23 | fraction_bits; and adjust the exponent in the final ldexpf. (Omit the 1<<23 if the raw exponent is zero, to handle denormals). Any 24-bit integer can be converted to IEEE 754 float without error. (Also, your loop includes an iteration where ii is 23, which has no effect, since bit 23 of your fraction_bits is 0.) Sep 4, 2012 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.