xref: /freebsd/include/endian.h (revision b2d2a78ad80ec68d4a17f5aef97d21686cb1e29b)
1 /*-
2  * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 /*
8  * A mostly Linux/glibc-compatible endian.h
9  */
10 
11 #ifndef _ENDIAN_H_
12 #define _ENDIAN_H_
13 
14 /*
15  * POSIX.1-2024 requires that endian.h define uint{16,32,64}_t. Although POSIX
16  * allows stdint.h symbols here, be conservative and only define there required
17  * ones. FreeBSD's sys/_endian.h doesn't need to expose those types since it
18  * implements all the [bl]eXtoh hto[bl]eX interfaces as macros calling builtin
19  * functions. POSIX.1-2024 allows functions, macros or both. We opt for macros
20  * only.
21  */
22 #include <sys/_types.h>
23 
24 #ifndef _UINT16_T_DECLARED
25 typedef	__uint16_t		uint16_t;
26 #define	_UINT16_T_DECLARED
27 #endif
28 
29 #ifndef _UINT32_T_DECLARED
30 typedef	__uint32_t		uint32_t;
31 #define	_UINT32_T_DECLARED
32 #endif
33 
34 #ifndef _UINT64_T_DECLARED
35 typedef	__uint64_t		uint64_t;
36 #define	_UINT64_T_DECLARED
37 #endif
38 
39 /*
40  * FreeBSD's sys/_endian.h is very close to the interface provided on Linux by
41  * glibc's endian.h as well as POSIX.1-2024's endian.h.
42  */
43 #include <sys/_endian.h>
44 
45 /*
46  * glibc uses double underscore for these symbols. Define these unconditionally.
47  * The compiler defines __BYTE_ORDER__ these days, so we don't do anything
48  * with that since sys/endian.h defines _BYTE_ORDER based on it.
49  */
50 #define __BIG_ENDIAN		_BIG_ENDIAN
51 #define __BYTE_ORDER		_BYTE_ORDER
52 #define __LITTLE_ENDIAN		_LITTLE_ENDIAN
53 #define __PDP_ENDIAN		_PDP_ENDIAN
54 
55 /*
56  * FreeBSD's sys/endian.h and machine/endian.h doesn't define a separate
57  * byte order for floats. Use the host non-float byte order.
58  */
59 #define __FLOAT_WORD_ORDER	_BYTE_ORDER
60 
61 /*
62  * We don't define BIG_ENDI, LITTLE_ENDI, HIGH_HALF and LOW_HALF macros that
63  * glibc's endian.h defines since those appear to be internal to glibc.
64  * We also don't try to emulate the various helper macros that glibc uses to
65  * limit namespace visibility.
66  */
67 
68 #endif /* _ENDIAN_H_ */
69