Most efficient standard-compliant way of reinterpreting int as float

后端 未结 4 1770
不思量自难忘°
不思量自难忘° 2020-12-13 18:20

Assume I have guarantees that float is IEEE 754 binary32. Given a bit pattern that corresponds to a valid float, stored in std::uint32_t, how does

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 19:13

    memcpy is always safe but does involve a copy

    casting may lead to problems

    union - seems to be allowed in C99 and C11, not sure about C++

    Take a look at:

    What is the strict aliasing rule?

    and

    Is type-punning through a union unspecified in C99, and has it become specified in C11?

提交回复
热议问题