I've recently been working on a system that needs to store and load large quantities of data, including single-precision floating-point values. I decided to standardise on network byte order for integers, and also decided to store floating point values in big-endian format, i.e.:
|-- Byte 0 --| |-- Byte 1 -| Byte 2 Byte 3
# ####### # ####### ######## ########
Sign Exponent Mantissa
1b 8b, MSB first 23b, MSB first
Ideally, I want to provide functions like htonl()
and ntohl()
, since I have already been using these for swabbing integers, and I also want to implement this in a way that has as much platform-independence as possible (while assuming that the float
type corresponds to IEEE754 32-bit floating point values). Is there some way, possibly using ieee754.h
, to do this?
I have one answer that seems to work, and I will post it below, but it seems pretty slow and inefficient and I would appreciate any suggestions about how to make it faster and/or more reliable.
Here's a portable IEEE 754 write routine. It will write a double in IEEE 754 format, regardless of the floating point representation on the host machine.
Much simpler, and depending on the same assumption as yours (which is that float and integer types have the same byte order, and is almost universally valid -- realistically you'll never encounter a system where it isn't true):
Any reasonably good compiler will optimize away the two
memcpy
calls; they are present to defeat over-eager strict aliasing optimizations, so this ends up being as efficient ashtonl
plus the overhead of a single function call.As mentioned in the question above, I have a solution to my problem, but I'm not particularly attached to it, and I welcome other answers, so I'm posting it here rather than in the question. In particular, it seems likely to be slow, and I'm not sure whether it breaks strict aliasing, among other potential problems.