I am cleaning up some of our network-code and while replacing various integer types (int, unsigned short, ...) with more explcit typedefs such as int32_t and uint16_t I was wondering how portable double and float are between different compilers. We are sending structures similar to the following:
struct foo
{
uint32_t id;
double dbl;
};
...
// copy_packed() copies packed bytes of integral type and adheres
// to network byte-ordering
copy_packed(int32_t data, char* buffer, char* end);
copy_packed(uint32_t data, char* buffer, char* end);
copy_packed(uint16_t data, char* buffer, char* end);
...
// overload for structure types calls overloads for its members
void copy_packed(foo &data, char* buffer, char* end)
{
copy_packed(data.id, buffer);
copy_packed(data.id, buffer);
};
Until now only programs compiled with VC++ are used, but use of the protocol on GCC is scheduled.
The question is now: can double amd float be safely send over the wire even when then interpreted by different compilers? The question - as far as I understand - boils down to whether GCC and VC++ comply to IEEE754. Or maybe provide functions to convert to conformant packaging?
Any hints on the matter?
Not very. IEEE doesn't define byte order, and not all systems are IEEE.
The real question is how portable you have to be. IEEE is pretty much
universal for modern Unix platforms, as well as Windows; if your
portability is limited to these, you can generally type pun a float
into a uint32_t, and a double into a uint64_t, and handle input and
output that way. If you have to take mainframes into consideration (and
possibly embedded processors—I'm less familiar about those),
you'll have a lot more work to do; floating point on the major
mainframes isn't even base 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With