diff options
Diffstat (limited to 'src/common/swap.h')
| -rw-r--r-- | src/common/swap.h | 332 |
1 files changed, 190 insertions, 142 deletions
diff --git a/src/common/swap.h b/src/common/swap.h index 1749bd7a4..e241c9f73 100644 --- a/src/common/swap.h +++ b/src/common/swap.h | |||
| @@ -18,15 +18,13 @@ | |||
| 18 | #pragma once | 18 | #pragma once |
| 19 | 19 | ||
| 20 | #if defined(_MSC_VER) | 20 | #if defined(_MSC_VER) |
| 21 | #include <cstdlib> | 21 | #include <cstdlib> |
| 22 | #elif defined(__linux__) | 22 | #elif defined(__linux__) |
| 23 | #include <byteswap.h> | 23 | #include <byteswap.h> |
| 24 | #elif defined(__FreeBSD__) | 24 | #elif defined(__FreeBSD__) |
| 25 | #include <sys/endian.h> | 25 | #include <sys/endian.h> |
| 26 | #endif | 26 | #endif |
| 27 | |||
| 28 | #include <cstring> | 27 | #include <cstring> |
| 29 | |||
| 30 | #include "common/common_types.h" | 28 | #include "common/common_types.h" |
| 31 | 29 | ||
| 32 | // GCC 4.6+ | 30 | // GCC 4.6+ |
| @@ -61,38 +59,73 @@ | |||
| 61 | namespace Common { | 59 | namespace Common { |
| 62 | 60 | ||
| 63 | #ifdef _MSC_VER | 61 | #ifdef _MSC_VER |
| 64 | inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);} | 62 | inline u16 swap16(u16 _data) { |
| 65 | inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);} | 63 | return _byteswap_ushort(_data); |
| 66 | inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);} | 64 | } |
| 65 | inline u32 swap32(u32 _data) { | ||
| 66 | return _byteswap_ulong(_data); | ||
| 67 | } | ||
| 68 | inline u64 swap64(u64 _data) { | ||
| 69 | return _byteswap_uint64(_data); | ||
| 70 | } | ||
| 67 | #elif _M_ARM | 71 | #elif _M_ARM |
| 68 | inline u16 swap16 (u16 _data) { u32 data = _data; __asm__ ("rev16 %0, %1\n" : "=l" (data) : "l" (data)); return (u16)data;} | 72 | inline u16 swap16(u16 _data) { |
| 69 | inline u32 swap32 (u32 _data) {__asm__ ("rev %0, %1\n" : "=l" (_data) : "l" (_data)); return _data;} | 73 | u32 data = _data; |
| 70 | inline u64 swap64(u64 _data) {return ((u64)swap32(_data) << 32) | swap32(_data >> 32);} | 74 | __asm__("rev16 %0, %1\n" : "=l"(data) : "l"(data)); |
| 75 | return (u16)data; | ||
| 76 | } | ||
| 77 | inline u32 swap32(u32 _data) { | ||
| 78 | __asm__("rev %0, %1\n" : "=l"(_data) : "l"(_data)); | ||
| 79 | return _data; | ||
| 80 | } | ||
| 81 | inline u64 swap64(u64 _data) { | ||
| 82 | return ((u64)swap32(_data) << 32) | swap32(_data >> 32); | ||
| 83 | } | ||
| 71 | #elif __linux__ | 84 | #elif __linux__ |
| 72 | inline u16 swap16(u16 _data) {return bswap_16(_data);} | 85 | inline u16 swap16(u16 _data) { |
| 73 | inline u32 swap32(u32 _data) {return bswap_32(_data);} | 86 | return bswap_16(_data); |
| 74 | inline u64 swap64(u64 _data) {return bswap_64(_data);} | 87 | } |
| 88 | inline u32 swap32(u32 _data) { | ||
| 89 | return bswap_32(_data); | ||
| 90 | } | ||
| 91 | inline u64 swap64(u64 _data) { | ||
| 92 | return bswap_64(_data); | ||
| 93 | } | ||
| 75 | #elif __APPLE__ | 94 | #elif __APPLE__ |
| 76 | inline __attribute__((always_inline)) u16 swap16(u16 _data) | 95 | inline __attribute__((always_inline)) u16 swap16(u16 _data) { |
| 77 | {return (_data >> 8) | (_data << 8);} | 96 | return (_data >> 8) | (_data << 8); |
| 78 | inline __attribute__((always_inline)) u32 swap32(u32 _data) | 97 | } |
| 79 | {return __builtin_bswap32(_data);} | 98 | inline __attribute__((always_inline)) u32 swap32(u32 _data) { |
| 80 | inline __attribute__((always_inline)) u64 swap64(u64 _data) | 99 | return __builtin_bswap32(_data); |
| 81 | {return __builtin_bswap64(_data);} | 100 | } |
| 101 | inline __attribute__((always_inline)) u64 swap64(u64 _data) { | ||
| 102 | return __builtin_bswap64(_data); | ||
| 103 | } | ||
| 82 | #elif __FreeBSD__ | 104 | #elif __FreeBSD__ |
| 83 | inline u16 swap16(u16 _data) {return bswap16(_data);} | 105 | inline u16 swap16(u16 _data) { |
| 84 | inline u32 swap32(u32 _data) {return bswap32(_data);} | 106 | return bswap16(_data); |
| 85 | inline u64 swap64(u64 _data) {return bswap64(_data);} | 107 | } |
| 108 | inline u32 swap32(u32 _data) { | ||
| 109 | return bswap32(_data); | ||
| 110 | } | ||
| 111 | inline u64 swap64(u64 _data) { | ||
| 112 | return bswap64(_data); | ||
| 113 | } | ||
| 86 | #else | 114 | #else |
| 87 | // Slow generic implementation. | 115 | // Slow generic implementation. |
| 88 | inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);} | 116 | inline u16 swap16(u16 data) { |
| 89 | inline u32 swap32(u32 data) {return (swap16(data) << 16) | swap16(data >> 16);} | 117 | return (data >> 8) | (data << 8); |
| 90 | inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 32);} | 118 | } |
| 119 | inline u32 swap32(u32 data) { | ||
| 120 | return (swap16(data) << 16) | swap16(data >> 16); | ||
| 121 | } | ||
| 122 | inline u64 swap64(u64 data) { | ||
| 123 | return ((u64)swap32(data) << 32) | swap32(data >> 32); | ||
| 124 | } | ||
| 91 | #endif | 125 | #endif |
| 92 | 126 | ||
| 93 | inline float swapf(float f) { | 127 | inline float swapf(float f) { |
| 94 | static_assert(sizeof(u32) == sizeof(float), | 128 | static_assert(sizeof(u32) == sizeof(float), "float must be the same size as uint32_t."); |
| 95 | "float must be the same size as uint32_t."); | ||
| 96 | 129 | ||
| 97 | u32 value; | 130 | u32 value; |
| 98 | std::memcpy(&value, &f, sizeof(u32)); | 131 | std::memcpy(&value, &f, sizeof(u32)); |
| @@ -104,8 +137,7 @@ inline float swapf(float f) { | |||
| 104 | } | 137 | } |
| 105 | 138 | ||
| 106 | inline double swapd(double f) { | 139 | inline double swapd(double f) { |
| 107 | static_assert(sizeof(u64) == sizeof(double), | 140 | static_assert(sizeof(u64) == sizeof(double), "double must be the same size as uint64_t."); |
| 108 | "double must be the same size as uint64_t."); | ||
| 109 | 141 | ||
| 110 | u64 value; | 142 | u64 value; |
| 111 | std::memcpy(&value, &f, sizeof(u64)); | 143 | std::memcpy(&value, &f, sizeof(u64)); |
| @@ -116,8 +148,7 @@ inline double swapd(double f) { | |||
| 116 | return f; | 148 | return f; |
| 117 | } | 149 | } |
| 118 | 150 | ||
| 119 | } // Namespace Common | 151 | } // Namespace Common |
| 120 | |||
| 121 | 152 | ||
| 122 | template <typename T, typename F> | 153 | template <typename T, typename F> |
| 123 | struct swap_struct_t { | 154 | struct swap_struct_t { |
| @@ -129,251 +160,271 @@ protected: | |||
| 129 | static T swap(T v) { | 160 | static T swap(T v) { |
| 130 | return F::swap(v); | 161 | return F::swap(v); |
| 131 | } | 162 | } |
| 163 | |||
| 132 | public: | 164 | public: |
| 133 | T const swap() const { | 165 | T const swap() const { |
| 134 | return swap(value); | 166 | return swap(value); |
| 135 | |||
| 136 | } | 167 | } |
| 137 | swap_struct_t() = default; | 168 | swap_struct_t() = default; |
| 138 | swap_struct_t(const T &v): value(swap(v)) {} | 169 | swap_struct_t(const T& v) : value(swap(v)) {} |
| 139 | 170 | ||
| 140 | template <typename S> | 171 | template <typename S> |
| 141 | swapped_t& operator=(const S &source) { | 172 | swapped_t& operator=(const S& source) { |
| 142 | value = swap((T)source); | 173 | value = swap((T)source); |
| 143 | return *this; | 174 | return *this; |
| 144 | } | 175 | } |
| 145 | 176 | ||
| 146 | operator s8() const { return (s8)swap(); } | 177 | operator s8() const { |
| 147 | operator u8() const { return (u8)swap(); } | 178 | return (s8)swap(); |
| 148 | operator s16() const { return (s16)swap(); } | 179 | } |
| 149 | operator u16() const { return (u16)swap(); } | 180 | operator u8() const { |
| 150 | operator s32() const { return (s32)swap(); } | 181 | return (u8)swap(); |
| 151 | operator u32() const { return (u32)swap(); } | 182 | } |
| 152 | operator s64() const { return (s64)swap(); } | 183 | operator s16() const { |
| 153 | operator u64() const { return (u64)swap(); } | 184 | return (s16)swap(); |
| 154 | operator float() const { return (float)swap(); } | 185 | } |
| 155 | operator double() const { return (double)swap(); } | 186 | operator u16() const { |
| 187 | return (u16)swap(); | ||
| 188 | } | ||
| 189 | operator s32() const { | ||
| 190 | return (s32)swap(); | ||
| 191 | } | ||
| 192 | operator u32() const { | ||
| 193 | return (u32)swap(); | ||
| 194 | } | ||
| 195 | operator s64() const { | ||
| 196 | return (s64)swap(); | ||
| 197 | } | ||
| 198 | operator u64() const { | ||
| 199 | return (u64)swap(); | ||
| 200 | } | ||
| 201 | operator float() const { | ||
| 202 | return (float)swap(); | ||
| 203 | } | ||
| 204 | operator double() const { | ||
| 205 | return (double)swap(); | ||
| 206 | } | ||
| 156 | 207 | ||
| 157 | // +v | 208 | // +v |
| 158 | swapped_t operator +() const { | 209 | swapped_t operator+() const { |
| 159 | return +swap(); | 210 | return +swap(); |
| 160 | } | 211 | } |
| 161 | // -v | 212 | // -v |
| 162 | swapped_t operator -() const { | 213 | swapped_t operator-() const { |
| 163 | return -swap(); | 214 | return -swap(); |
| 164 | } | 215 | } |
| 165 | 216 | ||
| 166 | // v / 5 | 217 | // v / 5 |
| 167 | swapped_t operator/(const swapped_t &i) const { | 218 | swapped_t operator/(const swapped_t& i) const { |
| 168 | return swap() / i.swap(); | 219 | return swap() / i.swap(); |
| 169 | } | 220 | } |
| 170 | template <typename S> | 221 | template <typename S> |
| 171 | swapped_t operator/(const S &i) const { | 222 | swapped_t operator/(const S& i) const { |
| 172 | return swap() / i; | 223 | return swap() / i; |
| 173 | } | 224 | } |
| 174 | 225 | ||
| 175 | // v * 5 | 226 | // v * 5 |
| 176 | swapped_t operator*(const swapped_t &i) const { | 227 | swapped_t operator*(const swapped_t& i) const { |
| 177 | return swap() * i.swap(); | 228 | return swap() * i.swap(); |
| 178 | } | 229 | } |
| 179 | template <typename S> | 230 | template <typename S> |
| 180 | swapped_t operator*(const S &i) const { | 231 | swapped_t operator*(const S& i) const { |
| 181 | return swap() * i; | 232 | return swap() * i; |
| 182 | } | 233 | } |
| 183 | 234 | ||
| 184 | // v + 5 | 235 | // v + 5 |
| 185 | swapped_t operator+(const swapped_t &i) const { | 236 | swapped_t operator+(const swapped_t& i) const { |
| 186 | return swap() + i.swap(); | 237 | return swap() + i.swap(); |
| 187 | } | 238 | } |
| 188 | template <typename S> | 239 | template <typename S> |
| 189 | swapped_t operator+(const S &i) const { | 240 | swapped_t operator+(const S& i) const { |
| 190 | return swap() + (T)i; | 241 | return swap() + (T)i; |
| 191 | } | 242 | } |
| 192 | // v - 5 | 243 | // v - 5 |
| 193 | swapped_t operator-(const swapped_t &i) const { | 244 | swapped_t operator-(const swapped_t& i) const { |
| 194 | return swap() - i.swap(); | 245 | return swap() - i.swap(); |
| 195 | } | 246 | } |
| 196 | template <typename S> | 247 | template <typename S> |
| 197 | swapped_t operator-(const S &i) const { | 248 | swapped_t operator-(const S& i) const { |
| 198 | return swap() - (T)i; | 249 | return swap() - (T)i; |
| 199 | } | 250 | } |
| 200 | 251 | ||
| 201 | // v += 5 | 252 | // v += 5 |
| 202 | swapped_t& operator+=(const swapped_t &i) { | 253 | swapped_t& operator+=(const swapped_t& i) { |
| 203 | value = swap(swap() + i.swap()); | 254 | value = swap(swap() + i.swap()); |
| 204 | return *this; | 255 | return *this; |
| 205 | } | 256 | } |
| 206 | template <typename S> | 257 | template <typename S> |
| 207 | swapped_t& operator+=(const S &i) { | 258 | swapped_t& operator+=(const S& i) { |
| 208 | value = swap(swap() + (T)i); | 259 | value = swap(swap() + (T)i); |
| 209 | return *this; | 260 | return *this; |
| 210 | } | 261 | } |
| 211 | // v -= 5 | 262 | // v -= 5 |
| 212 | swapped_t& operator-=(const swapped_t &i) { | 263 | swapped_t& operator-=(const swapped_t& i) { |
| 213 | value = swap(swap() - i.swap()); | 264 | value = swap(swap() - i.swap()); |
| 214 | return *this; | 265 | return *this; |
| 215 | } | 266 | } |
| 216 | template <typename S> | 267 | template <typename S> |
| 217 | swapped_t& operator-=(const S &i) { | 268 | swapped_t& operator-=(const S& i) { |
| 218 | value = swap(swap() - (T)i); | 269 | value = swap(swap() - (T)i); |
| 219 | return *this; | 270 | return *this; |
| 220 | } | 271 | } |
| 221 | 272 | ||
| 222 | // ++v | 273 | // ++v |
| 223 | swapped_t& operator++() { | 274 | swapped_t& operator++() { |
| 224 | value = swap(swap()+1); | 275 | value = swap(swap() + 1); |
| 225 | return *this; | 276 | return *this; |
| 226 | } | 277 | } |
| 227 | // --v | 278 | // --v |
| 228 | swapped_t& operator--() { | 279 | swapped_t& operator--() { |
| 229 | value = swap(swap()-1); | 280 | value = swap(swap() - 1); |
| 230 | return *this; | 281 | return *this; |
| 231 | } | 282 | } |
| 232 | 283 | ||
| 233 | // v++ | 284 | // v++ |
| 234 | swapped_t operator++(int) { | 285 | swapped_t operator++(int) { |
| 235 | swapped_t old = *this; | 286 | swapped_t old = *this; |
| 236 | value = swap(swap()+1); | 287 | value = swap(swap() + 1); |
| 237 | return old; | 288 | return old; |
| 238 | } | 289 | } |
| 239 | // v-- | 290 | // v-- |
| 240 | swapped_t operator--(int) { | 291 | swapped_t operator--(int) { |
| 241 | swapped_t old = *this; | 292 | swapped_t old = *this; |
| 242 | value = swap(swap()-1); | 293 | value = swap(swap() - 1); |
| 243 | return old; | 294 | return old; |
| 244 | } | 295 | } |
| 245 | // Comparaison | 296 | // Comparaison |
| 246 | // v == i | 297 | // v == i |
| 247 | bool operator==(const swapped_t &i) const { | 298 | bool operator==(const swapped_t& i) const { |
| 248 | return swap() == i.swap(); | 299 | return swap() == i.swap(); |
| 249 | } | 300 | } |
| 250 | template <typename S> | 301 | template <typename S> |
| 251 | bool operator==(const S &i) const { | 302 | bool operator==(const S& i) const { |
| 252 | return swap() == i; | 303 | return swap() == i; |
| 253 | } | 304 | } |
| 254 | 305 | ||
| 255 | // v != i | 306 | // v != i |
| 256 | bool operator!=(const swapped_t &i) const { | 307 | bool operator!=(const swapped_t& i) const { |
| 257 | return swap() != i.swap(); | 308 | return swap() != i.swap(); |
| 258 | } | 309 | } |
| 259 | template <typename S> | 310 | template <typename S> |
| 260 | bool operator!=(const S &i) const { | 311 | bool operator!=(const S& i) const { |
| 261 | return swap() != i; | 312 | return swap() != i; |
| 262 | } | 313 | } |
| 263 | 314 | ||
| 264 | // v > i | 315 | // v > i |
| 265 | bool operator>(const swapped_t &i) const { | 316 | bool operator>(const swapped_t& i) const { |
| 266 | return swap() > i.swap(); | 317 | return swap() > i.swap(); |
| 267 | } | 318 | } |
| 268 | template <typename S> | 319 | template <typename S> |
| 269 | bool operator>(const S &i) const { | 320 | bool operator>(const S& i) const { |
| 270 | return swap() > i; | 321 | return swap() > i; |
| 271 | } | 322 | } |
| 272 | 323 | ||
| 273 | // v < i | 324 | // v < i |
| 274 | bool operator<(const swapped_t &i) const { | 325 | bool operator<(const swapped_t& i) const { |
| 275 | return swap() < i.swap(); | 326 | return swap() < i.swap(); |
| 276 | } | 327 | } |
| 277 | template <typename S> | 328 | template <typename S> |
| 278 | bool operator<(const S &i) const { | 329 | bool operator<(const S& i) const { |
| 279 | return swap() < i; | 330 | return swap() < i; |
| 280 | } | 331 | } |
| 281 | 332 | ||
| 282 | // v >= i | 333 | // v >= i |
| 283 | bool operator>=(const swapped_t &i) const { | 334 | bool operator>=(const swapped_t& i) const { |
| 284 | return swap() >= i.swap(); | 335 | return swap() >= i.swap(); |
| 285 | } | 336 | } |
| 286 | template <typename S> | 337 | template <typename S> |
| 287 | bool operator>=(const S &i) const { | 338 | bool operator>=(const S& i) const { |
| 288 | return swap() >= i; | 339 | return swap() >= i; |
| 289 | } | 340 | } |
| 290 | 341 | ||
| 291 | // v <= i | 342 | // v <= i |
| 292 | bool operator<=(const swapped_t &i) const { | 343 | bool operator<=(const swapped_t& i) const { |
| 293 | return swap() <= i.swap(); | 344 | return swap() <= i.swap(); |
| 294 | } | 345 | } |
| 295 | template <typename S> | 346 | template <typename S> |
| 296 | bool operator<=(const S &i) const { | 347 | bool operator<=(const S& i) const { |
| 297 | return swap() <= i; | 348 | return swap() <= i; |
| 298 | } | 349 | } |
| 299 | 350 | ||
| 300 | // logical | 351 | // logical |
| 301 | swapped_t operator !() const { | 352 | swapped_t operator!() const { |
| 302 | return !swap(); | 353 | return !swap(); |
| 303 | } | 354 | } |
| 304 | 355 | ||
| 305 | // bitmath | 356 | // bitmath |
| 306 | swapped_t operator ~() const { | 357 | swapped_t operator~() const { |
| 307 | return ~swap(); | 358 | return ~swap(); |
| 308 | } | 359 | } |
| 309 | 360 | ||
| 310 | swapped_t operator &(const swapped_t &b) const { | 361 | swapped_t operator&(const swapped_t& b) const { |
| 311 | return swap() & b.swap(); | 362 | return swap() & b.swap(); |
| 312 | } | 363 | } |
| 313 | template <typename S> | 364 | template <typename S> |
| 314 | swapped_t operator &(const S &b) const { | 365 | swapped_t operator&(const S& b) const { |
| 315 | return swap() & b; | 366 | return swap() & b; |
| 316 | } | 367 | } |
| 317 | swapped_t& operator &=(const swapped_t &b) { | 368 | swapped_t& operator&=(const swapped_t& b) { |
| 318 | value = swap(swap() & b.swap()); | 369 | value = swap(swap() & b.swap()); |
| 319 | return *this; | 370 | return *this; |
| 320 | } | 371 | } |
| 321 | template <typename S> | 372 | template <typename S> |
| 322 | swapped_t& operator &=(const S b) { | 373 | swapped_t& operator&=(const S b) { |
| 323 | value = swap(swap() & b); | 374 | value = swap(swap() & b); |
| 324 | return *this; | 375 | return *this; |
| 325 | } | 376 | } |
| 326 | 377 | ||
| 327 | swapped_t operator |(const swapped_t &b) const { | 378 | swapped_t operator|(const swapped_t& b) const { |
| 328 | return swap() | b.swap(); | 379 | return swap() | b.swap(); |
| 329 | } | 380 | } |
| 330 | template <typename S> | 381 | template <typename S> |
| 331 | swapped_t operator |(const S &b) const { | 382 | swapped_t operator|(const S& b) const { |
| 332 | return swap() | b; | 383 | return swap() | b; |
| 333 | } | 384 | } |
| 334 | swapped_t& operator |=(const swapped_t &b) { | 385 | swapped_t& operator|=(const swapped_t& b) { |
| 335 | value = swap(swap() | b.swap()); | 386 | value = swap(swap() | b.swap()); |
| 336 | return *this; | 387 | return *this; |
| 337 | } | 388 | } |
| 338 | template <typename S> | 389 | template <typename S> |
| 339 | swapped_t& operator |=(const S &b) { | 390 | swapped_t& operator|=(const S& b) { |
| 340 | value = swap(swap() | b); | 391 | value = swap(swap() | b); |
| 341 | return *this; | 392 | return *this; |
| 342 | } | 393 | } |
| 343 | 394 | ||
| 344 | swapped_t operator ^(const swapped_t &b) const { | 395 | swapped_t operator^(const swapped_t& b) const { |
| 345 | return swap() ^ b.swap(); | 396 | return swap() ^ b.swap(); |
| 346 | } | 397 | } |
| 347 | template <typename S> | 398 | template <typename S> |
| 348 | swapped_t operator ^(const S &b) const { | 399 | swapped_t operator^(const S& b) const { |
| 349 | return swap() ^ b; | 400 | return swap() ^ b; |
| 350 | } | 401 | } |
| 351 | swapped_t& operator ^=(const swapped_t &b) { | 402 | swapped_t& operator^=(const swapped_t& b) { |
| 352 | value = swap(swap() ^ b.swap()); | 403 | value = swap(swap() ^ b.swap()); |
| 353 | return *this; | 404 | return *this; |
| 354 | } | 405 | } |
| 355 | template <typename S> | 406 | template <typename S> |
| 356 | swapped_t& operator ^=(const S &b) { | 407 | swapped_t& operator^=(const S& b) { |
| 357 | value = swap(swap() ^ b); | 408 | value = swap(swap() ^ b); |
| 358 | return *this; | 409 | return *this; |
| 359 | } | 410 | } |
| 360 | 411 | ||
| 361 | template <typename S> | 412 | template <typename S> |
| 362 | swapped_t operator <<(const S &b) const { | 413 | swapped_t operator<<(const S& b) const { |
| 363 | return swap() << b; | 414 | return swap() << b; |
| 364 | } | 415 | } |
| 365 | template <typename S> | 416 | template <typename S> |
| 366 | swapped_t& operator <<=(const S &b) const { | 417 | swapped_t& operator<<=(const S& b) const { |
| 367 | value = swap(swap() << b); | 418 | value = swap(swap() << b); |
| 368 | return *this; | 419 | return *this; |
| 369 | } | 420 | } |
| 370 | 421 | ||
| 371 | template <typename S> | 422 | template <typename S> |
| 372 | swapped_t operator >>(const S &b) const { | 423 | swapped_t operator>>(const S& b) const { |
| 373 | return swap() >> b; | 424 | return swap() >> b; |
| 374 | } | 425 | } |
| 375 | template <typename S> | 426 | template <typename S> |
| 376 | swapped_t& operator >>=(const S &b) const { | 427 | swapped_t& operator>>=(const S& b) const { |
| 377 | value = swap(swap() >> b); | 428 | value = swap(swap() >> b); |
| 378 | return *this; | 429 | return *this; |
| 379 | } | 430 | } |
| @@ -381,129 +432,126 @@ public: | |||
| 381 | // Member | 432 | // Member |
| 382 | /** todo **/ | 433 | /** todo **/ |
| 383 | 434 | ||
| 384 | |||
| 385 | // Arithmetics | 435 | // Arithmetics |
| 386 | template <typename S, typename T2, typename F2> | 436 | template <typename S, typename T2, typename F2> |
| 387 | friend S operator+(const S &p, const swapped_t v); | 437 | friend S operator+(const S& p, const swapped_t v); |
| 388 | 438 | ||
| 389 | template <typename S, typename T2, typename F2> | 439 | template <typename S, typename T2, typename F2> |
| 390 | friend S operator-(const S &p, const swapped_t v); | 440 | friend S operator-(const S& p, const swapped_t v); |
| 391 | 441 | ||
| 392 | template <typename S, typename T2, typename F2> | 442 | template <typename S, typename T2, typename F2> |
| 393 | friend S operator/(const S &p, const swapped_t v); | 443 | friend S operator/(const S& p, const swapped_t v); |
| 394 | 444 | ||
| 395 | template <typename S, typename T2, typename F2> | 445 | template <typename S, typename T2, typename F2> |
| 396 | friend S operator*(const S &p, const swapped_t v); | 446 | friend S operator*(const S& p, const swapped_t v); |
| 397 | 447 | ||
| 398 | template <typename S, typename T2, typename F2> | 448 | template <typename S, typename T2, typename F2> |
| 399 | friend S operator%(const S &p, const swapped_t v); | 449 | friend S operator%(const S& p, const swapped_t v); |
| 400 | 450 | ||
| 401 | // Arithmetics + assignements | 451 | // Arithmetics + assignements |
| 402 | template <typename S, typename T2, typename F2> | 452 | template <typename S, typename T2, typename F2> |
| 403 | friend S operator+=(const S &p, const swapped_t v); | 453 | friend S operator+=(const S& p, const swapped_t v); |
| 404 | 454 | ||
| 405 | template <typename S, typename T2, typename F2> | 455 | template <typename S, typename T2, typename F2> |
| 406 | friend S operator-=(const S &p, const swapped_t v); | 456 | friend S operator-=(const S& p, const swapped_t v); |
| 407 | 457 | ||
| 408 | // Bitmath | 458 | // Bitmath |
| 409 | template <typename S, typename T2, typename F2> | 459 | template <typename S, typename T2, typename F2> |
| 410 | friend S operator&(const S &p, const swapped_t v); | 460 | friend S operator&(const S& p, const swapped_t v); |
| 411 | 461 | ||
| 412 | // Comparison | 462 | // Comparison |
| 413 | template <typename S, typename T2, typename F2> | 463 | template <typename S, typename T2, typename F2> |
| 414 | friend bool operator<(const S &p, const swapped_t v); | 464 | friend bool operator<(const S& p, const swapped_t v); |
| 415 | 465 | ||
| 416 | template <typename S, typename T2, typename F2> | 466 | template <typename S, typename T2, typename F2> |
| 417 | friend bool operator>(const S &p, const swapped_t v); | 467 | friend bool operator>(const S& p, const swapped_t v); |
| 418 | 468 | ||
| 419 | template <typename S, typename T2, typename F2> | 469 | template <typename S, typename T2, typename F2> |
| 420 | friend bool operator<=(const S &p, const swapped_t v); | 470 | friend bool operator<=(const S& p, const swapped_t v); |
| 421 | 471 | ||
| 422 | template <typename S, typename T2, typename F2> | 472 | template <typename S, typename T2, typename F2> |
| 423 | friend bool operator>=(const S &p, const swapped_t v); | 473 | friend bool operator>=(const S& p, const swapped_t v); |
| 424 | 474 | ||
| 425 | template <typename S, typename T2, typename F2> | 475 | template <typename S, typename T2, typename F2> |
| 426 | friend bool operator!=(const S &p, const swapped_t v); | 476 | friend bool operator!=(const S& p, const swapped_t v); |
| 427 | 477 | ||
| 428 | template <typename S, typename T2, typename F2> | 478 | template <typename S, typename T2, typename F2> |
| 429 | friend bool operator==(const S &p, const swapped_t v); | 479 | friend bool operator==(const S& p, const swapped_t v); |
| 430 | }; | 480 | }; |
| 431 | 481 | ||
| 432 | |||
| 433 | // Arithmetics | 482 | // Arithmetics |
| 434 | template <typename S, typename T, typename F> | 483 | template <typename S, typename T, typename F> |
| 435 | S operator+(const S &i, const swap_struct_t<T, F> v) { | 484 | S operator+(const S& i, const swap_struct_t<T, F> v) { |
| 436 | return i + v.swap(); | 485 | return i + v.swap(); |
| 437 | } | 486 | } |
| 438 | 487 | ||
| 439 | template <typename S, typename T, typename F> | 488 | template <typename S, typename T, typename F> |
| 440 | S operator-(const S &i, const swap_struct_t<T, F> v) { | 489 | S operator-(const S& i, const swap_struct_t<T, F> v) { |
| 441 | return i - v.swap(); | 490 | return i - v.swap(); |
| 442 | } | 491 | } |
| 443 | 492 | ||
| 444 | template <typename S, typename T, typename F> | 493 | template <typename S, typename T, typename F> |
| 445 | S operator/(const S &i, const swap_struct_t<T, F> v) { | 494 | S operator/(const S& i, const swap_struct_t<T, F> v) { |
| 446 | return i / v.swap(); | 495 | return i / v.swap(); |
| 447 | } | 496 | } |
| 448 | 497 | ||
| 449 | template <typename S, typename T, typename F> | 498 | template <typename S, typename T, typename F> |
| 450 | S operator*(const S &i, const swap_struct_t<T, F> v) { | 499 | S operator*(const S& i, const swap_struct_t<T, F> v) { |
| 451 | return i * v.swap(); | 500 | return i * v.swap(); |
| 452 | } | 501 | } |
| 453 | 502 | ||
| 454 | template <typename S, typename T, typename F> | 503 | template <typename S, typename T, typename F> |
| 455 | S operator%(const S &i, const swap_struct_t<T, F> v) { | 504 | S operator%(const S& i, const swap_struct_t<T, F> v) { |
| 456 | return i % v.swap(); | 505 | return i % v.swap(); |
| 457 | } | 506 | } |
| 458 | 507 | ||
| 459 | // Arithmetics + assignements | 508 | // Arithmetics + assignements |
| 460 | template <typename S, typename T, typename F> | 509 | template <typename S, typename T, typename F> |
| 461 | S &operator+=(S &i, const swap_struct_t<T, F> v) { | 510 | S& operator+=(S& i, const swap_struct_t<T, F> v) { |
| 462 | i += v.swap(); | 511 | i += v.swap(); |
| 463 | return i; | 512 | return i; |
| 464 | } | 513 | } |
| 465 | 514 | ||
| 466 | template <typename S, typename T, typename F> | 515 | template <typename S, typename T, typename F> |
| 467 | S &operator-=(S &i, const swap_struct_t<T, F> v) { | 516 | S& operator-=(S& i, const swap_struct_t<T, F> v) { |
| 468 | i -= v.swap(); | 517 | i -= v.swap(); |
| 469 | return i; | 518 | return i; |
| 470 | } | 519 | } |
| 471 | 520 | ||
| 472 | // Logical | 521 | // Logical |
| 473 | template <typename S, typename T, typename F> | 522 | template <typename S, typename T, typename F> |
| 474 | S operator&(const S &i, const swap_struct_t<T, F> v) { | 523 | S operator&(const S& i, const swap_struct_t<T, F> v) { |
| 475 | return i & v.swap(); | 524 | return i & v.swap(); |
| 476 | } | 525 | } |
| 477 | 526 | ||
| 478 | template <typename S, typename T, typename F> | 527 | template <typename S, typename T, typename F> |
| 479 | S operator&(const swap_struct_t<T, F> v, const S &i) { | 528 | S operator&(const swap_struct_t<T, F> v, const S& i) { |
| 480 | return (S)(v.swap() & i); | 529 | return (S)(v.swap() & i); |
| 481 | } | 530 | } |
| 482 | 531 | ||
| 483 | |||
| 484 | // Comparaison | 532 | // Comparaison |
| 485 | template <typename S, typename T, typename F> | 533 | template <typename S, typename T, typename F> |
| 486 | bool operator<(const S &p, const swap_struct_t<T, F> v) { | 534 | bool operator<(const S& p, const swap_struct_t<T, F> v) { |
| 487 | return p < v.swap(); | 535 | return p < v.swap(); |
| 488 | } | 536 | } |
| 489 | template <typename S, typename T, typename F> | 537 | template <typename S, typename T, typename F> |
| 490 | bool operator>(const S &p, const swap_struct_t<T, F> v) { | 538 | bool operator>(const S& p, const swap_struct_t<T, F> v) { |
| 491 | return p > v.swap(); | 539 | return p > v.swap(); |
| 492 | } | 540 | } |
| 493 | template <typename S, typename T, typename F> | 541 | template <typename S, typename T, typename F> |
| 494 | bool operator<=(const S &p, const swap_struct_t<T, F> v) { | 542 | bool operator<=(const S& p, const swap_struct_t<T, F> v) { |
| 495 | return p <= v.swap(); | 543 | return p <= v.swap(); |
| 496 | } | 544 | } |
| 497 | template <typename S, typename T, typename F> | 545 | template <typename S, typename T, typename F> |
| 498 | bool operator>=(const S &p, const swap_struct_t<T, F> v) { | 546 | bool operator>=(const S& p, const swap_struct_t<T, F> v) { |
| 499 | return p >= v.swap(); | 547 | return p >= v.swap(); |
| 500 | } | 548 | } |
| 501 | template <typename S, typename T, typename F> | 549 | template <typename S, typename T, typename F> |
| 502 | bool operator!=(const S &p, const swap_struct_t<T, F> v) { | 550 | bool operator!=(const S& p, const swap_struct_t<T, F> v) { |
| 503 | return p != v.swap(); | 551 | return p != v.swap(); |
| 504 | } | 552 | } |
| 505 | template <typename S, typename T, typename F> | 553 | template <typename S, typename T, typename F> |
| 506 | bool operator==(const S &p, const swap_struct_t<T, F> v) { | 554 | bool operator==(const S& p, const swap_struct_t<T, F> v) { |
| 507 | return p == v.swap(); | 555 | return p == v.swap(); |
| 508 | } | 556 | } |
| 509 | 557 | ||
| @@ -554,30 +602,30 @@ typedef s64 s64_le; | |||
| 554 | typedef float float_le; | 602 | typedef float float_le; |
| 555 | typedef double double_le; | 603 | typedef double double_le; |
| 556 | 604 | ||
| 557 | typedef swap_struct_t<u64, swap_64_t<u64> > u64_be; | 605 | typedef swap_struct_t<u64, swap_64_t<u64>> u64_be; |
| 558 | typedef swap_struct_t<s64, swap_64_t<s64> > s64_be; | 606 | typedef swap_struct_t<s64, swap_64_t<s64>> s64_be; |
| 559 | 607 | ||
| 560 | typedef swap_struct_t<u32, swap_32_t<u32> > u32_be; | 608 | typedef swap_struct_t<u32, swap_32_t<u32>> u32_be; |
| 561 | typedef swap_struct_t<s32, swap_32_t<s32> > s32_be; | 609 | typedef swap_struct_t<s32, swap_32_t<s32>> s32_be; |
| 562 | 610 | ||
| 563 | typedef swap_struct_t<u16, swap_16_t<u16> > u16_be; | 611 | typedef swap_struct_t<u16, swap_16_t<u16>> u16_be; |
| 564 | typedef swap_struct_t<s16, swap_16_t<s16> > s16_be; | 612 | typedef swap_struct_t<s16, swap_16_t<s16>> s16_be; |
| 565 | 613 | ||
| 566 | typedef swap_struct_t<float, swap_float_t<float> > float_be; | 614 | typedef swap_struct_t<float, swap_float_t<float>> float_be; |
| 567 | typedef swap_struct_t<double, swap_double_t<double> > double_be; | 615 | typedef swap_struct_t<double, swap_double_t<double>> double_be; |
| 568 | #else | 616 | #else |
| 569 | 617 | ||
| 570 | typedef swap_struct_t<u64, swap_64_t<u64> > u64_le; | 618 | typedef swap_struct_t<u64, swap_64_t<u64>> u64_le; |
| 571 | typedef swap_struct_t<s64, swap_64_t<s64> > s64_le; | 619 | typedef swap_struct_t<s64, swap_64_t<s64>> s64_le; |
| 572 | 620 | ||
| 573 | typedef swap_struct_t<u32, swap_32_t<u32> > u32_le; | 621 | typedef swap_struct_t<u32, swap_32_t<u32>> u32_le; |
| 574 | typedef swap_struct_t<s32, swap_32_t<s32> > s32_le; | 622 | typedef swap_struct_t<s32, swap_32_t<s32>> s32_le; |
| 575 | 623 | ||
| 576 | typedef swap_struct_t<u16, swap_16_t<u16> > u16_le; | 624 | typedef swap_struct_t<u16, swap_16_t<u16>> u16_le; |
| 577 | typedef swap_struct_t< s16, swap_16_t<s16> > s16_le; | 625 | typedef swap_struct_t<s16, swap_16_t<s16>> s16_le; |
| 578 | 626 | ||
| 579 | typedef swap_struct_t<float, swap_float_t<float> > float_le; | 627 | typedef swap_struct_t<float, swap_float_t<float>> float_le; |
| 580 | typedef swap_struct_t<double, swap_double_t<double> > double_le; | 628 | typedef swap_struct_t<double, swap_double_t<double>> double_le; |
| 581 | 629 | ||
| 582 | typedef u32 u32_be; | 630 | typedef u32 u32_be; |
| 583 | typedef u16 u16_be; | 631 | typedef u16 u16_be; |