Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_BYTEORDER_SWABB_H |
| 2 | #define _LINUX_BYTEORDER_SWABB_H |
| 3 | |
| 4 | /* |
| 5 | * linux/byteorder/swabb.h |
| 6 | * SWAp Bytes Bizarrely |
| 7 | * swaHHXX[ps]?(foo) |
| 8 | * |
| 9 | * Support for obNUXIous pdp-endian and other bizarre architectures. |
| 10 | * Will Linux ever run on such ancient beasts? if not, this file |
| 11 | * will be but a programming pearl. Still, it's a reminder that we |
| 12 | * shouldn't be making too many assumptions when trying to be portable. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * Meaning of the names I chose (vaxlinux people feel free to correct them): |
| 18 | * swahw32 swap 16-bit half-words in a 32-bit word |
| 19 | * swahb32 swap 8-bit halves of each 16-bit half-word in a 32-bit word |
| 20 | * |
| 21 | * No 64-bit support yet. I don't know NUXI conventions for long longs. |
| 22 | * I guarantee it will be a mess when it's there, though :-> |
| 23 | * It will be even worse if there are conflicting 64-bit conventions. |
| 24 | * Hopefully, no one ever used 64-bit objects on NUXI machines. |
| 25 | * |
| 26 | */ |
| 27 | |
Adrian Bunk | bfb5847 | 2007-02-10 01:46:06 -0800 | [diff] [blame] | 28 | #include <linux/types.h> |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #define ___swahw32(x) \ |
| 31 | ({ \ |
| 32 | __u32 __x = (x); \ |
| 33 | ((__u32)( \ |
| 34 | (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \ |
| 35 | (((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \ |
| 36 | }) |
| 37 | #define ___swahb32(x) \ |
| 38 | ({ \ |
| 39 | __u32 __x = (x); \ |
| 40 | ((__u32)( \ |
| 41 | (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \ |
| 42 | (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )); \ |
| 43 | }) |
| 44 | |
| 45 | #define ___constant_swahw32(x) \ |
| 46 | ((__u32)( \ |
| 47 | (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \ |
| 48 | (((__u32)(x) & (__u32)0xffff0000UL) >> 16) )) |
| 49 | #define ___constant_swahb32(x) \ |
| 50 | ((__u32)( \ |
| 51 | (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \ |
| 52 | (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) )) |
| 53 | |
| 54 | /* |
| 55 | * provide defaults when no architecture-specific optimization is detected |
| 56 | */ |
| 57 | #ifndef __arch__swahw32 |
| 58 | # define __arch__swahw32(x) ___swahw32(x) |
| 59 | #endif |
| 60 | #ifndef __arch__swahb32 |
| 61 | # define __arch__swahb32(x) ___swahb32(x) |
| 62 | #endif |
| 63 | |
| 64 | #ifndef __arch__swahw32p |
| 65 | # define __arch__swahw32p(x) __swahw32(*(x)) |
| 66 | #endif |
| 67 | #ifndef __arch__swahb32p |
| 68 | # define __arch__swahb32p(x) __swahb32(*(x)) |
| 69 | #endif |
| 70 | |
| 71 | #ifndef __arch__swahw32s |
| 72 | # define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0) |
| 73 | #endif |
| 74 | #ifndef __arch__swahb32s |
| 75 | # define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0) |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Allow constant folding |
| 81 | */ |
Adrian Bunk | bfb5847 | 2007-02-10 01:46:06 -0800 | [diff] [blame] | 82 | #define __swahw32(x) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | (__builtin_constant_p((__u32)(x)) ? \ |
| 84 | ___swahw32((x)) : \ |
| 85 | __fswahw32((x))) |
Adrian Bunk | bfb5847 | 2007-02-10 01:46:06 -0800 | [diff] [blame] | 86 | #define __swahb32(x) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | (__builtin_constant_p((__u32)(x)) ? \ |
| 88 | ___swahb32((x)) : \ |
| 89 | __fswahb32((x))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | |
Andrew Morton | 05133fc | 2005-06-28 20:44:54 -0700 | [diff] [blame] | 92 | static inline __u32 __fswahw32(__u32 x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | return __arch__swahw32(x); |
| 95 | } |
Andrew Morton | 05133fc | 2005-06-28 20:44:54 -0700 | [diff] [blame] | 96 | |
| 97 | static inline __u32 __swahw32p(__u32 *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
| 99 | return __arch__swahw32p(x); |
| 100 | } |
Andrew Morton | 05133fc | 2005-06-28 20:44:54 -0700 | [diff] [blame] | 101 | |
| 102 | static inline void __swahw32s(__u32 *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | __arch__swahw32s(addr); |
| 105 | } |
| 106 | |
Andrew Morton | 05133fc | 2005-06-28 20:44:54 -0700 | [diff] [blame] | 107 | static inline __u32 __fswahb32(__u32 x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | return __arch__swahb32(x); |
| 110 | } |
Andrew Morton | 05133fc | 2005-06-28 20:44:54 -0700 | [diff] [blame] | 111 | |
| 112 | static inline __u32 __swahb32p(__u32 *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | return __arch__swahb32p(x); |
| 115 | } |
Andrew Morton | 05133fc | 2005-06-28 20:44:54 -0700 | [diff] [blame] | 116 | |
| 117 | static inline void __swahb32s(__u32 *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | __arch__swahb32s(addr); |
| 120 | } |
| 121 | |
| 122 | #ifdef __BYTEORDER_HAS_U64__ |
| 123 | /* |
| 124 | * Not supported yet |
| 125 | */ |
| 126 | #endif /* __BYTEORDER_HAS_U64__ */ |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | #define swahw32 __swahw32 |
| 129 | #define swahb32 __swahb32 |
| 130 | #define swahw32p __swahw32p |
| 131 | #define swahb32p __swahb32p |
| 132 | #define swahw32s __swahw32s |
| 133 | #define swahb32s __swahb32s |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | #endif /* _LINUX_BYTEORDER_SWABB_H */ |