1 /****************************************************************************** 2 * 3 * Copyright(c) 2007 - 2017 Realtek Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 *****************************************************************************/ 15 #ifndef _LINUX_BYTEORDER_SWABB_H 16 #define _LINUX_BYTEORDER_SWABB_H 17 18 /* 19 * linux/byteorder/swabb.h 20 * SWAp Bytes Bizarrely 21 * swaHHXX[ps]?(foo) 22 * 23 * Support for obNUXIous pdp-endian and other bizarre architectures. 24 * Will Linux ever run on such ancient beasts? if not, this file 25 * will be but a programming pearl. Still, it's a reminder that we 26 * shouldn't be making too many assumptions when trying to be portable. 27 * 28 */ 29 30 /* 31 * Meaning of the names I chose (vaxlinux people feel free to correct them): 32 * swahw32 swap 16-bit half-words in a 32-bit word 33 * swahb32 swap 8-bit halves of each 16-bit half-word in a 32-bit word 34 * 35 * No 64-bit support yet. I don't know NUXI conventions for long longs. 36 * I guarantee it will be a mess when it's there, though :-> 37 * It will be even worse if there are conflicting 64-bit conventions. 38 * Hopefully, no one ever used 64-bit objects on NUXI machines. 39 * 40 */ 41 42 #define ___swahw32(x) \ 43 ({ \ 44 __u32 __x = (x); \ 45 ((__u32)(\ 46 (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \ 47 (((__u32)(__x) & (__u32)0xffff0000UL) >> 16))); \ 48 }) 49 #define ___swahb32(x) \ 50 ({ \ 51 __u32 __x = (x); \ 52 ((__u32)(\ 53 (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \ 54 (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8))); \ 55 }) 56 57 #define ___constant_swahw32(x) \ 58 ((__u32)(\ 59 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \ 60 (((__u32)(x) & (__u32)0xffff0000UL) >> 16))) 61 #define ___constant_swahb32(x) \ 62 ((__u32)(\ 63 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \ 64 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8))) 65 66 /* 67 * provide defaults when no architecture-specific optimization is detected 68 */ 69 #ifndef __arch__swahw32 70 #define __arch__swahw32(x) ___swahw32(x) 71 #endif 72 #ifndef __arch__swahb32 73 #define __arch__swahb32(x) ___swahb32(x) 74 #endif 75 76 #ifndef __arch__swahw32p 77 #define __arch__swahw32p(x) __swahw32(*(x)) 78 #endif 79 #ifndef __arch__swahb32p 80 #define __arch__swahb32p(x) __swahb32(*(x)) 81 #endif 82 83 #ifndef __arch__swahw32s 84 #define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0) 85 #endif 86 #ifndef __arch__swahb32s 87 #define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0) 88 #endif 89 90 91 /* 92 * Allow constant folding 93 */ 94 #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__) 95 # define __swahw32(x) \ 96 (__builtin_constant_p((__u32)(x)) ? \ 97 ___swahw32((x)) : \ 98 __fswahw32((x))) 99 # define __swahb32(x) \ 100 (__builtin_constant_p((__u32)(x)) ? \ 101 ___swahb32((x)) : \ 102 __fswahb32((x))) 103 #else 104 # define __swahw32(x) __fswahw32(x) 105 # define __swahb32(x) __fswahb32(x) 106 #endif /* OPTIMIZE */ 107 108 __fswahw32(__u32 x)109__inline static__ __const__ __u32 __fswahw32(__u32 x) 110 { 111 return __arch__swahw32(x); 112 } __swahw32p(__u32 * x)113__inline static__ __u32 __swahw32p(__u32 *x) 114 { 115 return __arch__swahw32p(x); 116 } __swahw32s(__u32 * addr)117__inline static__ void __swahw32s(__u32 *addr) 118 { 119 __arch__swahw32s(addr); 120 } 121 122 __fswahb32(__u32 x)123__inline static__ __const__ __u32 __fswahb32(__u32 x) 124 { 125 return __arch__swahb32(x); 126 } __swahb32p(__u32 * x)127__inline static__ __u32 __swahb32p(__u32 *x) 128 { 129 return __arch__swahb32p(x); 130 } __swahb32s(__u32 * addr)131__inline static__ void __swahb32s(__u32 *addr) 132 { 133 __arch__swahb32s(addr); 134 } 135 136 #ifdef __BYTEORDER_HAS_U64__ 137 /* 138 * Not supported yet 139 */ 140 #endif /* __BYTEORDER_HAS_U64__ */ 141 142 #if defined(PLATFORM_LINUX) 143 #define swahw32 __swahw32 144 #define swahb32 __swahb32 145 #define swahw32p __swahw32p 146 #define swahb32p __swahb32p 147 #define swahw32s __swahw32s 148 #define swahb32s __swahb32s 149 #endif 150 151 #endif /* _LINUX_BYTEORDER_SWABB_H */ 152