]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - include/arch.h
white space cleanup
[thirdparty/xfsprogs-dev.git] / include / arch.h
CommitLineData
2bd0ea18 1/*
854f7c66 2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
5000d01d 3 *
2bd0ea18
NS
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
5000d01d 7 *
2bd0ea18
NS
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5000d01d 11 *
2bd0ea18
NS
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
5000d01d 14 * or the like. Any license provided herein, whether implied or
2bd0ea18
NS
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
5000d01d 18 *
2bd0ea18
NS
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
5000d01d 22 *
2bd0ea18
NS
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
5000d01d
SL
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
2bd0ea18
NS
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32#ifndef __XFS_SUPPORT_ARCH_H__
33#define __XFS_SUPPORT_ARCH_H__
34
35#ifdef __KERNEL__
36
37#include <asm/byteorder.h>
38
39#ifdef __LITTLE_ENDIAN
40# define __BYTE_ORDER __LITTLE_ENDIAN
41#endif
42#ifdef __BIG_ENDIAN
43# define __BYTE_ORDER __BIG_ENDIAN
44#endif
45
2bd0ea18
NS
46#endif /* __KERNEL__ */
47
48/* do we need conversion? */
49
50#define ARCH_NOCONVERT 1
5000d01d 51#if __BYTE_ORDER == __LITTLE_ENDIAN
2bd0ea18
NS
52#define ARCH_CONVERT 0
53#else
54#define ARCH_CONVERT ARCH_NOCONVERT
55#endif
56
57/* generic swapping macros */
58
8b087f23
SL
59#define INT_SWAP16(type,var) ((typeof(type))(__swab16((__u16)(var))))
60#define INT_SWAP32(type,var) ((typeof(type))(__swab32((__u32)(var))))
61#define INT_SWAP64(type,var) ((typeof(type))(__swab64((__u64)(var))))
2bd0ea18
NS
62
63#define INT_SWAP(type, var) \
8b087f23
SL
64 ((sizeof(type) == 8) ? INT_SWAP64(type,var) : \
65 ((sizeof(type) == 4) ? INT_SWAP32(type,var) : \
66 ((sizeof(type) == 2) ? INT_SWAP16(type,var) : \
2bd0ea18 67 (var))))
2bd0ea18
NS
68
69#define INT_SWAP_UNALIGNED_32(from,to) \
70 { \
5000d01d
SL
71 ((__u8*)(to))[0] = ((__u8*)(from))[3]; \
72 ((__u8*)(to))[1] = ((__u8*)(from))[2]; \
73 ((__u8*)(to))[2] = ((__u8*)(from))[1]; \
74 ((__u8*)(to))[3] = ((__u8*)(from))[0]; \
2bd0ea18
NS
75 }
76
77#define INT_SWAP_UNALIGNED_64(from,to) \
78 { \
5000d01d
SL
79 INT_SWAP_UNALIGNED_32( ((__u8*)(from)) + 4, ((__u8*)(to))); \
80 INT_SWAP_UNALIGNED_32( ((__u8*)(from)), ((__u8*)(to)) + 4); \
2bd0ea18
NS
81 }
82
5000d01d 83/*
2bd0ea18
NS
84 * get and set integers from potentially unaligned locations
85 */
5000d01d 86
2bd0ea18 87#define INT_GET_UNALIGNED_16_LE(pointer) \
5000d01d 88 ((__u16)((((__u8*)(pointer))[0] ) | (((__u8*)(pointer))[1] << 8 )))
2bd0ea18
NS
89#define INT_GET_UNALIGNED_16_BE(pointer) \
90 ((__u16)((((__u8*)(pointer))[0] << 8) | (((__u8*)(pointer))[1])))
91#define INT_SET_UNALIGNED_16_LE(pointer,value) \
92 { \
5000d01d
SL
93 ((__u8*)(pointer))[0] = (((value) ) & 0xff); \
94 ((__u8*)(pointer))[1] = (((value) >> 8) & 0xff); \
2bd0ea18
NS
95 }
96#define INT_SET_UNALIGNED_16_BE(pointer,value) \
97 { \
5000d01d
SL
98 ((__u8*)(pointer))[0] = (((value) >> 8) & 0xff); \
99 ((__u8*)(pointer))[1] = (((value) ) & 0xff); \
2bd0ea18 100 }
5000d01d 101
2bd0ea18 102#define INT_GET_UNALIGNED_32_LE(pointer) \
5000d01d
SL
103 ((__u32)((((__u8*)(pointer))[0] ) | (((__u8*)(pointer))[1] << 8 ) \
104 |(((__u8*)(pointer))[2] << 16) | (((__u8*)(pointer))[3] << 24)))
2bd0ea18
NS
105#define INT_GET_UNALIGNED_32_BE(pointer) \
106 ((__u32)((((__u8*)(pointer))[0] << 24) | (((__u8*)(pointer))[1] << 16) \
5000d01d
SL
107 |(((__u8*)(pointer))[2] << 8) | (((__u8*)(pointer))[3] )))
108
2bd0ea18
NS
109#define INT_GET_UNALIGNED_64_LE(pointer) \
110 (((__u64)(INT_GET_UNALIGNED_32_LE(((__u8*)(pointer))+4)) << 32 ) \
5000d01d 111 |((__u64)(INT_GET_UNALIGNED_32_LE(((__u8*)(pointer)) )) ))
2bd0ea18 112#define INT_GET_UNALIGNED_64_BE(pointer) \
5000d01d
SL
113 (((__u64)(INT_GET_UNALIGNED_32_BE(((__u8*)(pointer)) )) << 32 ) \
114 |((__u64)(INT_GET_UNALIGNED_32_BE(((__u8*)(pointer))+4)) ))
115
2bd0ea18
NS
116/*
117 * now pick the right ones for our MACHINE ARCHITECTURE
118 */
5000d01d
SL
119
120#if __BYTE_ORDER == __LITTLE_ENDIAN
121#define INT_GET_UNALIGNED_16(pointer) INT_GET_UNALIGNED_16_LE(pointer)
2bd0ea18 122#define INT_SET_UNALIGNED_16(pointer,value) INT_SET_UNALIGNED_16_LE(pointer,value)
5000d01d
SL
123#define INT_GET_UNALIGNED_32(pointer) INT_GET_UNALIGNED_32_LE(pointer)
124#define INT_GET_UNALIGNED_64(pointer) INT_GET_UNALIGNED_64_LE(pointer)
2bd0ea18 125#else
5000d01d 126#define INT_GET_UNALIGNED_16(pointer) INT_GET_UNALIGNED_16_BE(pointer)
2bd0ea18 127#define INT_SET_UNALIGNED_16(pointer,value) INT_SET_UNALIGNED_16_BE(pointer,value)
5000d01d
SL
128#define INT_GET_UNALIGNED_32(pointer) INT_GET_UNALIGNED_32_BE(pointer)
129#define INT_GET_UNALIGNED_64(pointer) INT_GET_UNALIGNED_64_BE(pointer)
2bd0ea18
NS
130#endif
131
132/* define generic INT_ macros */
133
134#define INT_GET(reference,arch) \
135 (((arch) == ARCH_NOCONVERT) \
5000d01d
SL
136 ? \
137 (reference) \
138 : \
139 INT_SWAP((reference),(reference)) \
2bd0ea18
NS
140 )
141
5000d01d 142/* does not return a value */
2bd0ea18 143#define INT_SET(reference,arch,valueref) \
8b087f23
SL
144 (__builtin_constant_p(valueref) ? \
145 (void)( (reference) = ( ((arch) != ARCH_NOCONVERT) ? (INT_SWAP((reference),(valueref))) : (valueref)) ) : \
146 (void)( \
147 ((reference) = (valueref)), \
148 ( ((arch) != ARCH_NOCONVERT) ? (reference) = INT_SWAP((reference),(reference)) : 0 ) \
149 ) \
2bd0ea18
NS
150 )
151
5000d01d 152/* does not return a value */
2bd0ea18
NS
153#define INT_MOD_EXPR(reference,arch,code) \
154 (void)(((arch) == ARCH_NOCONVERT) \
5000d01d
SL
155 ? \
156 ((reference) code) \
157 : \
158 ( \
159 (reference) = INT_GET((reference),arch) , \
160 ((reference) code), \
161 INT_SET(reference, arch, reference) \
162 ) \
2bd0ea18 163 )
5000d01d
SL
164
165/* does not return a value */
2bd0ea18
NS
166#define INT_MOD(reference,arch,delta) \
167 (void)( \
5000d01d 168 INT_MOD_EXPR(reference,arch,+=(delta)) \
2bd0ea18 169 )
5000d01d 170
2bd0ea18
NS
171/*
172 * INT_COPY - copy a value between two locations with the
5000d01d 173 * _same architecture_ but _potentially different sizes_
2bd0ea18 174 *
5000d01d
SL
175 * if the types of the two parameters are equal or they are
176 * in native architecture, a simple copy is done
2bd0ea18 177 *
5000d01d 178 * otherwise, architecture conversions are done
2bd0ea18
NS
179 *
180 */
5000d01d
SL
181
182/* does not return a value */
2bd0ea18
NS
183#define INT_COPY(dst,src,arch) \
184 (void)( \
5000d01d
SL
185 ((sizeof(dst) == sizeof(src)) || ((arch) == ARCH_NOCONVERT)) \
186 ? \
187 ((dst) = (src)) \
188 : \
189 INT_SET(dst, arch, INT_GET(src, arch)) \
2bd0ea18 190 )
5000d01d 191
2bd0ea18 192/*
5000d01d
SL
193 * INT_XLATE - copy a value in either direction between two locations
194 * with different architectures
2bd0ea18 195 *
5000d01d
SL
196 * dir < 0 - copy from memory to buffer (native to arch)
197 * dir > 0 - copy from buffer to memory (arch to native)
2bd0ea18 198 */
5000d01d
SL
199
200/* does not return a value */
2bd0ea18
NS
201#define INT_XLATE(buf,mem,dir,arch) {\
202 ASSERT(dir); \
203 if (dir>0) { \
5000d01d 204 (mem)=INT_GET(buf, arch); \
2bd0ea18 205 } else { \
5000d01d 206 INT_SET(buf, arch, mem); \
2bd0ea18
NS
207 } \
208}
209
210#define INT_ISZERO(reference,arch) \
211 ((reference) == 0)
5000d01d 212
2bd0ea18
NS
213#define INT_ZERO(reference,arch) \
214 ((reference) = 0)
5000d01d 215
2bd0ea18
NS
216#define INT_GET_UNALIGNED_16_ARCH(pointer,arch) \
217 ( ((arch) == ARCH_NOCONVERT) \
5000d01d
SL
218 ? \
219 (INT_GET_UNALIGNED_16(pointer)) \
220 : \
221 (INT_GET_UNALIGNED_16_BE(pointer)) \
2bd0ea18
NS
222 )
223#define INT_SET_UNALIGNED_16_ARCH(pointer,value,arch) \
224 if ((arch) == ARCH_NOCONVERT) { \
5000d01d 225 INT_SET_UNALIGNED_16(pointer,value); \
2bd0ea18 226 } else { \
5000d01d 227 INT_SET_UNALIGNED_16_BE(pointer,value); \
2bd0ea18
NS
228 }
229
230#endif /* __XFS_SUPPORT_ARCH_H__ */