3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
32 * \brief Endianness and Byte Ordering macros and functions.
34 * \copydetails Group_Endianness
37 /** \ingroup Group_Endianness
38 * \defgroup Group_ByteSwapping Byte Reordering
39 * \brief Macros and functions for forced byte reordering.
42 /** \ingroup Group_Endianness
43 * \defgroup Group_EndianConversion Endianness Conversion
44 * \brief Macros and functions for automatic endianness conversion.
47 /** \ingroup Group_Common
48 * \defgroup Group_Endianness Endianness and Byte Ordering
49 * \brief Convenience macros and functions relating to byte (re-)ordering
51 * Common library convenience macros and functions relating to byte (re-)ordering.
56 #ifndef __LUFA_ENDIANNESS_H__
57 #define __LUFA_ENDIANNESS_H__
59 /* Preprocessor Checks: */
60 #if !defined(__INCLUDE_FROM_COMMON_H)
61 #error Do not include this file directly. Include LUFA/Common/Common.h instead to gain this functionality.
64 #if !(defined(ARCH_BIG_ENDIAN) || defined(ARCH_LITTLE_ENDIAN))
65 #error ARCH_BIG_ENDIAN or ARCH_LITTLE_ENDIAN not set for the specified architecture.
68 /* Public Interface - May be used in end-application: */
70 /** Swaps the byte ordering of a 16-bit value at compile-time. Do not use this macro for swapping byte orderings
71 * of dynamic values computed at runtime, use \ref SwapEndian_16() instead. The result of this macro can be used
72 * inside struct or other variable initializers outside of a function, something that is not possible with the
73 * inline function variant.
75 * \ingroup Group_ByteSwapping
77 * \param[in] x 16-bit value whose byte ordering is to be swapped.
79 * \return Input value with the byte ordering reversed.
81 #define SWAPENDIAN_16(x) (uint16_t)((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
83 /** Swaps the byte ordering of a 32-bit value at compile-time. Do not use this macro for swapping byte orderings
84 * of dynamic values computed at runtime- use \ref SwapEndian_32() instead. The result of this macro can be used
85 * inside struct or other variable initializers outside of a function, something that is not possible with the
86 * inline function variant.
88 * \ingroup Group_ByteSwapping
90 * \param[in] x 32-bit value whose byte ordering is to be swapped.
92 * \return Input value with the byte ordering reversed.
94 #define SWAPENDIAN_32(x) (uint32_t)((((x) & 0xFF000000UL) >> 24UL) | (((x) & 0x00FF0000UL) >> 8UL) | \
95 (((x) & 0x0000FF00UL) << 8UL) | (((x) & 0x000000FFUL) << 24UL))
97 #if defined(ARCH_BIG_ENDIAN) && !defined(le16_to_cpu)
98 #define le16_to_cpu(x) SwapEndian_16(x)
99 #define le32_to_cpu(x) SwapEndian_32(x)
100 #define be16_to_cpu(x) x
101 #define be32_to_cpu(x) x
102 #define cpu_to_le16(x) SwapEndian_16(x)
103 #define cpu_to_le32(x) SwapEndian_32(x)
104 #define cpu_to_be16(x) x
105 #define cpu_to_be32(x) x
106 #define LE16_TO_CPU(x) SWAPENDIAN_16(x)
107 #define LE32_TO_CPU(x) SWAPENDIAN_32(x)
108 #define BE16_TO_CPU(x) x
109 #define BE32_TO_CPU(x) x
110 #define CPU_TO_LE16(x) SWAPENDIAN_16(x)
111 #define CPU_TO_LE32(x) SWAPENDIAN_32(x)
112 #define CPU_TO_BE16(x) x
113 #define CPU_TO_BE32(x) x
114 #elif !defined(le16_to_cpu)
115 /** \name Run-time endianness conversion */
118 /** Performs a conversion between a Little Endian encoded 16-bit piece of data and the
119 * Endianness of the currently selected CPU architecture.
121 * On little endian architectures, this macro does nothing.
123 * \note This macro is designed for run-time conversion of data - for compile-time endianness
124 * conversion, use \ref LE16_TO_CPU instead.
126 * \ingroup Group_EndianConversion
128 * \param[in] x Data to perform the endianness conversion on.
130 * \return Endian corrected version of the input value.
132 #define le16_to_cpu(x) x
134 /** Performs a conversion between a Little Endian encoded 32-bit piece of data and the
135 * Endianness of the currently selected CPU architecture.
137 * On little endian architectures, this macro does nothing.
139 * \note This macro is designed for run-time conversion of data - for compile-time endianness
140 * conversion, use \ref LE32_TO_CPU instead.
142 * \ingroup Group_EndianConversion
144 * \param[in] x Data to perform the endianness conversion on.
146 * \return Endian corrected version of the input value.
148 #define le32_to_cpu(x) x
150 /** Performs a conversion between a Big Endian encoded 16-bit piece of data and the
151 * Endianness of the currently selected CPU architecture.
153 * On big endian architectures, this macro does nothing.
155 * \note This macro is designed for run-time conversion of data - for compile-time endianness
156 * conversion, use \ref BE16_TO_CPU instead.
158 * \ingroup Group_EndianConversion
160 * \param[in] x Data to perform the endianness conversion on.
162 * \return Endian corrected version of the input value.
164 #define be16_to_cpu(x) SwapEndian_16(x)
166 /** Performs a conversion between a Big Endian encoded 32-bit piece of data and the
167 * Endianness of the currently selected CPU architecture.
169 * On big endian architectures, this macro does nothing.
171 * \note This macro is designed for run-time conversion of data - for compile-time endianness
172 * conversion, use \ref BE32_TO_CPU instead.
174 * \ingroup Group_EndianConversion
176 * \param[in] x Data to perform the endianness conversion on.
178 * \return Endian corrected version of the input value.
180 #define be32_to_cpu(x) SwapEndian_32(x)
182 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
183 * is in Little Endian format regardless of the currently selected CPU architecture.
185 * On little endian architectures, this macro does nothing.
187 * \note This macro is designed for run-time conversion of data - for compile-time endianness
188 * conversion, use \ref CPU_TO_LE16 instead.
190 * \ingroup Group_EndianConversion
192 * \param[in] x Data to perform the endianness conversion on.
194 * \return Endian corrected version of the input value.
196 #define cpu_to_le16(x) x
198 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
199 * is in Little Endian format regardless of the currently selected CPU architecture.
201 * On little endian architectures, this macro does nothing.
203 * \note This macro is designed for run-time conversion of data - for compile-time endianness
204 * conversion, use \ref CPU_TO_LE32 instead.
206 * \ingroup Group_EndianConversion
208 * \param[in] x Data to perform the endianness conversion on.
210 * \return Endian corrected version of the input value.
212 #define cpu_to_le32(x) x
214 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
215 * is in Big Endian format regardless of the currently selected CPU architecture.
217 * On big endian architectures, this macro does nothing.
219 * \note This macro is designed for run-time conversion of data - for compile-time endianness
220 * conversion, use \ref CPU_TO_BE16 instead.
222 * \ingroup Group_EndianConversion
224 * \param[in] x Data to perform the endianness conversion on.
226 * \return Endian corrected version of the input value.
228 #define cpu_to_be16(x) SwapEndian_16(x)
230 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
231 * is in Big Endian format regardless of the currently selected CPU architecture.
233 * On big endian architectures, this macro does nothing.
235 * \note This macro is designed for run-time conversion of data - for compile-time endianness
236 * conversion, use \ref CPU_TO_BE32 instead.
238 * \ingroup Group_EndianConversion
240 * \param[in] x Data to perform the endianness conversion on.
242 * \return Endian corrected version of the input value.
244 #define cpu_to_be32(x) SwapEndian_32(x)
248 /** \name Compile-time endianness conversion */
251 /** Performs a conversion between a Little Endian encoded 16-bit piece of data and the
252 * Endianness of the currently selected CPU architecture.
254 * On little endian architectures, this macro does nothing.
256 * \note This macro is designed for compile-time conversion of data - for run time endianness
257 * conversion, use \ref le16_to_cpu instead.
259 * \ingroup Group_EndianConversion
261 * \param[in] x Data to perform the endianness conversion on.
263 * \return Endian corrected version of the input value.
265 #define LE16_TO_CPU(x) x
267 /** Performs a conversion between a Little Endian encoded 32-bit piece of data and the
268 * Endianness of the currently selected CPU architecture.
270 * On little endian architectures, this macro does nothing.
272 * \note This macro is designed for compile-time conversion of data - for run time endianness
273 * conversion, use \ref le32_to_cpu instead.
275 * \ingroup Group_EndianConversion
277 * \param[in] x Data to perform the endianness conversion on.
279 * \return Endian corrected version of the input value.
281 #define LE32_TO_CPU(x) x
283 /** Performs a conversion between a Big Endian encoded 16-bit piece of data and the
284 * Endianness of the currently selected CPU architecture.
286 * On big endian architectures, this macro does nothing.
288 * \note This macro is designed for compile-time conversion of data - for run-time endianness
289 * conversion, use \ref be16_to_cpu instead.
291 * \ingroup Group_EndianConversion
293 * \param[in] x Data to perform the endianness conversion on.
295 * \return Endian corrected version of the input value.
297 #define BE16_TO_CPU(x) SWAPENDIAN_16(x)
299 /** Performs a conversion between a Big Endian encoded 32-bit piece of data and the
300 * Endianness of the currently selected CPU architecture.
302 * On big endian architectures, this macro does nothing.
304 * \note This macro is designed for compile-time conversion of data - for run-time endianness
305 * conversion, use \ref be32_to_cpu instead.
307 * \ingroup Group_EndianConversion
309 * \param[in] x Data to perform the endianness conversion on.
311 * \return Endian corrected version of the input value.
313 #define BE32_TO_CPU(x) SWAPENDIAN_32(x)
315 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
316 * is in Little Endian format regardless of the currently selected CPU architecture.
318 * On little endian architectures, this macro does nothing.
320 * \note This macro is designed for compile-time conversion of data - for run-time endianness
321 * conversion, use \ref cpu_to_le16 instead.
323 * \ingroup Group_EndianConversion
325 * \param[in] x Data to perform the endianness conversion on.
327 * \return Endian corrected version of the input value.
329 #define CPU_TO_LE16(x) x
331 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
332 * is in Little Endian format regardless of the currently selected CPU architecture.
334 * On little endian architectures, this macro does nothing.
336 * \note This macro is designed for compile-time conversion of data - for run-time endianness
337 * conversion, use \ref cpu_to_le32 instead.
339 * \ingroup Group_EndianConversion
341 * \param[in] x Data to perform the endianness conversion on.
343 * \return Endian corrected version of the input value.
345 #define CPU_TO_LE32(x) x
347 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
348 * is in Big Endian format regardless of the currently selected CPU architecture.
350 * On big endian architectures, this macro does nothing.
352 * \note This macro is designed for compile-time conversion of data - for run-time endianness
353 * conversion, use \ref cpu_to_be16 instead.
355 * \ingroup Group_EndianConversion
357 * \param[in] x Data to perform the endianness conversion on.
359 * \return Endian corrected version of the input value.
361 #define CPU_TO_BE16(x) SWAPENDIAN_16(x)
363 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
364 * is in Big Endian format regardless of the currently selected CPU architecture.
366 * On big endian architectures, this macro does nothing.
368 * \note This macro is designed for compile-time conversion of data - for run-time endianness
369 * conversion, use \ref cpu_to_be32 instead.
371 * \ingroup Group_EndianConversion
373 * \param[in] x Data to perform the endianness conversion on.
375 * \return Endian corrected version of the input value.
377 #define CPU_TO_BE32(x) SWAPENDIAN_32(x)
382 /* Inline Functions: */
383 /** Function to reverse the byte ordering of the individual bytes in a 16 bit value.
385 * \ingroup Group_ByteSwapping
387 * \param[in] Word Word of data whose bytes are to be swapped.
389 static inline uint16_t SwapEndian_16(const uint16_t Word
) ATTR_WARN_UNUSED_RESULT ATTR_CONST
;
390 static inline uint16_t SwapEndian_16(const uint16_t Word
)
402 Temp
= Data
.Bytes
[0];
403 Data
.Bytes
[0] = Data
.Bytes
[1];
404 Data
.Bytes
[1] = Temp
;
409 /** Function to reverse the byte ordering of the individual bytes in a 32 bit value.
411 * \ingroup Group_ByteSwapping
413 * \param[in] DWord Double word of data whose bytes are to be swapped.
415 static inline uint32_t SwapEndian_32(const uint32_t DWord
) ATTR_WARN_UNUSED_RESULT ATTR_CONST
;
416 static inline uint32_t SwapEndian_32(const uint32_t DWord
)
428 Temp
= Data
.Bytes
[0];
429 Data
.Bytes
[0] = Data
.Bytes
[3];
430 Data
.Bytes
[3] = Temp
;
432 Temp
= Data
.Bytes
[1];
433 Data
.Bytes
[1] = Data
.Bytes
[2];
434 Data
.Bytes
[2] = Temp
;
439 /** Function to reverse the byte ordering of the individual bytes in a n byte value.
441 * \ingroup Group_ByteSwapping
443 * \param[in,out] Data Pointer to a number containing an even number of bytes to be reversed.
444 * \param[in] Length Length of the data in bytes.
446 static inline void SwapEndian_n(void* const Data
,
447 uint8_t Length
) ATTR_NON_NULL_PTR_ARG(1);
448 static inline void SwapEndian_n(void* const Data
,
451 uint8_t* CurrDataPos
= (uint8_t*)Data
;
455 uint8_t Temp
= *CurrDataPos
;
456 *CurrDataPos
= *(CurrDataPos
+ Length
- 1);
457 *(CurrDataPos
+ Length
- 1) = Temp
;