orx 1.16
Portable Game Engine
Loading...
Searching...
No Matches
orxMath.h
Go to the documentation of this file.
1/* Orx - Portable Game Engine
2 *
3 * Copyright (c) 2008- 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
32
41
42
43#ifndef _orxMATH_H_
44#define _orxMATH_H_
45
46#include "orxInclude.h"
47#include "debug/orxDebug.h"
48
51#include <math.h>
52#ifdef __orxMSVC__
53 #include <intrin.h>
54#endif /* __orxMSVC__ */
55
56
59
66#define orxLERP(A, B, T) ((A) + ((T) * ((B) - (A))))
67
76#define orxREMAP(A1, B1, A2, B2, V) (((V) - (A1)) / ((B1) - (A1)) * ((B2) - (A2)) + (A2))
77
83#define orxMIN(A, B) (((A) > (B)) ? (B) : (A))
84
90#define orxMAX(A, B) (((A) < (B)) ? (B) : (A))
91
98#define orxCLAMP(V, MIN, MAX) orxMAX(orxMIN(V, MAX), MIN)
99
104#define orxF2U(V) ((orxU32) (V))
105
110#define orxF2S(V) ((orxS32) (V))
111
116#define orxU2F(V) ((orxFLOAT)(V))
117
122#define orxS2F(V) ((orxFLOAT)(V))
123
124
125/*** Module functions *** */
126
130extern orxDLLAPI void orxFASTCALL orxMath_InitRandom(orxU32 _u32Seed);
131
137extern orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax);
138
145extern orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetSteppedRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fStep);
146
152extern orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max);
153
160extern orxDLLAPI orxU32 orxFASTCALL orxMath_GetSteppedRandomU32(orxU32 _u32Min, orxU32 _u32Max, orxU32 _u32Step);
161
167extern orxDLLAPI orxS32 orxFASTCALL orxMath_GetRandomS32(orxS32 _s32Min, orxS32 _s32Max);
168
175extern orxDLLAPI orxS32 orxFASTCALL orxMath_GetSteppedRandomS32(orxS32 _s32Min, orxS32 _s32Max, orxS32 _s32Step);
176
182extern orxDLLAPI orxU64 orxFASTCALL orxMath_GetRandomU64(orxU64 _u64Min, orxU64 _u64Max);
183
190extern orxDLLAPI orxU64 orxFASTCALL orxMath_GetSteppedRandomU64(orxU64 _u64Min, orxU64 _u64Max, orxU64 _u64Step);
191
197extern orxDLLAPI orxS64 orxFASTCALL orxMath_GetRandomS64(orxS64 _s64Min, orxS64 _s64Max);
198
205extern orxDLLAPI orxS64 orxFASTCALL orxMath_GetSteppedRandomS64(orxS64 _s64Min, orxS64 _s64Max, orxS64 _s64Step);
206
210extern orxDLLAPI void orxFASTCALL orxMath_GetRandomSeeds(orxU32 _au32Seeds[4]);
211
215extern orxDLLAPI void orxFASTCALL orxMath_SetRandomSeeds(const orxU32 _au32Seeds[4]);
216
217
218/*** Inlined functions *** */
219
224static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
225{
226 orxU32 u32Result;
227
228#ifdef __orxMSVC__
229
230 /* Uses intrinsic */
231 u32Result = __popcnt(_u32Value);
232
233#else /* __orxMSVC__ */
234
235 /* Uses intrinsic */
236 u32Result = (orxU32)__builtin_popcount(_u32Value);
237
238#endif /* __orxMSVC__ */
239
240 /* Done! */
241 return u32Result;
242}
243
248static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
249{
250 orxU32 u32Result;
251
252 /* Checks */
253 orxASSERT(_u32Value != 0);
254
255#ifdef __orxMSVC__
256
257 /* Uses intrinsic */
258 _BitScanForward((unsigned long *)&u32Result, _u32Value);
259
260#else /* __orxMSVC__ */
261
262 /* Uses intrinsic */
263 u32Result = (orxU32)__builtin_ctz(_u32Value);
264
265#endif /* __orxMSVC__ */
266
267 /* Done! */
268 return u32Result;
269}
270
275static orxINLINE orxU32 orxMath_GetTrailingZeroCount64(orxU64 _u64Value)
276{
277 orxU32 u32Result;
278
279 /* Checks */
280 orxASSERT(_u64Value != 0);
281
282#ifdef __orxMSVC__
283
284 #ifdef __orx64__
285
286 /* Uses intrinsic */
287 _BitScanForward64((unsigned long *)&u32Result, _u64Value);
288
289 #else /* __orx64__ */
290
291 /* Updates result */
292 u32Result = ((_u64Value & 0xFFFFFFFFULL) == 0) ? orxMath_GetTrailingZeroCount((orxU32)(_u64Value >> 32)) + 32 : orxMath_GetTrailingZeroCount((orxU32)_u64Value);
293
294 #endif /* __orx64__ */
295
296#else /* __orxMSVC__ */
297
298 /* Uses intrinsic */
299 u32Result = (orxU32)__builtin_ctzll(_u64Value);
300
301#endif /* __orxMSVC__ */
302
303 /* Done! */
304 return u32Result;
305}
306
311static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
312{
313 orxBOOL bResult;
314
315 /* Updates result */
316 bResult = ((_u32Value & (_u32Value - 1)) == 0) ? orxTRUE : orxFALSE;
317
318 /* Done! */
319 return bResult;
320}
321
326static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
327{
328 orxU32 u32Result;
329
330 /* Non-zero? */
331 if(_u32Value != 0)
332 {
333 /* Updates result */
334 u32Result = _u32Value - 1;
335 u32Result = u32Result | (u32Result >> 1);
336 u32Result = u32Result | (u32Result >> 2);
337 u32Result = u32Result | (u32Result >> 4);
338 u32Result = u32Result | (u32Result >> 8);
339 u32Result = u32Result | (u32Result >> 16);
340 u32Result++;
341 }
342 else
343 {
344 /* Updates result */
345 u32Result = 1;
346 }
347
348 /* Done! */
349 return u32Result;
350}
351
358static orxINLINE orxFLOAT orxMath_SmoothStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
359{
360 orxFLOAT fTemp, fResult;
361
362 /* Gets normalized and clamped value */
363 fTemp = (_fValue - _fMin) / (_fMax - _fMin);
364 fTemp = orxCLAMP(fTemp, orxFLOAT_0, orxFLOAT_1);
365
366 /* Gets smoothed result */
367 fResult = fTemp * fTemp * (orx2F(3.0f) - (orx2F(2.0f) * fTemp));
368
369 /* Done! */
370 return fResult;
371}
372
379static orxINLINE orxFLOAT orxMath_SmootherStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
380{
381 orxFLOAT fTemp, fResult;
382
383 /* Gets normalized and clamped value */
384 fTemp = (_fValue - _fMin) / (_fMax - _fMin);
385 fTemp = orxCLAMP(fTemp, orxFLOAT_0, orxFLOAT_1);
386
387 /* Gets smoothed result */
388 fResult = fTemp * fTemp * fTemp * (fTemp * ((fTemp * orx2F(6.0f)) - orx2F(15.0f)) + orx2F(10.0f));
389
390 /* Done! */
391 return fResult;
392}
393
394
395/*** Math Definitions ***/
396
397#define orxMATH_KF_SQRT_2 orx2F(1.414213562f)
398#define orxMATH_KF_EPSILON orx2F(0.0001f)
399#define orxMATH_KF_TINY_EPSILON orx2F(1.0e-037f)
400#define orxMATH_KF_MAX orx2F(3.402823466e+38F)
401#define orxMATH_KF_2_PI orx2F(6.283185307f)
402#define orxMATH_KF_PI orx2F(3.141592654f)
403#define orxMATH_KF_PI_BY_2 orx2F(1.570796327f)
404#define orxMATH_KF_PI_BY_4 orx2F(0.785398163f)
405#define orxMATH_KF_DEG_TO_RAD orx2F(3.141592654f / 180.0f)
406#define orxMATH_KF_RAD_TO_DEG orx2F(180.0f / 3.141592654f)
407
408
409/*** Trigonometric function ***/
410
415static orxINLINE orxFLOAT orxMath_Sin(orxFLOAT _fOp)
416{
417 orxFLOAT fResult;
418
419 /* Updates result */
420 fResult = sinf(_fOp);
421
422 /* Done! */
423 return fResult;
424}
425
430static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
431{
432 orxFLOAT fResult;
433
434 /* Updates result */
435 fResult = cosf(_fOp);
436
437 /* Done! */
438 return fResult;
439}
440
445static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
446{
447 orxFLOAT fResult;
448
449 /* Updates result */
450 fResult = tanf(_fOp);
451
452 /* Done! */
453 return fResult;
454}
455
460static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
461{
462 orxFLOAT fResult;
463
464 /* Updates result */
465 fResult = acosf(_fOp);
466
467 /* Done! */
468 return fResult;
469}
470
475static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
476{
477 orxFLOAT fResult;
478
479 /* Updates result */
480 fResult = asinf(_fOp);
481
482 /* Done! */
483 return fResult;
484}
485
491static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
492{
493 orxFLOAT fResult;
494
495 /* Updates result */
496 fResult = atan2f(_fOp1, _fOp2);
497
498 /* Done! */
499 return fResult;
500}
501
502
503/*** Misc functions ***/
504
509static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
510{
511 orxFLOAT fResult;
512
513 /* Updates result */
514 fResult = sqrtf(_fOp);
515
516 /* Done! */
517 return fResult;
518}
519
524static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
525{
526 orxFLOAT fResult;
527
528 /* Updates result */
529 fResult = floorf(_fOp);
530
531 /* Done! */
532 return fResult;
533}
534
539static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
540{
541 orxFLOAT fResult;
542
543 /* Updates result */
544 fResult = ceilf(_fOp);
545
546 /* Done! */
547 return fResult;
548}
549
554static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
555{
556 orxFLOAT fResult;
557
558#ifdef __orxMSVC__
559
560 /* Updates result */
561 fResult = floorf(_fOp + orx2F(0.5f));
562
563#else /* __orxMSVC__ */
564
565 /* Updates result */
566 fResult = rintf(_fOp);
567
568#endif /* __orxMSVC__ */
569
570 /* Done! */
571 return fResult;
572}
573
579static orxINLINE orxFLOAT orxMath_Mod(orxFLOAT _fOp1, orxFLOAT _fOp2)
580{
581 orxFLOAT fResult;
582
583 /* Updates result */
584 fResult = fmodf(_fOp1, _fOp2);
585
586 /* Done! */
587 return fResult;
588}
589
595static orxINLINE orxFLOAT orxMath_Pow(orxFLOAT _fOp, orxFLOAT _fExp)
596{
597 orxFLOAT fResult;
598
599 /* Updates result */
600 fResult = powf(_fOp, _fExp);
601
602 /* Done! */
603 return fResult;
604}
605
610static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
611{
612 orxFLOAT fResult;
613
614 /* Updates result */
615 fResult = fabsf(_fOp);
616
617 /* Done! */
618 return fResult;
619}
620
621#endif /* _orxMATH_H_ */
622
#define orxASSERT(TEST,...)
Definition orxDebug.h:378
#define orxDLLAPI
Definition orxDecl.h:381
orxDLLAPI orxU64 orxFASTCALL orxMath_GetRandomU64(orxU64 _u64Min, orxU64 _u64Max)
static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
Definition orxMath.h:524
orxDLLAPI orxU64 orxFASTCALL orxMath_GetSteppedRandomU64(orxU64 _u64Min, orxU64 _u64Max, orxU64 _u64Step)
orxDLLAPI orxS32 orxFASTCALL orxMath_GetSteppedRandomS32(orxS32 _s32Min, orxS32 _s32Max, orxS32 _s32Step)
static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
Definition orxMath.h:326
orxDLLAPI orxS32 orxFASTCALL orxMath_GetRandomS32(orxS32 _s32Min, orxS32 _s32Max)
orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max)
orxDLLAPI void orxFASTCALL orxMath_SetRandomSeeds(const orxU32 _au32Seeds[4])
orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax)
static orxINLINE orxFLOAT orxMath_Mod(orxFLOAT _fOp1, orxFLOAT _fOp2)
Definition orxMath.h:579
static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
Definition orxMath.h:445
static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
Definition orxMath.h:430
static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
Definition orxMath.h:491
orxDLLAPI orxU32 orxFASTCALL orxMath_GetSteppedRandomU32(orxU32 _u32Min, orxU32 _u32Max, orxU32 _u32Step)
orxDLLAPI orxS64 orxFASTCALL orxMath_GetRandomS64(orxS64 _s64Min, orxS64 _s64Max)
static orxINLINE orxFLOAT orxMath_SmootherStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
Definition orxMath.h:379
orxDLLAPI void orxFASTCALL orxMath_InitRandom(orxU32 _u32Seed)
static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
Definition orxMath.h:610
static orxINLINE orxFLOAT orxMath_SmoothStep(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fValue)
Definition orxMath.h:358
static orxINLINE orxFLOAT orxMath_Pow(orxFLOAT _fOp, orxFLOAT _fExp)
Definition orxMath.h:595
static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
Definition orxMath.h:475
static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
Definition orxMath.h:460
static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
Definition orxMath.h:554
static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
Definition orxMath.h:224
static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
Definition orxMath.h:539
static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
Definition orxMath.h:248
orxDLLAPI void orxFASTCALL orxMath_GetRandomSeeds(orxU32 _au32Seeds[4])
static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
Definition orxMath.h:509
#define orxCLAMP(V, MIN, MAX)
Definition orxMath.h:98
static orxINLINE orxFLOAT orxMath_Sin(orxFLOAT _fOp)
Definition orxMath.h:415
orxDLLAPI orxS64 orxFASTCALL orxMath_GetSteppedRandomS64(orxS64 _s64Min, orxS64 _s64Max, orxS64 _s64Step)
orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetSteppedRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax, orxFLOAT _fStep)
static orxINLINE orxU32 orxMath_GetTrailingZeroCount64(orxU64 _u64Value)
Definition orxMath.h:275
static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
Definition orxMath.h:311
#define orxFALSE
Definition orxType.h:210
#define orxTRUE
Definition orxType.h:211
static const orxFLOAT orxFLOAT_1
Definition orxType.h:216
static const orxFLOAT orxFLOAT_0
Definition orxType.h:215

Generated for orx by doxygen 1.8.11