You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Endianness.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2012.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaim all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief Endianness and Byte Ordering macros and functions.
  28. *
  29. * \copydetails Group_Endianness
  30. */
  31. /** \ingroup Group_Endianness
  32. * \defgroup Group_ByteSwapping Byte Reordering
  33. * \brief Macros and functions for forced byte reordering.
  34. */
  35. /** \ingroup Group_Endianness
  36. * \defgroup Group_EndianConversion Endianness Conversion
  37. * \brief Macros and functions for automatic endianness conversion.
  38. */
  39. /** \ingroup Group_Common
  40. * \defgroup Group_Endianness Endianness and Byte Ordering
  41. * \brief Convenience macros and functions relating to byte (re-)ordering
  42. *
  43. * Common library convenience macros and functions relating to byte (re-)ordering.
  44. *
  45. * @{
  46. */
  47. #ifndef __LUFA_ENDIANNESS_H__
  48. #define __LUFA_ENDIANNESS_H__
  49. /* Enable C linkage for C++ Compilers: */
  50. #if defined(__cplusplus)
  51. extern "C" {
  52. #endif
  53. /* Preprocessor Checks: */
  54. #if !defined(__INCLUDE_FROM_COMMON_H)
  55. #error Do not include this file directly. Include LUFA/Common/Common.h instead to gain this functionality.
  56. #endif
  57. #if !(defined(ARCH_BIG_ENDIAN) || defined(ARCH_LITTLE_ENDIAN))
  58. #error ARCH_BIG_ENDIAN or ARCH_LITTLE_ENDIAN not set for the specified architecture.
  59. #endif
  60. /* Public Interface - May be used in end-application: */
  61. /* Macros: */
  62. /** Swaps the byte ordering of a 16-bit value at compile-time. Do not use this macro for swapping byte orderings
  63. * of dynamic values computed at runtime, use \ref SwapEndian_16() instead. The result of this macro can be used
  64. * inside struct or other variable initializers outside of a function, something that is not possible with the
  65. * inline function variant.
  66. *
  67. * \ingroup Group_ByteSwapping
  68. *
  69. * \param[in] x 16-bit value whose byte ordering is to be swapped.
  70. *
  71. * \return Input value with the byte ordering reversed.
  72. */
  73. #define SWAPENDIAN_16(x) (uint16_t)((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
  74. /** Swaps the byte ordering of a 32-bit value at compile-time. Do not use this macro for swapping byte orderings
  75. * of dynamic values computed at runtime- use \ref SwapEndian_32() instead. The result of this macro can be used
  76. * inside struct or other variable initializers outside of a function, something that is not possible with the
  77. * inline function variant.
  78. *
  79. * \ingroup Group_ByteSwapping
  80. *
  81. * \param[in] x 32-bit value whose byte ordering is to be swapped.
  82. *
  83. * \return Input value with the byte ordering reversed.
  84. */
  85. #define SWAPENDIAN_32(x) (uint32_t)((((x) & 0xFF000000UL) >> 24UL) | (((x) & 0x00FF0000UL) >> 8UL) | \
  86. (((x) & 0x0000FF00UL) << 8UL) | (((x) & 0x000000FFUL) << 24UL))
  87. #if defined(ARCH_BIG_ENDIAN) && !defined(le16_to_cpu)
  88. #define le16_to_cpu(x) SwapEndian_16(x)
  89. #define le32_to_cpu(x) SwapEndian_32(x)
  90. #define be16_to_cpu(x) (x)
  91. #define be32_to_cpu(x) (x)
  92. #define cpu_to_le16(x) SwapEndian_16(x)
  93. #define cpu_to_le32(x) SwapEndian_32(x)
  94. #define cpu_to_be16(x) (x)
  95. #define cpu_to_be32(x) (x)
  96. #define LE16_TO_CPU(x) SWAPENDIAN_16(x)
  97. #define LE32_TO_CPU(x) SWAPENDIAN_32(x)
  98. #define BE16_TO_CPU(x) (x)
  99. #define BE32_TO_CPU(x) (x)
  100. #define CPU_TO_LE16(x) SWAPENDIAN_16(x)
  101. #define CPU_TO_LE32(x) SWAPENDIAN_32(x)
  102. #define CPU_TO_BE16(x) (x)
  103. #define CPU_TO_BE32(x) (x)
  104. #elif !defined(le16_to_cpu)
  105. /** \name Run-time endianness conversion */
  106. //@{
  107. /** Performs a conversion between a Little Endian encoded 16-bit piece of data and the
  108. * Endianness of the currently selected CPU architecture.
  109. *
  110. * On little endian architectures, this macro does nothing.
  111. *
  112. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  113. * conversion, use \ref LE16_TO_CPU instead.
  114. *
  115. * \ingroup Group_EndianConversion
  116. *
  117. * \param[in] x Data to perform the endianness conversion on.
  118. *
  119. * \return Endian corrected version of the input value.
  120. */
  121. #define le16_to_cpu(x) (x)
  122. /** Performs a conversion between a Little Endian encoded 32-bit piece of data and the
  123. * Endianness of the currently selected CPU architecture.
  124. *
  125. * On little endian architectures, this macro does nothing.
  126. *
  127. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  128. * conversion, use \ref LE32_TO_CPU instead.
  129. *
  130. * \ingroup Group_EndianConversion
  131. *
  132. * \param[in] x Data to perform the endianness conversion on.
  133. *
  134. * \return Endian corrected version of the input value.
  135. */
  136. #define le32_to_cpu(x) (x)
  137. /** Performs a conversion between a Big Endian encoded 16-bit piece of data and the
  138. * Endianness of the currently selected CPU architecture.
  139. *
  140. * On big endian architectures, this macro does nothing.
  141. *
  142. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  143. * conversion, use \ref BE16_TO_CPU instead.
  144. *
  145. * \ingroup Group_EndianConversion
  146. *
  147. * \param[in] x Data to perform the endianness conversion on.
  148. *
  149. * \return Endian corrected version of the input value.
  150. */
  151. #define be16_to_cpu(x) SwapEndian_16(x)
  152. /** Performs a conversion between a Big Endian encoded 32-bit piece of data and the
  153. * Endianness of the currently selected CPU architecture.
  154. *
  155. * On big endian architectures, this macro does nothing.
  156. *
  157. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  158. * conversion, use \ref BE32_TO_CPU instead.
  159. *
  160. * \ingroup Group_EndianConversion
  161. *
  162. * \param[in] x Data to perform the endianness conversion on.
  163. *
  164. * \return Endian corrected version of the input value.
  165. */
  166. #define be32_to_cpu(x) SwapEndian_32(x)
  167. /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
  168. * is in Little Endian format regardless of the currently selected CPU architecture.
  169. *
  170. * On little endian architectures, this macro does nothing.
  171. *
  172. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  173. * conversion, use \ref CPU_TO_LE16 instead.
  174. *
  175. * \ingroup Group_EndianConversion
  176. *
  177. * \param[in] x Data to perform the endianness conversion on.
  178. *
  179. * \return Endian corrected version of the input value.
  180. */
  181. #define cpu_to_le16(x) (x)
  182. /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
  183. * is in Little Endian format regardless of the currently selected CPU architecture.
  184. *
  185. * On little endian architectures, this macro does nothing.
  186. *
  187. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  188. * conversion, use \ref CPU_TO_LE32 instead.
  189. *
  190. * \ingroup Group_EndianConversion
  191. *
  192. * \param[in] x Data to perform the endianness conversion on.
  193. *
  194. * \return Endian corrected version of the input value.
  195. */
  196. #define cpu_to_le32(x) (x)
  197. /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
  198. * is in Big Endian format regardless of the currently selected CPU architecture.
  199. *
  200. * On big endian architectures, this macro does nothing.
  201. *
  202. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  203. * conversion, use \ref CPU_TO_BE16 instead.
  204. *
  205. * \ingroup Group_EndianConversion
  206. *
  207. * \param[in] x Data to perform the endianness conversion on.
  208. *
  209. * \return Endian corrected version of the input value.
  210. */
  211. #define cpu_to_be16(x) SwapEndian_16(x)
  212. /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
  213. * is in Big Endian format regardless of the currently selected CPU architecture.
  214. *
  215. * On big endian architectures, this macro does nothing.
  216. *
  217. * \note This macro is designed for run-time conversion of data - for compile-time endianness
  218. * conversion, use \ref CPU_TO_BE32 instead.
  219. *
  220. * \ingroup Group_EndianConversion
  221. *
  222. * \param[in] x Data to perform the endianness conversion on.
  223. *
  224. * \return Endian corrected version of the input value.
  225. */
  226. #define cpu_to_be32(x) SwapEndian_32(x)
  227. //@}
  228. /** \name Compile-time endianness conversion */
  229. //@{
  230. /** Performs a conversion between a Little Endian encoded 16-bit piece of data and the
  231. * Endianness of the currently selected CPU architecture.
  232. *
  233. * On little endian architectures, this macro does nothing.
  234. *
  235. * \note This macro is designed for compile-time conversion of data - for run time endianness
  236. * conversion, use \ref le16_to_cpu instead.
  237. *
  238. * \ingroup Group_EndianConversion
  239. *
  240. * \param[in] x Data to perform the endianness conversion on.
  241. *
  242. * \return Endian corrected version of the input value.
  243. */
  244. #define LE16_TO_CPU(x) (x)
  245. /** Performs a conversion between a Little Endian encoded 32-bit piece of data and the
  246. * Endianness of the currently selected CPU architecture.
  247. *
  248. * On little endian architectures, this macro does nothing.
  249. *
  250. * \note This macro is designed for compile-time conversion of data - for run time endianness
  251. * conversion, use \ref le32_to_cpu instead.
  252. *
  253. * \ingroup Group_EndianConversion
  254. *
  255. * \param[in] x Data to perform the endianness conversion on.
  256. *
  257. * \return Endian corrected version of the input value.
  258. */
  259. #define LE32_TO_CPU(x) (x)
  260. /** Performs a conversion between a Big Endian encoded 16-bit piece of data and the
  261. * Endianness of the currently selected CPU architecture.
  262. *
  263. * On big endian architectures, this macro does nothing.
  264. *
  265. * \note This macro is designed for compile-time conversion of data - for run-time endianness
  266. * conversion, use \ref be16_to_cpu instead.
  267. *
  268. * \ingroup Group_EndianConversion
  269. *
  270. * \param[in] x Data to perform the endianness conversion on.
  271. *
  272. * \return Endian corrected version of the input value.
  273. */
  274. #define BE16_TO_CPU(x) SWAPENDIAN_16(x)
  275. /** Performs a conversion between a Big Endian encoded 32-bit piece of data and the
  276. * Endianness of the currently selected CPU architecture.
  277. *
  278. * On big endian architectures, this macro does nothing.
  279. *
  280. * \note This macro is designed for compile-time conversion of data - for run-time endianness
  281. * conversion, use \ref be32_to_cpu instead.
  282. *
  283. * \ingroup Group_EndianConversion
  284. *
  285. * \param[in] x Data to perform the endianness conversion on.
  286. *
  287. * \return Endian corrected version of the input value.
  288. */
  289. #define BE32_TO_CPU(x) SWAPENDIAN_32(x)
  290. /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
  291. * is in Little Endian format regardless of the currently selected CPU architecture.
  292. *
  293. * On little endian architectures, this macro does nothing.
  294. *
  295. * \note This macro is designed for compile-time conversion of data - for run-time endianness
  296. * conversion, use \ref cpu_to_le16 instead.
  297. *
  298. * \ingroup Group_EndianConversion
  299. *
  300. * \param[in] x Data to perform the endianness conversion on.
  301. *
  302. * \return Endian corrected version of the input value.
  303. */
  304. #define CPU_TO_LE16(x) (x)
  305. /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
  306. * is in Little Endian format regardless of the currently selected CPU architecture.
  307. *
  308. * On little endian architectures, this macro does nothing.
  309. *
  310. * \note This macro is designed for compile-time conversion of data - for run-time endianness
  311. * conversion, use \ref cpu_to_le32 instead.
  312. *
  313. * \ingroup Group_EndianConversion
  314. *
  315. * \param[in] x Data to perform the endianness conversion on.
  316. *
  317. * \return Endian corrected version of the input value.
  318. */
  319. #define CPU_TO_LE32(x) (x)
  320. /** Performs a conversion on a natively encoded 16-bit piece of data to ensure that it
  321. * is in Big Endian format regardless of the currently selected CPU architecture.
  322. *
  323. * On big endian architectures, this macro does nothing.
  324. *
  325. * \note This macro is designed for compile-time conversion of data - for run-time endianness
  326. * conversion, use \ref cpu_to_be16 instead.
  327. *
  328. * \ingroup Group_EndianConversion
  329. *
  330. * \param[in] x Data to perform the endianness conversion on.
  331. *
  332. * \return Endian corrected version of the input value.
  333. */
  334. #define CPU_TO_BE16(x) SWAPENDIAN_16(x)
  335. /** Performs a conversion on a natively encoded 32-bit piece of data to ensure that it
  336. * is in Big Endian format regardless of the currently selected CPU architecture.
  337. *
  338. * On big endian architectures, this macro does nothing.
  339. *
  340. * \note This macro is designed for compile-time conversion of data - for run-time endianness
  341. * conversion, use \ref cpu_to_be32 instead.
  342. *
  343. * \ingroup Group_EndianConversion
  344. *
  345. * \param[in] x Data to perform the endianness conversion on.
  346. *
  347. * \return Endian corrected version of the input value.
  348. */
  349. #define CPU_TO_BE32(x) SWAPENDIAN_32(x)
  350. //! @}
  351. #endif
  352. /* Inline Functions: */
  353. /** Function to reverse the byte ordering of the individual bytes in a 16 bit value.
  354. *
  355. * \ingroup Group_ByteSwapping
  356. *
  357. * \param[in] Word Word of data whose bytes are to be swapped.
  358. *
  359. * \return Input data with the individual bytes reversed.
  360. */
  361. static inline uint16_t SwapEndian_16(const uint16_t Word) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
  362. static inline uint16_t SwapEndian_16(const uint16_t Word)
  363. {
  364. if (GCC_IS_COMPILE_CONST(Word))
  365. return SWAPENDIAN_16(Word);
  366. uint8_t Temp;
  367. union
  368. {
  369. uint16_t Word;
  370. uint8_t Bytes[2];
  371. } Data;
  372. Data.Word = Word;
  373. Temp = Data.Bytes[0];
  374. Data.Bytes[0] = Data.Bytes[1];
  375. Data.Bytes[1] = Temp;
  376. return Data.Word;
  377. }
  378. /** Function to reverse the byte ordering of the individual bytes in a 32 bit value.
  379. *
  380. * \ingroup Group_ByteSwapping
  381. *
  382. * \param[in] DWord Double word of data whose bytes are to be swapped.
  383. *
  384. * \return Input data with the individual bytes reversed.
  385. */
  386. static inline uint32_t SwapEndian_32(const uint32_t DWord) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
  387. static inline uint32_t SwapEndian_32(const uint32_t DWord)
  388. {
  389. if (GCC_IS_COMPILE_CONST(DWord))
  390. return SWAPENDIAN_32(DWord);
  391. uint8_t Temp;
  392. union
  393. {
  394. uint32_t DWord;
  395. uint8_t Bytes[4];
  396. } Data;
  397. Data.DWord = DWord;
  398. Temp = Data.Bytes[0];
  399. Data.Bytes[0] = Data.Bytes[3];
  400. Data.Bytes[3] = Temp;
  401. Temp = Data.Bytes[1];
  402. Data.Bytes[1] = Data.Bytes[2];
  403. Data.Bytes[2] = Temp;
  404. return Data.DWord;
  405. }
  406. /** Function to reverse the byte ordering of the individual bytes in a n byte value.
  407. *
  408. * \ingroup Group_ByteSwapping
  409. *
  410. * \param[in,out] Data Pointer to a number containing an even number of bytes to be reversed.
  411. * \param[in] Length Length of the data in bytes.
  412. *
  413. * \return Input data with the individual bytes reversed.
  414. */
  415. static inline void SwapEndian_n(void* const Data,
  416. uint8_t Length) ATTR_NON_NULL_PTR_ARG(1);
  417. static inline void SwapEndian_n(void* const Data,
  418. uint8_t Length)
  419. {
  420. uint8_t* CurrDataPos = (uint8_t*)Data;
  421. while (Length > 1)
  422. {
  423. uint8_t Temp = *CurrDataPos;
  424. *CurrDataPos = *(CurrDataPos + Length - 1);
  425. *(CurrDataPos + Length - 1) = Temp;
  426. CurrDataPos++;
  427. Length -= 2;
  428. }
  429. }
  430. /* Disable C linkage for C++ Compilers: */
  431. #if defined(__cplusplus)
  432. }
  433. #endif
  434. #endif
  435. /** @} */