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

Re: What does it take to make code endian clean?

On Tue, 13 Mar 2001, Geert Uytterhoeven wrote:

> On 13 Mar 2001, Gjermund Gusland Thorsen wrote:
> > What does it take to make code not depending on little or big endian
>   - Never make assumptions about the ordering of bytes in a multi-word entity
>     (short, int, long, long long). I.e. don't cast the address of an int to a
>     char pointer and access the individual bytes. Avoid casts of pointers to
>     pointers to entities of different sizes.
>   - When accessing the hardware, use the correct functions for the specific bus
>     type. PCI memory space requires {read,write}[bwl](), PCI I/O space requires
>     {in,out}[bwl](). These functions take care of the PCI bus being little
>     endian.
>   - When handling data that is in a specific endianness, use the cpu_to[bl]e*()
>     and [bl]e*_to_cpu() conversion routines. In the case of TCP/IP (which is
>     big endian), use the hton*() and ntoh*() functions.
> Anything I'm missing?

Yes: what he was really complaining about was hardcoded Intel assembly in
some source, not endianness issues :-) What does this take? Repeated
flaming of the code author...

Other than that, I think your HOWTO pretty much covers it. Writing code
that is supposed to exchange structured data among machines running a
variety of endianness and word size is another HOWTO (RPC, XDR and


Reply to: