summaryrefslogtreecommitdiff
path: root/firmware/export
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2011-01-30 00:58:45 +0000
committerMichael Sevakis <jethead71@rockbox.org>2011-01-30 00:58:45 +0000
commit18770dac2e560c88daa3ca9944917be561c3548f (patch)
tree5adf8aef7cb7999a6e07ddefcbb9e1d9bf18a15c /firmware/export
parentc0c769c5a86c56c2ab2c9e88515a64da98575182 (diff)
downloadrockbox-18770dac2e560c88daa3ca9944917be561c3548f.tar.gz
rockbox-18770dac2e560c88daa3ca9944917be561c3548f.zip
Use __builtin_constant_p() to select the best byteswapping method: constant or target optimized. Same macro can then be used for constant values and inits as well as non-constant.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29171 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/export')
-rw-r--r--firmware/export/system.h129
1 files changed, 69 insertions, 60 deletions
diff --git a/firmware/export/system.h b/firmware/export/system.h
index 4a5dcf0ea3..b246d154a4 100644
--- a/firmware/export/system.h
+++ b/firmware/export/system.h
@@ -137,61 +137,6 @@ int get_cpu_boost_counter(void);
137#undef htobe32 137#undef htobe32
138#endif 138#endif
139 139
140/* live endianness conversion */
141#ifdef ROCKBOX_LITTLE_ENDIAN
142#define letoh16(x) (x)
143#define letoh32(x) (x)
144#define htole16(x) (x)
145#define htole32(x) (x)
146#define betoh16(x) swap16(x)
147#define betoh32(x) swap32(x)
148#define htobe16(x) swap16(x)
149#define htobe32(x) swap32(x)
150#define swap_odd_even_be32(x) (x)
151#define swap_odd_even_le32(x) swap_odd_even32(x)
152#else
153#define letoh16(x) swap16(x)
154#define letoh32(x) swap32(x)
155#define htole16(x) swap16(x)
156#define htole32(x) swap32(x)
157#define betoh16(x) (x)
158#define betoh32(x) (x)
159#define htobe16(x) (x)
160#define htobe32(x) (x)
161#define swap_odd_even_be32(x) swap_odd_even32(x)
162#define swap_odd_even_le32(x) (x)
163#endif
164
165
166/* static endianness conversion */
167#define SWAP_16(x) ((typeof(x))(unsigned short)(((unsigned short)(x) >> 8) | \
168 ((unsigned short)(x) << 8)))
169
170#define SWAP_32(x) ((typeof(x))(unsigned long)( ((unsigned long)(x) >> 24) | \
171 (((unsigned long)(x) & 0xff0000ul) >> 8) | \
172 (((unsigned long)(x) & 0xff00ul) << 8) | \
173 ((unsigned long)(x) << 24)))
174
175#ifdef ROCKBOX_LITTLE_ENDIAN
176#define LE_TO_H16(x) (x)
177#define LE_TO_H32(x) (x)
178#define H_TO_LE16(x) (x)
179#define H_TO_LE32(x) (x)
180#define BE_TO_H16(x) SWAP_16(x)
181#define BE_TO_H32(x) SWAP_32(x)
182#define H_TO_BE16(x) SWAP_16(x)
183#define H_TO_BE32(x) SWAP_32(x)
184#else
185#define LE_TO_H16(x) SWAP_16(x)
186#define LE_TO_H32(x) SWAP_32(x)
187#define H_TO_LE16(x) SWAP_16(x)
188#define H_TO_LE32(x) SWAP_32(x)
189#define BE_TO_H16(x) (x)
190#define BE_TO_H32(x) (x)
191#define H_TO_BE16(x) (x)
192#define H_TO_BE32(x) (x)
193#endif
194
195/* Get the byte offset of a type's member */ 140/* Get the byte offset of a type's member */
196#define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername) 141#define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
197 142
@@ -237,7 +182,7 @@ enum {
237#endif 182#endif
238 183
239#ifdef NEED_GENERIC_BYTESWAPS 184#ifdef NEED_GENERIC_BYTESWAPS
240static inline uint16_t swap16(uint16_t value) 185static inline uint16_t swap16_hw(uint16_t value)
241 /* 186 /*
242 result[15..8] = value[ 7..0]; 187 result[15..8] = value[ 7..0];
243 result[ 7..0] = value[15..8]; 188 result[ 7..0] = value[15..8];
@@ -246,7 +191,7 @@ static inline uint16_t swap16(uint16_t value)
246 return (value >> 8) | (value << 8); 191 return (value >> 8) | (value << 8);
247} 192}
248 193
249static inline uint32_t swap32(uint32_t value) 194static inline uint32_t swap32_hw(uint32_t value)
250 /* 195 /*
251 result[31..24] = value[ 7.. 0]; 196 result[31..24] = value[ 7.. 0];
252 result[23..16] = value[15.. 8]; 197 result[23..16] = value[15.. 8];
@@ -254,12 +199,12 @@ static inline uint32_t swap32(uint32_t value)
254 result[ 7.. 0] = value[31..24]; 199 result[ 7.. 0] = value[31..24];
255 */ 200 */
256{ 201{
257 uint32_t hi = swap16(value >> 16); 202 uint32_t hi = swap16_hw(value >> 16);
258 uint32_t lo = swap16(value & 0xffff); 203 uint32_t lo = swap16_hw(value & 0xffff);
259 return (lo << 16) | hi; 204 return (lo << 16) | hi;
260} 205}
261 206
262static inline uint32_t swap_odd_even32(uint32_t value) 207static inline uint32_t swap_odd_even32_hw(uint32_t value)
263{ 208{
264 /* 209 /*
265 result[31..24],[15.. 8] = value[23..16],[ 7.. 0] 210 result[31..24],[15.. 8] = value[23..16],[ 7.. 0]
@@ -269,8 +214,72 @@ static inline uint32_t swap_odd_even32(uint32_t value)
269 return (t >> 8) | ((t ^ value) << 8); 214 return (t >> 8) | ((t ^ value) << 8);
270} 215}
271 216
217static inline uint32_t swaw32_hw(uint32_t value)
218{
219 /*
220 result[31..16] = value[15.. 0];
221 result[15.. 0] = value[31..16];
222 */
223 return (value >> 16) | (value << 16);
224}
225
272#endif /* NEED_GENERIC_BYTESWAPS */ 226#endif /* NEED_GENERIC_BYTESWAPS */
273 227
228/* static endianness conversion */
229#define SWAP16_CONST(x) \
230 ((typeof(x))( ((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8) ))
231
232#define SWAP32_CONST(x) \
233 ((typeof(x))( ((uint32_t)(x) >> 24) | \
234 (((uint32_t)(x) & 0xff0000) >> 8) | \
235 (((uint32_t)(x) & 0xff00) << 8) | \
236 ((uint32_t)(x) << 24) ))
237
238#define SWAP_ODD_EVEN32_CONST(x) \
239 ((typeof(x))( ((uint32_t)SWAP16_CONST((uint32_t)(x) >> 16) << 16) | \
240 SWAP16_CONST((uint32_t)(x))) )
241
242#define SWAW32_CONST(x) \
243 ((typeof(x))( ((uint32_t)(x) << 16) | ((uint32_t)(x) >> 16) ))
244
245/* Select best method based upon whether x is a constant expression */
246#define swap16(x) \
247 ( __builtin_constant_p(x) ? SWAP16_CONST(x) : (typeof(x))swap16_hw(x) )
248
249#define swap32(x) \
250 ( __builtin_constant_p(x) ? SWAP32_CONST(x) : (typeof(x))swap32_hw(x) )
251
252#define swap_odd_even32(x) \
253 ( __builtin_constant_p(x) ? SWAP_ODD_EVEN32_CONST(x) : (typeof(x))swap_odd_even32_hw(x) )
254
255#define swaw32(x) \
256 ( __builtin_constant_p(x) ? SWAW32_CONST(x) : (typeof(x))swaw32_hw(x) )
257
258
259#ifdef ROCKBOX_LITTLE_ENDIAN
260#define letoh16(x) (x)
261#define letoh32(x) (x)
262#define htole16(x) (x)
263#define htole32(x) (x)
264#define betoh16(x) swap16(x)
265#define betoh32(x) swap32(x)
266#define htobe16(x) swap16(x)
267#define htobe32(x) swap32(x)
268#define swap_odd_even_be32(x) (x)
269#define swap_odd_even_le32(x) swap_odd_even32(x)
270#else
271#define letoh16(x) swap16(x)
272#define letoh32(x) swap32(x)
273#define htole16(x) swap16(x)
274#define htole32(x) swap32(x)
275#define betoh16(x) (x)
276#define betoh32(x) (x)
277#define htobe16(x) (x)
278#define htobe32(x) (x)
279#define swap_odd_even_be32(x) swap_odd_even32(x)
280#define swap_odd_even_le32(x) (x)
281#endif
282
274#ifndef BIT_N 283#ifndef BIT_N
275#define BIT_N(n) (1U << (n)) 284#define BIT_N(n) (1U << (n))
276#endif 285#endif