swap.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // Copyright (c) 2012- PPSSPP Project / Dolphin Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0 or later versions.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official git repository and contact information can be found at
  12. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
  13. #pragma once
  14. #include <type_traits>
  15. #if defined(_MSC_VER)
  16. #include <cstdlib>
  17. #endif
  18. #include <cstring>
  19. #include "common/common_types.h"
  20. // GCC
  21. #ifdef __GNUC__
  22. #if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && !defined(COMMON_LITTLE_ENDIAN)
  23. #define COMMON_LITTLE_ENDIAN 1
  24. #elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) && !defined(COMMON_BIG_ENDIAN)
  25. #define COMMON_BIG_ENDIAN 1
  26. #endif
  27. // LLVM/clang
  28. #elif defined(__clang__)
  29. #if __LITTLE_ENDIAN__ && !defined(COMMON_LITTLE_ENDIAN)
  30. #define COMMON_LITTLE_ENDIAN 1
  31. #elif __BIG_ENDIAN__ && !defined(COMMON_BIG_ENDIAN)
  32. #define COMMON_BIG_ENDIAN 1
  33. #endif
  34. // MSVC
  35. #elif defined(_MSC_VER) && !defined(COMMON_BIG_ENDIAN) && !defined(COMMON_LITTLE_ENDIAN)
  36. #define COMMON_LITTLE_ENDIAN 1
  37. #endif
  38. // Worst case, default to little endian.
  39. #if !COMMON_BIG_ENDIAN && !COMMON_LITTLE_ENDIAN
  40. #define COMMON_LITTLE_ENDIAN 1
  41. #endif
  42. namespace Common {
  43. #ifdef _MSC_VER
  44. [[nodiscard]] inline u16 swap16(u16 data) noexcept {
  45. return _byteswap_ushort(data);
  46. }
  47. [[nodiscard]] inline u32 swap32(u32 data) noexcept {
  48. return _byteswap_ulong(data);
  49. }
  50. [[nodiscard]] inline u64 swap64(u64 data) noexcept {
  51. return _byteswap_uint64(data);
  52. }
  53. #elif defined(__clang__) || defined(__GNUC__)
  54. #if defined(__Bitrig__) || defined(__OpenBSD__)
  55. // redefine swap16, swap32, swap64 as inline functions
  56. #undef swap16
  57. #undef swap32
  58. #undef swap64
  59. #endif
  60. [[nodiscard]] inline u16 swap16(u16 data) noexcept {
  61. return __builtin_bswap16(data);
  62. }
  63. [[nodiscard]] inline u32 swap32(u32 data) noexcept {
  64. return __builtin_bswap32(data);
  65. }
  66. [[nodiscard]] inline u64 swap64(u64 data) noexcept {
  67. return __builtin_bswap64(data);
  68. }
  69. #else
  70. // Generic implementation.
  71. [[nodiscard]] inline u16 swap16(u16 data) noexcept {
  72. return (data >> 8) | (data << 8);
  73. }
  74. [[nodiscard]] inline u32 swap32(u32 data) noexcept {
  75. return ((data & 0xFF000000U) >> 24) | ((data & 0x00FF0000U) >> 8) |
  76. ((data & 0x0000FF00U) << 8) | ((data & 0x000000FFU) << 24);
  77. }
  78. [[nodiscard]] inline u64 swap64(u64 data) noexcept {
  79. return ((data & 0xFF00000000000000ULL) >> 56) | ((data & 0x00FF000000000000ULL) >> 40) |
  80. ((data & 0x0000FF0000000000ULL) >> 24) | ((data & 0x000000FF00000000ULL) >> 8) |
  81. ((data & 0x00000000FF000000ULL) << 8) | ((data & 0x0000000000FF0000ULL) << 24) |
  82. ((data & 0x000000000000FF00ULL) << 40) | ((data & 0x00000000000000FFULL) << 56);
  83. }
  84. #endif
  85. [[nodiscard]] inline float swapf(float f) noexcept {
  86. static_assert(sizeof(u32) == sizeof(float), "float must be the same size as uint32_t.");
  87. u32 value;
  88. std::memcpy(&value, &f, sizeof(u32));
  89. value = swap32(value);
  90. std::memcpy(&f, &value, sizeof(u32));
  91. return f;
  92. }
  93. [[nodiscard]] inline double swapd(double f) noexcept {
  94. static_assert(sizeof(u64) == sizeof(double), "double must be the same size as uint64_t.");
  95. u64 value;
  96. std::memcpy(&value, &f, sizeof(u64));
  97. value = swap64(value);
  98. std::memcpy(&f, &value, sizeof(u64));
  99. return f;
  100. }
  101. } // Namespace Common
  102. template <typename T, typename F>
  103. struct swap_struct_t {
  104. using swapped_t = swap_struct_t;
  105. protected:
  106. T value;
  107. static T swap(T v) {
  108. return F::swap(v);
  109. }
  110. public:
  111. T swap() const {
  112. return swap(value);
  113. }
  114. swap_struct_t() = default;
  115. swap_struct_t(const T& v) : value(swap(v)) {}
  116. template <typename S>
  117. swapped_t& operator=(const S& source) {
  118. value = swap(static_cast<T>(source));
  119. return *this;
  120. }
  121. operator s8() const {
  122. return static_cast<s8>(swap());
  123. }
  124. operator u8() const {
  125. return static_cast<u8>(swap());
  126. }
  127. operator s16() const {
  128. return static_cast<s16>(swap());
  129. }
  130. operator u16() const {
  131. return static_cast<u16>(swap());
  132. }
  133. operator s32() const {
  134. return static_cast<s32>(swap());
  135. }
  136. operator u32() const {
  137. return static_cast<u32>(swap());
  138. }
  139. operator s64() const {
  140. return static_cast<s64>(swap());
  141. }
  142. operator u64() const {
  143. return static_cast<u64>(swap());
  144. }
  145. operator float() const {
  146. return static_cast<float>(swap());
  147. }
  148. operator double() const {
  149. return static_cast<double>(swap());
  150. }
  151. // +v
  152. swapped_t operator+() const {
  153. return +swap();
  154. }
  155. // -v
  156. swapped_t operator-() const {
  157. return -swap();
  158. }
  159. // v / 5
  160. swapped_t operator/(const swapped_t& i) const {
  161. return swap() / i.swap();
  162. }
  163. template <typename S>
  164. swapped_t operator/(const S& i) const {
  165. return swap() / i;
  166. }
  167. // v * 5
  168. swapped_t operator*(const swapped_t& i) const {
  169. return swap() * i.swap();
  170. }
  171. template <typename S>
  172. swapped_t operator*(const S& i) const {
  173. return swap() * i;
  174. }
  175. // v + 5
  176. swapped_t operator+(const swapped_t& i) const {
  177. return swap() + i.swap();
  178. }
  179. template <typename S>
  180. swapped_t operator+(const S& i) const {
  181. return swap() + static_cast<T>(i);
  182. }
  183. // v - 5
  184. swapped_t operator-(const swapped_t& i) const {
  185. return swap() - i.swap();
  186. }
  187. template <typename S>
  188. swapped_t operator-(const S& i) const {
  189. return swap() - static_cast<T>(i);
  190. }
  191. // v += 5
  192. swapped_t& operator+=(const swapped_t& i) {
  193. value = swap(swap() + i.swap());
  194. return *this;
  195. }
  196. template <typename S>
  197. swapped_t& operator+=(const S& i) {
  198. value = swap(swap() + static_cast<T>(i));
  199. return *this;
  200. }
  201. // v -= 5
  202. swapped_t& operator-=(const swapped_t& i) {
  203. value = swap(swap() - i.swap());
  204. return *this;
  205. }
  206. template <typename S>
  207. swapped_t& operator-=(const S& i) {
  208. value = swap(swap() - static_cast<T>(i));
  209. return *this;
  210. }
  211. // ++v
  212. swapped_t& operator++() {
  213. value = swap(swap() + 1);
  214. return *this;
  215. }
  216. // --v
  217. swapped_t& operator--() {
  218. value = swap(swap() - 1);
  219. return *this;
  220. }
  221. // v++
  222. swapped_t operator++(int) {
  223. swapped_t old = *this;
  224. value = swap(swap() + 1);
  225. return old;
  226. }
  227. // v--
  228. swapped_t operator--(int) {
  229. swapped_t old = *this;
  230. value = swap(swap() - 1);
  231. return old;
  232. }
  233. // Comparaison
  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. // v != i
  243. bool operator!=(const swapped_t& i) const {
  244. return swap() != i.swap();
  245. }
  246. template <typename S>
  247. bool operator!=(const S& i) const {
  248. return swap() != i;
  249. }
  250. // v > i
  251. bool operator>(const swapped_t& i) const {
  252. return swap() > i.swap();
  253. }
  254. template <typename S>
  255. bool operator>(const S& i) const {
  256. return swap() > i;
  257. }
  258. // v < i
  259. bool operator<(const swapped_t& i) const {
  260. return swap() < i.swap();
  261. }
  262. template <typename S>
  263. bool operator<(const S& i) const {
  264. return swap() < i;
  265. }
  266. // v >= i
  267. bool operator>=(const swapped_t& i) const {
  268. return swap() >= i.swap();
  269. }
  270. template <typename S>
  271. bool operator>=(const S& i) const {
  272. return swap() >= i;
  273. }
  274. // v <= i
  275. bool operator<=(const swapped_t& i) const {
  276. return swap() <= i.swap();
  277. }
  278. template <typename S>
  279. bool operator<=(const S& i) const {
  280. return swap() <= i;
  281. }
  282. // logical
  283. swapped_t operator!() const {
  284. return !swap();
  285. }
  286. // bitmath
  287. swapped_t operator~() const {
  288. return ~swap();
  289. }
  290. swapped_t operator&(const swapped_t& b) const {
  291. return swap() & b.swap();
  292. }
  293. template <typename S>
  294. swapped_t operator&(const S& b) const {
  295. return swap() & b;
  296. }
  297. swapped_t& operator&=(const swapped_t& b) {
  298. value = swap(swap() & b.swap());
  299. return *this;
  300. }
  301. template <typename S>
  302. swapped_t& operator&=(const S b) {
  303. value = swap(swap() & b);
  304. return *this;
  305. }
  306. swapped_t operator|(const swapped_t& b) const {
  307. return swap() | b.swap();
  308. }
  309. template <typename S>
  310. swapped_t operator|(const S& b) const {
  311. return swap() | b;
  312. }
  313. swapped_t& operator|=(const swapped_t& b) {
  314. value = swap(swap() | b.swap());
  315. return *this;
  316. }
  317. template <typename S>
  318. swapped_t& operator|=(const S& b) {
  319. value = swap(swap() | b);
  320. return *this;
  321. }
  322. swapped_t operator^(const swapped_t& b) const {
  323. return swap() ^ b.swap();
  324. }
  325. template <typename S>
  326. swapped_t operator^(const S& b) const {
  327. return swap() ^ b;
  328. }
  329. swapped_t& operator^=(const swapped_t& b) {
  330. value = swap(swap() ^ b.swap());
  331. return *this;
  332. }
  333. template <typename S>
  334. swapped_t& operator^=(const S& b) {
  335. value = swap(swap() ^ b);
  336. return *this;
  337. }
  338. template <typename S>
  339. swapped_t operator<<(const S& b) const {
  340. return swap() << b;
  341. }
  342. template <typename S>
  343. swapped_t& operator<<=(const S& b) const {
  344. value = swap(swap() << b);
  345. return *this;
  346. }
  347. template <typename S>
  348. swapped_t operator>>(const S& b) const {
  349. return swap() >> b;
  350. }
  351. template <typename S>
  352. swapped_t& operator>>=(const S& b) const {
  353. value = swap(swap() >> b);
  354. return *this;
  355. }
  356. // Member
  357. /** todo **/
  358. // Arithmetics
  359. template <typename S, typename T2, typename F2>
  360. friend S operator+(const S& p, const swapped_t v);
  361. template <typename S, typename T2, typename F2>
  362. friend S operator-(const S& p, const swapped_t v);
  363. template <typename S, typename T2, typename F2>
  364. friend S operator/(const S& p, const swapped_t v);
  365. template <typename S, typename T2, typename F2>
  366. friend S operator*(const S& p, const swapped_t v);
  367. template <typename S, typename T2, typename F2>
  368. friend S operator%(const S& p, const swapped_t v);
  369. // Arithmetics + assignements
  370. template <typename S, typename T2, typename F2>
  371. friend S operator+=(const S& p, const swapped_t v);
  372. template <typename S, typename T2, typename F2>
  373. friend S operator-=(const S& p, const swapped_t v);
  374. // Bitmath
  375. template <typename S, typename T2, typename F2>
  376. friend S operator&(const S& p, const swapped_t v);
  377. // Comparison
  378. template <typename S, typename T2, typename F2>
  379. friend bool operator<(const S& p, const swapped_t v);
  380. template <typename S, typename T2, typename F2>
  381. friend bool operator>(const S& p, const swapped_t v);
  382. template <typename S, typename T2, typename F2>
  383. friend bool operator<=(const S& p, const swapped_t v);
  384. template <typename S, typename T2, typename F2>
  385. friend bool operator>=(const S& p, const swapped_t v);
  386. template <typename S, typename T2, typename F2>
  387. friend bool operator!=(const S& p, const swapped_t v);
  388. template <typename S, typename T2, typename F2>
  389. friend bool operator==(const S& p, const swapped_t v);
  390. };
  391. // Arithmetics
  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. template <typename S, typename T, typename F>
  397. S operator-(const S& i, const swap_struct_t<T, F> v) {
  398. return i - v.swap();
  399. }
  400. template <typename S, typename T, typename F>
  401. S operator/(const S& i, const swap_struct_t<T, F> v) {
  402. return i / v.swap();
  403. }
  404. template <typename S, typename T, typename F>
  405. S operator*(const S& i, const swap_struct_t<T, F> v) {
  406. return i * v.swap();
  407. }
  408. template <typename S, typename T, typename F>
  409. S operator%(const S& i, const swap_struct_t<T, F> v) {
  410. return i % v.swap();
  411. }
  412. // Arithmetics + assignements
  413. template <typename S, typename T, typename F>
  414. S& operator+=(S& i, const swap_struct_t<T, F> v) {
  415. i += v.swap();
  416. return i;
  417. }
  418. template <typename S, typename T, typename F>
  419. S& operator-=(S& i, const swap_struct_t<T, F> v) {
  420. i -= v.swap();
  421. return i;
  422. }
  423. // Logical
  424. template <typename S, typename T, typename F>
  425. S operator&(const S& i, const swap_struct_t<T, F> v) {
  426. return i & v.swap();
  427. }
  428. template <typename S, typename T, typename F>
  429. S operator&(const swap_struct_t<T, F> v, const S& i) {
  430. return static_cast<S>(v.swap() & i);
  431. }
  432. // Comparaison
  433. template <typename S, typename T, typename F>
  434. bool operator<(const S& p, const swap_struct_t<T, F> v) {
  435. return p < v.swap();
  436. }
  437. template <typename S, typename T, typename F>
  438. bool operator>(const S& p, const swap_struct_t<T, F> v) {
  439. return p > v.swap();
  440. }
  441. template <typename S, typename T, typename F>
  442. bool operator<=(const S& p, const swap_struct_t<T, F> v) {
  443. return p <= v.swap();
  444. }
  445. template <typename S, typename T, typename F>
  446. bool operator>=(const S& p, const swap_struct_t<T, F> v) {
  447. return p >= v.swap();
  448. }
  449. template <typename S, typename T, typename F>
  450. bool operator!=(const S& p, const swap_struct_t<T, F> v) {
  451. return p != v.swap();
  452. }
  453. template <typename S, typename T, typename F>
  454. bool operator==(const S& p, const swap_struct_t<T, F> v) {
  455. return p == v.swap();
  456. }
  457. template <typename T>
  458. struct swap_64_t {
  459. static T swap(T x) {
  460. return static_cast<T>(Common::swap64(x));
  461. }
  462. };
  463. template <typename T>
  464. struct swap_32_t {
  465. static T swap(T x) {
  466. return static_cast<T>(Common::swap32(x));
  467. }
  468. };
  469. template <typename T>
  470. struct swap_16_t {
  471. static T swap(T x) {
  472. return static_cast<T>(Common::swap16(x));
  473. }
  474. };
  475. template <typename T>
  476. struct swap_float_t {
  477. static T swap(T x) {
  478. return static_cast<T>(Common::swapf(x));
  479. }
  480. };
  481. template <typename T>
  482. struct swap_double_t {
  483. static T swap(T x) {
  484. return static_cast<T>(Common::swapd(x));
  485. }
  486. };
  487. template <typename T>
  488. struct swap_enum_t {
  489. static_assert(std::is_enum_v<T>);
  490. using base = std::underlying_type_t<T>;
  491. public:
  492. swap_enum_t() = default;
  493. swap_enum_t(const T& v) : value(swap(v)) {}
  494. swap_enum_t& operator=(const T& v) {
  495. value = swap(v);
  496. return *this;
  497. }
  498. operator T() const {
  499. return swap(value);
  500. }
  501. explicit operator base() const {
  502. return static_cast<base>(swap(value));
  503. }
  504. protected:
  505. T value{};
  506. // clang-format off
  507. using swap_t = std::conditional_t<
  508. std::is_same_v<base, u16>, swap_16_t<u16>, std::conditional_t<
  509. std::is_same_v<base, s16>, swap_16_t<s16>, std::conditional_t<
  510. std::is_same_v<base, u32>, swap_32_t<u32>, std::conditional_t<
  511. std::is_same_v<base, s32>, swap_32_t<s32>, std::conditional_t<
  512. std::is_same_v<base, u64>, swap_64_t<u64>, std::conditional_t<
  513. std::is_same_v<base, s64>, swap_64_t<s64>, void>>>>>>;
  514. // clang-format on
  515. static T swap(T x) {
  516. return static_cast<T>(swap_t::swap(static_cast<base>(x)));
  517. }
  518. };
  519. struct SwapTag {}; // Use the different endianness from the system
  520. struct KeepTag {}; // Use the same endianness as the system
  521. template <typename T, typename Tag>
  522. struct AddEndian;
  523. // KeepTag specializations
  524. template <typename T>
  525. struct AddEndian<T, KeepTag> {
  526. using type = T;
  527. };
  528. // SwapTag specializations
  529. template <>
  530. struct AddEndian<u8, SwapTag> {
  531. using type = u8;
  532. };
  533. template <>
  534. struct AddEndian<u16, SwapTag> {
  535. using type = swap_struct_t<u16, swap_16_t<u16>>;
  536. };
  537. template <>
  538. struct AddEndian<u32, SwapTag> {
  539. using type = swap_struct_t<u32, swap_32_t<u32>>;
  540. };
  541. template <>
  542. struct AddEndian<u64, SwapTag> {
  543. using type = swap_struct_t<u64, swap_64_t<u64>>;
  544. };
  545. template <>
  546. struct AddEndian<s8, SwapTag> {
  547. using type = s8;
  548. };
  549. template <>
  550. struct AddEndian<s16, SwapTag> {
  551. using type = swap_struct_t<s16, swap_16_t<s16>>;
  552. };
  553. template <>
  554. struct AddEndian<s32, SwapTag> {
  555. using type = swap_struct_t<s32, swap_32_t<s32>>;
  556. };
  557. template <>
  558. struct AddEndian<s64, SwapTag> {
  559. using type = swap_struct_t<s64, swap_64_t<s64>>;
  560. };
  561. template <>
  562. struct AddEndian<float, SwapTag> {
  563. using type = swap_struct_t<float, swap_float_t<float>>;
  564. };
  565. template <>
  566. struct AddEndian<double, SwapTag> {
  567. using type = swap_struct_t<double, swap_double_t<double>>;
  568. };
  569. template <typename T>
  570. struct AddEndian<T, SwapTag> {
  571. static_assert(std::is_enum_v<T>);
  572. using type = swap_enum_t<T>;
  573. };
  574. // Alias LETag/BETag as KeepTag/SwapTag depending on the system
  575. #if COMMON_LITTLE_ENDIAN
  576. using LETag = KeepTag;
  577. using BETag = SwapTag;
  578. #else
  579. using BETag = KeepTag;
  580. using LETag = SwapTag;
  581. #endif
  582. // Aliases for LE types
  583. using u16_le = AddEndian<u16, LETag>::type;
  584. using u32_le = AddEndian<u32, LETag>::type;
  585. using u64_le = AddEndian<u64, LETag>::type;
  586. using s16_le = AddEndian<s16, LETag>::type;
  587. using s32_le = AddEndian<s32, LETag>::type;
  588. using s64_le = AddEndian<s64, LETag>::type;
  589. template <typename T>
  590. using enum_le = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, LETag>::type>;
  591. using float_le = AddEndian<float, LETag>::type;
  592. using double_le = AddEndian<double, LETag>::type;
  593. // Aliases for BE types
  594. using u16_be = AddEndian<u16, BETag>::type;
  595. using u32_be = AddEndian<u32, BETag>::type;
  596. using u64_be = AddEndian<u64, BETag>::type;
  597. using s16_be = AddEndian<s16, BETag>::type;
  598. using s32_be = AddEndian<s32, BETag>::type;
  599. using s64_be = AddEndian<s64, BETag>::type;
  600. template <typename T>
  601. using enum_be = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, BETag>::type>;
  602. using float_be = AddEndian<float, BETag>::type;
  603. using double_be = AddEndian<double, BETag>::type;