Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 Otto-von-Guericke-Universität Magdeburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for more
6 * details.
7 */
8
23#ifndef ENDIAN_H
24#define ENDIAN_H
25
26#include <stdint.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#ifdef DOXYGEN
36#define LITTLE_ENDIAN magic-number
37
41#define BIG_ENDIAN magic-number
42
46#define PDP_ENDIAN magic-number
47
56#define BYTE_ORDER <LITTLE_ENDIAN or BIG_ENDIAN>
57
58uint16_t htobe16(uint16_t host_16bits);
59uint16_t htole16(uint16_t host_16bits);
60uint16_t be16toh(uint16_t big_endian_16bits);
61uint16_t le16toh(uint16_t little_endian_16bits);
63uint32_t htobe32(uint32_t host_32bits);
64uint32_t htole32(uint32_t host_32bits);
65uint32_t be32toh(uint32_t big_endian_32bits);
66uint32_t le32toh(uint32_t little_endian_32bits);
68uint64_t htobe64(uint64_t host_64bits);
69uint64_t htole64(uint64_t host_64bits);
70uint64_t be64toh(uint64_t big_endian_64bits);
71uint64_t le64toh(uint64_t little_endian_64bits);
73#else /* DOXYGEN */
74
75/* Depending on the version of newlib used, newlib may provide them indirectly
76 * as well. We don't want to redefine them in this case */
77#ifndef LITTLE_ENDIAN
78# define LITTLE_ENDIAN 1234
79#endif
80#ifndef BIG_ENDIAN
81# define BIG_ENDIAN 4321
82#endif
83#ifndef PDP_ENDIAN
84# define PDP_ENDIAN 3412
85#endif
86#ifndef BYTE_ORDER
87# define BYTE_ORDER __BYTE_ORDER__
88#endif
89
90/* But to avoid lots of pain in the ass: Let's at least make sure everyone
91 * agrees on what magic number is what */
92#if (LITTLE_ENDIAN != 1234) || (BIG_ENDIAN != 4321) || (PDP_ENDIAN != 3412)
93# error "Mismatching magic numbers to refer to endianness"
94#endif
95
96#if BYTE_ORDER == LITTLE_ENDIAN
97# define htobe16(_x) __builtin_bswap16(_x)
98# define htole16(_x) ((uint16_t)(_x))
99# define be16toh(_x) __builtin_bswap16(_x)
100# define le16toh(_x) ((uint16_t)(_x))
101# define htobe32(_x) __builtin_bswap32(_x)
102# define htole32(_x) ((uint32_t)(_x))
103# define be32toh(_x) __builtin_bswap32(_x)
104# define le32toh(_x) ((uint32_t)(_x))
105# define htobe64(_x) __builtin_bswap64(_x)
106# define htole64(_x) ((uint64_t)(_x))
107# define be64toh(_x) __builtin_bswap64(_x)
108# define le64toh(_x) ((uint64_t)(_x))
109#elif BYTE_ORDER == BIG_ENDIAN
110# define htole16(_x) __builtin_bswap16(_x)
111# define htobe16(_x) ((uint16_t)(_x))
112# define le16toh(_x) __builtin_bswap16(_x)
113# define be16toh(_x) ((uint16_t)(_x))
114# define htole32(_x) __builtin_bswap32(_x)
115# define htobe32(_x) ((uint32_t)(_x))
116# define le32toh(_x) __builtin_bswap32(_x)
117# define be32toh(_x) ((uint32_t)(_x))
118# define htole64(_x) __builtin_bswap64(_x)
119# define htobe64(_x) ((uint64_t)(_x))
120# define le64toh(_x) __builtin_bswap64(_x)
121# define be64toh(_x) ((uint64_t)(_x))
122#else
123# error "Byte order not supported"
124#endif
125
126#endif /* DOXYGEN */
127
128#ifdef __cplusplus
129}
130#endif
131
132#endif /* ENDIAN_H */
uint16_t be16toh(uint16_t big_endian_16bits)
big endian to host, 16 bit
uint16_t htobe16(uint16_t host_16bits)
host to big endian, 16 bit
uint32_t le32toh(uint32_t little_endian_32bits)
little endian to host, 32 bit
uint32_t htobe32(uint32_t host_32bits)
host to big endian, 32 bit
uint16_t le16toh(uint16_t little_endian_16bits)
little endian to host, 16 bit
uint64_t htobe64(uint64_t host_64bits)
host to big endian, 64 bit
uint64_t be64toh(uint64_t big_endian_64bits)
big endian to host, 64 bit
uint32_t be32toh(uint32_t big_endian_32bits)
big endian to host, 32 bit
uint64_t htole64(uint64_t host_64bits)
host to little endian, 64 bit
uint32_t htole32(uint32_t host_32bits)
host to little endian, 32 bit
uint16_t htole16(uint16_t host_16bits)
host to little endian, 16 bit
uint64_t le64toh(uint64_t little_endian_64bits)
little endian to host, 64 bit