NW4F G3d
Loading...
Searching...
No Matches
g3d_MathCommon-inl.h
Go to the documentation of this file.
1#include <cmath>
2#include <algorithm>
3#include <limits>
4
5#if defined( __ghs__ )
6#include <ppc_ghs.h>
7#endif
8
9#include <nw/g3d/ut/g3d_Inlines.h>
10
11namespace nw { namespace g3d { namespace math {
12
13//--------------------------------------------------------------------------------------------------
14
16float Math::Abs(float x)
17{
18 return std::fabs(x);
19}
20
22float Math::NAbs(float x)
23{
24 return -std::fabs(x);
25}
26
28float Math::Mod(float x, float y)
29{
30 return std::fmod(x, y);
31}
32
34float Math::Select(float cond, float pos, float neg)
35{
36#if defined( __ghs__ )
37 return __FSEL(cond, pos, neg);
38#else
39 return cond >= 0.0f ? pos : neg;
40#endif
41}
42
44float Math::Min(float x, float y)
45{
46 return Select(x - y, y, x);
47}
48
50float Math::Max(float x, float y)
51{
52 return Select(x - y, x, y);
53}
54
56float Math::Clamp(float x, float minVal, float maxVal)
57{
58 return Max(minVal, Min(maxVal, x));
59}
60
61//--------------------------------------------------------------------------------------------------
62
63template <typename T>
65T Math::Ceil(float x)
66{
67 return StaticCast<T>(std::ceil(x));
68}
69
70template <typename T>
72T Math::Floor(float x)
73{
75 {
76 return StaticCast<T>(std::floor(x));
77 }
78 else
79 {
80 return Trunc<T>(x);
81 }
82}
83
84template <typename T>
86T Math::Trunc(float x)
87{
88 return StaticCast<T>(x);
89}
90
91template <>
93float Math::Trunc<float>(float x)
94{
95 float out;
96 std::modff(x, &out);
97 return out;
98}
99
100template <typename T>
102T Math::Round(float x)
103{
105 {
106 return StaticCast<T>(x + Select(x, 0.5f, -0.5f));
107 }
108 else
109 {
110 return StaticCast<T>(x);
111 }
112}
113
114template <>
116float Math::Round<float>(float x)
117{
118 float out;
119 std::modff(x + Select(x, 0.5f, -0.5f), &out);
120 return out;
121}
122
123
125float Math::Ceil(float x) { return Ceil<float>(x); }
126
128float Math::Floor(float x) { return Floor<float>(x); }
129
131float Math::Trunc(float x) { return Trunc<float>(x); }
132
134float Math::Round(float x) { return Round<float>(x); }
135
136//--------------------------------------------------------------------------------------------------
137
139float Math::Rcp(float x)
140{
141#if defined( __ghs__ )
142 float inv = __FRES(x);
143 return (inv + inv) - (x * inv) * inv;
144#else
145 return 1.0f / x;
146#endif
147}
148
150float Math::RSqrt(float x)
151{
152#if defined( __ghs__ )
153 float half = 0.5f * x;
154 float rsqrt = __FRSQRTE(x);
155 rsqrt = rsqrt * (1.5f - half * rsqrt * rsqrt);
156 return rsqrt;
157#else
158 return 1.0f / std::sqrt(x);
159#endif
160}
161
163float Math::Sqrt(float x)
164{
165#if defined( __ghs__ )
166 return x == 0.0f ? 0.0f : x * RSqrt(x);
167#else
168 return std::sqrt(x);
169#endif
170}
171
173float Math::Exp(float x)
174{
175 return std::exp(x);
176}
177
179float Math::Log(float x)
180{
181 return std::log(x);
182}
183
185float Math::Log10(float x)
186{
187 return std::log10(x);
188}
189
190//--------------------------------------------------------------------------------------------------
191
193float Math::Sin(float rad)
194{
195 return SinIdx(RadToIdx(rad));
196}
197
199float Math::Cos(float rad)
200{
201 return CosIdx(RadToIdx(rad));
202}
203
205float Math::Tan(float rad)
206{
207 return TanIdx(RadToIdx(rad));
208}
209
211float Math::Asin(float x)
212{
213 NW_G3D_ASSERT(-1 <= x && x <= 1);
214 return std::asin(x);
215}
216
218float Math::Acos(float x)
219{
220 NW_G3D_ASSERT(-1 <= x && x <= 1);
221 return std::acos(x);
222}
223
225float Math::Atan(float x)
226{
227 NW_G3D_ASSERT(-1 <= x && x <= 1);
228 return std::atan(x);
229}
230
232float Math::Atan2(float y, float x)
233{
234 return std::atan2(y, x);
235}
236
238void Math::SinCos(float* pSin, float* pCos, float rad)
239{
241}
242
243//--------------------------------------------------------------------------------------------------
244
247{
249
251 float fraction = static_cast<float>(idx & Sample::FRAC_MASK) / static_cast<float>(Sample::FRAC_SIZE);
253
255}
256
259{
261
263 float fraction = static_cast<float>(idx & Sample::FRAC_MASK) / static_cast<float>(Sample::FRAC_SIZE);
265
267}
268
271{
272 float fSin, fCos;
273 SinCosIdx(&fSin, &fCos, idx);
274 return fSin * Rcp(fCos);
275}
276
279{
280 NW_G3D_ASSERT(-1 <= x && x <= 1);
281 return RadToIdx(std::asin(x));
282}
283
286{
287 NW_G3D_ASSERT(-1 <= x && x <= 1);
288 return RadToIdx(std::acos(x));
289}
290
293{
294 NW_G3D_ASSERT(-1 <= x && x <= 1);
295 return RadToIdx(std::atan(x));
296}
297
299u32 Math::Atan2Idx(float y, float x)
300{
301 return RadToIdx(std::atan2(y, x));
302}
303
305void Math::SinCosIdx(float* pSin, float* pCos, u32 idx)
306{
310
312 float fraction = static_cast<float>(idx & Sample::FRAC_MASK) / static_cast<float>(Sample::FRAC_SIZE);
314
317}
318
319//--------------------------------------------------------------------------------------------------
320
321#if defined( __ghs__ )
322
325{
326 f32x2 inv = __PS_RES(x);
327 return __PS_NMSUB(__PS_MUL(x, inv), inv, __PS_ADD(inv, inv));
328}
329
332{
334 f32x2 half = __PS_MULS0F(x, 0.5f);
335 f32x2 onehalf = __PS_FDUP(1.5f);
337}
338
340f32x2 Math::SinCos(float rad)
341{
342 return SinCosIdx(RadToIdx(rad));
343}
344
347{
349
351
352 u16 fracU16 = (idx >> 8) & 0xFFFF;
354 fraction = __PS_MULS0F(fraction, static_cast<float>(1.0f / 0xFFFF));
355
357 const f32x2& value = reinterpret_cast<const f32x2&>(sample.sinValue);
358 const f32x2& delta = reinterpret_cast<const f32x2&>(sample.sinDelta);
360}
361
362#endif // defined( __ghs__ )
363
364//--------------------------------------------------------------------------------------------------
365
367bool Math::IsNaN(float x)
368{
369 return x != x;
370}
371
372} } } // namespace nw::g3d::math
#define NW_G3D_ASSERT_NOT_NULL(exp)
Definition g3d_assert.h:20
#define NW_G3D_ASSERT(exp)
Definition g3d_assert.h:17
#define NW_G3D_STATIC_CONDITION(exp)
Definition g3d_defs.h:75
#define NW_G3D_MATH_INLINE
Definition g3d_defs.h:69
Definition g3d_MathCommon.h:6
Definition g3d_GfxManage.cpp:10