Eliminate type-punning and replace it with one that is not fragile in the face of smoothing:
#include <string.h>
inline float __HTON_F32(float x) {
int i;
memcpy(&i, &x, sizeof x);
i = HTON_I32(i);
memcpy(&x, &i, sizeof x);
return x;
}
Intelligent optimizing compilers will skip calls memcpyand generate equivalent (sometimes better) code for what you get from punning.
Another common solution you will see is unions. All of these solutions suggest that sizeof(int) == sizeof(float). You can add a statement about this.
source
share