xref: /freebsd/sys/contrib/ncsw/inc/endian_ext.h (revision acc1a9ef8333c798c210fa94be6af4d5fe2dd794)
1 /* Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of Freescale Semiconductor nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  *
16  * ALTERNATIVELY, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") as published by the Free Software
18  * Foundation, either version 2 of that License or (at your option) any
19  * later version.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /**************************************************************************//**
34 
35  @File          endian_ext.h
36 
37  @Description   Big/little endian swapping routines.
38 *//***************************************************************************/
39 
40 #ifndef __ENDIAN_EXT_H
41 #define __ENDIAN_EXT_H
42 
43 #include "std_ext.h"
44 
45 
46 /**************************************************************************//**
47  @Group         gen_id  General Drivers Utilities
48 
49  @Description   General usage API. This API is intended for usage by both the
50                 internal modules and the user's application.
51 
52  @{
53 *//***************************************************************************/
54 
55 /**************************************************************************//**
56  @Group         endian_id Big/Little-Endian Conversion
57 
58  @Description   Routines and macros for Big/Little-Endian conversion and
59                 general byte swapping.
60 
61                 All routines and macros are expecting unsigned values as
62                 parameters, but will generate the correct result also for
63                 signed values. Therefore, signed/unsigned casting is allowed.
64  @{
65 *//***************************************************************************/
66 
67 /**************************************************************************//**
68  @Collection    Byte-Swap Macros
69 
70                 Macros for swapping byte order.
71 
72  @Cautions      The parameters of these macros are evaluated multiple times.
73                 For calculated expressions or expressions that contain function
74                 calls it is recommended to use the byte-swap routines.
75 
76  @{
77 *//***************************************************************************/
78 
79 /**************************************************************************//**
80  @Description   Swaps the byte order of a given 16-bit value.
81 
82  @Param[in]     val - The 16-bit value to swap.
83 
84  @Return        The byte-swapped value..
85 
86  @Cautions      The given value is evaluated multiple times by this macro.
87                 For calculated expressions or expressions that contain function
88                 calls it is recommended to use the SwapUint16() routine.
89 
90  @hideinitializer
91 *//***************************************************************************/
92 #define SWAP_UINT16(val) \
93     ((uint16_t)((((val) & 0x00FF) << 8) | (((val) & 0xFF00) >> 8)))
94 
95 /**************************************************************************//**
96  @Description   Swaps the byte order of a given 32-bit value.
97 
98  @Param[in]     val - The 32-bit value to swap.
99 
100  @Return        The byte-swapped value..
101 
102  @Cautions      The given value is evaluated multiple times by this macro.
103                 For calculated expressions or expressions that contain function
104                 calls it is recommended to use the SwapUint32() routine.
105 
106  @hideinitializer
107 *//***************************************************************************/
108 #define SWAP_UINT32(val) \
109     ((uint32_t)((((val) & 0x000000FF) << 24) | \
110                 (((val) & 0x0000FF00) <<  8) | \
111                 (((val) & 0x00FF0000) >>  8) | \
112                 (((val) & 0xFF000000) >> 24)))
113 
114 /**************************************************************************//**
115  @Description   Swaps the byte order of a given 64-bit value.
116 
117  @Param[in]     val - The 64-bit value to swap.
118 
119  @Return        The byte-swapped value..
120 
121  @Cautions      The given value is evaluated multiple times by this macro.
122                 For calculated expressions or expressions that contain function
123                 calls it is recommended to use the SwapUint64() routine.
124 
125  @hideinitializer
126 *//***************************************************************************/
127 #define SWAP_UINT64(val) \
128     ((uint64_t)((((val) & 0x00000000000000FFULL) << 56) | \
129                 (((val) & 0x000000000000FF00ULL) << 40) | \
130                 (((val) & 0x0000000000FF0000ULL) << 24) | \
131                 (((val) & 0x00000000FF000000ULL) <<  8) | \
132                 (((val) & 0x000000FF00000000ULL) >>  8) | \
133                 (((val) & 0x0000FF0000000000ULL) >> 24) | \
134                 (((val) & 0x00FF000000000000ULL) >> 40) | \
135                 (((val) & 0xFF00000000000000ULL) >> 56)))
136 
137 /* @} */
138 
139 /**************************************************************************//**
140  @Collection    Byte-Swap Routines
141 
142                 Routines for swapping the byte order of a given parameter and
143                 returning the swapped value.
144 
145                 These inline routines are safer than the byte-swap macros,
146                 because they evaluate the parameter expression only once.
147  @{
148 *//***************************************************************************/
149 
150 /**************************************************************************//**
151  @Function      SwapUint16
152 
153  @Description   Returns the byte-swapped value of a given 16-bit value.
154 
155  @Param[in]     val - The 16-bit value.
156 
157  @Return        The byte-swapped value of the parameter.
158 *//***************************************************************************/
159 static __inline__ uint16_t SwapUint16(uint16_t val)
160 {
161     return (uint16_t)(((val & 0x00FF) << 8) |
162                       ((val & 0xFF00) >> 8));
163 }
164 
165 /**************************************************************************//**
166  @Function      SwapUint32
167 
168  @Description   Returns the byte-swapped value of a given 32-bit value.
169 
170  @Param[in]     val - The 32-bit value.
171 
172  @Return        The byte-swapped value of the parameter.
173 *//***************************************************************************/
174 static __inline__ uint32_t SwapUint32(uint32_t val)
175 {
176     return (uint32_t)(((val & 0x000000FF) << 24) |
177                       ((val & 0x0000FF00) <<  8) |
178                       ((val & 0x00FF0000) >>  8) |
179                       ((val & 0xFF000000) >> 24));
180 }
181 
182 /**************************************************************************//**
183  @Function      SwapUint64
184 
185  @Description   Returns the byte-swapped value of a given 64-bit value.
186 
187  @Param[in]     val - The 64-bit value.
188 
189  @Return        The byte-swapped value of the parameter.
190 *//***************************************************************************/
191 static __inline__ uint64_t SwapUint64(uint64_t val)
192 {
193     return (uint64_t)(((val & 0x00000000000000FFULL) << 56) |
194                       ((val & 0x000000000000FF00ULL) << 40) |
195                       ((val & 0x0000000000FF0000ULL) << 24) |
196                       ((val & 0x00000000FF000000ULL) <<  8) |
197                       ((val & 0x000000FF00000000ULL) >>  8) |
198                       ((val & 0x0000FF0000000000ULL) >> 24) |
199                       ((val & 0x00FF000000000000ULL) >> 40) |
200                       ((val & 0xFF00000000000000ULL) >> 56));
201 }
202 
203 /* @} */
204 
205 /**************************************************************************//**
206  @Collection    In-place Byte-Swap-And-Set Routines
207 
208                 Routines for swapping the byte order of a given variable and
209                 setting the swapped value back to the same variable.
210  @{
211 *//***************************************************************************/
212 
213 /**************************************************************************//**
214  @Function      SwapUint16P
215 
216  @Description   Swaps the byte order of a given 16-bit variable.
217 
218  @Param[in]     p_Val - Pointer to the 16-bit variable.
219 
220  @Return        None.
221 *//***************************************************************************/
222 static __inline__ void SwapUint16P(uint16_t *p_Val)
223 {
224     *p_Val = SwapUint16(*p_Val);
225 }
226 
227 /**************************************************************************//**
228  @Function      SwapUint32P
229 
230  @Description   Swaps the byte order of a given 32-bit variable.
231 
232  @Param[in]     p_Val - Pointer to the 32-bit variable.
233 
234  @Return        None.
235 *//***************************************************************************/
236 static __inline__ void SwapUint32P(uint32_t *p_Val)
237 {
238     *p_Val = SwapUint32(*p_Val);
239 }
240 
241 /**************************************************************************//**
242  @Function      SwapUint64P
243 
244  @Description   Swaps the byte order of a given 64-bit variable.
245 
246  @Param[in]     p_Val - Pointer to the 64-bit variable.
247 
248  @Return        None.
249 *//***************************************************************************/
250 static __inline__ void SwapUint64P(uint64_t *p_Val)
251 {
252     *p_Val = SwapUint64(*p_Val);
253 }
254 
255 /* @} */
256 
257 
258 /**************************************************************************//**
259  @Collection    Little-Endian Conversion Macros
260 
261                 These macros convert given parameters to or from Little-Endian
262                 format. Use these macros when you want to read or write a specific
263                 Little-Endian value in memory, without a-priori knowing the CPU
264                 byte order.
265 
266                 These macros use the byte-swap routines. For conversion of
267                 constants in initialization structures, you may use the CONST
268                 versions of these macros (see below), which are using the
269                 byte-swap macros instead.
270  @{
271 *//***************************************************************************/
272 
273 /**************************************************************************//**
274  @Description   Converts a given 16-bit value from CPU byte order to
275                 Little-Endian byte order.
276 
277  @Param[in]     val - The 16-bit value to convert.
278 
279  @Return        The converted value.
280 
281  @hideinitializer
282 *//***************************************************************************/
283 #define CPU_TO_LE16(val)        SwapUint16(val)
284 
285 /**************************************************************************//**
286  @Description   Converts a given 32-bit value from CPU byte order to
287                 Little-Endian byte order.
288 
289  @Param[in]     val - The 32-bit value to convert.
290 
291  @Return        The converted value.
292 
293  @hideinitializer
294 *//***************************************************************************/
295 #define CPU_TO_LE32(val)        SwapUint32(val)
296 
297 /**************************************************************************//**
298  @Description   Converts a given 64-bit value from CPU byte order to
299                 Little-Endian byte order.
300 
301  @Param[in]     val - The 64-bit value to convert.
302 
303  @Return        The converted value.
304 
305  @hideinitializer
306 *//***************************************************************************/
307 #define CPU_TO_LE64(val)        SwapUint64(val)
308 
309 
310 /**************************************************************************//**
311  @Description   Converts a given 16-bit value from Little-Endian byte order to
312                 CPU byte order.
313 
314  @Param[in]     val - The 16-bit value to convert.
315 
316  @Return        The converted value.
317 
318  @hideinitializer
319 *//***************************************************************************/
320 #define LE16_TO_CPU(val)        CPU_TO_LE16(val)
321 
322 /**************************************************************************//**
323  @Description   Converts a given 32-bit value from Little-Endian byte order to
324                 CPU byte order.
325 
326  @Param[in]     val - The 32-bit value to convert.
327 
328  @Return        The converted value.
329 
330  @hideinitializer
331 *//***************************************************************************/
332 #define LE32_TO_CPU(val)        CPU_TO_LE32(val)
333 
334 /**************************************************************************//**
335  @Description   Converts a given 64-bit value from Little-Endian byte order to
336                 CPU byte order.
337 
338  @Param[in]     val - The 64-bit value to convert.
339 
340  @Return        The converted value.
341 
342  @hideinitializer
343 *//***************************************************************************/
344 #define LE64_TO_CPU(val)        CPU_TO_LE64(val)
345 
346 /* @} */
347 
348 /**************************************************************************//**
349  @Collection    Little-Endian Constant Conversion Macros
350 
351                 These macros convert given constants to or from Little-Endian
352                 format. Use these macros when you want to read or write a specific
353                 Little-Endian constant in memory, without a-priori knowing the
354                 CPU byte order.
355 
356                 These macros use the byte-swap macros, therefore can be used for
357                 conversion of constants in initialization structures.
358 
359  @Cautions      The parameters of these macros are evaluated multiple times.
360                 For non-constant expressions, use the non-CONST macro versions.
361 
362  @{
363 *//***************************************************************************/
364 
365 /**************************************************************************//**
366  @Description   Converts a given 16-bit constant from CPU byte order to
367                 Little-Endian byte order.
368 
369  @Param[in]     val - The 16-bit value to convert.
370 
371  @Return        The converted value.
372 
373  @hideinitializer
374 *//***************************************************************************/
375 #define CONST_CPU_TO_LE16(val)  SWAP_UINT16(val)
376 
377 /**************************************************************************//**
378  @Description   Converts a given 32-bit constant from CPU byte order to
379                 Little-Endian byte order.
380 
381  @Param[in]     val - The 32-bit value to convert.
382 
383  @Return        The converted value.
384 
385  @hideinitializer
386 *//***************************************************************************/
387 #define CONST_CPU_TO_LE32(val)  SWAP_UINT32(val)
388 
389 /**************************************************************************//**
390  @Description   Converts a given 64-bit constant from CPU byte order to
391                 Little-Endian byte order.
392 
393  @Param[in]     val - The 64-bit value to convert.
394 
395  @Return        The converted value.
396 
397  @hideinitializer
398 *//***************************************************************************/
399 #define CONST_CPU_TO_LE64(val)  SWAP_UINT64(val)
400 
401 
402 /**************************************************************************//**
403  @Description   Converts a given 16-bit constant from Little-Endian byte order
404                 to CPU byte order.
405 
406  @Param[in]     val - The 16-bit value to convert.
407 
408  @Return        The converted value.
409 
410  @hideinitializer
411 *//***************************************************************************/
412 #define CONST_LE16_TO_CPU(val)  CONST_CPU_TO_LE16(val)
413 
414 /**************************************************************************//**
415  @Description   Converts a given 32-bit constant from Little-Endian byte order
416                 to CPU byte order.
417 
418  @Param[in]     val - The 32-bit value to convert.
419 
420  @Return        The converted value.
421 
422  @hideinitializer
423 *//***************************************************************************/
424 #define CONST_LE32_TO_CPU(val)  CONST_CPU_TO_LE32(val)
425 
426 /**************************************************************************//**
427  @Description   Converts a given 64-bit constant from Little-Endian byte order
428                 to CPU byte order.
429 
430  @Param[in]     val - The 64-bit value to convert.
431 
432  @Return        The converted value.
433 
434  @hideinitializer
435 *//***************************************************************************/
436 #define CONST_LE64_TO_CPU(val)  CONST_CPU_TO_LE64(val)
437 
438 /* @} */
439 
440 
441 /** @} */ /* end of endian_id group */
442 /** @} */ /* end of gen_id group */
443 
444 
445 #endif /* __ENDIAN_EXT_H */
446 
447