1 /*             ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
2  *
3  *                 Copyright (c) 2014-2015 Datalight, Inc.
4  *                     All Rights Reserved Worldwide.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; use version 2 of the License.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
12  *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*  Businesses and individuals that for commercial or other reasons cannot
21  *  comply with the terms of the GPLv2 license may obtain a commercial license
22  *  before incorporating Reliance Edge into proprietary software for
23  *  distribution in any form.  Visit http://www.datalight.com/reliance-edge for
24  *  more information.
25  */
26 
27 /** @file
28  *  @brief Implements utilities for performing endian swaps.
29  */
30 #include <redfs.h>
31 
32 
33 #ifdef REDCONF_ENDIAN_SWAP
34 
35 /** @brief Reverse the byte order of a 64-bit number.
36  *
37  *  @param ullToRev Number whose bytes will be reversed
38  *
39  *  @retval @p ullToRev with its bytes reversed.
40  */
RedRev64(uint64_t ullToRev)41     uint64_t RedRev64( uint64_t ullToRev )
42     {
43         uint64_t ullRet = ullToRev;
44 
45         ullRet = ( ( ullRet & UINT64_SUFFIX( 0x00000000FFFFFFFF ) ) << 32U ) | ( ( ullRet & UINT64_SUFFIX( 0xFFFFFFFF00000000 ) ) >> 32U );
46         ullRet = ( ( ullRet & UINT64_SUFFIX( 0x0000FFFF0000FFFF ) ) << 16U ) | ( ( ullRet & UINT64_SUFFIX( 0xFFFF0000FFFF0000 ) ) >> 16U );
47         ullRet = ( ( ullRet & UINT64_SUFFIX( 0x00FF00FF00FF00FF ) ) << 8U ) | ( ( ullRet & UINT64_SUFFIX( 0xFF00FF00FF00FF00 ) ) >> 8U );
48 
49         return ullRet;
50     }
51 
52 
53 /** @brief Reverse the byte order of a 32-bit number.
54  *
55  *  @param ulToRev  Number whose bytes will be reversed
56  *
57  *  @retval @p ulToRev with its bytes reversed.
58  */
RedRev32(uint32_t ulToRev)59     uint32_t RedRev32( uint32_t ulToRev )
60     {
61         return ( ( ulToRev & 0x000000FFU ) << 24U )
62                | ( ( ulToRev & 0x0000FF00U ) << 8U )
63                | ( ( ulToRev & 0x00FF0000U ) >> 8U )
64                | ( ( ulToRev & 0xFF000000U ) >> 24U );
65     }
66 
67 
68 /** @brief Reverse the byte order of a 16-bit number.
69  *
70  *  @param uToRev   Number whose bytes will be reversed
71  *
72  *  @retval @p uToRev with its bytes reversed.
73  */
RedRev16(uint16_t uToRev)74     uint16_t RedRev16( uint16_t uToRev )
75     {
76         return ( ( uToRev & 0xFF00U ) >> 8U )
77                | ( ( uToRev & 0x00FFU ) << 8U );
78     }
79 
80 #endif /* ifdef REDCONF_ENDIAN_SWAP */
81