diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2018-05-04 14:15:12 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2018-05-21 09:44:40 -0400 |
commit | c2d48aba874ef3d8afbd5142f47793116f2293f1 (patch) | |
tree | 4779a37eb127e47cfca04902d093a70d184dd69a | |
parent | e3e3aa63d746f9187d5733bef627343771eb4a9d (diff) | |
download | kutter-c2d48aba874ef3d8afbd5142f47793116f2293f1.tar.gz kutter-c2d48aba874ef3d8afbd5142f47793116f2293f1.tar.xz kutter-c2d48aba874ef3d8afbd5142f47793116f2293f1.zip |
byteorder: Add a header file defining common byteswap functions
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r-- | src/byteorder.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/byteorder.h b/src/byteorder.h new file mode 100644 index 00000000..0ecd68d3 --- /dev/null +++ b/src/byteorder.h @@ -0,0 +1,40 @@ +#ifndef __BYTEORDER_H +#define __BYTEORDER_H + +#include <stdint.h> // uint32_t + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + +#define cpu_to_le16(x) ((uint16_t)(x)) +#define cpu_to_le32(x) ((uint32_t)(x)) +#define cpu_to_le64(x) ((uint64_t)(x)) +#define le16_to_cpu(x) ((uint16_t)(x)) +#define le32_to_cpu(x) ((uint32_t)(x)) +#define le64_to_cpu(x) ((uint64_t)(x)) + +#define cpu_to_be16(x) __builtin_bswap16(x) +#define cpu_to_be32(x) __builtin_bswap32(x) +#define cpu_to_be64(x) __builtin_bswap64(x) +#define be16_to_cpu(x) __builtin_bswap16(x) +#define be32_to_cpu(x) __builtin_bswap32(x) +#define be64_to_cpu(x) __builtin_bswap64(x) + +#else // big endian + +#define cpu_to_le16(x) __builtin_bswap16(x) +#define cpu_to_le32(x) __builtin_bswap32(x) +#define cpu_to_le64(x) __builtin_bswap64(x) +#define le16_to_cpu(x) __builtin_bswap16(x) +#define le32_to_cpu(x) __builtin_bswap32(x) +#define le64_to_cpu(x) __builtin_bswap64(x) + +#define cpu_to_be16(x) ((uint16_t)(x)) +#define cpu_to_be32(x) ((uint32_t)(x)) +#define cpu_to_be64(x) ((uint64_t)(x)) +#define be16_to_cpu(x) ((uint16_t)(x)) +#define be32_to_cpu(x) ((uint32_t)(x)) +#define be64_to_cpu(x) ((uint64_t)(x)) + +#endif + +#endif // byteorder.h |