2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: @(#)endian.h 7.8 (Berkeley) 4/3/91
36 * $FreeBSD: src/sys/i386/include/endian.h,v 1.18 1999/12/29 04:33:01 peter Exp $
37 * $DragonFly: src/sys/cpu/i386/include/endian.h,v 1.5 2004/08/23 21:16:38 dillon Exp $
40 #ifndef _MACHINE_ENDIAN_H_
41 #define _MACHINE_ENDIAN_H_
43 #include <sys/cdefs.h>
44 #include <machine/stdint.h>
47 * Define the order of 32-bit words in 64-bit words.
49 #define _QUAD_HIGHWORD 1
50 #define _QUAD_LOWWORD 0
53 * Definitions for byte order, according to byte significance from low
56 #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
57 #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
58 #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
60 #define _BYTE_ORDER _LITTLE_ENDIAN
63 * Deprecated variants that don't have enough underscores to be useful in more
67 #define LITTLE_ENDIAN _LITTLE_ENDIAN
68 #define BIG_ENDIAN _BIG_ENDIAN
69 #define PDP_ENDIAN _PDP_ENDIAN
70 #define BYTE_ORDER _BYTE_ORDER
73 #define __htonl(x) __bswap32(x)
74 #define __htons(x) __bswap16(x)
75 #define __ntohl(x) __bswap32(x)
76 #define __ntohs(x) __bswap16(x)
78 #define __byte_swap16_const(x) \
79 ((((x) & 0xff00) >> 8) | \
80 (((x) & 0x00ff) << 8))
82 #define __byte_swap32_const(x) \
83 ((((x) & 0xff000000) >> 24) | \
84 (((x) & 0x00ff0000) >> 8) | \
85 (((x) & 0x0000ff00) << 8) | \
86 (((x) & 0x000000ff) << 24))
88 #define __byte_swap64_const(x) \
89 (((x) >> 56) | (((x) >> 40) & 0xff00) | (((x) >> 24) & 0xff0000) | \
90 (((x) >> 8) & 0xff000000) | (((x) << 8) & ((__uint64_t)0xff << 32)) | \
91 (((x) << 24) & ((__uint64_t)0xff << 40)) | \
92 (((x) << 40) & ((__uint64_t)0xff << 48)) | (((x) << 56)))
94 #if defined(__INTEL_COMPILER)
95 # if !defined(__cplusplus) || (defined(__cplusplus) && __INTEL_COMPILER >= 800)
96 # define __INTEL_COMPILER_with_DragonFly_endian 1
100 #if defined(__GNUC__) || defined(__INTEL_COMPILER_with_DragonFly_endian)
102 #if (defined(_KERNEL) && !defined(I386_CPU) && \
103 (defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU))) || \
104 defined(__i486__) || defined(__i586__) || defined(__i686__)
106 #define __byte_swap32_var(x) \
107 __extension__ ({ register __uint32_t __X = (x); \
108 __asm ("bswap %0" : "+r" (__X)); \
111 #else /* !I386_CPU */
113 #define __byte_swap32_var(x) \
114 __extension__ ({ register __uint32_t __X = (x); \
115 __asm ("xchgb %h0, %b0\n\trorl $16, %0\n\txchgb %h0, %b0" \
118 #endif /* !I386_CPU */
120 #define __byte_swap16_var(x) \
121 __extension__ ({ register __uint16_t __X = (x); \
122 __asm ("xchgb %h0, %b0" : "+q" (__X)); \
127 #define __byte_swap16(x) (__builtin_constant_p(x) ? \
128 __byte_swap16_const(x) : __byte_swap16_var(x))
130 #define __byte_swap32(x) (__builtin_constant_p(x) ? \
131 __byte_swap32_const(x) : __byte_swap32_var(x))
133 #else /* __OPTIMIZE__ */
135 #define __byte_swap16(x) __byte_swap16_var(x)
136 #define __byte_swap32(x) __byte_swap32_var(x)
138 #endif /* __OPTIMIZE__ */
140 #endif /* __GNUC__ || __INTEL_COMPILER_with_DragonFly_endian */
143 * If the compiler-specific part didn't provide this, fallback
144 * to the generic versions.
147 #ifndef __byte_swap16
148 #define __byte_swap16(x) __byte_swap16_const(x)
151 #ifndef __byte_swap32
152 #define __byte_swap32(x) __byte_swap32_const(x)
155 #ifndef __byte_swap64
156 #define __byte_swap64(x) __byte_swap64_const(x)
161 static __inline __uint16_t
162 __bswap16(__uint16_t _x)
164 return (__byte_swap16(_x));
167 static __inline __uint32_t
168 __bswap32(__uint32_t _x)
170 return (__byte_swap32(_x));
173 static __inline __uint64_t
174 __bswap64(__uint64_t _x)
176 return (__byte_swap64(_x));
181 #endif /* !_MACHINE_ENDIAN_H_ */