1 /***********************license start*********************************** 2 * Copyright (C) 2021-2026 Marvell. 3 * SPDX-License-Identifier: BSD-3-Clause 4 * https://spdx.org/licenses 5 ***********************license end**************************************/ 6 7 /** 8 * @file 9 * 10 * Utility functions for endian swapping 11 * 12 * <hr>$Revision: 32636 $<hr> 13 * 14 * @addtogroup hal 15 * @{ 16 */ 17 18 /** 19 * Byte swap a 16 bit number 20 * 21 * @param x 16 bit number 22 * @return Byte swapped result 23 */ ody_swap16(uint16_t x)24static inline uint16_t ody_swap16(uint16_t x) 25 { 26 return ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | 27 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8))); 28 } 29 30 31 /** 32 * Byte swap a 32 bit number 33 * 34 * @param x 32 bit number 35 * @return Byte swapped result 36 */ ody_swap32(uint32_t x)37static inline uint32_t ody_swap32(uint32_t x) 38 { 39 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) 40 return __builtin_bswap32(x); 41 #else 42 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF); 43 x = (x>>16) | (x<<16); 44 return x; 45 #endif 46 } 47 48 49 /** 50 * Byte swap a 64 bit number 51 * 52 * @param x 64 bit number 53 * @return Byte swapped result 54 */ ody_swap64(uint64_t x)55static inline uint64_t ody_swap64(uint64_t x) 56 { 57 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) 58 return __builtin_bswap64(x); 59 #else 60 x = ((x << 8)&0xFF00FF00FF00FF00ULL) | ((x >> 8)&0x00FF00FF00FF00FFULL); 61 x = ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL); 62 return (x>>32) | (x<<32); 63 #endif 64 } 65 66 67 #if __BYTE_ORDER == __BIG_ENDIAN 68 69 #define ody_cpu_to_le16(x) ody_swap16(x) 70 #define ody_cpu_to_le32(x) ody_swap32(x) 71 #define ody_cpu_to_le64(x) ody_swap64(x) 72 73 #define ody_cpu_to_be16(x) (x) 74 #define ody_cpu_to_be32(x) (x) 75 #define ody_cpu_to_be64(x) (x) 76 77 #else 78 79 #define ody_cpu_to_le16(x) (x) 80 #define ody_cpu_to_le32(x) (x) 81 #define ody_cpu_to_le64(x) (x) 82 83 #define ody_cpu_to_be16(x) ody_swap16(x) 84 #define ody_cpu_to_be32(x) ody_swap32(x) 85 #define ody_cpu_to_be64(x) ody_swap64(x) 86 87 #endif 88 89 #define ody_le16_to_cpu(x) ody_cpu_to_le16(x) 90 #define ody_le32_to_cpu(x) ody_cpu_to_le32(x) 91 #define ody_le64_to_cpu(x) ody_cpu_to_le64(x) 92 93 #define ody_be16_to_cpu(x) ody_cpu_to_be16(x) 94 #define ody_be32_to_cpu(x) ody_cpu_to_be32(x) 95 #define ody_be64_to_cpu(x) ody_cpu_to_be64(x) 96 97 /** @} */ 98