DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Code fragments for handling byte order (``endianness'')

Code fragments for handling byte order (endianness)

This topic contains code fragments that can be used in drivers and other code that must deal with endianness. This code is relevant to SVR5 and SCO OpenServer 5. See ``Endian issues for drivers'' for a discussion.

The following files are included. The same code works for both SVR5 DDI drivers and SCO OpenServer 5 ODDI drivers.

Both code fragments can be included as in your driver's header file, or can be used as models for functions you code yourself.

All device accesses must be bracketed by macros to encapsulate the endianness handling.

The driver typically knows the endianness of the target device. In this example, the card_is_little_endian variable is set by the programmer, based on the capabilities of the card. This fragment shows complete host endian-independence.

   #define  WRITE_BOARD_32(a,d)  a = must_swap ? swap_32(d) : d
   #define  WRITE_BOARD_16(a,d)  a = must_swap ? swap_16(d) : d
   #define  READ_BOARD_32(d)     must_swap ? swap_32(d) : d
   #define  READ_BOARD_16(d)     must_swap ? swap_16(d) : d
   

int must_swap = is_little_endian() ^ card_is_little_endian;

If you know the endianness of your host at compilation time, you can reduce the runtime cost with code such as the following:
   #if YOU_KNOW_HOST_ENDIANNESS != DEVICE_ENDIANNESS
   #  define  WRITE_BOARD_32(a,d)  a = swap_32(d)
   #  define  WRITE_BOARD_16(a,d)  a = swap_16(d)
   #  define  READ_BOARD_32(d)     swap_32(d)
   #  define  READ_BOARD_16(d)     swap_16(d)
   

#else # define WRITE_BOARD_32(a,d) a = d # define WRITE_BOARD_16(a,d) a = d # define READ_BOARD_32(d) d # define READ_BOARD_16(d) d #endif


© 2005 The SCO Group, Inc. All rights reserved.
OpenServer 6 and UnixWare (SVR5) HDK - June 2005