1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2020-2021 NXP                                                  */
3 /*                                                                          */
4 /* NXP Confidential. This software is owned or controlled by NXP and may    */
5 /* only be used strictly in accordance with the applicable license terms.   */
6 /* By expressly accepting such terms or by downloading, installing,         */
7 /* activating and/or otherwise using the software, you are agreeing that    */
8 /* you have read, and that you agree to comply with and are bound by, such  */
9 /* license terms. If you do not agree to be bound by the applicable license */
10 /* terms, then you may not retain, install, activate or otherwise use the   */
11 /* software.                                                                */
12 /*--------------------------------------------------------------------------*/
13 
14 /** @file  mcuxClMemory_Endianness.h
15  *  @brief Memory header for endianness support functions.
16  * This header exposes macros that enable using endianness support functions.
17  */
18 
19 /**
20  * @defgroup mcuxClMemory_Endianness mcuxClMemory_Endianness
21  * @brief These macros implement endianess management on integers
22  * @ingroup mcuxClMemory
23  * @{
24  */
25 
26 #ifndef MCUXCLMEMORY_ENDIANNESS_H_
27 #define MCUXCLMEMORY_ENDIANNESS_H_
28 
29 #include <mcuxClMemory_Types.h>
30 
31 
32 /**********************************************
33  * MACROS
34  **********************************************/
35 
36 /**
37  * @brief Converts a 32-bit unsigned integer to a little-endian order @c uint8_t array .
38  *
39  * @note Implementation is platform independent.
40  *
41  * @param[out]      destination     pointer to a 4 byte buffer were 32-bit integer in little-endian will be encoded.
42  * @param[in]       value           pointer to the 32-bit integer to be encoded.
43  *
44  */
45 #define mcuxClMemory_StoreLittleEndian32( destination, value )               \
46         {                                                                   \
47             (destination)[0] = (uint8_t) (((value) & 0x000000FFU) >>  0u);  \
48             (destination)[1] = (uint8_t) (((value) & 0x0000FF00U) >>  8u);  \
49             (destination)[2] = (uint8_t) (((value) & 0x00FF0000U) >> 16u);  \
50             (destination)[3] = (uint8_t) (((value) & 0xFF000000U) >> 24u);  \
51         }
52 
53 /**
54  * @brief Converts a 32-bit unsigned integer to a big-endian order @c uint8_t array.
55  *
56  * @note Implementation is platform independent.
57  *
58  * @param[in]       source          pointer to a 4 byte big-endian order @c uint8_t buffer that will be converted to an unsigned integer
59  *
60  */
61 #define mcuxClMemory_StoreBigEndian32( destination, value )                  \
62         {                                                                   \
63             (destination)[0] = (uint8_t) (((value) & 0xFF000000U) >> 24u);  \
64             (destination)[1] = (uint8_t) (((value) & 0x00FF0000U) >> 16u);  \
65             (destination)[2] = (uint8_t) (((value) & 0x0000FF00U) >>  8u);  \
66             (destination)[3] = (uint8_t) (((value) & 0x000000FFU) >>  0u);  \
67         }
68 
69 /**
70  * @brief Converts a little-endian order @c uint8_t array to a 32-bit unsigned integer.
71  *
72  * @note Implementation is platform independent.
73  *
74  * @param[in]       source          pointer to a 4 byte little-endian order @c uint8_t buffer that will be converted to an unsigned integer
75  *
76  */
77 #define mcuxClMemory_LoadLittleEndian32( source )                            \
78     ( (((uint32_t) (source)[0]) <<  0u) |                                   \
79       (((uint32_t) (source)[1]) <<  8u) |                                   \
80       (((uint32_t) (source)[2]) << 16u) |                                   \
81       (((uint32_t) (source)[3]) << 24u) )
82 
83 
84 /**
85  * @brief Converts a big-endian order @c uint8_t array to a 32-bit unsigned integer.
86  *
87  * @param[in]       destination     pointer to a 4 byte buffer were 32-bit integer in big-endian will be decoded.
88  *
89  * @return a 32-bit unsigned integer
90  */
91 #define mcuxClMemory_LoadBigEndian32( source )                               \
92     ( (((uint32_t) (source)[0]) << 24u) |                                   \
93       (((uint32_t) (source)[1]) << 16u) |                                   \
94       (((uint32_t) (source)[2]) <<  8u) |                                   \
95       (((uint32_t) (source)[3]) <<  0u) )
96 
97 /**
98  * @brief MACRO that switches byte endianness of given CPU word.
99  *
100  * @param[in]       input           a 32-bit unsigned integer whose endianness will be reversed.
101  *
102  */
103 
104 #define MCUXCLMEMORY_SWITCH_4BYTE_ENDIANNESS(input)  __REV(input)
105 
106 #endif /* MCUXCLMEMORY_ENDIANNESS_H_ */
107 /**
108  * @}
109  */
110