lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #ifndef USBHOST_SWAB_H |
| 2 | #define USBHOST_SWAB_H |
| 3 | |
| 4 | //#include <endian.h> |
| 5 | #ifndef _OS_LINUX |
| 6 | #define swab16(x) \ |
| 7 | ((uint16_t)( \ |
| 8 | (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ |
| 9 | (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) )) |
| 10 | #define swab32(x) \ |
| 11 | ((uint32_t)( \ |
| 12 | (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ |
| 13 | (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ |
| 14 | (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
| 15 | (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) )) |
| 16 | |
| 17 | #define swab64(x) \ |
| 18 | ((uint64_t)( \ |
| 19 | (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
| 20 | (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
| 21 | (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
| 22 | (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
| 23 | (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
| 24 | (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
| 25 | (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
| 26 | (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) )) |
| 27 | |
| 28 | |
| 29 | #if 0//__BYTE_ORDER == __BIG_ENDIAN |
| 30 | #define cpu_to_le16(x) ({ uint16_t _x = x; swab16(_x); }) |
| 31 | #define cpu_to_le32(x) ({ uint32_t _x = x; swab32(_x); }) |
| 32 | #define cpu_to_le64(x) ({ uint64_t _x = x; swab64(_x); }) |
| 33 | #define cpu_to_be16(x) (x) |
| 34 | #define cpu_to_be32(x) (x) |
| 35 | #define cpu_to_be64(x) (x) |
| 36 | #else |
| 37 | #define cpu_to_le16(x) (x) |
| 38 | #define cpu_to_le32(x) (x) |
| 39 | #define cpu_to_le64(x) (x) |
| 40 | #define cpu_to_be16(x) ({ uint16_t _x = x; swab16(_x); }) |
| 41 | #define cpu_to_be32(x) ({ uint32_t _x = x; swab32(_x); }) |
| 42 | #define cpu_to_be64(x) ({ uint64_t _x = x; swab64(_x); }) |
| 43 | #endif |
| 44 | #define le16_to_cpu(x) cpu_to_le16(x) |
| 45 | #define be16_to_cpu(x) cpu_to_be16(x) |
| 46 | #define le32_to_cpu(x) cpu_to_le32(x) |
| 47 | #define be32_to_cpu(x) cpu_to_be32(x) |
| 48 | #define le64_to_cpu(x) cpu_to_le64(x) |
| 49 | #define be64_to_cpu(x) cpu_to_be64(x) |
| 50 | |
| 51 | #endif |
| 52 | |
| 53 | #endif |