summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Lioncash2016-05-08 23:21:44 -0400
committerGravatar Lioncash2016-05-08 23:21:47 -0400
commit47ca79ba4bc7e5c18cf9427c975a789efd65a414 (patch)
treef54015b536c5fc10f784e7264eedc7488ddc91b1
parentswap: Remove unused methods (diff)
downloadyuzu-47ca79ba4bc7e5c18cf9427c975a789efd65a414.tar.gz
yuzu-47ca79ba4bc7e5c18cf9427c975a789efd65a414.tar.xz
yuzu-47ca79ba4bc7e5c18cf9427c975a789efd65a414.zip
swap: Get rid of undefined behavior in swapf and swapd
This isn't well-defined in C++.
-rw-r--r--src/common/swap.h32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/common/swap.h b/src/common/swap.h
index f51751d29..beedd6c7e 100644
--- a/src/common/swap.h
+++ b/src/common/swap.h
@@ -25,6 +25,8 @@
25 #include <sys/endian.h> 25 #include <sys/endian.h>
26#endif 26#endif
27 27
28#include <cstring>
29
28#include "common/common_types.h" 30#include "common/common_types.h"
29 31
30// GCC 4.6+ 32// GCC 4.6+
@@ -89,27 +91,29 @@ inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 3
89#endif 91#endif
90 92
91inline float swapf(float f) { 93inline float swapf(float f) {
92 union { 94 static_assert(sizeof(u32) == sizeof(float),
93 float f; 95 "float must be the same size as uint32_t.");
94 unsigned int u32; 96
95 } dat1, dat2; 97 u32 value;
98 std::memcpy(&value, &f, sizeof(u32));
96 99
97 dat1.f = f; 100 value = swap32(value);
98 dat2.u32 = swap32(dat1.u32); 101 std::memcpy(&f, &value, sizeof(u32));
99 102
100 return dat2.f; 103 return f;
101} 104}
102 105
103inline double swapd(double f) { 106inline double swapd(double f) {
104 union { 107 static_assert(sizeof(u64) == sizeof(double),
105 double f; 108 "double must be the same size as uint64_t.");
106 unsigned long long u64; 109
107 } dat1, dat2; 110 u64 value;
111 std::memcpy(&value, &f, sizeof(u64));
108 112
109 dat1.f = f; 113 value = swap64(value);
110 dat2.u64 = swap64(dat1.u64); 114 std::memcpy(&f, &value, sizeof(u64));
111 115
112 return dat2.f; 116 return f;
113} 117}
114 118
115} // Namespace Common 119} // Namespace Common