2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1991 Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #ifndef _SYS__ENDIAN_H_
33 #define _SYS__ENDIAN_H_
35 #if !defined(_MACHINE_ENDIAN_H_) && !defined(_BYTESWAP_H_) && !defined(_ENDIAN_H_)
36 #error "sys/_endian.h should not be included directly"
39 #include <sys/cdefs.h> /* visibility macros */
41 /* BSD Compatibility */
42 #define _BYTE_ORDER __BYTE_ORDER__
45 * Definitions for byte order, according to byte significance from low
46 * address to high. We undefine any prior definition of them because
47 * powerpc compilers define _LITTLE_ENDIAN and _BIG_ENDIAN to mean
51 #define _LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ /* LSB first: 1234 */
53 #define _BIG_ENDIAN __ORDER_BIG_ENDIAN__ /* MSB first: 4321 */
54 #define _PDP_ENDIAN __ORDER_PDP_ENDIAN__ /* LSB first in word,
55 * MSW first in long: 3412 */
58 * Define the order of 32-bit words in 64-bit words.
60 #if _BYTE_ORDER == _LITTLE_ENDIAN
61 #define _QUAD_HIGHWORD 1
62 #define _QUAD_LOWWORD 0
63 #elif _BYTE_ORDER == _BIG_ENDIAN
64 #define _QUAD_HIGHWORD 0
65 #define _QUAD_LOWWORD 1
67 #error "Unsupported endian"
71 * POSIX Issue 8 will require these for endian.h. Define them there and in the
72 * traditional BSD compilation environment. Since issue 8 doesn't yet have an
73 * assigned date, use strictly greater than issue 7's date.
75 #if __BSD_VISIBLE || _POSIX_C_SOURCE > 200809
76 #define LITTLE_ENDIAN _LITTLE_ENDIAN
77 #define BIG_ENDIAN _BIG_ENDIAN
78 #define PDP_ENDIAN _PDP_ENDIAN
79 #define BYTE_ORDER _BYTE_ORDER
82 /* bswap primitives, based on compiler builtins */
83 #define __bswap16(x) __builtin_bswap16(x)
84 #define __bswap32(x) __builtin_bswap32(x)
85 #define __bswap64(x) __builtin_bswap64(x)
87 #if _BYTE_ORDER == _LITTLE_ENDIAN
88 #define __ntohl(x) (__bswap32(x))
89 #define __ntohs(x) (__bswap16(x))
90 #define __htonl(x) (__bswap32(x))
91 #define __htons(x) (__bswap16(x))
92 #elif _BYTE_ORDER == _BIG_ENDIAN
93 #define __htonl(x) ((__uint32_t)(x))
94 #define __htons(x) ((__uint16_t)(x))
95 #define __ntohl(x) ((__uint32_t)(x))
96 #define __ntohs(x) ((__uint16_t)(x))
100 * Host to big endian, host to little endian, big endian to host, and little
101 * endian to host byte order functions as detailed in byteorder(9).
103 #if _BYTE_ORDER == _LITTLE_ENDIAN
104 #define htobe16(x) __bswap16((x))
105 #define htobe32(x) __bswap32((x))
106 #define htobe64(x) __bswap64((x))
107 #define htole16(x) ((uint16_t)(x))
108 #define htole32(x) ((uint32_t)(x))
109 #define htole64(x) ((uint64_t)(x))
111 #define be16toh(x) __bswap16((x))
112 #define be32toh(x) __bswap32((x))
113 #define be64toh(x) __bswap64((x))
114 #define le16toh(x) ((uint16_t)(x))
115 #define le32toh(x) ((uint32_t)(x))
116 #define le64toh(x) ((uint64_t)(x))
117 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
118 #define htobe16(x) ((uint16_t)(x))
119 #define htobe32(x) ((uint32_t)(x))
120 #define htobe64(x) ((uint64_t)(x))
121 #define htole16(x) __bswap16((x))
122 #define htole32(x) __bswap32((x))
123 #define htole64(x) __bswap64((x))
125 #define be16toh(x) ((uint16_t)(x))
126 #define be32toh(x) ((uint32_t)(x))
127 #define be64toh(x) ((uint64_t)(x))
128 #define le16toh(x) __bswap16((x))
129 #define le32toh(x) __bswap32((x))
130 #define le64toh(x) __bswap64((x))
131 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
133 #endif /* _SYS__ENDIAN_H_ */