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/i386/include/Attic/endian.h,v 1.6 2004/11/15 08:16:02 joerg 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__) || \
105 defined(__k6__) || defined(__athlon__) || defined(__k8__) || \
106 defined(__pentium4__)
108 #define __byte_swap32_var(x) \
109 __extension__ ({ register __uint32_t __X = (x); \
110 __asm ("bswap %0" : "+r" (__X)); \
113 #else /* !I386_CPU */
115 #define __byte_swap32_var(x) \
116 __extension__ ({ register __uint32_t __X = (x); \
117 __asm ("xchgb %h0, %b0\n\trorl $16, %0\n\txchgb %h0, %b0" \
120 #endif /* !I386_CPU */
122 #define __byte_swap16_var(x) \
123 __extension__ ({ register __uint16_t __X = (x); \
124 __asm ("xchgb %h0, %b0" : "+q" (__X)); \
129 #define __byte_swap16(x) (__builtin_constant_p(x) ? \
130 __byte_swap16_const(x) : __byte_swap16_var(x))
132 #define __byte_swap32(x) (__builtin_constant_p(x) ? \
133 __byte_swap32_const(x) : __byte_swap32_var(x))
135 #else /* __OPTIMIZE__ */
137 #define __byte_swap16(x) __byte_swap16_var(x)
138 #define __byte_swap32(x) __byte_swap32_var(x)
140 #endif /* __OPTIMIZE__ */
142 #endif /* __GNUC__ || __INTEL_COMPILER_with_DragonFly_endian */
145 * If the compiler-specific part didn't provide this, fallback
146 * to the generic versions.
149 #ifndef __byte_swap16
150 #define __byte_swap16(x) __byte_swap16_const(x)
153 #ifndef __byte_swap32
154 #define __byte_swap32(x) __byte_swap32_const(x)
157 #ifndef __byte_swap64
158 #define __byte_swap64(x) __byte_swap64_const(x)
163 static __inline __uint16_t
164 __bswap16(__uint16_t _x)
166 return (__byte_swap16(_x));
169 static __inline __uint32_t
170 __bswap32(__uint32_t _x)
172 return (__byte_swap32(_x));
175 static __inline __uint64_t
176 __bswap64(__uint64_t _x)
178 return (__byte_swap64(_x));
183 #endif /* !_MACHINE_ENDIAN_H_ */