Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Otto-von-Guericke-Universität Magdeburg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
21
22#include <stdint.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#ifdef DOXYGEN
32#define LITTLE_ENDIAN magic-number
33
37#define BIG_ENDIAN magic-number
38
42#define PDP_ENDIAN magic-number
43
52#define BYTE_ORDER <LITTLE_ENDIAN or BIG_ENDIAN>
53
54uint16_t htobe16(uint16_t host_16bits);
55uint16_t htole16(uint16_t host_16bits);
56uint16_t be16toh(uint16_t big_endian_16bits);
57uint16_t le16toh(uint16_t little_endian_16bits);
58
59uint32_t htobe32(uint32_t host_32bits);
60uint32_t htole32(uint32_t host_32bits);
61uint32_t be32toh(uint32_t big_endian_32bits);
62uint32_t le32toh(uint32_t little_endian_32bits);
63
64uint64_t htobe64(uint64_t host_64bits);
65uint64_t htole64(uint64_t host_64bits);
66uint64_t be64toh(uint64_t big_endian_64bits);
67uint64_t le64toh(uint64_t little_endian_64bits);
68
69#else /* DOXYGEN */
70
71/* Depending on the version of newlib used, newlib may provide them indirectly
72 * as well. We don't want to redefine them in this case */
73#ifndef LITTLE_ENDIAN
74# define LITTLE_ENDIAN 1234
75#endif
76#ifndef BIG_ENDIAN
77# define BIG_ENDIAN 4321
78#endif
79#ifndef PDP_ENDIAN
80# define PDP_ENDIAN 3412
81#endif
82#ifndef BYTE_ORDER
83# define BYTE_ORDER __BYTE_ORDER__
84#endif
85
86/* But to avoid lots of pain in the ass: Let's at least make sure everyone
87 * agrees on what magic number is what */
88#if (LITTLE_ENDIAN != 1234) || (BIG_ENDIAN != 4321) || (PDP_ENDIAN != 3412)
89# error "Mismatching magic numbers to refer to endianness"
90#endif
91
92#if BYTE_ORDER == LITTLE_ENDIAN
93# ifndef htobe16
94# define htobe16(_x) __builtin_bswap16(_x)
95# endif
96# ifndef htole16
97# define htole16(_x) ((uint16_t)(_x))
98# endif
99# ifndef be16toh
100# define be16toh(_x) __builtin_bswap16(_x)
101# endif
102# ifndef le16toh
103# define le16toh(_x) ((uint16_t)(_x))
104# endif
105# ifndef htobe32
106# define htobe32(_x) __builtin_bswap32(_x)
107# endif
108# ifndef htole32
109# define htole32(_x) ((uint32_t)(_x))
110# endif
111# ifndef be32toh
112# define be32toh(_x) __builtin_bswap32(_x)
113# endif
114# ifndef le32toh
115# define le32toh(_x) ((uint32_t)(_x))
116# endif
117# ifndef htobe64
118# define htobe64(_x) __builtin_bswap64(_x)
119# endif
120# ifndef htole64
121# define htole64(_x) ((uint64_t)(_x))
122# endif
123# ifndef be64toh
124# define be64toh(_x) __builtin_bswap64(_x)
125# endif
126# ifndef le64toh
127# define le64toh(_x) ((uint64_t)(_x))
128# endif
129#elif BYTE_ORDER == BIG_ENDIAN
130# ifndef htole16
131# define htole16(_x) __builtin_bswap16(_x)
132# endif
133# ifndef htobe16
134# define htobe16(_x) ((uint16_t)(_x))
135# endif
136# ifndef le16toh
137# define le16toh(_x) __builtin_bswap16(_x)
138# endif
139# ifndef be16toh
140# define be16toh(_x) ((uint16_t)(_x))
141# endif
142# ifndef htole32
143# define htole32(_x) __builtin_bswap32(_x)
144# endif
145# ifndef htobe32
146# define htobe32(_x) ((uint32_t)(_x))
147# endif
148# ifndef le32toh
149# define le32toh(_x) __builtin_bswap32(_x)
150# endif
151# ifndef be32toh
152# define be32toh(_x) ((uint32_t)(_x))
153# endif
154# ifndef htole64
155# define htole64(_x) __builtin_bswap64(_x)
156# endif
157# ifndef htobe64
158# define htobe64(_x) ((uint64_t)(_x))
159# endif
160# ifndef le64toh
161# define le64toh(_x) __builtin_bswap64(_x)
162# endif
163# ifndef be64toh
164# define be64toh(_x) ((uint64_t)(_x))
165# endif
166#else
167# error "Byte order not supported"
168#endif
169
170#endif /* DOXYGEN */
171
172#ifdef __cplusplus
173}
174#endif
175
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