Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Portability of Misaligned Data Access . . . . . . .



In my experience at getting random boxes with badly designed
protocols/file formats talking to each other, I've concluded:
1. never use byte equivalency between types, be it union or memcpy
2. the endianness will always be wrong.


On point (2), let it be noted that you *never*, *ever* have to
discover the endianness of your host:

uint16_t read_le_u16(char *x) {
  return (uint16_t)x[0]+(((uint16_t)x[1]) <<8);
}

In the nature of these things, this looks obviously inefficient and
slow, and therefore it's almost guaranteed not to be the thing that
takes up the majority of the time (rule #3).

Doing the same thing, portably, for a double in IEE754 format is left
as an exercise to the bored reader.  Who, it is to be hoped, doesn't
have to port to VAXen, where the FP formats have a completely
different range and accuracy.  Believe me, I know.

--
Ian.


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links