xref: /freebsd/include/endian.h (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 Issue 8 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 allows functions, macros or both. We opt for macros only.
20  */
21 #include <sys/_types.h>
22 
23 #ifndef _UINT16_T_DECLARED
24 typedef	__uint16_t		uint16_t;
25 #define	_UINT16_T_DECLARED
26 #endif
27 
28 #ifndef _UINT32_T_DECLARED
29 typedef	__uint32_t		uint32_t;
30 #define	_UINT32_T_DECLARED
31 #endif
32 
33 #ifndef _UINT64_T_DECLARED
34 typedef	__uint64_t		uint64_t;
35 #define	_UINT64_T_DECLARED
36 #endif
37 
38 /*
39  * FreeBSD's sys/_endian.h is very close to the interface provided on Linux by
40  * glibc's endian.h as well as POSIX Issue 8's endian.h.
41  */
42 #include <sys/_endian.h>
43 
44 /*
45  * glibc uses double underscore for these symbols. Define these unconditionally.
46  * The compiler defines __BYTE_ORDER__ these days, so we don't do anything
47  * with that since sys/endian.h defines _BYTE_ORDER based on it.
48  */
49 #define __BIG_ENDIAN		_BIG_ENDIAN
50 #define __BYTE_ORDER		_BYTE_ORDER
51 #define __LITTLE_ENDIAN		_LITTLE_ENDIAN
52 #define __PDP_ENDIAN		_PDP_ENDIAN
53 
54 /*
55  * FreeBSD's sys/endian.h and machine/endian.h doesn't define a separate
56  * byte order for floats. Use the host non-float byte order.
57  */
58 #define __FLOAT_WORD_ORDER	_BYTE_ORDER
59 
60 /*
61  * We don't define BIG_ENDI, LITTLE_ENDI, HIGH_HALF and LOW_HALF macros that
62  * glibc's endian.h defines since those appear to be internal to glibc.
63  * We also don't try to emulate the various helper macros that glibc uses to
64  * limit namespace visibility.
65  */
66 
67 #endif /* _ENDIAN_H_ */
68