orx  stable
Portable Game Engine
orxMath.h
Go to the documentation of this file.
1 /* Orx - Portable Game Engine
2  *
3  * Copyright (c) 2008-2017 Orx-Project
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  *
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  * misrepresented as being the original software.
20  *
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  */
24 
43 #ifndef _orxMATH_H_
44 #define _orxMATH_H_
45 
46 
47 #include "orxInclude.h"
48 
51 #include <math.h>
52 
53 
63 #define orxLERP(A, B, T) ((A) + ((T) * ((B) - (A))))
64 
65 
71 #define orxMIN(A, B) (((A) > (B)) ? (B) : (A))
72 
78 #define orxMAX(A, B) (((A) < (B)) ? (B) : (A))
79 
86 #define orxCLAMP(V, MIN, MAX) orxMAX(orxMIN(V, MAX), MIN)
87 
92 #define orxF2U(V) ((orxU32) (V))
93 
98 #define orxF2S(V) ((orxS32) (V))
99 
104 #define orxU2F(V) ((orxFLOAT)(V))
105 
110 #define orxS2F(V) ((orxFLOAT)(V))
111 
112 
113 /*** Module functions *** */
114 
118 extern orxDLLAPI void orxFASTCALL orxMath_InitRandom(orxU32 _u32Seed);
119 
125 extern orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax);
126 
132 extern orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max);
133 
139 extern orxDLLAPI orxS32 orxFASTCALL orxMath_GetRandomS32(orxS32 _s32Min, orxS32 _s32Max);
140 
146 extern orxDLLAPI orxU64 orxFASTCALL orxMath_GetRandomU64(orxU64 _u64Min, orxU64 _u64Max);
147 
153 extern orxDLLAPI orxS64 orxFASTCALL orxMath_GetRandomS64(orxS64 _s64Min, orxS64 _s64Max);
154 
158 extern orxDLLAPI void orxFASTCALL orxMath_GetRandomSeeds(orxU32 _au32Seeds[4]);
159 
163 extern orxDLLAPI void orxFASTCALL orxMath_SetRandomSeeds(const orxU32 _au32Seeds[4]);
164 
165 
166 /*** Inlined functions *** */
167 
172 static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
173 {
174  _u32Value -= (_u32Value >> 1) & 0x55555555;
175  _u32Value = (_u32Value & 0x33333333) + ((_u32Value >> 2) & 0x33333333);
176  _u32Value = (((_u32Value + (_u32Value >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
177 
178  /* Done! */
179  return _u32Value;
180 }
181 
186 static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
187 {
188  /* De Bruijn multiply look up table */
189  static const orxU32 sau32DeBruijnLUT[32] =
190  {
191  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
192  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
193  };
194 
195  /* Done! */
196  return sau32DeBruijnLUT[(((orxU32)((orxS32)_u32Value & -(orxS32)_u32Value)) * 0x077CB531U) >> 27];
197 }
198 
203 static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
204 {
205  orxBOOL bResult;
206 
207  /* Updates result */
208  bResult = ((_u32Value & (_u32Value - 1)) == 0) ? orxTRUE : orxFALSE;
209 
210  /* Done! */
211  return bResult;
212 }
213 
218 static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
219 {
220  orxU32 u32Result;
221 
222  /* Non-zero? */
223  if(_u32Value != 0)
224  {
225  /* Updates result */
226  u32Result = _u32Value - 1;
227  u32Result = u32Result | (u32Result >> 1);
228  u32Result = u32Result | (u32Result >> 2);
229  u32Result = u32Result | (u32Result >> 4);
230  u32Result = u32Result | (u32Result >> 8);
231  u32Result = u32Result | (u32Result >> 16);
232  u32Result++;
233  }
234  else
235  {
236  /* Updates result */
237  u32Result = 1;
238  }
239 
240  /* Done! */
241  return u32Result;
242 }
243 
250 static orxINLINE orxFLOAT orxMath_SmoothStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
251 {
252  orxFLOAT fTemp, fResult;
253 
254  /* Gets normalized and clamped value */
255  fTemp = (_fValue - _fMin) / (_fMax - _fMin);
256  fTemp = orxCLAMP(fTemp, orxFLOAT_0, orxFLOAT_1);
257 
258  /* Gets smoothed result */
259  fResult = fTemp * fTemp * (orx2F(3.0f) - (orx2F(2.0f) * fTemp));
260 
261  /* Done! */
262  return fResult;
263 }
264 
271 static orxINLINE orxFLOAT orxMath_SmootherStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
272 {
273  orxFLOAT fTemp, fResult;
274 
275  /* Gets normalized and clamped value */
276  fTemp = (_fValue - _fMin) / (_fMax - _fMin);
277  fTemp = orxCLAMP(fTemp, orxFLOAT_0, orxFLOAT_1);
278 
279  /* Gets smoothed result */
280  fResult = fTemp * fTemp * fTemp * (fTemp * ((fTemp * orx2F(6.0f)) - orx2F(15.0f)) + orx2F(10.0f));
281 
282  /* Done! */
283  return fResult;
284 }
285 
286 
287 /*** Math Definitions ***/
288 
289 #define orxMATH_KF_SQRT_2 orx2F(1.414213562f)
290 #define orxMATH_KF_EPSILON orx2F(0.0001f)
291 #define orxMATH_KF_TINY_EPSILON orx2F(1.0e-037f)
292 #define orxMATH_KF_2_PI orx2F(6.283185307f)
293 #define orxMATH_KF_PI orx2F(3.141592654f)
294 #define orxMATH_KF_PI_BY_2 orx2F(1.570796327f)
295 #define orxMATH_KF_PI_BY_4 orx2F(0.785398163f)
296 #define orxMATH_KF_DEG_TO_RAD orx2F(3.141592654f / 180.0f)
297 #define orxMATH_KF_RAD_TO_DEG orx2F(180.0f / 3.141592654f)
300 /*** Trigonometric function ***/
301 
306 static orxINLINE orxFLOAT orxMath_Sin(orxFLOAT _fOp)
307 {
308  orxFLOAT fResult;
309 
310  /* Updates result */
311  fResult = sinf(_fOp);
312 
313  /* Done! */
314  return fResult;
315 }
316 
321 static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
322 {
323  orxFLOAT fResult;
324 
325  /* Updates result */
326  fResult = cosf(_fOp);
327 
328  /* Done! */
329  return fResult;
330 }
331 
336 static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
337 {
338  orxFLOAT fResult;
339 
340  /* Updates result */
341  fResult = tanf(_fOp);
342 
343  /* Done! */
344  return fResult;
345 }
346 
351 static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
352 {
353  orxFLOAT fResult;
354 
355  /* Updates result */
356  fResult = acosf(_fOp);
357 
358  /* Done! */
359  return fResult;
360 }
361 
366 static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
367 {
368  orxFLOAT fResult;
369 
370  /* Updates result */
371  fResult = asinf(_fOp);
372 
373  /* Done! */
374  return fResult;
375 }
376 
382 static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
383 {
384  orxFLOAT fResult;
385 
386  /* Updates result */
387  fResult = atan2f(_fOp1, _fOp2);
388 
389  /* Done! */
390  return fResult;
391 }
392 
393 
394 /*** Misc functions ***/
395 
400 static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
401 {
402  orxFLOAT fResult;
403 
404  /* Updates result */
405  fResult = sqrtf(_fOp);
406 
407  /* Done! */
408  return fResult;
409 }
410 
415 static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
416 {
417  orxFLOAT fResult;
418 
419  /* Updates result */
420  fResult = floorf(_fOp);
421 
422  /* Done! */
423  return fResult;
424 }
425 
430 static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
431 {
432  orxFLOAT fResult;
433 
434  /* Updates result */
435  fResult = ceilf(_fOp);
436 
437  /* Done! */
438  return fResult;
439 }
440 
445 static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
446 {
447  orxFLOAT fResult;
448 
449 #ifdef __orxMSVC__
450 
451  /* Updates result */
452  fResult = floorf(_fOp + orx2F(0.5f));
453 
454 #else /* __orxMSVC__ */
455 
456  /* Updates result */
457  fResult = rintf(_fOp);
458 
459 #endif /* __orxMSVC__ */
460 
461  /* Done! */
462  return fResult;
463 }
464 
470 static orxINLINE orxFLOAT orxMath_Mod(orxFLOAT _fOp1, orxFLOAT _fOp2)
471 {
472  orxFLOAT fResult;
473 
474  /* Updates result */
475  fResult = fmodf(_fOp1, _fOp2);
476 
477  /* Done! */
478  return fResult;
479 }
480 
486 static orxINLINE orxFLOAT orxMath_Pow(orxFLOAT _fOp, orxFLOAT _fExp)
487 {
488  orxFLOAT fResult;
489 
490  /* Updates result */
491  fResult = powf(_fOp, _fExp);
492 
493  /* Done! */
494  return fResult;
495 }
496 
501 static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
502 {
503  orxFLOAT fResult;
504 
505  /* Updates result */
506  fResult = fabsf(_fOp);
507 
508  /* Done! */
509  return fResult;
510 }
511 
512 #endif /* _orxMATH_H_ */
513 
static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
Definition: orxMath.h:430
static orxINLINE orxFLOAT orxMath_SmoothStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
Definition: orxMath.h:250
static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
Definition: orxMath.h:445
static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
Definition: orxMath.h:172
static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
Definition: orxMath.h:186
static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
Definition: orxMath.h:366
#define orxTRUE
Definition: orxType.h:186
static const orxFLOAT orxFLOAT_1
Definition: orxType.h:191
#define orxCLAMP(V, MIN, MAX)
Definition: orxMath.h:86
static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
Definition: orxMath.h:400
#define orxFALSE
Definition: orxType.h:185
orxDLLAPI orxS32 orxFASTCALL orxMath_GetRandomS32(orxS32 _s32Min, orxS32 _s32Max)
static orxINLINE orxFLOAT orxMath_Sin(orxFLOAT _fOp)
Definition: orxMath.h:306
static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
Definition: orxMath.h:415
static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
Definition: orxMath.h:501
static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
Definition: orxMath.h:218
static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
Definition: orxMath.h:351
orxDLLAPI void orxFASTCALL orxMath_SetRandomSeeds(const orxU32 _au32Seeds[4])
static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
Definition: orxMath.h:321
orxDLLAPI void orxFASTCALL orxMath_GetRandomSeeds(orxU32 _au32Seeds[4])
orxDLLAPI void orxFASTCALL orxMath_InitRandom(orxU32 _u32Seed)
static orxINLINE orxFLOAT orxMath_Mod(orxFLOAT _fOp1, orxFLOAT _fOp2)
Definition: orxMath.h:470
orxDLLAPI orxU64 orxFASTCALL orxMath_GetRandomU64(orxU64 _u64Min, orxU64 _u64Max)
#define orxDLLAPI
Definition: orxDecl.h:387
static orxINLINE orxFLOAT orxMath_SmootherStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
Definition: orxMath.h:271
orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax)
orxDLLAPI orxS64 orxFASTCALL orxMath_GetRandomS64(orxS64 _s64Min, orxS64 _s64Max)
static orxINLINE orxFLOAT orxMath_Pow(orxFLOAT _fOp, orxFLOAT _fExp)
Definition: orxMath.h:486
static const orxFLOAT orxFLOAT_0
Definition: orxType.h:190
static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
Definition: orxMath.h:382
orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max)
static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
Definition: orxMath.h:336
static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
Definition: orxMath.h:203

Generated for orx by doxygen 1.5.6