Merge from vendor branch CVS:
[dragonfly.git] / sys / cpu / i386 / include / endian.h
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * Copyright (c) 1987, 1991 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
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.6 2004/11/15 08:16:02 joerg Exp $
38  */
39
40 #ifndef _MACHINE_ENDIAN_H_
41 #define _MACHINE_ENDIAN_H_
42
43 #include <sys/cdefs.h>
44 #include <machine/stdint.h>
45
46 /*
47  * Define the order of 32-bit words in 64-bit words.
48  */
49 #define _QUAD_HIGHWORD 1
50 #define _QUAD_LOWWORD 0
51
52 /*
53  * Definitions for byte order, according to byte significance from low
54  * address to high.
55  */
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 */
59
60 #define _BYTE_ORDER     _LITTLE_ENDIAN
61
62 /*
63  * Deprecated variants that don't have enough underscores to be useful in more
64  * strict namespaces.
65  */
66 #if __BSD_VISIBLE
67 #define LITTLE_ENDIAN   _LITTLE_ENDIAN
68 #define BIG_ENDIAN      _BIG_ENDIAN
69 #define PDP_ENDIAN      _PDP_ENDIAN
70 #define BYTE_ORDER      _BYTE_ORDER
71 #endif
72
73 #define __htonl(x)      __bswap32(x)
74 #define __htons(x)      __bswap16(x)
75 #define __ntohl(x)      __bswap32(x)
76 #define __ntohs(x)      __bswap16(x)
77
78 #define __byte_swap16_const(x) \
79         ((((x) & 0xff00) >> 8) | \
80          (((x) & 0x00ff) << 8))
81
82 #define __byte_swap32_const(x) \
83         ((((x) & 0xff000000) >> 24) | \
84          (((x) & 0x00ff0000) >>  8) | \
85          (((x) & 0x0000ff00) <<  8) | \
86          (((x) & 0x000000ff) << 24))
87
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)))
93
94 #if defined(__INTEL_COMPILER)
95 # if !defined(__cplusplus) || (defined(__cplusplus) && __INTEL_COMPILER >= 800)
96 #  define __INTEL_COMPILER_with_DragonFly_endian 1
97 # endif
98 #endif
99
100 #if defined(__GNUC__) || defined(__INTEL_COMPILER_with_DragonFly_endian)
101
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__)
107
108 #define __byte_swap32_var(x) \
109         __extension__ ({ register __uint32_t __X = (x); \
110            __asm ("bswap %0" : "+r" (__X)); \
111            __X; })
112
113 #else /* !I386_CPU */
114
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" \
118                : "+q" (__X)); \
119            __X; })
120 #endif /* !I386_CPU */
121
122 #define __byte_swap16_var(x) \
123         __extension__ ({ register __uint16_t __X = (x); \
124            __asm ("xchgb %h0, %b0" : "+q" (__X)); \
125            __X; })
126
127 #ifdef __OPTIMIZE__
128
129 #define __byte_swap16(x) (__builtin_constant_p(x) ? \
130         __byte_swap16_const(x) : __byte_swap16_var(x))
131
132 #define __byte_swap32(x) (__builtin_constant_p(x) ? \
133         __byte_swap32_const(x) : __byte_swap32_var(x))
134
135 #else   /* __OPTIMIZE__ */
136
137 #define __byte_swap16(x) __byte_swap16_var(x)
138 #define __byte_swap32(x) __byte_swap32_var(x)
139
140 #endif  /* __OPTIMIZE__ */
141
142 #endif /* __GNUC__ || __INTEL_COMPILER_with_DragonFly_endian */
143
144 /*
145  * If the compiler-specific part didn't provide this, fallback
146  * to the generic versions.
147  */
148
149 #ifndef __byte_swap16
150 #define __byte_swap16(x) __byte_swap16_const(x)
151 #endif
152
153 #ifndef __byte_swap32
154 #define __byte_swap32(x) __byte_swap32_const(x)
155 #endif
156
157 #ifndef __byte_swap64
158 #define __byte_swap64(x) __byte_swap64_const(x)
159 #endif
160
161 __BEGIN_DECLS
162
163 static __inline __uint16_t
164 __bswap16(__uint16_t _x)
165 {
166         return (__byte_swap16(_x));
167 }
168
169 static __inline __uint32_t
170 __bswap32(__uint32_t _x)
171 {
172         return (__byte_swap32(_x));
173 }
174
175 static __inline __uint64_t
176 __bswap64(__uint64_t _x)
177 {
178         return (__byte_swap64(_x));
179 }
180
181 __END_DECLS
182
183 #endif /* !_MACHINE_ENDIAN_H_ */