2
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
12
* Further, this software is distributed without any warranty that it is
13
* free of the rightful claim of any third person regarding infringement
14
* or the like. Any license provided herein, whether implied or
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.
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.
23
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24
* Mountain View, CA 94043, or:
28
* For further information regarding this notice, see:
30
* http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32
#ifndef __XFS_SUPPORT_ARCH_H__
33
#define __XFS_SUPPORT_ARCH_H__
37
#include <asm/byteorder.h>
39
#ifdef __LITTLE_ENDIAN
40
# define __BYTE_ORDER __LITTLE_ENDIAN
43
# define __BYTE_ORDER __BIG_ENDIAN
46
#endif /* __KERNEL__ */
48
/* do we need conversion? */
50
#define ARCH_NOCONVERT 1
51
#if __BYTE_ORDER == __LITTLE_ENDIAN
52
#define ARCH_CONVERT 0
54
#define ARCH_CONVERT ARCH_NOCONVERT
57
/* generic swapping macros */
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))))
63
#define INT_SWAP(type, var) \
64
((sizeof(type) == 8) ? INT_SWAP64(type,var) : \
65
((sizeof(type) == 4) ? INT_SWAP32(type,var) : \
66
((sizeof(type) == 2) ? INT_SWAP16(type,var) : \
69
#define INT_SWAP_UNALIGNED_32(from,to) \
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]; \
77
#define INT_SWAP_UNALIGNED_64(from,to) \
79
INT_SWAP_UNALIGNED_32( ((__u8*)(from)) + 4, ((__u8*)(to))); \
80
INT_SWAP_UNALIGNED_32( ((__u8*)(from)), ((__u8*)(to)) + 4); \
84
* get and set integers from potentially unaligned locations
87
#define INT_GET_UNALIGNED_16_LE(pointer) \
88
((__u16)((((__u8*)(pointer))[0] ) | (((__u8*)(pointer))[1] << 8 )))
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) \
93
((__u8*)(pointer))[0] = (((value) ) & 0xff); \
94
((__u8*)(pointer))[1] = (((value) >> 8) & 0xff); \
96
#define INT_SET_UNALIGNED_16_BE(pointer,value) \
98
((__u8*)(pointer))[0] = (((value) >> 8) & 0xff); \
99
((__u8*)(pointer))[1] = (((value) ) & 0xff); \
102
#define INT_GET_UNALIGNED_32_LE(pointer) \
103
((__u32)((((__u8*)(pointer))[0] ) | (((__u8*)(pointer))[1] << 8 ) \
104
|(((__u8*)(pointer))[2] << 16) | (((__u8*)(pointer))[3] << 24)))
105
#define INT_GET_UNALIGNED_32_BE(pointer) \
106
((__u32)((((__u8*)(pointer))[0] << 24) | (((__u8*)(pointer))[1] << 16) \
107
|(((__u8*)(pointer))[2] << 8) | (((__u8*)(pointer))[3] )))
109
#define INT_GET_UNALIGNED_64_LE(pointer) \
110
(((__u64)(INT_GET_UNALIGNED_32_LE(((__u8*)(pointer))+4)) << 32 ) \
111
|((__u64)(INT_GET_UNALIGNED_32_LE(((__u8*)(pointer)) )) ))
112
#define INT_GET_UNALIGNED_64_BE(pointer) \
113
(((__u64)(INT_GET_UNALIGNED_32_BE(((__u8*)(pointer)) )) << 32 ) \
114
|((__u64)(INT_GET_UNALIGNED_32_BE(((__u8*)(pointer))+4)) ))
117
* now pick the right ones for our MACHINE ARCHITECTURE
120
#if __BYTE_ORDER == __LITTLE_ENDIAN
121
#define INT_GET_UNALIGNED_16(pointer) INT_GET_UNALIGNED_16_LE(pointer)
122
#define INT_SET_UNALIGNED_16(pointer,value) INT_SET_UNALIGNED_16_LE(pointer,value)
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)
126
#define INT_GET_UNALIGNED_16(pointer) INT_GET_UNALIGNED_16_BE(pointer)
127
#define INT_SET_UNALIGNED_16(pointer,value) INT_SET_UNALIGNED_16_BE(pointer,value)
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)
132
/* define generic INT_ macros */
134
#define INT_GET(reference,arch) \
135
(((arch) == ARCH_NOCONVERT) \
139
INT_SWAP((reference),(reference)) \
142
/* does not return a value */
143
#define INT_SET(reference,arch,valueref) \
144
(__builtin_constant_p(valueref) ? \
145
(void)( (reference) = ( ((arch) != ARCH_NOCONVERT) ? (INT_SWAP((reference),(valueref))) : (valueref)) ) : \
147
((reference) = (valueref)), \
148
( ((arch) != ARCH_NOCONVERT) ? (reference) = INT_SWAP((reference),(reference)) : 0 ) \
152
/* does not return a value */
153
#define INT_MOD_EXPR(reference,arch,code) \
154
(void)(((arch) == ARCH_NOCONVERT) \
159
(reference) = INT_GET((reference),arch) , \
160
((reference) code), \
161
INT_SET(reference, arch, reference) \
165
/* does not return a value */
166
#define INT_MOD(reference,arch,delta) \
168
INT_MOD_EXPR(reference,arch,+=(delta)) \
172
* INT_COPY - copy a value between two locations with the
173
* _same architecture_ but _potentially different sizes_
175
* if the types of the two parameters are equal or they are
176
* in native architecture, a simple copy is done
178
* otherwise, architecture conversions are done
182
/* does not return a value */
183
#define INT_COPY(dst,src,arch) \
185
((sizeof(dst) == sizeof(src)) || ((arch) == ARCH_NOCONVERT)) \
189
INT_SET(dst, arch, INT_GET(src, arch)) \
193
* INT_XLATE - copy a value in either direction between two locations
194
* with different architectures
196
* dir < 0 - copy from memory to buffer (native to arch)
197
* dir > 0 - copy from buffer to memory (arch to native)
200
/* does not return a value */
201
#define INT_XLATE(buf,mem,dir,arch) {\
204
(mem)=INT_GET(buf, arch); \
206
INT_SET(buf, arch, mem); \
210
#define INT_ISZERO(reference,arch) \
213
#define INT_ZERO(reference,arch) \
216
#define INT_GET_UNALIGNED_16_ARCH(pointer,arch) \
217
( ((arch) == ARCH_NOCONVERT) \
219
(INT_GET_UNALIGNED_16(pointer)) \
221
(INT_GET_UNALIGNED_16_BE(pointer)) \
223
#define INT_SET_UNALIGNED_16_ARCH(pointer,value,arch) \
224
if ((arch) == ARCH_NOCONVERT) { \
225
INT_SET_UNALIGNED_16(pointer,value); \
227
INT_SET_UNALIGNED_16_BE(pointer,value); \
230
#endif /* __XFS_SUPPORT_ARCH_H__ */