Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #ifndef _SWAB_H |
| 15 | #define _SWAB_H |
| 16 | |
| 17 | /* |
| 18 | * linux/byteorder/swab.h |
| 19 | * Byte-swapping, independently from CPU endianness |
| 20 | * swabXX[ps]?(foo) |
| 21 | * |
| 22 | * Francois-Rene Rideau <fare@tunes.org> 19971205 |
| 23 | * separated swab functions from cpu_to_XX, |
| 24 | * to clean up support for bizarre-endian architectures. |
| 25 | * |
| 26 | * See asm-i386/byteorder.h and such for examples of how to provide |
| 27 | * architecture-dependent optimized versions |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | /* casts are necessary for constants, because we never know how for sure |
| 32 | * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way. |
| 33 | */ |
| 34 | #define swab16(x) \ |
| 35 | ((unsigned short)( \ |
| 36 | (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \ |
| 37 | (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) )) |
| 38 | |
| 39 | #define swab32(x) \ |
| 40 | ((unsigned int)( \ |
| 41 | (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \ |
| 42 | (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \ |
| 43 | (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \ |
| 44 | (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) )) |
| 45 | |
| 46 | #define swab64(x) \ |
| 47 | ((uint64_t)( \ |
| 48 | (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
| 49 | (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
| 50 | (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
| 51 | (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
| 52 | (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
| 53 | (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
| 54 | (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
| 55 | (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) )) |
| 56 | |
| 57 | #endif /* _SWAB_H */ |