3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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 disclaims 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         /* Enable C linkage for C++ Compilers: */ 
  60                 #if defined(__cplusplus) 
  64         /* Preprocessor Checks: */ 
  65                 #if !defined(__INCLUDE_FROM_COMMON_H) 
  66                         #error Do not include this file directly. Include LUFA/Common/Common.h instead to gain this functionality. 
  69                 #if !(defined(ARCH_BIG_ENDIAN) || defined(ARCH_LITTLE_ENDIAN)) 
  70                         #error ARCH_BIG_ENDIAN or ARCH_LITTLE_ENDIAN not set for the specified architecture. 
  73         /* Public Interface - May be used in end-application: */ 
  75                         /** Swaps the byte ordering of a 16-bit value at compile-time. Do not use this macro for swapping byte orderings 
  76                          *  of dynamic values computed at runtime, use \ref SwapEndian_16() instead. The result of this macro can be used 
  77                          *  inside struct or other variable initializers outside of a function, something that is not possible with the 
  78                          *  inline function variant. 
  82                          *  \ingroup Group_ByteSwapping 
  84                          *  \param[in] x  16-bit value whose byte ordering is to be swapped. 
  86                          *  \return Input value with the byte ordering reversed. 
  88                         #define SWAPENDIAN_16(x)            (uint16_t)((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) 
  90                         /** Swaps the byte ordering of a 32-bit value at compile-time. Do not use this macro for swapping byte orderings 
  91                          *  of dynamic values computed at runtime- use \ref SwapEndian_32() instead. The result of this macro can be used 
  92                          *  inside struct or other variable initializers outside of a function, something that is not possible with the 
  93                          *  inline function variant. 
  97                          *  \ingroup Group_ByteSwapping 
  99                          *  \param[in] x  32-bit value whose byte ordering is to be swapped. 
 101                          *  \return Input value with the byte ordering reversed. 
 103                         #define SWAPENDIAN_32(x)            (uint32_t)((((x) & 0xFF000000UL) >> 24UL) | (((x) & 0x00FF0000UL) >> 8UL) | \ 
 104                                                                        (((x) & 0x0000FF00UL) << 8UL)  | (((x) & 0x000000FFUL) << 24UL)) 
 106                         #if defined(ARCH_BIG_ENDIAN) && !defined(le16_to_cpu) 
 107                                 #define le16_to_cpu(x)           SwapEndian_16(x) 
 108                                 #define le32_to_cpu(x)           SwapEndian_32(x) 
 109                                 #define be16_to_cpu(x)           (x) 
 110                                 #define be32_to_cpu(x)           (x) 
 111                                 #define cpu_to_le16(x)           SwapEndian_16(x) 
 112                                 #define cpu_to_le32(x)           SwapEndian_32(x) 
 113                                 #define cpu_to_be16(x)           (x) 
 114                                 #define cpu_to_be32(x)           (x) 
 115                                 #define LE16_TO_CPU(x)           SWAPENDIAN_16(x) 
 116                                 #define LE32_TO_CPU(x)           SWAPENDIAN_32(x) 
 117                                 #define BE16_TO_CPU(x)           (x) 
 118                                 #define BE32_TO_CPU(x)           (x) 
 119                                 #define CPU_TO_LE16(x)           SWAPENDIAN_16(x) 
 120                                 #define CPU_TO_LE32(x)           SWAPENDIAN_32(x) 
 121                                 #define CPU_TO_BE16(x)           (x) 
 122                                 #define CPU_TO_BE32(x)           (x) 
 123                         #elif !defined(le16_to_cpu) 
 124                                 /** \name Run-time endianness conversion */ 
 127                                 /** Performs a conversion between a Little Endian encoded 16-bit piece of data and the 
 128                                  *  Endianness of the currently selected CPU architecture. 
 130                                  *  On little endian architectures, this macro does nothing. 
 132                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 133                                  *        conversion, use \ref LE16_TO_CPU instead. 
 135                                  *  \ingroup Group_EndianConversion 
 137                                  *  \param[in] x  Data to perform the endianness conversion on. 
 139                                  *  \return Endian corrected version of the input value. 
 141                                 #define le16_to_cpu(x)           (x) 
 143                                 /** Performs a conversion between a Little Endian encoded 32-bit piece of data and the 
 144                                  *  Endianness of the currently selected CPU architecture. 
 146                                  *  On little endian architectures, this macro does nothing. 
 148                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 149                                  *        conversion, use \ref LE32_TO_CPU instead. 
 151                                  *  \ingroup Group_EndianConversion 
 153                                  *  \param[in] x  Data to perform the endianness conversion on. 
 155                                  *  \return Endian corrected version of the input value. 
 157                                 #define le32_to_cpu(x)           (x) 
 159                                 /** Performs a conversion between a Big Endian encoded 16-bit piece of data and the 
 160                                  *  Endianness of the currently selected CPU architecture. 
 162                                  *  On big endian architectures, this macro does nothing. 
 164                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 165                                  *        conversion, use \ref BE16_TO_CPU instead. 
 167                                  *  \ingroup Group_EndianConversion 
 169                                  *  \param[in] x  Data to perform the endianness conversion on. 
 171                                  *  \return Endian corrected version of the input value. 
 173                                 #define be16_to_cpu(x)           SwapEndian_16(x) 
 175                                 /** Performs a conversion between a Big Endian encoded 32-bit piece of data and the 
 176                                  *  Endianness of the currently selected CPU architecture. 
 178                                  *  On big endian architectures, this macro does nothing. 
 180                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 181                                  *        conversion, use \ref BE32_TO_CPU instead. 
 183                                  *  \ingroup Group_EndianConversion 
 185                                  *  \param[in] x  Data to perform the endianness conversion on. 
 187                                  *  \return Endian corrected version of the input value. 
 189                                 #define be32_to_cpu(x)           SwapEndian_32(x) 
 191                                 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it 
 192                                  *  is in Little Endian format regardless of the currently selected CPU architecture. 
 194                                  *  On little endian architectures, this macro does nothing. 
 196                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 197                                  *        conversion, use \ref CPU_TO_LE16 instead. 
 199                                  *  \ingroup Group_EndianConversion 
 201                                  *  \param[in] x  Data to perform the endianness conversion on. 
 203                                  *  \return Endian corrected version of the input value. 
 205                                 #define cpu_to_le16(x)           (x) 
 207                                 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it 
 208                                  *  is in Little Endian format regardless of the currently selected CPU architecture. 
 210                                  *  On little endian architectures, this macro does nothing. 
 212                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 213                                  *        conversion, use \ref CPU_TO_LE32 instead. 
 215                                  *  \ingroup Group_EndianConversion 
 217                                  *  \param[in] x  Data to perform the endianness conversion on. 
 219                                  *  \return Endian corrected version of the input value. 
 221                                 #define cpu_to_le32(x)           (x) 
 223                                 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it 
 224                                  *  is in Big Endian format regardless of the currently selected CPU architecture. 
 226                                  *  On big endian architectures, this macro does nothing. 
 228                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 229                                  *        conversion, use \ref CPU_TO_BE16 instead. 
 231                                  *  \ingroup Group_EndianConversion 
 233                                  *  \param[in] x  Data to perform the endianness conversion on. 
 235                                  *  \return Endian corrected version of the input value. 
 237                                 #define cpu_to_be16(x)           SwapEndian_16(x) 
 239                                 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it 
 240                                  *  is in Big Endian format regardless of the currently selected CPU architecture. 
 242                                  *  On big endian architectures, this macro does nothing. 
 244                                  *  \note This macro is designed for run-time conversion of data - for compile-time endianness 
 245                                  *        conversion, use \ref CPU_TO_BE32 instead. 
 247                                  *  \ingroup Group_EndianConversion 
 249                                  *  \param[in] x  Data to perform the endianness conversion on. 
 251                                  *  \return Endian corrected version of the input value. 
 253                                 #define cpu_to_be32(x)           SwapEndian_32(x) 
 257                                 /** \name Compile-time endianness conversion */ 
 260                                 /** Performs a conversion between a Little Endian encoded 16-bit piece of data and the 
 261                                  *  Endianness of the currently selected CPU architecture. 
 263                                  *  On little endian architectures, this macro does nothing. 
 265                                  *  \note This macro is designed for compile-time conversion of data - for run time endianness 
 266                                  *        conversion, use \ref le16_to_cpu instead. 
 268                                  *  \ingroup Group_EndianConversion 
 270                                  *  \param[in] x  Data to perform the endianness conversion on. 
 272                                  *  \return Endian corrected version of the input value. 
 274                                 #define LE16_TO_CPU(x)           (x) 
 276                                 /** Performs a conversion between a Little Endian encoded 32-bit piece of data and the 
 277                                  *  Endianness of the currently selected CPU architecture. 
 279                                  *  On little endian architectures, this macro does nothing. 
 281                                  *  \note This macro is designed for compile-time conversion of data - for run time endianness 
 282                                  *        conversion, use \ref le32_to_cpu instead. 
 284                                  *  \ingroup Group_EndianConversion 
 286                                  *  \param[in] x  Data to perform the endianness conversion on. 
 288                                  *  \return Endian corrected version of the input value. 
 290                                 #define LE32_TO_CPU(x)           (x) 
 292                                 /** Performs a conversion between a Big Endian encoded 16-bit piece of data and the 
 293                                  *  Endianness of the currently selected CPU architecture. 
 295                                  *  On big endian architectures, this macro does nothing. 
 297                                  *  \note This macro is designed for compile-time conversion of data - for run-time endianness 
 298                                  *        conversion, use \ref be16_to_cpu instead. 
 300                                  *  \ingroup Group_EndianConversion 
 302                                  *  \param[in] x  Data to perform the endianness conversion on. 
 304                                  *  \return Endian corrected version of the input value. 
 306                                 #define BE16_TO_CPU(x)           SWAPENDIAN_16(x) 
 308                                 /** Performs a conversion between a Big Endian encoded 32-bit piece of data and the 
 309                                  *  Endianness of the currently selected CPU architecture. 
 311                                  *  On big endian architectures, this macro does nothing. 
 313                                  *  \note This macro is designed for compile-time conversion of data - for run-time endianness 
 314                                  *        conversion, use \ref be32_to_cpu instead. 
 316                                  *  \ingroup Group_EndianConversion 
 318                                  *  \param[in] x  Data to perform the endianness conversion on. 
 320                                  *  \return Endian corrected version of the input value. 
 322                                 #define BE32_TO_CPU(x)           SWAPENDIAN_32(x) 
 324                                 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it 
 325                                  *  is in Little Endian format regardless of the currently selected CPU architecture. 
 327                                  *  On little endian architectures, this macro does nothing. 
 329                                  *  \note This macro is designed for compile-time conversion of data - for run-time endianness 
 330                                  *        conversion, use \ref cpu_to_le16 instead. 
 332                                  *  \ingroup Group_EndianConversion 
 334                                  *  \param[in] x  Data to perform the endianness conversion on. 
 336                                  *  \return Endian corrected version of the input value. 
 338                                 #define CPU_TO_LE16(x)           (x) 
 340                                 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it 
 341                                  *  is in Little Endian format regardless of the currently selected CPU architecture. 
 343                                  *  On little endian architectures, this macro does nothing. 
 345                                  *  \note This macro is designed for compile-time conversion of data - for run-time endianness 
 346                                  *        conversion, use \ref cpu_to_le32 instead. 
 348                                  *  \ingroup Group_EndianConversion 
 350                                  *  \param[in] x  Data to perform the endianness conversion on. 
 352                                  *  \return Endian corrected version of the input value. 
 354                                 #define CPU_TO_LE32(x)           (x) 
 356                                 /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it 
 357                                  *  is in Big Endian format regardless of the currently selected CPU architecture. 
 359                                  *  On big endian architectures, this macro does nothing. 
 361                                  *  \note This macro is designed for compile-time conversion of data - for run-time endianness 
 362                                  *        conversion, use \ref cpu_to_be16 instead. 
 364                                  *  \ingroup Group_EndianConversion 
 366                                  *  \param[in] x  Data to perform the endianness conversion on. 
 368                                  *  \return Endian corrected version of the input value. 
 370                                 #define CPU_TO_BE16(x)           SWAPENDIAN_16(x) 
 372                                 /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it 
 373                                  *  is in Big Endian format regardless of the currently selected CPU architecture. 
 375                                  *  On big endian architectures, this macro does nothing. 
 377                                  *  \note This macro is designed for compile-time conversion of data - for run-time endianness 
 378                                  *        conversion, use \ref cpu_to_be32 instead. 
 380                                  *  \ingroup Group_EndianConversion 
 382                                  *  \param[in] x  Data to perform the endianness conversion on. 
 384                                  *  \return Endian corrected version of the input value. 
 386                                 #define CPU_TO_BE32(x)           SWAPENDIAN_32(x) 
 391                 /* Inline Functions: */ 
 392                         /** Function to reverse the byte ordering of the individual bytes in a 16 bit value. 
 394                          *  \ingroup Group_ByteSwapping 
 396                          *  \param[in] Word  Word of data whose bytes are to be swapped. 
 398                          *  \return Input data with the individual bytes reversed. 
 400                         static inline uint16_t SwapEndian_16(const uint16_t Word
) ATTR_WARN_UNUSED_RESULT ATTR_CONST
; 
 401                         static inline uint16_t SwapEndian_16(const uint16_t Word
) 
 403                                 if (GCC_IS_COMPILE_CONST(Word
)) 
 404                                   return SWAPENDIAN_16(Word
); 
 416                                 Temp 
= Data
.Bytes
[0]; 
 417                                 Data
.Bytes
[0] = Data
.Bytes
[1]; 
 418                                 Data
.Bytes
[1] = Temp
; 
 423                         /** Function to reverse the byte ordering of the individual bytes in a 32 bit value. 
 425                          *  \ingroup Group_ByteSwapping 
 427                          *  \param[in] DWord  Double word of data whose bytes are to be swapped. 
 429                          *  \return Input data with the individual bytes reversed. 
 431                         static inline uint32_t SwapEndian_32(const uint32_t DWord
) ATTR_WARN_UNUSED_RESULT ATTR_CONST
; 
 432                         static inline uint32_t SwapEndian_32(const uint32_t DWord
) 
 434                                 if (GCC_IS_COMPILE_CONST(DWord
)) 
 435                                   return SWAPENDIAN_32(DWord
); 
 447                                 Temp 
= Data
.Bytes
[0]; 
 448                                 Data
.Bytes
[0] = Data
.Bytes
[3]; 
 449                                 Data
.Bytes
[3] = Temp
; 
 451                                 Temp 
= Data
.Bytes
[1]; 
 452                                 Data
.Bytes
[1] = Data
.Bytes
[2]; 
 453                                 Data
.Bytes
[2] = Temp
; 
 458                         /** Function to reverse the byte ordering of the individual bytes in a n byte value. 
 460                          *  \ingroup Group_ByteSwapping 
 462                          *  \param[in,out] Data    Pointer to a number containing an even number of bytes to be reversed. 
 463                          *  \param[in]     Length  Length of the data in bytes. 
 465                          *  \return Input data with the individual bytes reversed. 
 467                         static inline void SwapEndian_n(void* const Data
, 
 468                                                         uint8_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 469                         static inline void SwapEndian_n(void* const Data
, 
 472                                 uint8_t* CurrDataPos 
= (uint8_t*)Data
; 
 476                                         uint8_t Temp 
= *CurrDataPos
; 
 477                                         *CurrDataPos 
= *(CurrDataPos 
+ Length 
- 1); 
 478                                         *(CurrDataPos 
+ Length 
- 1) = Temp
; 
 485         /* Disable C linkage for C++ Compilers: */ 
 486                 #if defined(__cplusplus)