summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Emmanuel Gil Peyrot2015-05-14 16:16:15 +0200
committerGravatar Emmanuel Gil Peyrot2015-05-14 16:16:15 +0200
commita31fd7f86bea249135b66010569c22982e0015af (patch)
treeb06cde4fa721c4c096e07c6154437e3da30b6a20
parentdyncom: Removed irrelevant log. (diff)
downloadyuzu-a31fd7f86bea249135b66010569c22982e0015af.tar.gz
yuzu-a31fd7f86bea249135b66010569c22982e0015af.tar.xz
yuzu-a31fd7f86bea249135b66010569c22982e0015af.zip
Common: Remove unused cruft from math_util, and remove a duplicated Rect class in common_types.
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/common_types.h26
-rw-r--r--src/common/math_util.cpp211
-rw-r--r--src/common/math_util.h174
4 files changed, 3 insertions, 409 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 11659c3c5..f8fc6450f 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -9,7 +9,6 @@ set(SRCS
9 logging/filter.cpp 9 logging/filter.cpp
10 logging/text_formatter.cpp 10 logging/text_formatter.cpp
11 logging/backend.cpp 11 logging/backend.cpp
12 math_util.cpp
13 memory_util.cpp 12 memory_util.cpp
14 misc.cpp 13 misc.cpp
15 profiler.cpp 14 profiler.cpp
diff --git a/src/common/common_types.h b/src/common/common_types.h
index 644709ba6..f6de0adfc 100644
--- a/src/common/common_types.h
+++ b/src/common/common_types.h
@@ -87,29 +87,3 @@ protected:
87 NonCopyable(NonCopyable&) = delete; 87 NonCopyable(NonCopyable&) = delete;
88 NonCopyable& operator=(NonCopyable&) = delete; 88 NonCopyable& operator=(NonCopyable&) = delete;
89}; 89};
90
91namespace Common {
92/// Rectangle data structure
93class Rect {
94public:
95 Rect(int x0=0, int y0=0, int x1=0, int y1=0) {
96 x0_ = x0;
97 y0_ = y0;
98 x1_ = x1;
99 y1_ = y1;
100 }
101 ~Rect() { }
102
103 int x0_; ///< Rect top left X-coordinate
104 int y0_; ///< Rect top left Y-coordinate
105 int x1_; ///< Rect bottom left X-coordinate
106 int y1_; ///< Rect bottom right Y-coordinate
107
108 inline u32 width() const { return std::abs(x1_ - x0_); }
109 inline u32 height() const { return std::abs(y1_ - y0_); }
110
111 inline bool operator == (const Rect& val) const {
112 return (x0_ == val.x0_ && y0_ == val.y0_ && x1_ == val.x1_ && y1_ == val.y1_);
113 }
114};
115}
diff --git a/src/common/math_util.cpp b/src/common/math_util.cpp
deleted file mode 100644
index bcb70cae5..000000000
--- a/src/common/math_util.cpp
+++ /dev/null
@@ -1,211 +0,0 @@
1// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <numeric> // Necessary on OS X, but not Linux
7
8#include "common/common_types.h"
9#include "common/math_util.h"
10
11namespace MathUtil
12{
13
14u32 ClassifyDouble(double dvalue)
15{
16 // TODO: Optimize the below to be as fast as possible.
17 IntDouble value;
18 value.d = dvalue;
19 u64 sign = value.i & DOUBLE_SIGN;
20 u64 exp = value.i & DOUBLE_EXP;
21 if (exp > DOUBLE_ZERO && exp < DOUBLE_EXP)
22 {
23 // Nice normalized number.
24 return sign ? PPC_FPCLASS_NN : PPC_FPCLASS_PN;
25 }
26 else
27 {
28 u64 mantissa = value.i & DOUBLE_FRAC;
29 if (mantissa)
30 {
31 if (exp)
32 {
33 return PPC_FPCLASS_QNAN;
34 }
35 else
36 {
37 // Denormalized number.
38 return sign ? PPC_FPCLASS_ND : PPC_FPCLASS_PD;
39 }
40 }
41 else if (exp)
42 {
43 //Infinite
44 return sign ? PPC_FPCLASS_NINF : PPC_FPCLASS_PINF;
45 }
46 else
47 {
48 //Zero
49 return sign ? PPC_FPCLASS_NZ : PPC_FPCLASS_PZ;
50 }
51 }
52}
53
54u32 ClassifyFloat(float fvalue)
55{
56 // TODO: Optimize the below to be as fast as possible.
57 IntFloat value;
58 value.f = fvalue;
59 u32 sign = value.i & FLOAT_SIGN;
60 u32 exp = value.i & FLOAT_EXP;
61 if (exp > FLOAT_ZERO && exp < FLOAT_EXP)
62 {
63 // Nice normalized number.
64 return sign ? PPC_FPCLASS_NN : PPC_FPCLASS_PN;
65 }
66 else
67 {
68 u32 mantissa = value.i & FLOAT_FRAC;
69 if (mantissa)
70 {
71 if (exp)
72 {
73 return PPC_FPCLASS_QNAN; // Quiet NAN
74 }
75 else
76 {
77 // Denormalized number.
78 return sign ? PPC_FPCLASS_ND : PPC_FPCLASS_PD;
79 }
80 }
81 else if (exp)
82 {
83 // Infinite
84 return sign ? PPC_FPCLASS_NINF : PPC_FPCLASS_PINF;
85 }
86 else
87 {
88 //Zero
89 return sign ? PPC_FPCLASS_NZ : PPC_FPCLASS_PZ;
90 }
91 }
92}
93
94
95} // namespace
96
97inline void MatrixMul(int n, const float *a, const float *b, float *result)
98{
99 for (int i = 0; i < n; ++i)
100 {
101 for (int j = 0; j < n; ++j)
102 {
103 float temp = 0;
104 for (int k = 0; k < n; ++k)
105 {
106 temp += a[i * n + k] * b[k * n + j];
107 }
108 result[i * n + j] = temp;
109 }
110 }
111}
112
113// Calculate sum of a float list
114float MathFloatVectorSum(const std::vector<float>& Vec)
115{
116 return std::accumulate(Vec.begin(), Vec.end(), 0.0f);
117}
118
119void Matrix33::LoadIdentity(Matrix33 &mtx)
120{
121 memset(mtx.data, 0, sizeof(mtx.data));
122 mtx.data[0] = 1.0f;
123 mtx.data[4] = 1.0f;
124 mtx.data[8] = 1.0f;
125}
126
127void Matrix33::RotateX(Matrix33 &mtx, float rad)
128{
129 float s = sin(rad);
130 float c = cos(rad);
131 memset(mtx.data, 0, sizeof(mtx.data));
132 mtx.data[0] = 1;
133 mtx.data[4] = c;
134 mtx.data[5] = -s;
135 mtx.data[7] = s;
136 mtx.data[8] = c;
137}
138void Matrix33::RotateY(Matrix33 &mtx, float rad)
139{
140 float s = sin(rad);
141 float c = cos(rad);
142 memset(mtx.data, 0, sizeof(mtx.data));
143 mtx.data[0] = c;
144 mtx.data[2] = s;
145 mtx.data[4] = 1;
146 mtx.data[6] = -s;
147 mtx.data[8] = c;
148}
149
150void Matrix33::Multiply(const Matrix33 &a, const Matrix33 &b, Matrix33 &result)
151{
152 MatrixMul(3, a.data, b.data, result.data);
153}
154
155void Matrix33::Multiply(const Matrix33 &a, const float vec[3], float result[3])
156{
157 for (int i = 0; i < 3; ++i) {
158 result[i] = 0;
159 for (int k = 0; k < 3; ++k) {
160 result[i] += a.data[i * 3 + k] * vec[k];
161 }
162 }
163}
164
165void Matrix44::LoadIdentity(Matrix44 &mtx)
166{
167 memset(mtx.data, 0, sizeof(mtx.data));
168 mtx.data[0] = 1.0f;
169 mtx.data[5] = 1.0f;
170 mtx.data[10] = 1.0f;
171 mtx.data[15] = 1.0f;
172}
173
174void Matrix44::LoadMatrix33(Matrix44 &mtx, const Matrix33 &m33)
175{
176 for (int i = 0; i < 3; ++i)
177 {
178 for (int j = 0; j < 3; ++j)
179 {
180 mtx.data[i * 4 + j] = m33.data[i * 3 + j];
181 }
182 }
183
184 for (int i = 0; i < 3; ++i)
185 {
186 mtx.data[i * 4 + 3] = 0;
187 mtx.data[i + 12] = 0;
188 }
189 mtx.data[15] = 1.0f;
190}
191
192void Matrix44::Set(Matrix44 &mtx, const float mtxArray[16])
193{
194 for(int i = 0; i < 16; ++i) {
195 mtx.data[i] = mtxArray[i];
196 }
197}
198
199void Matrix44::Translate(Matrix44 &mtx, const float vec[3])
200{
201 LoadIdentity(mtx);
202 mtx.data[3] = vec[0];
203 mtx.data[7] = vec[1];
204 mtx.data[11] = vec[2];
205}
206
207void Matrix44::Multiply(const Matrix44 &a, const Matrix44 &b, Matrix44 &result)
208{
209 MatrixMul(4, a.data, b.data, result.data);
210}
211
diff --git a/src/common/math_util.h b/src/common/math_util.h
index 52f579cf7..0b1400b41 100644
--- a/src/common/math_util.h
+++ b/src/common/math_util.h
@@ -4,11 +4,9 @@
4 4
5#pragma once 5#pragma once
6 6
7#include "common/common_types.h"
8
9#include <algorithm> 7#include <algorithm>
8#include <cstdlib>
10#include <type_traits> 9#include <type_traits>
11#include <vector>
12 10
13namespace MathUtil 11namespace MathUtil
14{ 12{
@@ -19,83 +17,6 @@ inline T Clamp(const T val, const T& min, const T& max)
19 return std::max(min, std::min(max, val)); 17 return std::max(min, std::min(max, val));
20} 18}
21 19
22static const u64 DOUBLE_SIGN = 0x8000000000000000ULL,
23 DOUBLE_EXP = 0x7FF0000000000000ULL,
24 DOUBLE_FRAC = 0x000FFFFFFFFFFFFFULL,
25 DOUBLE_ZERO = 0x0000000000000000ULL;
26
27static const u32 FLOAT_SIGN = 0x80000000,
28 FLOAT_EXP = 0x7F800000,
29 FLOAT_FRAC = 0x007FFFFF,
30 FLOAT_ZERO = 0x00000000;
31
32union IntDouble {
33 double d;
34 u64 i;
35};
36union IntFloat {
37 float f;
38 u32 i;
39};
40
41inline bool IsNAN(double d)
42{
43 IntDouble x; x.d = d;
44 return ( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) &&
45 ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) );
46}
47
48inline bool IsQNAN(double d)
49{
50 IntDouble x; x.d = d;
51 return ( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) &&
52 ((x.i & 0x0007fffffffffffULL) == 0x000000000000000ULL) &&
53 ((x.i & 0x000800000000000ULL) == 0x000800000000000ULL) );
54}
55
56inline bool IsSNAN(double d)
57{
58 IntDouble x; x.d = d;
59 return( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) &&
60 ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
61 ((x.i & 0x0008000000000000ULL) == DOUBLE_ZERO) );
62}
63
64inline float FlushToZero(float f)
65{
66 IntFloat x; x.f = f;
67 if ((x.i & FLOAT_EXP) == 0)
68 x.i &= FLOAT_SIGN; // turn into signed zero
69 return x.f;
70}
71
72inline double FlushToZeroAsFloat(double d)
73{
74 IntDouble x; x.d = d;
75 if ((x.i & DOUBLE_EXP) < 0x3800000000000000ULL)
76 x.i &= DOUBLE_SIGN; // turn into signed zero
77 return x.d;
78}
79
80enum PPCFpClass
81{
82 PPC_FPCLASS_QNAN = 0x11,
83 PPC_FPCLASS_NINF = 0x9,
84 PPC_FPCLASS_NN = 0x8,
85 PPC_FPCLASS_ND = 0x18,
86 PPC_FPCLASS_NZ = 0x12,
87 PPC_FPCLASS_PZ = 0x2,
88 PPC_FPCLASS_PD = 0x14,
89 PPC_FPCLASS_PN = 0x4,
90 PPC_FPCLASS_PINF = 0x5,
91};
92
93// Uses PowerPC conventions for the return value, so it can be easily
94// used directly in CPU emulation.
95u32 ClassifyDouble(double dvalue);
96// More efficient float version.
97u32 ClassifyFloat(float fvalue);
98
99template<class T> 20template<class T>
100struct Rectangle 21struct Rectangle
101{ 22{
@@ -104,101 +25,12 @@ struct Rectangle
104 T right; 25 T right;
105 T bottom; 26 T bottom;
106 27
107 Rectangle() 28 Rectangle() {}
108 { }
109 29
110 Rectangle(T theLeft, T theTop, T theRight, T theBottom) 30 Rectangle(T left, T top, T right, T bottom) : left(left), top(top), right(right), bottom(bottom) {}
111 : left(theLeft), top(theTop), right(theRight), bottom(theBottom)
112 { }
113
114 bool operator==(const Rectangle& r) { return left==r.left && top==r.top && right==r.right && bottom==r.bottom; }
115 31
116 T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); } 32 T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); }
117 T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); } 33 T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); }
118
119 // If the rectangle is in a coordinate system with a lower-left origin, use
120 // this Clamp.
121 void ClampLL(T x1, T y1, T x2, T y2)
122 {
123 if (left < x1) left = x1;
124 if (right > x2) right = x2;
125 if (top > y1) top = y1;
126 if (bottom < y2) bottom = y2;
127 }
128
129 // If the rectangle is in a coordinate system with an upper-left origin,
130 // use this Clamp.
131 void ClampUL(T x1, T y1, T x2, T y2)
132 {
133 if (left < x1) left = x1;
134 if (right > x2) right = x2;
135 if (top < y1) top = y1;
136 if (bottom > y2) bottom = y2;
137 }
138}; 34};
139 35
140} // namespace MathUtil 36} // namespace MathUtil
141
142inline float pow2f(float x) {return x * x;}
143inline double pow2(double x) {return x * x;}
144
145float MathFloatVectorSum(const std::vector<float>&);
146
147#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
148#define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
149
150// Rounds down. 0 -> undefined
151inline u64 Log2(u64 val)
152{
153#if defined(__GNUC__)
154 return 63 - __builtin_clzll(val);
155
156#elif defined(_MSC_VER) && defined(_M_X64)
157 unsigned long result = -1;
158 _BitScanReverse64(&result, val);
159 return result;
160
161#else
162 u64 result = -1;
163 while (val != 0)
164 {
165 val >>= 1;
166 ++result;
167 }
168 return result;
169#endif
170}
171
172// Tiny matrix/vector library.
173// Used for things like Free-Look in the gfx backend.
174
175class Matrix33
176{
177public:
178 static void LoadIdentity(Matrix33 &mtx);
179
180 // set mtx to be a rotation matrix around the x axis
181 static void RotateX(Matrix33 &mtx, float rad);
182 // set mtx to be a rotation matrix around the y axis
183 static void RotateY(Matrix33 &mtx, float rad);
184
185 // set result = a x b
186 static void Multiply(const Matrix33 &a, const Matrix33 &b, Matrix33 &result);
187 static void Multiply(const Matrix33 &a, const float vec[3], float result[3]);
188
189 float data[9];
190};
191
192class Matrix44
193{
194public:
195 static void LoadIdentity(Matrix44 &mtx);
196 static void LoadMatrix33(Matrix44 &mtx, const Matrix33 &m33);
197 static void Set(Matrix44 &mtx, const float mtxArray[16]);
198
199 static void Translate(Matrix44 &mtx, const float vec[3]);
200
201 static void Multiply(const Matrix44 &a, const Matrix44 &b, Matrix44 &result);
202
203 float data[16];
204};