Merge from vendor branch READLINE:
[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.5 2004/08/23 21:16:38 dillon 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
106 #define __byte_swap32_var(x) \
107         __extension__ ({ register __uint32_t __X = (x); \
108            __asm ("bswap %0" : "+r" (__X)); \
109            __X; })
110
111 #else /* !I386_CPU */
112
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" \
116                : "+q" (__X)); \
117            __X; })
118 #endif /* !I386_CPU */
119
120 #define __byte_swap16_var(x) \
121         __extension__ ({ register __uint16_t __X = (x); \
122            __asm ("xchgb %h0, %b0" : "+q" (__X)); \
123            __X; })
124
125 #ifdef __OPTIMIZE__
126
127 #define __byte_swap16(x) (__builtin_constant_p(x) ? \
128         __byte_swap16_const(x) : __byte_swap16_var(x))
129
130 #define __byte_swap32(x) (__builtin_constant_p(x) ? \
131         __byte_swap32_const(x) : __byte_swap32_var(x))
132
133 #else   /* __OPTIMIZE__ */
134
135 #define __byte_swap16(x) __byte_swap16_var(x)
136 #define __byte_swap32(x) __byte_swap32_var(x)
137
138 #endif  /* __OPTIMIZE__ */
139
140 #endif /* __GNUC__ || __INTEL_COMPILER_with_DragonFly_endian */
141
142 /*
143  * If the compiler-specific part didn't provide this, fallback
144  * to the generic versions.
145  */
146
147 #ifndef __byte_swap16
148 #define __byte_swap16(x) __byte_swap16_const(x)
149 #endif
150
151 #ifndef __byte_swap32
152 #define __byte_swap32(x) __byte_swap32_const(x)
153 #endif
154
155 #ifndef __byte_swap64
156 #define __byte_swap64(x) __byte_swap64_const(x)
157 #endif
158
159 __BEGIN_DECLS
160
161 static __inline __uint16_t
162 __bswap16(__uint16_t _x)
163 {
164         return (__byte_swap16(_x));
165 }
166
167 static __inline __uint32_t
168 __bswap32(__uint32_t _x)
169 {
170         return (__byte_swap32(_x));
171 }
172
173 static __inline __uint64_t
174 __bswap64(__uint64_t _x)
175 {
176         return (__byte_swap64(_x));
177 }
178
179 __END_DECLS
180
181 #endif /* !_MACHINE_ENDIAN_H_ */