forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath3D.cpp
271 lines (233 loc) · 5.59 KB
/
Math3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Math3D.h"
namespace Math3D {
template<>
float Vec2<float>::Length() const
{
#if defined(_M_SSE)
float ret;
__m128 xy = _mm_loadu_ps(&x);
__m128 sq = _mm_mul_ps(xy, xy);
const __m128 r2 = _mm_shuffle_ps(sq, sq, _MM_SHUFFLE(0, 0, 0, 1));
const __m128 res = _mm_add_ss(sq, r2);
_mm_store_ss(&ret, _mm_sqrt_ss(res));
return ret;
#else
return sqrtf(Length2());
#endif
}
template<>
void Vec2<float>::SetLength(const float l)
{
(*this) *= l / Length();
}
template<>
Vec2<float> Vec2<float>::WithLength(const float l) const
{
return (*this) * l / Length();
}
template<>
float Vec2<float>::Distance2To(Vec2<float> &other)
{
return Vec2<float>(other-(*this)).Length2();
}
template<>
Vec2<float> Vec2<float>::Normalized() const
{
return (*this) / Length();
}
template<>
float Vec2<float>::Normalize()
{
float len = Length();
(*this) = (*this)/len;
return len;
}
template<>
float Vec3<float>::Length() const
{
#if defined(_M_SSE)
float ret;
__m128 xyz = _mm_loadu_ps(&x);
__m128 sq = _mm_mul_ps(xyz, xyz);
const __m128 r2 = _mm_shuffle_ps(sq, sq, _MM_SHUFFLE(0, 0, 0, 1));
const __m128 r3 = _mm_shuffle_ps(sq, sq, _MM_SHUFFLE(0, 0, 0, 2));
const __m128 res = _mm_add_ss(sq, _mm_add_ss(r2, r3));
_mm_store_ss(&ret, _mm_sqrt_ss(res));
return ret;
#else
return sqrtf(Length2());
#endif
}
template<>
void Vec3<float>::SetLength(const float l)
{
(*this) *= l / Length();
}
template<>
Vec3<float> Vec3<float>::WithLength(const float l) const
{
return (*this) * l / Length();
}
template<>
float Vec3<float>::Distance2To(Vec3<float> &other)
{
return Vec3<float>(other-(*this)).Length2();
}
#if defined(_M_SSE)
__m128 SSENormalizeMultiplierSSE2(__m128 v)
{
const __m128 sq = _mm_mul_ps(v, v);
const __m128 r2 = _mm_shuffle_ps(sq, sq, _MM_SHUFFLE(0, 0, 0, 1));
const __m128 r3 = _mm_shuffle_ps(sq, sq, _MM_SHUFFLE(0, 0, 0, 2));
const __m128 res = _mm_add_ss(r3, _mm_add_ss(r2, sq));
const __m128 rt = _mm_rsqrt_ss(res);
return _mm_shuffle_ps(rt, rt, _MM_SHUFFLE(0, 0, 0, 0));
}
#if _M_SSE >= 0x401
__m128 SSENormalizeMultiplierSSE4(__m128 v)
{
return _mm_rsqrt_ps(_mm_dp_ps(v, v, 0xFF));
}
__m128 SSENormalizeMultiplier(bool useSSE4, __m128 v)
{
if (useSSE4)
return SSENormalizeMultiplierSSE4(v);
return SSENormalizeMultiplierSSE2(v);
}
#else
__m128 SSENormalizeMultiplier(bool useSSE4, __m128 v)
{
return SSENormalizeMultiplierSSE2(v);
}
#endif
template<>
Vec3<float> Vec3<float>::Normalized(bool useSSE4) const
{
const __m128 normalize = SSENormalizeMultiplier(useSSE4, vec);
return _mm_mul_ps(normalize, vec);
}
#else
template<>
Vec3<float> Vec3<float>::Normalized(bool useSSE4) const
{
return (*this) / Length();
}
#endif
template<>
float Vec3<float>::Normalize()
{
float len = Length();
(*this) = (*this)/len;
return len;
}
template<>
Vec3Packed<float> Vec3Packed<float>::FromRGB(unsigned int rgb)
{
return Vec3Packed((rgb & 0xFF) * (1.0f/255.0f),
((rgb >> 8) & 0xFF) * (1.0f/255.0f),
((rgb >> 16) & 0xFF) * (1.0f/255.0f));
}
template<>
Vec3Packed<int> Vec3Packed<int>::FromRGB(unsigned int rgb)
{
return Vec3Packed(rgb & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 16) & 0xFF);
}
template<>
unsigned int Vec3Packed<float>::ToRGB() const
{
return ((unsigned int)(r()*255.f)) +
((unsigned int)(g()*255.f*256.f)) +
((unsigned int)(b()*255.f*256.f*256.f));
}
template<>
unsigned int Vec3Packed<int>::ToRGB() const
{
return (r()&0xFF) | ((g()&0xFF)<<8) | ((b()&0xFF)<<16);
}
template<>
float Vec3Packed<float>::Length() const
{
return sqrtf(Length2());
}
template<>
void Vec3Packed<float>::SetLength(const float l)
{
(*this) *= l / Length();
}
template<>
Vec3Packed<float> Vec3Packed<float>::WithLength(const float l) const
{
return (*this) * l / Length();
}
template<>
float Vec3Packed<float>::Distance2To(Vec3Packed<float> &other)
{
return Vec3Packed<float>(other-(*this)).Length2();
}
template<>
Vec3Packed<float> Vec3Packed<float>::Normalized() const
{
return (*this) / Length();
}
template<>
float Vec3Packed<float>::Normalize()
{
float len = Length();
(*this) = (*this)/len;
return len;
}
template<>
float Vec4<float>::Length() const
{
#if defined(_M_SSE)
float ret;
__m128 xyzw = _mm_loadu_ps(&x);
__m128 sq = _mm_mul_ps(xyzw, xyzw);
const __m128 r2 = _mm_add_ps(sq, _mm_movehl_ps(sq, sq));
const __m128 res = _mm_add_ss(r2, _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(0, 0, 0, 1)));
_mm_store_ss(&ret, _mm_sqrt_ss(res));
return ret;
#else
return sqrtf(Length2());
#endif
}
template<>
void Vec4<float>::SetLength(const float l)
{
(*this) *= l / Length();
}
template<>
Vec4<float> Vec4<float>::WithLength(const float l) const
{
return (*this) * l / Length();
}
template<>
float Vec4<float>::Distance2To(Vec4<float> &other)
{
return Vec4<float>(other-(*this)).Length2();
}
template<>
Vec4<float> Vec4<float>::Normalized() const
{
return (*this) / Length();
}
template<>
float Vec4<float>::Normalize()
{
float len = Length();
(*this) = (*this)/len;
return len;
}
}; // namespace Math3D