1*199767f8SToomas Soome /*-
2*199767f8SToomas Soome * Copyright (c) 1987, 1991 Regents of the University of California.
3*199767f8SToomas Soome * All rights reserved.
4*199767f8SToomas Soome *
5*199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
6*199767f8SToomas Soome * modification, are permitted provided that the following conditions
7*199767f8SToomas Soome * are met:
8*199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
9*199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
10*199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11*199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
12*199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
13*199767f8SToomas Soome * 4. Neither the name of the University nor the names of its contributors
14*199767f8SToomas Soome * may be used to endorse or promote products derived from this software
15*199767f8SToomas Soome * without specific prior written permission.
16*199767f8SToomas Soome *
17*199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*199767f8SToomas Soome * SUCH DAMAGE.
28*199767f8SToomas Soome *
29*199767f8SToomas Soome * @(#)endian.h 7.8 (Berkeley) 4/3/91
30*199767f8SToomas Soome * $FreeBSD$
31*199767f8SToomas Soome */
32*199767f8SToomas Soome
33*199767f8SToomas Soome #ifndef _MACHINE_ENDIAN_H_
34*199767f8SToomas Soome #define _MACHINE_ENDIAN_H_
35*199767f8SToomas Soome
36*199767f8SToomas Soome #include <sys/cdefs.h>
37*199767f8SToomas Soome #include <sys/_types.h>
38*199767f8SToomas Soome
39*199767f8SToomas Soome /*
40*199767f8SToomas Soome * Define the order of 32-bit words in 64-bit words.
41*199767f8SToomas Soome */
42*199767f8SToomas Soome #define _QUAD_HIGHWORD 1
43*199767f8SToomas Soome #define _QUAD_LOWWORD 0
44*199767f8SToomas Soome
45*199767f8SToomas Soome /*
46*199767f8SToomas Soome * Definitions for byte order, according to byte significance from low
47*199767f8SToomas Soome * address to high.
48*199767f8SToomas Soome */
49*199767f8SToomas Soome #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
50*199767f8SToomas Soome #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
51*199767f8SToomas Soome #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
52*199767f8SToomas Soome
53*199767f8SToomas Soome #define _BYTE_ORDER _LITTLE_ENDIAN
54*199767f8SToomas Soome
55*199767f8SToomas Soome /*
56*199767f8SToomas Soome * Deprecated variants that don't have enough underscores to be useful in more
57*199767f8SToomas Soome * strict namespaces.
58*199767f8SToomas Soome */
59*199767f8SToomas Soome #if __BSD_VISIBLE
60*199767f8SToomas Soome #define LITTLE_ENDIAN _LITTLE_ENDIAN
61*199767f8SToomas Soome #define BIG_ENDIAN _BIG_ENDIAN
62*199767f8SToomas Soome #define PDP_ENDIAN _PDP_ENDIAN
63*199767f8SToomas Soome #define BYTE_ORDER _BYTE_ORDER
64*199767f8SToomas Soome #endif
65*199767f8SToomas Soome
66*199767f8SToomas Soome #define __bswap16_gen(x) (__uint16_t)((x) << 8 | (x) >> 8)
67*199767f8SToomas Soome #define __bswap32_gen(x) \
68*199767f8SToomas Soome (((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16))
69*199767f8SToomas Soome #define __bswap64_gen(x) \
70*199767f8SToomas Soome (((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32))
71*199767f8SToomas Soome
72*199767f8SToomas Soome #ifdef __GNUCLIKE_BUILTIN_CONSTANT_P
73*199767f8SToomas Soome #define __bswap16(x) \
74*199767f8SToomas Soome ((__uint16_t)(__builtin_constant_p(x) ? \
75*199767f8SToomas Soome __bswap16_gen((__uint16_t)(x)) : __bswap16_var(x)))
76*199767f8SToomas Soome #define __bswap32(x) \
77*199767f8SToomas Soome (__builtin_constant_p(x) ? \
78*199767f8SToomas Soome __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x))
79*199767f8SToomas Soome #define __bswap64(x) \
80*199767f8SToomas Soome (__builtin_constant_p(x) ? \
81*199767f8SToomas Soome __bswap64_gen((__uint64_t)(x)) : __bswap64_var(x))
82*199767f8SToomas Soome #else
83*199767f8SToomas Soome /* XXX these are broken for use in static initializers. */
84*199767f8SToomas Soome #define __bswap16(x) __bswap16_var(x)
85*199767f8SToomas Soome #define __bswap32(x) __bswap32_var(x)
86*199767f8SToomas Soome #define __bswap64(x) __bswap64_var(x)
87*199767f8SToomas Soome #endif
88*199767f8SToomas Soome
89*199767f8SToomas Soome /* These are defined as functions to avoid multiple evaluation of x. */
90*199767f8SToomas Soome
91*199767f8SToomas Soome static __inline __uint16_t
__bswap16_var(__uint16_t _x)92*199767f8SToomas Soome __bswap16_var(__uint16_t _x)
93*199767f8SToomas Soome {
94*199767f8SToomas Soome
95*199767f8SToomas Soome return (__bswap16_gen(_x));
96*199767f8SToomas Soome }
97*199767f8SToomas Soome
98*199767f8SToomas Soome static __inline __uint32_t
__bswap32_var(__uint32_t _x)99*199767f8SToomas Soome __bswap32_var(__uint32_t _x)
100*199767f8SToomas Soome {
101*199767f8SToomas Soome
102*199767f8SToomas Soome #ifdef __GNUCLIKE_ASM
103*199767f8SToomas Soome __asm("bswap %0" : "+r" (_x));
104*199767f8SToomas Soome return (_x);
105*199767f8SToomas Soome #else
106*199767f8SToomas Soome return (__bswap32_gen(_x));
107*199767f8SToomas Soome #endif
108*199767f8SToomas Soome }
109*199767f8SToomas Soome
110*199767f8SToomas Soome static __inline __uint64_t
__bswap64_var(__uint64_t _x)111*199767f8SToomas Soome __bswap64_var(__uint64_t _x)
112*199767f8SToomas Soome {
113*199767f8SToomas Soome
114*199767f8SToomas Soome #if defined(__amd64__) && defined(__GNUCLIKE_ASM)
115*199767f8SToomas Soome __asm("bswap %0" : "+r" (_x));
116*199767f8SToomas Soome return (_x);
117*199767f8SToomas Soome #else
118*199767f8SToomas Soome /*
119*199767f8SToomas Soome * It is important for the optimizations that the following is not
120*199767f8SToomas Soome * really generic, but expands to 2 __bswap32_var()'s.
121*199767f8SToomas Soome */
122*199767f8SToomas Soome return (__bswap64_gen(_x));
123*199767f8SToomas Soome #endif
124*199767f8SToomas Soome }
125*199767f8SToomas Soome
126*199767f8SToomas Soome #define __htonl(x) __bswap32(x)
127*199767f8SToomas Soome #define __htons(x) __bswap16(x)
128*199767f8SToomas Soome #define __ntohl(x) __bswap32(x)
129*199767f8SToomas Soome #define __ntohs(x) __bswap16(x)
130*199767f8SToomas Soome
131*199767f8SToomas Soome #endif /* !_MACHINE_ENDIAN_H_ */
132