Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | #ifndef BCMOS_ENDIAN_H_ |
| 31 | #define BCMOS_ENDIAN_H_ |
| 32 | |
| 33 | #ifndef BCMOS_SYSTEM_H_ |
| 34 | #error Please do not include bcmos_endian.h directly. Include bcmos_system.h |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | |
| 39 | /** @} system_event */ |
| 40 | |
| 41 | /** \addtogroup system_endian |
| 42 | * @{ |
| 43 | */ |
| 44 | #ifndef BCMOS_ARCH_ENDIAN_SWAP |
| 45 | |
| 46 | /** Swaps the endianness of a 16-bit integer |
| 47 | * \param[in] n the original number |
| 48 | * \return number with swapped endianness |
| 49 | */ |
| 50 | static inline uint16_t bcmos_endian_swap_u16(uint16_t n) |
| 51 | { |
| 52 | return ((n << 8 ) & 0xFF00U)|((n >> 8) & 0x00FFU); |
| 53 | } |
| 54 | |
| 55 | /** Swaps the endianness of a 24-bit integer |
| 56 | * \param[in] n the original number |
| 57 | * \return number with swapped endianness |
| 58 | */ |
| 59 | static inline uint24_t bcmos_endian_swap_u24(uint24_t n) |
| 60 | { |
| 61 | return (uint24_t){ .u8 = { n.u8[2], n.u8[1], n.u8[0] } }; |
| 62 | } |
| 63 | |
| 64 | /** Swaps the endianness of a 32-bit integer |
| 65 | * \param[in] n the original number |
| 66 | * \return number with swapped endianness |
| 67 | */ |
| 68 | static inline uint32_t bcmos_endian_swap_u32(uint32_t n) |
| 69 | { |
| 70 | return ((n << 24) & 0xFF000000U)|((n << 8 ) & 0x00FF0000U)|((n >> 8) & 0x0000FF00U)|((n >> 24) & 0x000000FFU); |
| 71 | } |
| 72 | |
| 73 | /** Swaps the endianness of a 64-bit integer |
| 74 | * \param[in] n the original number |
| 75 | * \return number with swapped endianness |
| 76 | */ |
| 77 | static inline uint64_t bcmos_endian_swap_u64(uint64_t n) |
| 78 | { |
| 79 | return (((uint64_t)bcmos_endian_swap_u32(n & 0xFFFFFFFFU) << 32) | bcmos_endian_swap_u32((n >> 32) & 0xFFFFFFFFU)); |
| 80 | } |
| 81 | |
| 82 | #endif /* BCMOS_ARCH_ENDIAN_SWAP */ |
| 83 | |
| 84 | #if (BCM_CPU_ENDIAN == BCMOS_ENDIAN_BIG) |
| 85 | |
| 86 | #define BCMOS_ENDIAN_CPU_TO_BIG_U16(n) (n) |
| 87 | #define BCMOS_ENDIAN_CPU_TO_BIG_U24(n) (n) |
| 88 | #define BCMOS_ENDIAN_CPU_TO_BIG_U32(n) (n) |
| 89 | #define BCMOS_ENDIAN_CPU_TO_BIG_U64(n) (n) |
| 90 | #define BCMOS_ENDIAN_BIG_TO_CPU_U16(n) (n) |
| 91 | #define BCMOS_ENDIAN_BIG_TO_CPU_U24(n) (n) |
| 92 | #define BCMOS_ENDIAN_BIG_TO_CPU_U32(n) (n) |
| 93 | #define BCMOS_ENDIAN_BIG_TO_CPU_U64(n) (n) |
| 94 | |
| 95 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U16(n) (bcmos_endian_swap_u16(n)) |
| 96 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U24(n) (bcmos_endian_swap_u24(n)) |
| 97 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U32(n) (bcmos_endian_swap_u32(n)) |
| 98 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U64(n) (bcmos_endian_swap_u64(n)) |
| 99 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U16(n) (bcmos_endian_swap_u16(n)) |
| 100 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U24(n) (bcmos_endian_swap_u24(n)) |
| 101 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U32(n) (bcmos_endian_swap_u32(n)) |
| 102 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U64(n) (bcmos_endian_swap_u64(n)) |
| 103 | |
| 104 | #elif (BCM_CPU_ENDIAN == BCMOS_ENDIAN_LITTLE) |
| 105 | |
| 106 | #define BCMOS_ENDIAN_CPU_TO_BIG_U16(n) (bcmos_endian_swap_u16(n)) |
| 107 | #define BCMOS_ENDIAN_CPU_TO_BIG_U24(n) (bcmos_endian_swap_u24(n)) |
| 108 | #define BCMOS_ENDIAN_CPU_TO_BIG_U32(n) (bcmos_endian_swap_u32(n)) |
| 109 | #define BCMOS_ENDIAN_CPU_TO_BIG_U64(n) (bcmos_endian_swap_u64(n)) |
| 110 | #define BCMOS_ENDIAN_BIG_TO_CPU_U16(n) (bcmos_endian_swap_u16(n)) |
| 111 | #define BCMOS_ENDIAN_BIG_TO_CPU_U24(n) (bcmos_endian_swap_u24(n)) |
| 112 | #define BCMOS_ENDIAN_BIG_TO_CPU_U32(n) (bcmos_endian_swap_u32(n)) |
| 113 | #define BCMOS_ENDIAN_BIG_TO_CPU_U64(n) (bcmos_endian_swap_u64(n)) |
| 114 | |
| 115 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U16(n) (n) |
| 116 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U24(n) (n) |
| 117 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U32(n) (n) |
| 118 | #define BCMOS_ENDIAN_CPU_TO_LITTLE_U64(n) (n) |
| 119 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U16(n) (n) |
| 120 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U24(n) (n) |
| 121 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U32(n) (n) |
| 122 | #define BCMOS_ENDIAN_LITTLE_TO_CPU_U64(n) (n) |
| 123 | |
| 124 | #else |
| 125 | #error BCM_CPU_ENDIAN must be BCMOS_ENDIAN_BIG or _LITTLE |
| 126 | #endif |
| 127 | |
| 128 | /** @} system_endian */ |
| 129 | |
| 130 | #endif /* BCMOS_COMMON2_H_ */ |