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# ifndef htobe16
98# define htobe16(_x) __builtin_bswap16(_x)
99# endif
100# ifndef htole16
101# define htole16(_x) ((uint16_t)(_x))
102# endif
103# ifndef be16toh
104# define be16toh(_x) __builtin_bswap16(_x)
105# endif
106# ifndef le16toh
107# define le16toh(_x) ((uint16_t)(_x))
108# endif
109# ifndef htobe32
110# define htobe32(_x) __builtin_bswap32(_x)
111# endif
112# ifndef htole32
113# define htole32(_x) ((uint32_t)(_x))
114# endif
115# ifndef be32toh
116# define be32toh(_x) __builtin_bswap32(_x)
117# endif
118# ifndef le32toh
119# define le32toh(_x) ((uint32_t)(_x))
120# endif
121# ifndef htobe64
122# define htobe64(_x) __builtin_bswap64(_x)
123# endif
124# ifndef htole64
125# define htole64(_x) ((uint64_t)(_x))
126# endif
127# ifndef be64toh
128# define be64toh(_x) __builtin_bswap64(_x)
129# endif
130# ifndef le64toh
131# define le64toh(_x) ((uint64_t)(_x))
132# endif
133#elif BYTE_ORDER == BIG_ENDIAN
134# ifndef htole16
135# define htole16(_x) __builtin_bswap16(_x)
136# endif
137# ifndef htobe16
138# define htobe16(_x) ((uint16_t)(_x))
139# endif
140# ifndef le16toh
141# define le16toh(_x) __builtin_bswap16(_x)
142# endif
143# ifndef be16toh
144# define be16toh(_x) ((uint16_t)(_x))
145# endif
146# ifndef htole32
147# define htole32(_x) __builtin_bswap32(_x)
148# endif
149# ifndef htobe32
150# define htobe32(_x) ((uint32_t)(_x))
151# endif
152# ifndef le32toh
153# define le32toh(_x) __builtin_bswap32(_x)
154# endif
155# ifndef be32toh
156# define be32toh(_x) ((uint32_t)(_x))
157# endif
158# ifndef htole64
159# define htole64(_x) __builtin_bswap64(_x)
160# endif
161# ifndef htobe64
162# define htobe64(_x) ((uint64_t)(_x))
163# endif
164# ifndef le64toh
165# define le64toh(_x) __builtin_bswap64(_x)
166# endif
167# ifndef be64toh
168# define be64toh(_x) ((uint64_t)(_x))
169# endif
170#else
171# error "Byte order not supported"
172#endif
173
174#endif /* DOXYGEN */
175
176#ifdef __cplusplus
177}
178#endif
179
180#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