protobuf-c/protobuf-c — pull request #172 by plan44 (2014-10-31 13:19:51+00:00)
Title: Added handling for IEEE_DOUBLE_MIXED_ENDIAN case, which means that memor...

There are older ARM CPUs which have a "mixed endian" in-memory layout of double. This means that while bytes within 32-bit quantities are ordered in little endian, the two long words of which a double consist are ordered in big endian.

Without this patch, **protobuf wire representation of protobuf-c on mixed endian type CPUs is wrong and leads to value corruption** when communicating with standard x86 or ARM peers (first and second longword of double swapped).

The patch adds handling for this case, enabled when IEEE_DOUBLE_MIXED_ENDIAN is defined non-zero.

Note: I haven't found a reliable predefined macro in gcc or the build environment that could be used to automatically set this option, so for the time being IEEE_DOUBLE_MIXED_ENDIAN must be defined manually when building on affected CPUs.

For background information, see https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Endianness - "There are old ARM processors that have half little-endian, half big-endian floating point representation for double-precision numbers: both 32-bit words are stored in little-endian like integer registers, but the most significant one first."

The NS9210 ARM9 SOC with ARM926EJ-S core is an example of such an ARM processor (which led me to realize mixed endian doubles exist, and eventually to solve the problem with this patch).