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

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



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?
Is this part of a HOWTO?

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
							    -- Linus Torvalds



Reply to: