sys/vfs/hammer: Remove #if0'd hammer_mirror_write()
[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.8 2007/11/07 17:42:50 dillon Exp $
38  */
39
40 #ifndef _CPU_ENDIAN_H_
41 #define _CPU_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 #define __byte_swap32_var(x) \
103         __extension__ ({ register __uint32_t __X = (x); \
104            __asm ("bswap %0" : "+r" (__X)); \
105            __X; })
106
107 #define __byte_swap16_var(x) \
108         __extension__ ({ register __uint16_t __X = (x); \
109            __asm ("xchgb %h0, %b0" : "+q" (__X)); \
110            __X; })
111
112 #ifdef __OPTIMIZE__
113
114 #define __byte_swap16(x) (__builtin_constant_p(x) ? \
115         __byte_swap16_const(x) : __byte_swap16_var(x))
116
117 #define __byte_swap32(x) (__builtin_constant_p(x) ? \
118         __byte_swap32_const(x) : __byte_swap32_var(x))
119
120 #else   /* __OPTIMIZE__ */
121
122 #define __byte_swap16(x) __byte_swap16_var(x)
123 #define __byte_swap32(x) __byte_swap32_var(x)
124
125 #endif  /* __OPTIMIZE__ */
126
127 #endif /* __GNUC__ || __INTEL_COMPILER_with_DragonFly_endian */
128
129 /*
130  * If the compiler-specific part didn't provide this, fallback
131  * to the generic versions.
132  */
133
134 #ifndef __byte_swap16
135 #define __byte_swap16(x) __byte_swap16_const(x)
136 #endif
137
138 #ifndef __byte_swap32
139 #define __byte_swap32(x) __byte_swap32_const(x)
140 #endif
141
142 #ifndef __byte_swap64
143 #define __byte_swap64(x) __byte_swap64_const(x)
144 #endif
145
146 __BEGIN_DECLS
147
148 static __inline __uint16_t
149 __bswap16(__uint16_t _x)
150 {
151         return (__byte_swap16(_x));
152 }
153
154 static __inline __uint32_t
155 __bswap32(__uint32_t _x)
156 {
157         return (__byte_swap32(_x));
158 }
159
160 static __inline __uint64_t
161 __bswap64(__uint64_t _x)
162 {
163         return (__byte_swap64(_x));
164 }
165
166 __END_DECLS
167
168 #endif /* !_CPU_ENDIAN_H_ */