4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README for information.
8 * \brief Functions to convert integers to/from host byte-order.
12 * \author Bernardo Innocenti <bernie@develer.com>
13 * \author Stefano Fedrigo <aleph@develer.com>
18 *#* Revision 1.5 2004/08/25 14:12:09 rasky
19 *#* Aggiornato il comment block dei log RCS
21 *#* Revision 1.4 2004/07/22 01:08:43 bernie
22 *#* swab32(): Fix a very serious bug.
24 *#* Revision 1.3 2004/07/20 23:47:12 bernie
25 *#* Finally remove redundant protos.
27 *#* Revision 1.2 2004/07/20 17:09:11 bernie
28 *#* swab16(), swab32(), cpu_to_be32(), cpu_to_le32(): New functions.
30 *#* Revision 1.1 2004/07/20 16:26:15 bernie
31 *#* Import byte-order macros into DevLib.
35 #ifndef MWARE_BYTEORDER_H
36 #define MWARE_BYTEORDER_H
42 * \brief Swap upper and lower bytes in a 16-bit value.
44 INLINE uint16_t swab16(uint16_t x)
46 return ((x & (uint16_t)0x00FFU) << 8)
47 | ((x & (uint16_t)0xFF00U) >> 8);
51 * \brief Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
53 INLINE uint32_t swab32(uint32_t x)
55 return ((x & (uint32_t)0x000000FFUL) << 24)
56 | ((x & (uint32_t)0x0000FF00UL) << 8)
57 | ((x & (uint32_t)0x00FF0000UL) >> 8)
58 | ((x & (uint32_t)0xFF000000UL) >> 24);
61 INLINE uint16_t cpu_to_be16(uint16_t x)
63 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
66 INLINE uint16_t cpu_to_le16(uint16_t x)
68 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
71 INLINE uint32_t cpu_to_be32(uint32_t x)
73 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
76 INLINE uint32_t cpu_to_le32(uint32_t x)
78 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
81 #endif /* MWARE_BYTEORDER_H */