Byte Order Conversion
[Utility Library]

Collaboration diagram for Byte Order Conversion:

Conversion to/from Little Endian values



static uint16_t le16_to_cpu (le16_t x)
 Convert a 16-bit word from little endian to native byte order.
static uint32_t le32_to_cpu (le32_t x)
 Convert a 32-bit word from little endian to native byte order.
static le16_t cpu_to_le16 (uint16_t x)
 Convert a 16-bit word from native to little endian byte order.
static le32_t cpu_to_le32 (uint32_t x)
 Convert a 32-bit word from native to little endian byte order.
#define LE16(x)   ((le16_t __force)(x))
 Create a 16-bit little endian constant.
#define LE32(x)   ((le32_t __force)(x))
 Create a 32-bit little endian constant.

Conversion to/from Big Endian values



static uint16_t be16_to_cpu (be16_t x)
 Convert a 16-bit word from big endian to native byte order.
static uint32_t be32_to_cpu (be32_t x)
 Convert a 32-bit word from big endian to native byte order.
static be16_t cpu_to_be16 (uint16_t x)
 Convert a 16-bit word from native to big endian byte order.
static be32_t cpu_to_be32 (uint32_t x)
 Convert a 32-bit word from native to big endian byte order.
#define BE16(x)
 Create a 16-bit big endian constant.
#define BE32(x)
 Create a 32-bit big endian constant.

Byte Swapping



#define swab32(x)
 Swap the order of bytes in a 32-bit word.
#define swab16(x)   ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
 Swap the order of bytes in a 16-bit word.

Detailed Description

When communicating with the world outside the CPU on which the code is running, it is important that multi-byte values are interpreted the same way by all parties. Unfortunately, the CPUs of the world do interpret things differently unless special care is taken by the programmer; the vast majority of CPUs can be divided into two groups, depending on how multi-byte values are interpreted:

Every time two processors need to communicate, or need to access the same on-disk filesystem, or share any data in any way, the byte ordering (or endianness) must be clearly defined, and all processors must accept this regardless of their native byte ordering.

This module provides a set of helper functions which can be used to read and write data using a pre-defined byte order, regardless of the native byte ordering of the CPU. Byte swapping is performed as necessary, but if the pre-defined byte order matches the native byte order, no extra overhead is added.

Example
In the following example, a packet is received over some kind of network from some other, unknown CPU. The network protocol dictates that the sequence number is transfered as little endian (i.e. least significant byte first). The function get_seq_number() below will return the correct sequence number regardless of the CPU's native byte order.
struct packet {
        le32_t seq_number;
        le16_t size;
        uint8_t data[];
};

uint32_t get_seq_number(const struct packet *pkt)
{
        return le32_to_cpu(pkt->seq_number);
} 

Note that the helper functions in this module is the only legal way to convert between endian-aware types and native types. For example, to convert from a le16_t variable to a uint16_t variable, the function le16_to_cpu() must be used.


Define Documentation

#define BE16 (  ) 
Value:
((be16_t __force)((((x) & 0xff) << 8)           \
                                        | (((x) >> 8) & 0xff)))

Create a 16-bit big endian constant.

Definition at line 224 of file byteorder.h.

#define BE32 (  ) 
Value:
((be32_t __force)((((x) & 0xff) << 24)          \
                                        | (((x) & 0xff00) << 8)         \
                                        | (((x) >> 8) & 0xff00)         \
                                        | (((x) >> 24) & 0xff)))

Create a 32-bit big endian constant.

Definition at line 226 of file byteorder.h.

#define LE16 (  )     ((le16_t __force)(x))

Create a 16-bit little endian constant.

Definition at line 222 of file byteorder.h.

#define LE32 (  )     ((le32_t __force)(x))

Create a 32-bit little endian constant.

Definition at line 223 of file byteorder.h.

#define swab16 (  )     ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))

Swap the order of bytes in a 16-bit word.

Definition at line 172 of file byteorder.h.

Referenced by be16_to_cpu(), and cpu_to_be16().

#define swab32 (  ) 
Value:
((((x) & 0xff) << 24) | (((x) & 0xff00) << 8)   \
         | (((x) >> 8) & 0xff00) | (((x) >> 24) & 0xff))

Swap the order of bytes in a 32-bit word.

Definition at line 167 of file byteorder.h.

Referenced by be32_to_cpu(), and cpu_to_be32().


Function Documentation

uint16_t be16_to_cpu ( be16_t  x  )  [inline, static]

Convert a 16-bit word from big endian to native byte order.

Definition at line 248 of file byteorder.h.

References __force, and swab16.

uint32_t be32_to_cpu ( be32_t  x  )  [inline, static]

Convert a 32-bit word from big endian to native byte order.

Definition at line 252 of file byteorder.h.

References __force, and swab32.

be16_t cpu_to_be16 ( uint16_t  x  )  [inline, static]

Convert a 16-bit word from native to big endian byte order.

Definition at line 256 of file byteorder.h.

References __force, and swab16.

be32_t cpu_to_be32 ( uint32_t  x  )  [inline, static]

Convert a 32-bit word from native to big endian byte order.

Definition at line 260 of file byteorder.h.

References __force, and swab32.

le16_t cpu_to_le16 ( uint16_t  x  )  [inline, static]

Convert a 16-bit word from native to little endian byte order.

Definition at line 239 of file byteorder.h.

References __force.

le32_t cpu_to_le32 ( uint32_t  x  )  [inline, static]

Convert a 32-bit word from native to little endian byte order.

Definition at line 243 of file byteorder.h.

References __force.

uint16_t le16_to_cpu ( le16_t  x  )  [inline, static]

Convert a 16-bit word from little endian to native byte order.

Definition at line 231 of file byteorder.h.

References __force.

uint32_t le32_to_cpu ( le32_t  x  )  [inline, static]

Convert a 32-bit word from little endian to native byte order.

Definition at line 235 of file byteorder.h.

References __force.

Generated on Thu Apr 29 14:09:43 2010 for uart-loopback by  doxygen 1.6.3