]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
9911e5dc | 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
2bd0ea18 NS |
3 | * |
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. | |
7 | * | |
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. | |
11 | * | |
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. | |
18 | * | |
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. | |
22 | * | |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, | |
24 | * Mountain View, CA 94043, or: | |
25 | * | |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
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 | |
51 | #if __BYTE_ORDER == __LITTLE_ENDIAN | |
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 | { \ | |
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]; \ | |
75 | } | |
76 | ||
77 | #define INT_SWAP_UNALIGNED_64(from,to) \ | |
78 | { \ | |
79 | INT_SWAP_UNALIGNED_32( ((__u8*)(from)) + 4, ((__u8*)(to))); \ | |
80 | INT_SWAP_UNALIGNED_32( ((__u8*)(from)), ((__u8*)(to)) + 4); \ | |
81 | } | |
82 | ||
83 | /* | |
84 | * get and set integers from potentially unaligned locations | |
85 | */ | |
86 | ||
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) \ | |
92 | { \ | |
93 | ((__u8*)(pointer))[0] = (((value) ) & 0xff); \ | |
94 | ((__u8*)(pointer))[1] = (((value) >> 8) & 0xff); \ | |
95 | } | |
96 | #define INT_SET_UNALIGNED_16_BE(pointer,value) \ | |
97 | { \ | |
98 | ((__u8*)(pointer))[0] = (((value) >> 8) & 0xff); \ | |
99 | ((__u8*)(pointer))[1] = (((value) ) & 0xff); \ | |
100 | } | |
101 | ||
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] ))) | |
108 | ||
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)) )) | |
115 | ||
116 | /* | |
117 | * now pick the right ones for our MACHINE ARCHITECTURE | |
118 | */ | |
119 | ||
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) | |
125 | #else | |
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) | |
130 | #endif | |
131 | ||
132 | /* define generic INT_ macros */ | |
133 | ||
134 | #define INT_GET(reference,arch) \ | |
135 | (((arch) == ARCH_NOCONVERT) \ | |
136 | ? \ | |
137 | (reference) \ | |
138 | : \ | |
139 | INT_SWAP((reference),(reference)) \ | |
140 | ) | |
141 | ||
142 | /* does not return a value */ | |
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 | ||
152 | /* does not return a value */ | |
153 | #define INT_MOD_EXPR(reference,arch,code) \ | |
154 | (void)(((arch) == ARCH_NOCONVERT) \ | |
155 | ? \ | |
156 | ((reference) code) \ | |
157 | : \ | |
158 | ( \ | |
159 | (reference) = INT_GET((reference),arch) , \ | |
160 | ((reference) code), \ | |
161 | INT_SET(reference, arch, reference) \ | |
162 | ) \ | |
163 | ) | |
164 | ||
165 | /* does not return a value */ | |
166 | #define INT_MOD(reference,arch,delta) \ | |
167 | (void)( \ | |
168 | INT_MOD_EXPR(reference,arch,+=(delta)) \ | |
169 | ) | |
170 | ||
171 | /* | |
172 | * INT_COPY - copy a value between two locations with the | |
173 | * _same architecture_ but _potentially different sizes_ | |
174 | * | |
175 | * if the types of the two parameters are equal or they are | |
176 | * in native architecture, a simple copy is done | |
177 | * | |
178 | * otherwise, architecture conversions are done | |
179 | * | |
180 | */ | |
181 | ||
182 | /* does not return a value */ | |
183 | #define INT_COPY(dst,src,arch) \ | |
184 | (void)( \ | |
185 | ((sizeof(dst) == sizeof(src)) || ((arch) == ARCH_NOCONVERT)) \ | |
186 | ? \ | |
187 | ((dst) = (src)) \ | |
188 | : \ | |
189 | INT_SET(dst, arch, INT_GET(src, arch)) \ | |
190 | ) | |
191 | ||
192 | /* | |
193 | * INT_XLATE - copy a value in either direction between two locations | |
194 | * with different architectures | |
195 | * | |
196 | * dir < 0 - copy from memory to buffer (native to arch) | |
197 | * dir > 0 - copy from buffer to memory (arch to native) | |
198 | */ | |
199 | ||
200 | /* does not return a value */ | |
201 | #define INT_XLATE(buf,mem,dir,arch) {\ | |
202 | ASSERT(dir); \ | |
203 | if (dir>0) { \ | |
204 | (mem)=INT_GET(buf, arch); \ | |
205 | } else { \ | |
206 | INT_SET(buf, arch, mem); \ | |
207 | } \ | |
208 | } | |
209 | ||
210 | #define INT_ISZERO(reference,arch) \ | |
211 | ((reference) == 0) | |
212 | ||
213 | #define INT_ZERO(reference,arch) \ | |
214 | ((reference) = 0) | |
215 | ||
216 | #define INT_GET_UNALIGNED_16_ARCH(pointer,arch) \ | |
217 | ( ((arch) == ARCH_NOCONVERT) \ | |
218 | ? \ | |
219 | (INT_GET_UNALIGNED_16(pointer)) \ | |
220 | : \ | |
221 | (INT_GET_UNALIGNED_16_BE(pointer)) \ | |
222 | ) | |
223 | #define INT_SET_UNALIGNED_16_ARCH(pointer,value,arch) \ | |
224 | if ((arch) == ARCH_NOCONVERT) { \ | |
225 | INT_SET_UNALIGNED_16(pointer,value); \ | |
226 | } else { \ | |
227 | INT_SET_UNALIGNED_16_BE(pointer,value); \ | |
228 | } | |
229 | ||
230 | #endif /* __XFS_SUPPORT_ARCH_H__ */ |