diff options
Diffstat (limited to 'src/common/swap.h')
| -rw-r--r-- | src/common/swap.h | 535 |
1 files changed, 535 insertions, 0 deletions
diff --git a/src/common/swap.h b/src/common/swap.h new file mode 100644 index 000000000..d07d9fcc5 --- /dev/null +++ b/src/common/swap.h | |||
| @@ -0,0 +1,535 @@ | |||
| 1 | // Copyright (c) 2012- PPSSPP Project / Dolphin Project. | ||
| 2 | |||
| 3 | // This program is free software: you can redistribute it and/or modify | ||
| 4 | // it under the terms of the GNU General Public License as published by | ||
| 5 | // the Free Software Foundation, version 2.0 or later versions. | ||
| 6 | |||
| 7 | // This program is distributed in the hope that it will be useful, | ||
| 8 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | // GNU General Public License 2.0 for more details. | ||
| 11 | |||
| 12 | // A copy of the GPL 2.0 should have been included with the program. | ||
| 13 | // If not, see http://www.gnu.org/licenses/ | ||
| 14 | |||
| 15 | // Official git repository and contact information can be found at | ||
| 16 | // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. | ||
| 17 | |||
| 18 | #pragma once | ||
| 19 | |||
| 20 | // Android | ||
| 21 | #if defined(ANDROID) | ||
| 22 | #include <sys/endian.h> | ||
| 23 | |||
| 24 | #if _BYTE_ORDER == _LITTLE_ENDIAN && !defined(COMMON_LITTLE_ENDIAN) | ||
| 25 | #define COMMON_LITTLE_ENDIAN 1 | ||
| 26 | #elif _BYTE_ORDER == _BIG_ENDIAN && !defined(COMMON_BIG_ENDIAN) | ||
| 27 | #define COMMON_BIG_ENDIAN 1 | ||
| 28 | #endif | ||
| 29 | |||
| 30 | // GCC 4.6+ | ||
| 31 | #elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) | ||
| 32 | |||
| 33 | #if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && !defined(COMMON_LITTLE_ENDIAN) | ||
| 34 | #define COMMON_LITTLE_ENDIAN 1 | ||
| 35 | #elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) && !defined(COMMON_BIG_ENDIAN) | ||
| 36 | #define COMMON_BIG_ENDIAN 1 | ||
| 37 | #endif | ||
| 38 | |||
| 39 | // LLVM/clang | ||
| 40 | #elif __clang__ | ||
| 41 | |||
| 42 | #if __LITTLE_ENDIAN__ && !defined(COMMON_LITTLE_ENDIAN) | ||
| 43 | #define COMMON_LITTLE_ENDIAN 1 | ||
| 44 | #elif __BIG_ENDIAN__ && !defined(COMMON_BIG_ENDIAN) | ||
| 45 | #define COMMON_BIG_ENDIAN 1 | ||
| 46 | #endif | ||
| 47 | |||
| 48 | // MSVC | ||
| 49 | #elif defined(_MSC_VER) && !defined(COMMON_BIG_ENDIAN) && !defined(COMMON_LITTLE_ENDIAN) | ||
| 50 | |||
| 51 | #ifdef _XBOX | ||
| 52 | #define COMMON_BIG_ENDIAN 1 | ||
| 53 | #else | ||
| 54 | #define COMMON_LITTLE_ENDIAN 1 | ||
| 55 | #endif | ||
| 56 | |||
| 57 | #endif | ||
| 58 | |||
| 59 | // Worst case, default to little endian. | ||
| 60 | #if !COMMON_BIG_ENDIAN && !COMMON_LITTLE_ENDIAN | ||
| 61 | #define COMMON_LITTLE_ENDIAN 1 | ||
| 62 | #endif | ||
| 63 | |||
| 64 | template <typename T, typename F> | ||
| 65 | struct swap_struct_t { | ||
| 66 | typedef swap_struct_t<T, F> swapped_t; | ||
| 67 | |||
| 68 | protected: | ||
| 69 | T value; | ||
| 70 | |||
| 71 | static T swap(T v) { | ||
| 72 | return F::swap(v); | ||
| 73 | } | ||
| 74 | public: | ||
| 75 | T const swap() const { | ||
| 76 | return swap(value); | ||
| 77 | |||
| 78 | } | ||
| 79 | swap_struct_t() : value((T)0) {} | ||
| 80 | swap_struct_t(const T &v): value(swap(v)) {} | ||
| 81 | |||
| 82 | template <typename S> | ||
| 83 | swapped_t& operator=(const S &source) { | ||
| 84 | value = swap((T)source); | ||
| 85 | return *this; | ||
| 86 | } | ||
| 87 | |||
| 88 | operator long() const { return (long)swap(); } | ||
| 89 | operator s8() const { return (s8)swap(); } | ||
| 90 | operator u8() const { return (u8)swap(); } | ||
| 91 | operator s16() const { return (s16)swap(); } | ||
| 92 | operator u16() const { return (u16)swap(); } | ||
| 93 | operator s32() const { return (s32)swap(); } | ||
| 94 | operator u32() const { return (u32)swap(); } | ||
| 95 | operator s64() const { return (s64)swap(); } | ||
| 96 | operator u64() const { return (u64)swap(); } | ||
| 97 | operator float() const { return (float)swap(); } | ||
| 98 | operator double() const { return (double)swap(); } | ||
| 99 | |||
| 100 | // +v | ||
| 101 | swapped_t operator +() const { | ||
| 102 | return +swap(); | ||
| 103 | } | ||
| 104 | // -v | ||
| 105 | swapped_t operator -() const { | ||
| 106 | return -swap(); | ||
| 107 | } | ||
| 108 | |||
| 109 | // v / 5 | ||
| 110 | swapped_t operator/(const swapped_t &i) const { | ||
| 111 | return swap() / i.swap(); | ||
| 112 | } | ||
| 113 | template <typename S> | ||
| 114 | swapped_t operator/(const S &i) const { | ||
| 115 | return swap() / i; | ||
| 116 | } | ||
| 117 | |||
| 118 | // v * 5 | ||
| 119 | swapped_t operator*(const swapped_t &i) const { | ||
| 120 | return swap() * i.swap(); | ||
| 121 | } | ||
| 122 | template <typename S> | ||
| 123 | swapped_t operator*(const S &i) const { | ||
| 124 | return swap() * i; | ||
| 125 | } | ||
| 126 | |||
| 127 | // v + 5 | ||
| 128 | swapped_t operator+(const swapped_t &i) const { | ||
| 129 | return swap() + i.swap(); | ||
| 130 | } | ||
| 131 | template <typename S> | ||
| 132 | swapped_t operator+(const S &i) const { | ||
| 133 | return swap() + (T)i; | ||
| 134 | } | ||
| 135 | // v - 5 | ||
| 136 | swapped_t operator-(const swapped_t &i) const { | ||
| 137 | return swap() - i.swap(); | ||
| 138 | } | ||
| 139 | template <typename S> | ||
| 140 | swapped_t operator-(const S &i) const { | ||
| 141 | return swap() - (T)i; | ||
| 142 | } | ||
| 143 | |||
| 144 | // v += 5 | ||
| 145 | swapped_t& operator+=(const swapped_t &i) { | ||
| 146 | value = swap(swap() + i.swap()); | ||
| 147 | return *this; | ||
| 148 | } | ||
| 149 | template <typename S> | ||
| 150 | swapped_t& operator+=(const S &i) { | ||
| 151 | value = swap(swap() + (T)i); | ||
| 152 | return *this; | ||
| 153 | } | ||
| 154 | // v -= 5 | ||
| 155 | swapped_t& operator-=(const swapped_t &i) { | ||
| 156 | value = swap(swap() - i.swap()); | ||
| 157 | return *this; | ||
| 158 | } | ||
| 159 | template <typename S> | ||
| 160 | swapped_t& operator-=(const S &i) { | ||
| 161 | value = swap(swap() - (T)i); | ||
| 162 | return *this; | ||
| 163 | } | ||
| 164 | |||
| 165 | // ++v | ||
| 166 | swapped_t& operator++() { | ||
| 167 | value = swap(swap()+1); | ||
| 168 | return *this; | ||
| 169 | } | ||
| 170 | // --v | ||
| 171 | swapped_t& operator--() { | ||
| 172 | value = swap(swap()-1); | ||
| 173 | return *this; | ||
| 174 | } | ||
| 175 | |||
| 176 | // v++ | ||
| 177 | swapped_t operator++(int) { | ||
| 178 | swapped_t old = *this; | ||
| 179 | value = swap(swap()+1); | ||
| 180 | return old; | ||
| 181 | } | ||
| 182 | // v-- | ||
| 183 | swapped_t operator--(int) { | ||
| 184 | swapped_t old = *this; | ||
| 185 | value = swap(swap()-1); | ||
| 186 | return old; | ||
| 187 | } | ||
| 188 | // Comparaison | ||
| 189 | // v == i | ||
| 190 | bool operator==(const swapped_t &i) const { | ||
| 191 | return swap() == i.swap(); | ||
| 192 | } | ||
| 193 | template <typename S> | ||
| 194 | bool operator==(const S &i) const { | ||
| 195 | return swap() == i; | ||
| 196 | } | ||
| 197 | |||
| 198 | // v != i | ||
| 199 | bool operator!=(const swapped_t &i) const { | ||
| 200 | return swap() != i.swap(); | ||
| 201 | } | ||
| 202 | template <typename S> | ||
| 203 | bool operator!=(const S &i) const { | ||
| 204 | return swap() != i; | ||
| 205 | } | ||
| 206 | |||
| 207 | // v > i | ||
| 208 | bool operator>(const swapped_t &i) const { | ||
| 209 | return swap() > i.swap(); | ||
| 210 | } | ||
| 211 | template <typename S> | ||
| 212 | bool operator>(const S &i) const { | ||
| 213 | return swap() > i; | ||
| 214 | } | ||
| 215 | |||
| 216 | // v < i | ||
| 217 | bool operator<(const swapped_t &i) const { | ||
| 218 | return swap() < i.swap(); | ||
| 219 | } | ||
| 220 | template <typename S> | ||
| 221 | bool operator<(const S &i) const { | ||
| 222 | return swap() < i; | ||
| 223 | } | ||
| 224 | |||
| 225 | // v >= i | ||
| 226 | bool operator>=(const swapped_t &i) const { | ||
| 227 | return swap() >= i.swap(); | ||
| 228 | } | ||
| 229 | template <typename S> | ||
| 230 | bool operator>=(const S &i) const { | ||
| 231 | return swap() >= i; | ||
| 232 | } | ||
| 233 | |||
| 234 | // v <= i | ||
| 235 | bool operator<=(const swapped_t &i) const { | ||
| 236 | return swap() <= i.swap(); | ||
| 237 | } | ||
| 238 | template <typename S> | ||
| 239 | bool operator<=(const S &i) const { | ||
| 240 | return swap() <= i; | ||
| 241 | } | ||
| 242 | |||
| 243 | // logical | ||
| 244 | swapped_t operator !() const { | ||
| 245 | return !swap(); | ||
| 246 | } | ||
| 247 | |||
| 248 | // bitmath | ||
| 249 | swapped_t operator ~() const { | ||
| 250 | return ~swap(); | ||
| 251 | } | ||
| 252 | |||
| 253 | swapped_t operator &(const swapped_t &b) const { | ||
| 254 | return swap() & b.swap(); | ||
| 255 | } | ||
| 256 | template <typename S> | ||
| 257 | swapped_t operator &(const S &b) const { | ||
| 258 | return swap() & b; | ||
| 259 | } | ||
| 260 | swapped_t& operator &=(const swapped_t &b) { | ||
| 261 | value = swap(swap() & b.swap()); | ||
| 262 | return *this; | ||
| 263 | } | ||
| 264 | template <typename S> | ||
| 265 | swapped_t& operator &=(const S b) { | ||
| 266 | value = swap(swap() & b); | ||
| 267 | return *this; | ||
| 268 | } | ||
| 269 | |||
| 270 | swapped_t operator |(const swapped_t &b) const { | ||
| 271 | return swap() | b.swap(); | ||
| 272 | } | ||
| 273 | template <typename S> | ||
| 274 | swapped_t operator |(const S &b) const { | ||
| 275 | return swap() | b; | ||
| 276 | } | ||
| 277 | swapped_t& operator |=(const swapped_t &b) { | ||
| 278 | value = swap(swap() | b.swap()); | ||
| 279 | return *this; | ||
| 280 | } | ||
| 281 | template <typename S> | ||
| 282 | swapped_t& operator |=(const S &b) { | ||
| 283 | value = swap(swap() | b); | ||
| 284 | return *this; | ||
| 285 | } | ||
| 286 | |||
| 287 | swapped_t operator ^(const swapped_t &b) const { | ||
| 288 | return swap() ^ b.swap(); | ||
| 289 | } | ||
| 290 | template <typename S> | ||
| 291 | swapped_t operator ^(const S &b) const { | ||
| 292 | return swap() ^ b; | ||
| 293 | } | ||
| 294 | swapped_t& operator ^=(const swapped_t &b) { | ||
| 295 | value = swap(swap() ^ b.swap()); | ||
| 296 | return *this; | ||
| 297 | } | ||
| 298 | template <typename S> | ||
| 299 | swapped_t& operator ^=(const S &b) { | ||
| 300 | value = swap(swap() ^ b); | ||
| 301 | return *this; | ||
| 302 | } | ||
| 303 | |||
| 304 | template <typename S> | ||
| 305 | swapped_t operator <<(const S &b) const { | ||
| 306 | return swap() << b; | ||
| 307 | } | ||
| 308 | template <typename S> | ||
| 309 | swapped_t& operator <<=(const S &b) const { | ||
| 310 | value = swap(swap() << b); | ||
| 311 | return *this; | ||
| 312 | } | ||
| 313 | |||
| 314 | template <typename S> | ||
| 315 | swapped_t operator >>(const S &b) const { | ||
| 316 | return swap() >> b; | ||
| 317 | } | ||
| 318 | template <typename S> | ||
| 319 | swapped_t& operator >>=(const S &b) const { | ||
| 320 | value = swap(swap() >> b); | ||
| 321 | return *this; | ||
| 322 | } | ||
| 323 | |||
| 324 | // Member | ||
| 325 | /** todo **/ | ||
| 326 | |||
| 327 | |||
| 328 | // Arithmetics | ||
| 329 | template <typename S, typename T2, typename F2> | ||
| 330 | friend S operator+(const S &p, const swapped_t v); | ||
| 331 | |||
| 332 | template <typename S, typename T2, typename F2> | ||
| 333 | friend S operator-(const S &p, const swapped_t v); | ||
| 334 | |||
| 335 | template <typename S, typename T2, typename F2> | ||
| 336 | friend S operator/(const S &p, const swapped_t v); | ||
| 337 | |||
| 338 | template <typename S, typename T2, typename F2> | ||
| 339 | friend S operator*(const S &p, const swapped_t v); | ||
| 340 | |||
| 341 | template <typename S, typename T2, typename F2> | ||
| 342 | friend S operator%(const S &p, const swapped_t v); | ||
| 343 | |||
| 344 | // Arithmetics + assignements | ||
| 345 | template <typename S, typename T2, typename F2> | ||
| 346 | friend S operator+=(const S &p, const swapped_t v); | ||
| 347 | |||
| 348 | template <typename S, typename T2, typename F2> | ||
| 349 | friend S operator-=(const S &p, const swapped_t v); | ||
| 350 | |||
| 351 | // Bitmath | ||
| 352 | template <typename S, typename T2, typename F2> | ||
| 353 | friend S operator&(const S &p, const swapped_t v); | ||
| 354 | |||
| 355 | // Comparison | ||
| 356 | template <typename S, typename T2, typename F2> | ||
| 357 | friend bool operator<(const S &p, const swapped_t v); | ||
| 358 | |||
| 359 | template <typename S, typename T2, typename F2> | ||
| 360 | friend bool operator>(const S &p, const swapped_t v); | ||
| 361 | |||
| 362 | template <typename S, typename T2, typename F2> | ||
| 363 | friend bool operator<=(const S &p, const swapped_t v); | ||
| 364 | |||
| 365 | template <typename S, typename T2, typename F2> | ||
| 366 | friend bool operator>=(const S &p, const swapped_t v); | ||
| 367 | |||
| 368 | template <typename S, typename T2, typename F2> | ||
| 369 | friend bool operator!=(const S &p, const swapped_t v); | ||
| 370 | |||
| 371 | template <typename S, typename T2, typename F2> | ||
| 372 | friend bool operator==(const S &p, const swapped_t v); | ||
| 373 | }; | ||
| 374 | |||
| 375 | |||
| 376 | // Arithmetics | ||
| 377 | template <typename S, typename T, typename F> | ||
| 378 | S operator+(const S &i, const swap_struct_t<T, F> v) { | ||
| 379 | return i + v.swap(); | ||
| 380 | } | ||
| 381 | |||
| 382 | template <typename S, typename T, typename F> | ||
| 383 | S operator-(const S &i, const swap_struct_t<T, F> v) { | ||
| 384 | return i - v.swap(); | ||
| 385 | } | ||
| 386 | |||
| 387 | template <typename S, typename T, typename F> | ||
| 388 | S operator/(const S &i, const swap_struct_t<T, F> v) { | ||
| 389 | return i / v.swap(); | ||
| 390 | } | ||
| 391 | |||
| 392 | template <typename S, typename T, typename F> | ||
| 393 | S operator*(const S &i, const swap_struct_t<T, F> v) { | ||
| 394 | return i * v.swap(); | ||
| 395 | } | ||
| 396 | |||
| 397 | template <typename S, typename T, typename F> | ||
| 398 | S operator%(const S &i, const swap_struct_t<T, F> v) { | ||
| 399 | return i % v.swap(); | ||
| 400 | } | ||
| 401 | |||
| 402 | // Arithmetics + assignements | ||
| 403 | template <typename S, typename T, typename F> | ||
| 404 | S &operator+=(S &i, const swap_struct_t<T, F> v) { | ||
| 405 | i += v.swap(); | ||
| 406 | return i; | ||
| 407 | } | ||
| 408 | |||
| 409 | template <typename S, typename T, typename F> | ||
| 410 | S &operator-=(S &i, const swap_struct_t<T, F> v) { | ||
| 411 | i -= v.swap(); | ||
| 412 | return i; | ||
| 413 | } | ||
| 414 | |||
| 415 | // Logical | ||
| 416 | template <typename S, typename T, typename F> | ||
| 417 | S operator&(const S &i, const swap_struct_t<T, F> v) { | ||
| 418 | return i & v.swap(); | ||
| 419 | } | ||
| 420 | |||
| 421 | template <typename S, typename T, typename F> | ||
| 422 | S operator&(const swap_struct_t<T, F> v, const S &i) { | ||
| 423 | return (S)(v.swap() & i); | ||
| 424 | } | ||
| 425 | |||
| 426 | |||
| 427 | // Comparaison | ||
| 428 | template <typename S, typename T, typename F> | ||
| 429 | bool operator<(const S &p, const swap_struct_t<T, F> v) { | ||
| 430 | return p < v.swap(); | ||
| 431 | } | ||
| 432 | template <typename S, typename T, typename F> | ||
| 433 | bool operator>(const S &p, const swap_struct_t<T, F> v) { | ||
| 434 | return p > v.swap(); | ||
| 435 | } | ||
| 436 | template <typename S, typename T, typename F> | ||
| 437 | bool operator<=(const S &p, const swap_struct_t<T, F> v) { | ||
| 438 | return p <= v.swap(); | ||
| 439 | } | ||
| 440 | template <typename S, typename T, typename F> | ||
| 441 | bool operator>=(const S &p, const swap_struct_t<T, F> v) { | ||
| 442 | return p >= v.swap(); | ||
| 443 | } | ||
| 444 | template <typename S, typename T, typename F> | ||
| 445 | bool operator!=(const S &p, const swap_struct_t<T, F> v) { | ||
| 446 | return p != v.swap(); | ||
| 447 | } | ||
| 448 | template <typename S, typename T, typename F> | ||
| 449 | bool operator==(const S &p, const swap_struct_t<T, F> v) { | ||
| 450 | return p == v.swap(); | ||
| 451 | } | ||
| 452 | |||
| 453 | template <typename T> | ||
| 454 | struct swap_64_t { | ||
| 455 | static T swap(T x) { | ||
| 456 | return (T)bswap64(*(u64 *)&x); | ||
| 457 | } | ||
| 458 | }; | ||
| 459 | |||
| 460 | template <typename T> | ||
| 461 | struct swap_32_t { | ||
| 462 | static T swap(T x) { | ||
| 463 | return (T)bswap32(*(u32 *)&x); | ||
| 464 | } | ||
| 465 | }; | ||
| 466 | |||
| 467 | template <typename T> | ||
| 468 | struct swap_16_t { | ||
| 469 | static T swap(T x) { | ||
| 470 | return (T)bswap16(*(u16 *)&x); | ||
| 471 | } | ||
| 472 | }; | ||
| 473 | |||
| 474 | template <typename T> | ||
| 475 | struct swap_float_t { | ||
| 476 | static T swap(T x) { | ||
| 477 | return (T)bswapf(*(float *)&x); | ||
| 478 | } | ||
| 479 | }; | ||
| 480 | |||
| 481 | template <typename T> | ||
| 482 | struct swap_double_t { | ||
| 483 | static T swap(T x) { | ||
| 484 | return (T)bswapd(*(double *)&x); | ||
| 485 | } | ||
| 486 | }; | ||
| 487 | |||
| 488 | #if COMMON_LITTLE_ENDIAN | ||
| 489 | typedef u32 u32_le; | ||
| 490 | typedef u16 u16_le; | ||
| 491 | typedef u64 u64_le; | ||
| 492 | |||
| 493 | typedef s32 s32_le; | ||
| 494 | typedef s16 s16_le; | ||
| 495 | typedef s64 s64_le; | ||
| 496 | |||
| 497 | typedef float float_le; | ||
| 498 | typedef double double_le; | ||
| 499 | |||
| 500 | typedef swap_struct_t<u64, swap_64_t<u64>> u64_be; | ||
| 501 | typedef swap_struct_t<s64, swap_64_t<s64>> s64_be; | ||
| 502 | |||
| 503 | typedef swap_struct_t<u32, swap_32_t<u32>> u32_be; | ||
| 504 | typedef swap_struct_t<s32, swap_32_t<s32>> s32_be; | ||
| 505 | |||
| 506 | typedef swap_struct_t<u16, swap_16_t<u16>> u16_be; | ||
| 507 | typedef swap_struct_t<s16, swap_16_t<s16>> s16_be; | ||
| 508 | |||
| 509 | typedef swap_struct_t<float, swap_float_t<float> > float_be; | ||
| 510 | typedef swap_struct_t<double, swap_double_t<double> > double_be; | ||
| 511 | #else | ||
| 512 | |||
| 513 | typedef swap_struct_t<u64, swap_64_t<u64>> u64_le; | ||
| 514 | typedef swap_struct_t<s64, swap_64_t<s64>> s64_le; | ||
| 515 | |||
| 516 | typedef swap_struct_t<u32, swap_32_t<u32>> u32_le; | ||
| 517 | typedef swap_struct_t<s32, swap_32_t<s32>> s32_le; | ||
| 518 | |||
| 519 | typedef swap_struct_t<u16, swap_16_t<u16>> u16_le; | ||
| 520 | typedef swap_struct_t<s16, swap_16_t<s16>> s16_le; | ||
| 521 | |||
| 522 | typedef swap_struct_t<float, swap_float_t<float> > float_le; | ||
| 523 | typedef swap_struct_t<double, swap_double_t<double> > double_le; | ||
| 524 | |||
| 525 | typedef u32 u32_be; | ||
| 526 | typedef u16 u16_be; | ||
| 527 | typedef u64 u64_be; | ||
| 528 | |||
| 529 | typedef s32 s32_be; | ||
| 530 | typedef s16 s16_be; | ||
| 531 | typedef s64 s64_be; | ||
| 532 | |||
| 533 | typedef float float_be; | ||
| 534 | typedef double double_be; | ||
| 535 | #endif \ No newline at end of file | ||