diff options
| author | 2015-03-05 22:46:45 -0800 | |
|---|---|---|
| committer | 2015-03-05 22:46:45 -0800 | |
| commit | e011acaa84a6c2a60959a4b1ecd7e73e349605f2 (patch) | |
| tree | bea33469eebc06d4e72b1961f689cbc8b3df076d /src | |
| parent | Merge pull request #615 from Subv/services (diff) | |
| download | yuzu-e011acaa84a6c2a60959a4b1ecd7e73e349605f2.tar.gz yuzu-e011acaa84a6c2a60959a4b1ecd7e73e349605f2.tar.xz yuzu-e011acaa84a6c2a60959a4b1ecd7e73e349605f2.zip | |
Removed swap code redundancy and moved common swap code to swap.h
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/common.h | 36 | ||||
| -rw-r--r-- | src/common/common_funcs.h | 77 | ||||
| -rw-r--r-- | src/common/swap.h | 111 |
3 files changed, 97 insertions, 127 deletions
diff --git a/src/common/common.h b/src/common/common.h index 948dc536a..f7d0f55c5 100644 --- a/src/common/common.h +++ b/src/common/common.h | |||
| @@ -117,40 +117,4 @@ enum EMUSTATE_CHANGE | |||
| 117 | EMUSTATE_CHANGE_STOP | 117 | EMUSTATE_CHANGE_STOP |
| 118 | }; | 118 | }; |
| 119 | 119 | ||
| 120 | |||
| 121 | #ifdef _MSC_VER | ||
| 122 | inline unsigned long long bswap64(unsigned long long x) { return _byteswap_uint64(x); } | ||
| 123 | inline unsigned int bswap32(unsigned int x) { return _byteswap_ulong(x); } | ||
| 124 | inline unsigned short bswap16(unsigned short x) { return _byteswap_ushort(x); } | ||
| 125 | #else | ||
| 126 | // TODO: speedup | ||
| 127 | inline unsigned short bswap16(unsigned short x) { return (x << 8) | (x >> 8); } | ||
| 128 | inline unsigned int bswap32(unsigned int x) { return (x >> 24) | ((x & 0xFF0000) >> 8) | ((x & 0xFF00) << 8) | (x << 24);} | ||
| 129 | inline unsigned long long bswap64(unsigned long long x) {return ((unsigned long long)bswap32(x) << 32) | bswap32(x >> 32); } | ||
| 130 | #endif | ||
| 131 | |||
| 132 | inline float bswapf(float f) { | ||
| 133 | union { | ||
| 134 | float f; | ||
| 135 | unsigned int u32; | ||
| 136 | } dat1, dat2; | ||
| 137 | |||
| 138 | dat1.f = f; | ||
| 139 | dat2.u32 = bswap32(dat1.u32); | ||
| 140 | |||
| 141 | return dat2.f; | ||
| 142 | } | ||
| 143 | |||
| 144 | inline double bswapd(double f) { | ||
| 145 | union { | ||
| 146 | double f; | ||
| 147 | unsigned long long u64; | ||
| 148 | } dat1, dat2; | ||
| 149 | |||
| 150 | dat1.f = f; | ||
| 151 | dat2.u64 = bswap64(dat1.u64); | ||
| 152 | |||
| 153 | return dat2.f; | ||
| 154 | } | ||
| 155 | |||
| 156 | #include "swap.h" | 120 | #include "swap.h" |
diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index d56156e4a..e76cb7d68 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h | |||
| @@ -37,11 +37,6 @@ | |||
| 37 | #ifndef _MSC_VER | 37 | #ifndef _MSC_VER |
| 38 | 38 | ||
| 39 | #include <errno.h> | 39 | #include <errno.h> |
| 40 | #ifdef __linux__ | ||
| 41 | #include <byteswap.h> | ||
| 42 | #elif defined __FreeBSD__ | ||
| 43 | #include <sys/endian.h> | ||
| 44 | #endif | ||
| 45 | 40 | ||
| 46 | #if defined(__x86_64__) || defined(_M_X64) | 41 | #if defined(__x86_64__) || defined(_M_X64) |
| 47 | #define Crash() __asm__ __volatile__("int $3") | 42 | #define Crash() __asm__ __volatile__("int $3") |
| @@ -145,75 +140,3 @@ inline u64 _rotr64(u64 x, unsigned int shift){ | |||
| 145 | // This function might change the error code. | 140 | // This function might change the error code. |
| 146 | // Defined in Misc.cpp. | 141 | // Defined in Misc.cpp. |
| 147 | const char* GetLastErrorMsg(); | 142 | const char* GetLastErrorMsg(); |
| 148 | |||
| 149 | namespace Common | ||
| 150 | { | ||
| 151 | inline u8 swap8(u8 _data) {return _data;} | ||
| 152 | inline u32 swap24(const u8* _data) {return (_data[0] << 16) | (_data[1] << 8) | _data[2];} | ||
| 153 | |||
| 154 | #ifdef ANDROID | ||
| 155 | #undef swap16 | ||
| 156 | #undef swap32 | ||
| 157 | #undef swap64 | ||
| 158 | #endif | ||
| 159 | |||
| 160 | #ifdef _MSC_VER | ||
| 161 | inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);} | ||
| 162 | inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);} | ||
| 163 | inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);} | ||
| 164 | #elif _M_ARM | ||
| 165 | inline u16 swap16 (u16 _data) { u32 data = _data; __asm__ ("rev16 %0, %1\n" : "=l" (data) : "l" (data)); return (u16)data;} | ||
| 166 | inline u32 swap32 (u32 _data) {__asm__ ("rev %0, %1\n" : "=l" (_data) : "l" (_data)); return _data;} | ||
| 167 | inline u64 swap64(u64 _data) {return ((u64)swap32(_data) << 32) | swap32(_data >> 32);} | ||
| 168 | #elif __linux__ | ||
| 169 | inline u16 swap16(u16 _data) {return bswap_16(_data);} | ||
| 170 | inline u32 swap32(u32 _data) {return bswap_32(_data);} | ||
| 171 | inline u64 swap64(u64 _data) {return bswap_64(_data);} | ||
| 172 | #elif __APPLE__ | ||
| 173 | inline __attribute__((always_inline)) u16 swap16(u16 _data) | ||
| 174 | {return (_data >> 8) | (_data << 8);} | ||
| 175 | inline __attribute__((always_inline)) u32 swap32(u32 _data) | ||
| 176 | {return __builtin_bswap32(_data);} | ||
| 177 | inline __attribute__((always_inline)) u64 swap64(u64 _data) | ||
| 178 | {return __builtin_bswap64(_data);} | ||
| 179 | #elif __FreeBSD__ | ||
| 180 | inline u16 swap16(u16 _data) {return bswap16(_data);} | ||
| 181 | inline u32 swap32(u32 _data) {return bswap32(_data);} | ||
| 182 | inline u64 swap64(u64 _data) {return bswap64(_data);} | ||
| 183 | #else | ||
| 184 | // Slow generic implementation. | ||
| 185 | inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);} | ||
| 186 | inline u32 swap32(u32 data) {return (swap16(data) << 16) | swap16(data >> 16);} | ||
| 187 | inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 32);} | ||
| 188 | #endif | ||
| 189 | |||
| 190 | inline u16 swap16(const u8* _pData) {return swap16(*(const u16*)_pData);} | ||
| 191 | inline u32 swap32(const u8* _pData) {return swap32(*(const u32*)_pData);} | ||
| 192 | inline u64 swap64(const u8* _pData) {return swap64(*(const u64*)_pData);} | ||
| 193 | |||
| 194 | template <int count> | ||
| 195 | void swap(u8*); | ||
| 196 | |||
| 197 | template <> | ||
| 198 | inline void swap<1>(u8* data) | ||
| 199 | {} | ||
| 200 | |||
| 201 | template <> | ||
| 202 | inline void swap<2>(u8* data) | ||
| 203 | { | ||
| 204 | *reinterpret_cast<u16*>(data) = swap16(data); | ||
| 205 | } | ||
| 206 | |||
| 207 | template <> | ||
| 208 | inline void swap<4>(u8* data) | ||
| 209 | { | ||
| 210 | *reinterpret_cast<u32*>(data) = swap32(data); | ||
| 211 | } | ||
| 212 | |||
| 213 | template <> | ||
| 214 | inline void swap<8>(u8* data) | ||
| 215 | { | ||
| 216 | *reinterpret_cast<u64*>(data) = swap64(data); | ||
| 217 | } | ||
| 218 | |||
| 219 | } // Namespace Common | ||
diff --git a/src/common/swap.h b/src/common/swap.h index e2d918362..7e37655bf 100644 --- a/src/common/swap.h +++ b/src/common/swap.h | |||
| @@ -17,18 +17,14 @@ | |||
| 17 | 17 | ||
| 18 | #pragma once | 18 | #pragma once |
| 19 | 19 | ||
| 20 | // Android | 20 | #if defined(__linux__) |
| 21 | #if defined(ANDROID) | 21 | #include <byteswap.h> |
| 22 | #elif defined(__FreeBSD__) | ||
| 22 | #include <sys/endian.h> | 23 | #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 | 24 | #endif |
| 29 | 25 | ||
| 30 | // GCC 4.6+ | 26 | // GCC 4.6+ |
| 31 | #elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) | 27 | #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 32 | 28 | ||
| 33 | #if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && !defined(COMMON_LITTLE_ENDIAN) | 29 | #if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && !defined(COMMON_LITTLE_ENDIAN) |
| 34 | #define COMMON_LITTLE_ENDIAN 1 | 30 | #define COMMON_LITTLE_ENDIAN 1 |
| @@ -49,7 +45,6 @@ | |||
| 49 | #elif defined(_MSC_VER) && !defined(COMMON_BIG_ENDIAN) && !defined(COMMON_LITTLE_ENDIAN) | 45 | #elif defined(_MSC_VER) && !defined(COMMON_BIG_ENDIAN) && !defined(COMMON_LITTLE_ENDIAN) |
| 50 | 46 | ||
| 51 | #define COMMON_LITTLE_ENDIAN 1 | 47 | #define COMMON_LITTLE_ENDIAN 1 |
| 52 | |||
| 53 | #endif | 48 | #endif |
| 54 | 49 | ||
| 55 | // Worst case, default to little endian. | 50 | // Worst case, default to little endian. |
| @@ -57,6 +52,93 @@ | |||
| 57 | #define COMMON_LITTLE_ENDIAN 1 | 52 | #define COMMON_LITTLE_ENDIAN 1 |
| 58 | #endif | 53 | #endif |
| 59 | 54 | ||
| 55 | namespace Common { | ||
| 56 | |||
| 57 | inline u8 swap8(u8 _data) {return _data;} | ||
| 58 | inline u32 swap24(const u8* _data) {return (_data[0] << 16) | (_data[1] << 8) | _data[2];} | ||
| 59 | |||
| 60 | #ifdef _MSC_VER | ||
| 61 | inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);} | ||
| 62 | inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);} | ||
| 63 | inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);} | ||
| 64 | #elif _M_ARM | ||
| 65 | inline u16 swap16 (u16 _data) { u32 data = _data; __asm__ ("rev16 %0, %1\n" : "=l" (data) : "l" (data)); return (u16)data;} | ||
| 66 | inline u32 swap32 (u32 _data) {__asm__ ("rev %0, %1\n" : "=l" (_data) : "l" (_data)); return _data;} | ||
| 67 | inline u64 swap64(u64 _data) {return ((u64)swap32(_data) << 32) | swap32(_data >> 32);} | ||
| 68 | #elif __linux__ | ||
| 69 | inline u16 swap16(u16 _data) {return bswap_16(_data);} | ||
| 70 | inline u32 swap32(u32 _data) {return bswap_32(_data);} | ||
| 71 | inline u64 swap64(u64 _data) {return bswap_64(_data);} | ||
| 72 | #elif __APPLE__ | ||
| 73 | inline __attribute__((always_inline)) u16 swap16(u16 _data) | ||
| 74 | {return (_data >> 8) | (_data << 8);} | ||
| 75 | inline __attribute__((always_inline)) u32 swap32(u32 _data) | ||
| 76 | {return __builtin_bswap32(_data);} | ||
| 77 | inline __attribute__((always_inline)) u64 swap64(u64 _data) | ||
| 78 | {return __builtin_bswap64(_data);} | ||
| 79 | #elif __FreeBSD__ | ||
| 80 | inline u16 swap16(u16 _data) {return bswap16(_data);} | ||
| 81 | inline u32 swap32(u32 _data) {return bswap32(_data);} | ||
| 82 | inline u64 swap64(u64 _data) {return bswap64(_data);} | ||
| 83 | #else | ||
| 84 | // Slow generic implementation. | ||
| 85 | inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);} | ||
| 86 | inline u32 swap32(u32 data) {return (swap16(data) << 16) | swap16(data >> 16);} | ||
| 87 | inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 32);} | ||
| 88 | #endif | ||
| 89 | |||
| 90 | inline float swapf(float f) { | ||
| 91 | union { | ||
| 92 | float f; | ||
| 93 | unsigned int u32; | ||
| 94 | } dat1, dat2; | ||
| 95 | |||
| 96 | dat1.f = f; | ||
| 97 | dat2.u32 = swap32(dat1.u32); | ||
| 98 | |||
| 99 | return dat2.f; | ||
| 100 | } | ||
| 101 | |||
| 102 | inline double swapd(double f) { | ||
| 103 | union { | ||
| 104 | double f; | ||
| 105 | unsigned long long u64; | ||
| 106 | } dat1, dat2; | ||
| 107 | |||
| 108 | dat1.f = f; | ||
| 109 | dat2.u64 = swap64(dat1.u64); | ||
| 110 | |||
| 111 | return dat2.f; | ||
| 112 | } | ||
| 113 | |||
| 114 | inline u16 swap16(const u8* _pData) {return swap16(*(const u16*)_pData);} | ||
| 115 | inline u32 swap32(const u8* _pData) {return swap32(*(const u32*)_pData);} | ||
| 116 | inline u64 swap64(const u8* _pData) {return swap64(*(const u64*)_pData);} | ||
| 117 | |||
| 118 | template <int count> | ||
| 119 | void swap(u8*); | ||
| 120 | |||
| 121 | template <> | ||
| 122 | inline void swap<1>(u8* data) { } | ||
| 123 | |||
| 124 | template <> | ||
| 125 | inline void swap<2>(u8* data) { | ||
| 126 | *reinterpret_cast<u16*>(data) = swap16(data); | ||
| 127 | } | ||
| 128 | |||
| 129 | template <> | ||
| 130 | inline void swap<4>(u8* data) { | ||
| 131 | *reinterpret_cast<u32*>(data) = swap32(data); | ||
| 132 | } | ||
| 133 | |||
| 134 | template <> | ||
| 135 | inline void swap<8>(u8* data) { | ||
| 136 | *reinterpret_cast<u64*>(data) = swap64(data); | ||
| 137 | } | ||
| 138 | |||
| 139 | } // Namespace Common | ||
| 140 | |||
| 141 | |||
| 60 | template <typename T, typename F> | 142 | template <typename T, typename F> |
| 61 | struct swap_struct_t { | 143 | struct swap_struct_t { |
| 62 | typedef swap_struct_t<T, F> swapped_t; | 144 | typedef swap_struct_t<T, F> swapped_t; |
| @@ -448,35 +530,35 @@ bool operator==(const S &p, const swap_struct_t<T, F> v) { | |||
| 448 | template <typename T> | 530 | template <typename T> |
| 449 | struct swap_64_t { | 531 | struct swap_64_t { |
| 450 | static T swap(T x) { | 532 | static T swap(T x) { |
| 451 | return (T)bswap64(*(u64 *)&x); | 533 | return (T)Common::swap64(*(u64 *)&x); |
| 452 | } | 534 | } |
| 453 | }; | 535 | }; |
| 454 | 536 | ||
| 455 | template <typename T> | 537 | template <typename T> |
| 456 | struct swap_32_t { | 538 | struct swap_32_t { |
| 457 | static T swap(T x) { | 539 | static T swap(T x) { |
| 458 | return (T)bswap32(*(u32 *)&x); | 540 | return (T)Common::swap32(*(u32 *)&x); |
| 459 | } | 541 | } |
| 460 | }; | 542 | }; |
| 461 | 543 | ||
| 462 | template <typename T> | 544 | template <typename T> |
| 463 | struct swap_16_t { | 545 | struct swap_16_t { |
| 464 | static T swap(T x) { | 546 | static T swap(T x) { |
| 465 | return (T)bswap16(*(u16 *)&x); | 547 | return (T)Common::swap16(*(u16 *)&x); |
| 466 | } | 548 | } |
| 467 | }; | 549 | }; |
| 468 | 550 | ||
| 469 | template <typename T> | 551 | template <typename T> |
| 470 | struct swap_float_t { | 552 | struct swap_float_t { |
| 471 | static T swap(T x) { | 553 | static T swap(T x) { |
| 472 | return (T)bswapf(*(float *)&x); | 554 | return (T)Common::swapf(*(float *)&x); |
| 473 | } | 555 | } |
| 474 | }; | 556 | }; |
| 475 | 557 | ||
| 476 | template <typename T> | 558 | template <typename T> |
| 477 | struct swap_double_t { | 559 | struct swap_double_t { |
| 478 | static T swap(T x) { | 560 | static T swap(T x) { |
| 479 | return (T)bswapd(*(double *)&x); | 561 | return (T)Common::swapd(*(double *)&x); |
| 480 | } | 562 | } |
| 481 | }; | 563 | }; |
| 482 | 564 | ||
| @@ -527,4 +609,5 @@ typedef s64 s64_be; | |||
| 527 | 609 | ||
| 528 | typedef float float_be; | 610 | typedef float float_be; |
| 529 | typedef double double_be; | 611 | typedef double double_be; |
| 612 | |||
| 530 | #endif | 613 | #endif |