xref: /freebsd/include/endian.h (revision 1314d14c32713b448738cc821c04a5e7482bb4af)
130e0d2a5SWarner Losh /*-
230e0d2a5SWarner Losh  * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
330e0d2a5SWarner Losh  *
430e0d2a5SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
530e0d2a5SWarner Losh  */
630e0d2a5SWarner Losh 
730e0d2a5SWarner Losh /*
830e0d2a5SWarner Losh  * A mostly Linux/glibc-compatible endian.h
930e0d2a5SWarner Losh  */
1030e0d2a5SWarner Losh 
1130e0d2a5SWarner Losh #ifndef _ENDIAN_H_
1230e0d2a5SWarner Losh #define _ENDIAN_H_
1330e0d2a5SWarner Losh 
1430e0d2a5SWarner Losh /*
15*1314d14cSWarner Losh  * POSIX Issue 8 requires that endian.h define uint{16,32,64}_t. Although POSIX
16*1314d14cSWarner Losh  * allows stdint.h symbols here, be conservative and only define there required
17*1314d14cSWarner Losh  * ones. FreeBSD's sys/_endian.h doesn't need to expose those types since it
18*1314d14cSWarner Losh  * implements all the [bl]eXtoh hto[bl]eX interfaces as macros calling builtin
19*1314d14cSWarner Losh  * functions. POSIX allows functions, macros or both. We opt for macros only.
20*1314d14cSWarner Losh  */
21*1314d14cSWarner Losh #include <sys/_types.h>
22*1314d14cSWarner Losh 
23*1314d14cSWarner Losh #ifndef _UINT16_T_DECLARED
24*1314d14cSWarner Losh typedef	__uint16_t		uint16_t;
25*1314d14cSWarner Losh #define	_UINT16_T_DECLARED
26*1314d14cSWarner Losh #endif
27*1314d14cSWarner Losh 
28*1314d14cSWarner Losh #ifndef _UINT32_T_DECLARED
29*1314d14cSWarner Losh typedef	__uint32_t		uint32_t;
30*1314d14cSWarner Losh #define	_UINT32_T_DECLARED
31*1314d14cSWarner Losh #endif
32*1314d14cSWarner Losh 
33*1314d14cSWarner Losh #ifndef _UINT64_T_DECLARED
34*1314d14cSWarner Losh typedef	__uint64_t		uint64_t;
35*1314d14cSWarner Losh #define	_UINT64_T_DECLARED
36*1314d14cSWarner Losh #endif
37*1314d14cSWarner Losh 
38*1314d14cSWarner Losh /*
3930e0d2a5SWarner Losh  * FreeBSD's sys/_endian.h is very close to the interface provided on Linux by
40*1314d14cSWarner Losh  * glibc's endian.h as well as POSIX Issue 8's endian.h.
4130e0d2a5SWarner Losh  */
4230e0d2a5SWarner Losh #include <sys/_endian.h>
4330e0d2a5SWarner Losh 
4430e0d2a5SWarner Losh /*
4530e0d2a5SWarner Losh  * glibc uses double underscore for these symbols. Define these unconditionally.
4630e0d2a5SWarner Losh  * The compiler defines __BYTE_ORDER__ these days, so we don't do anything
4730e0d2a5SWarner Losh  * with that since sys/endian.h defines _BYTE_ORDER based on it.
4830e0d2a5SWarner Losh  */
4930e0d2a5SWarner Losh #define __BIG_ENDIAN		_BIG_ENDIAN
5030e0d2a5SWarner Losh #define __BYTE_ORDER		_BYTE_ORDER
5130e0d2a5SWarner Losh #define __LITTLE_ENDIAN		_LITTLE_ENDIAN
5230e0d2a5SWarner Losh #define __PDP_ENDIAN		_PDP_ENDIAN
5330e0d2a5SWarner Losh 
5430e0d2a5SWarner Losh /*
5530e0d2a5SWarner Losh  * FreeBSD's sys/endian.h and machine/endian.h doesn't define a separate
5630e0d2a5SWarner Losh  * byte order for floats. Use the host non-float byte order.
5730e0d2a5SWarner Losh  */
5830e0d2a5SWarner Losh #define __FLOAT_WORD_ORDER	_BYTE_ORDER
5930e0d2a5SWarner Losh 
6030e0d2a5SWarner Losh /*
6130e0d2a5SWarner Losh  * We don't define BIG_ENDI, LITTLE_ENDI, HIGH_HALF and LOW_HALF macros that
62dff3a80fSJose Luis Duran  * glibc's endian.h defines since those appear to be internal to glibc.
63dff3a80fSJose Luis Duran  * We also don't try to emulate the various helper macros that glibc uses to
64dff3a80fSJose Luis Duran  * limit namespace visibility.
6530e0d2a5SWarner Losh  */
6630e0d2a5SWarner Losh 
6730e0d2a5SWarner Losh #endif /* _ENDIAN_H_ */
68