orx  1.9
Portable Game Engine
orxMath.h
Go to the documentation of this file.
1 /* Orx - Portable Game Engine
2  *
3  * Copyright (c) 2008-2018 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 #include "orxInclude.h"
47 #include "debug/orxDebug.h"
48 
51 #include <math.h>
52 
53 #ifdef __orxMSVC__
54  #ifdef NO_WIN32_LEAN_AND_MEAN
55  #undef WIN32_LEAN_AND_MEAN
56  #else /* NO_WIN32_LEAN_AND_MEAN */
57  #ifndef WIN32_LEAN_AND_MEAN
58  #define WIN32_LEAN_AND_MEAN
59  #define DEFINED_WIN32_LEAN_AND_MEAN
60  #endif /* !WIN32_LEAN_AND_MEAN */
61  #endif /* NO_WIN32_LEAN_AND_MEAN */
62  #include <windows.h>
63  #ifdef DEFINED_WIN32_LEAN_AND_MEAN
64  #undef WIN32_LEAN_AND_MEAN
65  #undef DEFINED_WIN32_LEAN_AND_MEAN
66  #endif /* DEFINED_WIN32_LEAN_AND_MEAN */
67  #undef NO_WIN32_LEAN_AND_MEAN
68  #include <intrin.h>
69 #endif /* __orxMSVC__ */
70 
71 
81 #define orxLERP(A, B, T) ((A) + ((T) * ((B) - (A))))
82 
83 
89 #define orxMIN(A, B) (((A) > (B)) ? (B) : (A))
90 
96 #define orxMAX(A, B) (((A) < (B)) ? (B) : (A))
97 
104 #define orxCLAMP(V, MIN, MAX) orxMAX(orxMIN(V, MAX), MIN)
105 
110 #define orxF2U(V) ((orxU32) (V))
111 
116 #define orxF2S(V) ((orxS32) (V))
117 
122 #define orxU2F(V) ((orxFLOAT)(V))
123 
128 #define orxS2F(V) ((orxFLOAT)(V))
129 
130 
131 /*** Module functions *** */
132 
136 extern orxDLLAPI void orxFASTCALL orxMath_InitRandom(orxU32 _u32Seed);
137 
143 extern orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax);
144 
150 extern orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max);
151 
157 extern orxDLLAPI orxS32 orxFASTCALL orxMath_GetRandomS32(orxS32 _s32Min, orxS32 _s32Max);
158 
164 extern orxDLLAPI orxU64 orxFASTCALL orxMath_GetRandomU64(orxU64 _u64Min, orxU64 _u64Max);
165 
171 extern orxDLLAPI orxS64 orxFASTCALL orxMath_GetRandomS64(orxS64 _s64Min, orxS64 _s64Max);
172 
176 extern orxDLLAPI void orxFASTCALL orxMath_GetRandomSeeds(orxU32 _au32Seeds[4]);
177 
181 extern orxDLLAPI void orxFASTCALL orxMath_SetRandomSeeds(const orxU32 _au32Seeds[4]);
182 
183 
184 /*** Inlined functions *** */
185 
190 static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
191 {
192  orxU32 u32Result;
193 
194 #ifdef __orxMSVC__
195 
196  /* Uses intrinsic */
197  u32Result = __popcnt(_u32Value);
198 
199 #else /* __orxMSVC__ */
200 
201  /* Uses intrinsic */
202  u32Result = __builtin_popcount(_u32Value);
203 
204 #endif /* __orxMSVC__ */
205 
206  /* Done! */
207  return u32Result;
208 }
209 
214 static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
215 {
216  orxU32 u32Result;
217 
218  /* Checks */
219  orxASSERT(_u32Value != 0);
220 
221 #ifdef __orxMSVC__
222 
223  /* Uses intrinsic */
224  _BitScanForward((DWORD *)&u32Result, _u32Value);
225 
226 #else /* __orxMSVC__ */
227 
228  /* Uses intrinsic */
229  u32Result = __builtin_ctz(_u32Value);
230 
231 #endif /* __orxMSVC__ */
232 
233  /* Done! */
234  return u32Result;
235 }
236 
241 static orxINLINE orxU32 orxMath_GetTrailingZeroCount64(orxU64 _u64Value)
242 {
243  orxU32 u32Result;
244 
245  /* Checks */
246  orxASSERT(_u64Value != 0);
247 
248 #ifdef __orxMSVC__
249 
250  #ifdef __orx64__
251 
252  /* Uses intrinsic */
253  _BitScanForward64((DWORD *)&u32Result, _u64Value);
254 
255  #else /* __orx64__ */
256 
257  /* Updates result */
258  u32Result = ((_u64Value & 0xFFFFFFFFULL) == 0) ? orxMath_GetTrailingZeroCount((orxU32)(_u64Value >> 32)) + 32 : orxMath_GetTrailingZeroCount((orxU32)_u64Value);
259 
260  #endif /* __orx64__ */
261 
262 #else /* __orxMSVC__ */
263 
264  /* Uses intrinsic */
265  u32Result = __builtin_ctzll(_u64Value);
266 
267 #endif /* __orxMSVC__ */
268 
269  /* Done! */
270  return u32Result;
271 }
272 
277 static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
278 {
279  orxBOOL bResult;
280 
281  /* Updates result */
282  bResult = ((_u32Value & (_u32Value - 1)) == 0) ? orxTRUE : orxFALSE;
283 
284  /* Done! */
285  return bResult;
286 }
287 
292 static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
293 {
294  orxU32 u32Result;
295 
296  /* Non-zero? */
297  if(_u32Value != 0)
298  {
299  /* Updates result */
300  u32Result = _u32Value - 1;
301  u32Result = u32Result | (u32Result >> 1);
302  u32Result = u32Result | (u32Result >> 2);
303  u32Result = u32Result | (u32Result >> 4);
304  u32Result = u32Result | (u32Result >> 8);
305  u32Result = u32Result | (u32Result >> 16);
306  u32Result++;
307  }
308  else
309  {
310  /* Updates result */
311  u32Result = 1;
312  }
313 
314  /* Done! */
315  return u32Result;
316 }
317 
324 static orxINLINE orxFLOAT orxMath_SmoothStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
325 {
326  orxFLOAT fTemp, fResult;
327 
328  /* Gets normalized and clamped value */
329  fTemp = (_fValue - _fMin) / (_fMax - _fMin);
330  fTemp = orxCLAMP(fTemp, orxFLOAT_0, orxFLOAT_1);
331 
332  /* Gets smoothed result */
333  fResult = fTemp * fTemp * (orx2F(3.0f) - (orx2F(2.0f) * fTemp));
334 
335  /* Done! */
336  return fResult;
337 }
338 
345 static orxINLINE orxFLOAT orxMath_SmootherStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
346 {
347  orxFLOAT fTemp, fResult;
348 
349  /* Gets normalized and clamped value */
350  fTemp = (_fValue - _fMin) / (_fMax - _fMin);
351  fTemp = orxCLAMP(fTemp, orxFLOAT_0, orxFLOAT_1);
352 
353  /* Gets smoothed result */
354  fResult = fTemp * fTemp * fTemp * (fTemp * ((fTemp * orx2F(6.0f)) - orx2F(15.0f)) + orx2F(10.0f));
355 
356  /* Done! */
357  return fResult;
358 }
359 
360 
361 /*** Math Definitions ***/
362 
363 #define orxMATH_KF_SQRT_2 orx2F(1.414213562f)
364 #define orxMATH_KF_EPSILON orx2F(0.0001f)
365 #define orxMATH_KF_TINY_EPSILON orx2F(1.0e-037f)
366 #define orxMATH_KF_2_PI orx2F(6.283185307f)
367 #define orxMATH_KF_PI orx2F(3.141592654f)
368 #define orxMATH_KF_PI_BY_2 orx2F(1.570796327f)
369 #define orxMATH_KF_PI_BY_4 orx2F(0.785398163f)
370 #define orxMATH_KF_DEG_TO_RAD orx2F(3.141592654f / 180.0f)
371 #define orxMATH_KF_RAD_TO_DEG orx2F(180.0f / 3.141592654f)
374 /*** Trigonometric function ***/
375 
380 static orxINLINE orxFLOAT orxMath_Sin(orxFLOAT _fOp)
381 {
382  orxFLOAT fResult;
383 
384  /* Updates result */
385  fResult = sinf(_fOp);
386 
387  /* Done! */
388  return fResult;
389 }
390 
395 static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
396 {
397  orxFLOAT fResult;
398 
399  /* Updates result */
400  fResult = cosf(_fOp);
401 
402  /* Done! */
403  return fResult;
404 }
405 
410 static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
411 {
412  orxFLOAT fResult;
413 
414  /* Updates result */
415  fResult = tanf(_fOp);
416 
417  /* Done! */
418  return fResult;
419 }
420 
425 static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
426 {
427  orxFLOAT fResult;
428 
429  /* Updates result */
430  fResult = acosf(_fOp);
431 
432  /* Done! */
433  return fResult;
434 }
435 
440 static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
441 {
442  orxFLOAT fResult;
443 
444  /* Updates result */
445  fResult = asinf(_fOp);
446 
447  /* Done! */
448  return fResult;
449 }
450 
456 static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
457 {
458  orxFLOAT fResult;
459 
460  /* Updates result */
461  fResult = atan2f(_fOp1, _fOp2);
462 
463  /* Done! */
464  return fResult;
465 }
466 
467 
468 /*** Misc functions ***/
469 
474 static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
475 {
476  orxFLOAT fResult;
477 
478  /* Updates result */
479  fResult = sqrtf(_fOp);
480 
481  /* Done! */
482  return fResult;
483 }
484 
489 static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
490 {
491  orxFLOAT fResult;
492 
493  /* Updates result */
494  fResult = floorf(_fOp);
495 
496  /* Done! */
497  return fResult;
498 }
499 
504 static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
505 {
506  orxFLOAT fResult;
507 
508  /* Updates result */
509  fResult = ceilf(_fOp);
510 
511  /* Done! */
512  return fResult;
513 }
514 
519 static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
520 {
521  orxFLOAT fResult;
522 
523 #ifdef __orxMSVC__
524 
525  /* Updates result */
526  fResult = floorf(_fOp + orx2F(0.5f));
527 
528 #else /* __orxMSVC__ */
529 
530  /* Updates result */
531  fResult = rintf(_fOp);
532 
533 #endif /* __orxMSVC__ */
534 
535  /* Done! */
536  return fResult;
537 }
538 
544 static orxINLINE orxFLOAT orxMath_Mod(orxFLOAT _fOp1, orxFLOAT _fOp2)
545 {
546  orxFLOAT fResult;
547 
548  /* Updates result */
549  fResult = fmodf(_fOp1, _fOp2);
550 
551  /* Done! */
552  return fResult;
553 }
554 
560 static orxINLINE orxFLOAT orxMath_Pow(orxFLOAT _fOp, orxFLOAT _fExp)
561 {
562  orxFLOAT fResult;
563 
564  /* Updates result */
565  fResult = powf(_fOp, _fExp);
566 
567  /* Done! */
568  return fResult;
569 }
570 
575 static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
576 {
577  orxFLOAT fResult;
578 
579  /* Updates result */
580  fResult = fabsf(_fOp);
581 
582  /* Done! */
583  return fResult;
584 }
585 
586 #endif /* _orxMATH_H_ */
587 
static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
Definition: orxMath.h:504
static orxINLINE orxFLOAT orxMath_SmoothStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
Definition: orxMath.h:324
static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
Definition: orxMath.h:519
static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
Definition: orxMath.h:190
static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
Definition: orxMath.h:214
static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
Definition: orxMath.h:440
#define orxTRUE
Definition: orxType.h:186
static const orxFLOAT orxFLOAT_1
Definition: orxType.h:191
#define orxCLAMP(V, MIN, MAX)
Definition: orxMath.h:104
static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
Definition: orxMath.h:474
#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:380
static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
Definition: orxMath.h:489
static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
Definition: orxMath.h:575
static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
Definition: orxMath.h:292
static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
Definition: orxMath.h:425
static orxINLINE orxU32 orxMath_GetTrailingZeroCount64(orxU64 _u64Value)
Definition: orxMath.h:241
orxDLLAPI void orxFASTCALL orxMath_SetRandomSeeds(const orxU32 _au32Seeds[4])
static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
Definition: orxMath.h:395
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:544
orxDLLAPI orxU64 orxFASTCALL orxMath_GetRandomU64(orxU64 _u64Min, orxU64 _u64Max)
#define orxDLLAPI
Definition: orxDecl.h:394
static orxINLINE orxFLOAT orxMath_SmootherStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
Definition: orxMath.h:345
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:560
static const orxFLOAT orxFLOAT_0
Definition: orxType.h:190
static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
Definition: orxMath.h:456
orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max)
static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
Definition: orxMath.h:410
#define orxASSERT(TEST,...)
Definition: orxDebug.h:298
static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
Definition: orxMath.h:277

Generated for orx by doxygen 1.8.11