Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

fxmath.h
1 /********************************************************************************
2 * *
3 * M a t h F u n c t i o n s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2015,2022 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXMATH_H
22 #define FXMATH_H
23 
24 // Remove macros
25 #undef fabs
26 #undef fabsf
27 #undef fmod
28 #undef fmodf
29 
30 #undef ceil
31 #undef ceilf
32 #undef floor
33 #undef floorf
34 #undef round
35 #undef roundf
36 #undef trunc
37 #undef truncf
38 #undef nearbyint
39 #undef nearbyintf
40 #undef rint
41 #undef rintf
42 
43 #undef sin
44 #undef sinf
45 #undef cos
46 #undef cosf
47 #undef tan
48 #undef tanf
49 #undef asin
50 #undef asinf
51 #undef acos
52 #undef acosf
53 #undef atan
54 #undef atanf
55 #undef atan2
56 #undef atan2f
57 #undef sincos
58 #undef sincosf
59 #undef sinh
60 #undef sinhf
61 #undef cosh
62 #undef coshf
63 #undef tanh
64 #undef tanhf
65 #undef asinh
66 #undef asinhf
67 #undef acosh
68 #undef acoshf
69 #undef atanh
70 #undef atanhf
71 
72 #undef pow
73 #undef powf
74 #undef pow10
75 #undef pow10f
76 #undef exp
77 #undef expf
78 #undef expm1
79 #undef expm1f
80 #undef exp2
81 #undef exp2f
82 #undef exp10
83 #undef exp10f
84 #undef log
85 #undef logf
86 #undef log1p
87 #undef log1pf
88 #undef log2
89 #undef log2f
90 #undef log10
91 #undef log10f
92 
93 #undef sqrt
94 #undef sqrtf
95 #undef cbrt
96 #undef cbrtf
97 #undef sqr
98 #undef sqrf
99 #undef cub
100 #undef cubf
101 
102 #undef max
103 #undef min
104 #undef fmax
105 #undef fmaxf
106 #undef fmin
107 #undef fminf
108 #undef copysign
109 #undef copysignf
110 #undef hypot
111 #undef hypotf
112 
113 
114 // Switch on remedial math on Windows with VC++
115 #if defined(WIN32) && (defined(_MSC_VER) || defined(__MINGW32__))
116 #define NO_CEILF
117 #define NO_FLOORF
118 #define NO_ROUNDF
119 #define NO_ROUND
120 #define NO_TRUNCF
121 #define NO_TRUNC
122 #define NO_NEARBYINTF
123 #define NO_NEARBYINT
124 #define NO_RINTF
125 #define NO_RINT
126 #define NO_EXPM1F
127 #define NO_EXPM1
128 #define NO_EXP2F
129 #define NO_EXP2
130 #define NO_EXP10F
131 #define NO_EXP10
132 #define NO_POW10F
133 #define NO_POW10
134 #define NO_LOG1PF
135 #define NO_LOG1P
136 #define NO_LOG2F
137 #define NO_LOG2
138 #define NO_CBRTF
139 #define NO_CBRT
140 #define NO_SINHF
141 #define NO_SINH
142 #define NO_COSHF
143 #define NO_COSH
144 #define NO_TANHF
145 #define NO_TANH
146 #define NO_ASINHF
147 #define NO_ASINH
148 #define NO_ACOSHF
149 #define NO_ACOSH
150 #define NO_ATANHF
151 #define NO_ATANH
152 #define NO_FMAXF
153 #define NO_FMAX
154 #define NO_FMINF
155 #define NO_FMIN
156 #define NO_COPYSIGNF
157 #define NO_COPYSIGN
158 #define NO_HYPOTF
159 #define NO_HYPOT
160 #define NO_FDIMF
161 #define NO_FDIM
162 #define NO_SINCOS
163 #define NO_SINCOSF
164 #define NO_LRINT
165 #define NO_LRINTF
166 #endif
167 
168 // Systems below are missing these functions
169 #if defined(__sun__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__)
170 #define NO_EXP10F
171 #define NO_EXP10
172 #endif
173 
174 // Apple is missing sincos
175 #if defined(__APPLE__)
176 #define NO_SINCOS
177 #define NO_SINCOSF
178 #endif
179 
180 
181 namespace FX {
182 
183 /********************************* Constants *********************************/
184 
186 const FXdouble PI=3.1415926535897932384626433833;
187 
189 const FXdouble EULER=2.7182818284590452353602874713;
190 
192 const FXdouble DTOR=0.0174532925199432957692369077;
193 
195 const FXdouble RTOD=57.295779513082320876798154814;
196 
198 const FXdouble FEIGENBAUM=4.6692016091029906718532038215;
199 
200 /********************************* Functions *********************************/
201 
202 // FOX math functions live here
203 namespace Math {
204 
205 
207 static inline FXuint fpBits(FXfloat x){
208  union{ FXfloat f; FXuint u; } z={x};
209  return z.u;
210  }
211 
213 static inline FXulong fpBits(FXdouble x){
214  union{ FXdouble f; FXulong u; } z={x};
215  return z.u;
216  }
217 
218 
220 static inline FXint fpSign(FXfloat x){
221  FXint sign=fpBits(x)>>31;
222  return sign;
223  }
224 
226 static inline FXlong fpSign(FXdouble x){
227  FXlong sign=fpBits(x)>>63;
228  return sign;
229  }
230 
231 
233 static inline FXint fpExponent(FXfloat x){
234  FXint exponent=(fpBits(x)>>23)&0xff;
235  FXint bias=126-(-exponent>>31);
236  return exponent-bias;
237  }
238 
240 static inline FXlong fpExponent(FXdouble x){
241  FXlong exponent=(fpBits(x)>>52)&0x7ff;
242  FXlong bias=1022-(-exponent>>63);
243  return exponent-bias;
244  }
245 
246 
248 static inline FXint fpMantissa(FXfloat x){
249  FXint mantissa=fpBits(x)&0x007fffff;
250  FXint exponent=fpBits(x)&0x7f800000;
251  FXint extrabit=-(-exponent>>31); // 1 if exponent!=0
252  return mantissa|(extrabit<<23);
253  }
254 
256 static inline FXlong fpMantissa(FXdouble x){
257  FXlong mantissa=fpBits(x)&FXLONG(0x000fffffffffffff);
258  FXlong exponent=fpBits(x)&FXLONG(0x7ff0000000000000);
259  FXlong extrabit=-(-exponent>>63); // 1 if exponent!=0
260  return mantissa|(extrabit<<52);
261  }
262 
263 
265 static inline FXbool fpFinite(FXfloat x){
266  return ((fpBits(x)&0x7fffffff)<0x7f800000);
267  }
268 
270 static inline FXbool fpFinite(FXdouble x){
271  return ((fpBits(x)&FXULONG(0x7fffffffffffffff))<FXULONG(0x7ff0000000000000));
272  }
273 
274 
276 static inline FXbool fpInfinite(FXfloat x){
277  return ((fpBits(x)&0x7fffffff)==0x7f800000);
278  }
279 
281 static inline FXbool fpInfinite(FXdouble x){
282  return ((fpBits(x)&FXULONG(0x7fffffffffffffff))==FXULONG(0x7ff0000000000000));
283  }
284 
285 
287 static inline FXbool fpNan(FXfloat x){
288  return (0x7f800000<(fpBits(x)&0x7fffffff));
289  }
290 
292 static inline FXbool fpNan(FXdouble x){
293  return (FXULONG(0x7ff0000000000000)<(fpBits(x)&FXULONG(0x7fffffffffffffff)));
294  }
295 
296 
298 static inline FXbool fpNormal(FXfloat x){
299  FXuint bits=fpBits(x)&0x7fffffff;
300  return bits==0 || (0x00800000<=bits && bits<0x7f800000);
301  }
302 
304 static inline FXbool fpNormal(FXdouble x){
305  FXulong bits=fpBits(x)&FXULONG(0x7fffffffffffffff);
306  return (bits==0) || ((FXULONG(0x0010000000000000)<=bits) && (bits<FXULONG(0x7ff0000000000000)));
307  }
308 
309 
311 static inline FXint iblend(FXint a,FXint b,FXint x,FXint y){
312  return a<b ? x : y;
313  }
314 
316 static inline FXlong iblend(FXlong a,FXlong b,FXlong x,FXlong y){
317  return a<b ? x : y;
318  }
319 
320 
322 static inline FXint imin(FXint x,FXint y){
323  return (x<y)?x:y;
324  }
325 
327 static inline FXuint imin(FXuint x,FXuint y){
328  return (x<y)?x:y;
329  }
330 
332 static inline FXlong imin(FXlong x,FXlong y){
333  return (x<y)?x:y;
334  }
335 
337 static inline FXulong imin(FXulong x,FXulong y){
338  return (x<y)?x:y;
339  }
340 
341 
343 static inline FXint imax(FXint x,FXint y){
344  return (x>y)?x:y;
345  }
346 
348 static inline FXuint imax(FXuint x,FXuint y){
349  return (x>y)?x:y;
350  }
351 
352 
354 static inline FXlong imax(FXlong x,FXlong y){
355  return (x>y)?x:y;
356  }
357 
359 static inline FXulong imax(FXulong x,FXulong y){
360  return (x>y)?x:y;
361  }
362 
363 
365 static inline FXshort iabs(FXshort x){
366  return 0<x?x:-x;
367  }
368 
370 static inline FXint iabs(FXint x){
371  return 0<x?x:-x;
372  }
373 
375 static inline FXlong iabs(FXlong x){
376  return 0<x?x:-x;
377  }
378 
379 
381 static inline FXint iclamp(FXint lo,FXint x,FXint hi){
382  return Math::imin(Math::imax(x,lo),hi);
383  }
384 
386 static inline FXlong iclamp(FXlong lo,FXlong x,FXlong hi){
387  return Math::imin(Math::imax(x,lo),hi);
388  }
389 
390 
392 static inline FXint iclamp(FXint x,FXint lim){
393  return imin(imax(x,-lim),lim);
394  }
395 
397 static inline FXlong iclamp(FXlong x,FXlong lim){
398  return imin(imax(x,-lim),lim);
399  }
400 
401 
403 static inline FXint isign(FXint x){
404  return (x>0)-(x<0);
405  }
406 
408 static inline FXlong isign(FXlong x){
409  return (x>0)-(x<0);
410  }
411 
412 
414 static inline FXfloat fmin(FXfloat x,FXfloat y){
415 #if defined(NO_FMINF)
416  return (x<y)?x:y;
417 #else
418  return ::fminf(x,y);
419 #endif
420  }
421 
422 
424 static inline FXdouble fmin(FXdouble x,FXdouble y){
425 #if defined(NO_FMIN)
426  return (x<y)?x:y;
427 #else
428  return ::fmin(x,y);
429 #endif
430  }
431 
432 
434 static inline FXfloat fmax(FXfloat x,FXfloat y){
435 #if defined(NO_FMAXF)
436  return (x>y)?x:y;
437 #else
438  return ::fmaxf(x,y);
439 #endif
440  }
441 
443 static inline FXdouble fmax(FXdouble x,FXdouble y){
444 #if defined(NO_FMAX)
445  return (x>y)?x:y;
446 #else
447  return ::fmax(x,y);
448 #endif
449  }
450 
451 
453 static inline FXfloat fabs(FXfloat x){
454 #if defined(NO_FABSF)
455  return (x<0.0f) ? -x : x;
456 #else
457  return ::fabsf(x);
458 #endif
459  }
460 
462 static inline FXdouble fabs(FXdouble x){
463  return ::fabs(x);
464  }
465 
466 
468 static inline FXfloat fclamp(FXfloat lo,FXfloat x,FXfloat hi){
469  return Math::fmin(Math::fmax(x,lo),hi);
470  }
471 
473 static inline FXdouble fclamp(FXdouble lo,FXdouble x,FXdouble hi){
474  return Math::fmin(Math::fmax(x,lo),hi);
475  }
476 
477 
479 static inline FXfloat fclamp(FXfloat x,FXfloat lim){
480  return fmin(fmax(x,-lim),lim);
481  }
482 
484 static inline FXdouble fclamp(FXdouble x,FXdouble lim){
485  return fmin(fmax(x,-lim),lim);
486  }
487 
488 
490 static inline FXfloat fdim(FXfloat x,FXfloat y){
491 #if defined(NO_FDIMF)
492  return Math::fmax(x-y,0.0f);
493 #else
494  return ::fdimf(x,y);
495 #endif
496  }
497 
499 static inline FXdouble fdim(FXdouble x,FXdouble y){
500 #if defined(NO_FDIM)
501  return Math::fmax(x-y,0.0);
502 #else
503  return ::fdim(x,y);
504 #endif
505  }
506 
507 
509 static inline FXfloat fmod(FXfloat x,FXfloat y){
510  return ::fmodf(x,y);
511  }
512 
514 static inline FXdouble fmod(FXdouble x,FXdouble y){
515  return ::fmod(x,y);
516  }
517 
518 
520 static inline FXfloat fblend(FXfloat a,FXfloat b,FXfloat x,FXfloat y){
521  return a<b ? x : y;
522  }
523 
524 
526 static inline FXdouble fblend(FXdouble a,FXdouble b,FXdouble x,FXdouble y){
527  return a<b ? x : y;
528  }
529 
530 
532 static inline FXfloat copysign(FXfloat x,FXfloat y){
533 #if defined(NO_COPYSIGNF)
534  union{ FXfloat f; FXuint u; } xx={x},yy={y};
535  xx.u&=0x7fffffff;
536  xx.u|=(yy.u&0x80000000);
537  return xx.f;
538 #else
539  return ::copysignf(x,y);
540 #endif
541  }
542 
544 static inline FXdouble copysign(FXdouble x,FXdouble y){
545 #if defined(NO_COPYSIGN)
546  union{ FXdouble f; FXulong u; } xx={x},yy={y};
547  xx.u&=FXULONG(0x7fffffffffffffff);
548  xx.u|=(yy.u&FXULONG(0x8000000000000000));
549  return xx.f;
550 #else
551  return ::copysign(x,y);
552 #endif
553  }
554 
555 
557 #if defined(NO_CEILF)
558 extern FXAPI FXfloat ceil(FXfloat x);
559 #else
560 static inline FXfloat ceil(FXfloat x){ return ::ceilf(x); }
561 #endif
562 
564 static inline FXdouble ceil(FXdouble x){ return ::ceil(x); }
565 
566 
568 #if defined(NO_FLOORF)
569 extern FXAPI FXfloat floor(FXfloat x);
570 #else
571 static inline FXfloat floor(FXfloat x){ return ::floorf(x); }
572 #endif
573 
575 static inline FXdouble floor(FXdouble x){ return ::floor(x); }
576 
578 #if defined(NO_ROUNDF)
579 extern FXAPI FXfloat round(FXfloat x);
580 #else
581 static inline FXfloat round(FXfloat x){ return ::roundf(x); }
582 #endif
583 
585 #if defined(NO_ROUND)
586 extern FXAPI FXdouble round(FXdouble x);
587 #else
588 static inline FXdouble round(FXdouble x){ return ::round(x); }
589 #endif
590 
591 
593 #if defined(NO_TRUNCF)
594 extern FXAPI FXfloat trunc(FXfloat x);
595 #else
596 static inline FXfloat trunc(FXfloat x){ return ::truncf(x); }
597 #endif
598 
600 #if defined(NO_TRUNC)
601 extern FXAPI FXdouble trunc(FXdouble x);
602 #else
603 static inline FXdouble trunc(FXdouble x){ return ::trunc(x); }
604 #endif
605 
606 
608 #if defined(NO_NEARBYINTF)
609 extern FXAPI FXfloat nearbyint(FXfloat x);
610 #else
611 static inline FXfloat nearbyint(FXfloat x){ return ::nearbyintf(x); }
612 #endif
613 
615 #if defined(NO_NEARBYINT)
616 extern FXAPI FXdouble nearbyint(FXdouble x);
617 #else
618 static inline FXdouble nearbyint(FXdouble x){ return ::nearbyint(x); }
619 #endif
620 
622 #if defined(NO_RINTF)
623 extern FXAPI FXfloat rint(FXfloat x);
624 #else
625 static inline FXfloat rint(FXfloat x){ return ::rintf(x); }
626 #endif
627 
629 #if defined(NO_RINT)
630 extern FXAPI FXdouble rint(FXdouble x);
631 #else
632 static inline FXdouble rint(FXdouble x){ return ::rint(x); }
633 #endif
634 
635 
637 static inline FXint lrint(FXfloat x){
638 #if defined(NO_LRINTF)
639  return (FXint)(x+Math::copysign(0.5f,x));
640 #else
641  return (FXint)::lrintf(x);
642 #endif
643  }
644 
646 static inline FXlong lrint(FXdouble x){
647 #if defined(NO_LRINT)
648  return (FXlong)(x+Math::copysign(0.5,x));
649 #else
650  return (FXlong)::lrint(x);
651 #endif
652  }
653 
654 
656 static inline FXfloat wrap(FXfloat x){
657 #if defined(__SSE4_1__)
658  return x-Math::nearbyint(x*0.159154943091895335768883763373f)*6.28318530717958647692528676656f;
659 #else
660  return x-Math::lrint(x*0.159154943091895335768883763373f)*6.28318530717958647692528676656f;
661 #endif
662  }
663 
665 static inline FXdouble wrap(FXdouble x){
666 #if defined(__SSE4_1__)
667  return x-Math::nearbyint(x*0.159154943091895335768883763373)*6.28318530717958647692528676656;
668 #else
669  return x-Math::lrint(x*0.159154943091895335768883763373)*6.28318530717958647692528676656;
670 #endif
671  }
672 
673 
675 static inline FXfloat wrap4(FXfloat x){
676  return x-Math::floor(x*0.159154943091895335768883763373f)*6.28318530717958647692528676656f;
677  }
678 
679 
681 static inline FXdouble wrap4(FXdouble x){
682  return x-Math::floor(x*0.159154943091895335768883763373)*6.28318530717958647692528676656;
683  }
684 
685 
687 static inline FXfloat stepify(FXfloat x,FXfloat s){
688  return Math::nearbyint(x/s)*s;
689  }
690 
692 static inline FXdouble stepify(FXdouble x,FXdouble s){
693  return Math::nearbyint(x/s)*s;
694  }
695 
696 
698 static inline FXfloat zigzag(FXfloat x){
699  return Math::fabs(2.0f*(x-Math::nearbyint(x)));
700  }
701 
703 static inline FXdouble zigzag(FXdouble x){
704  return Math::fabs(2.0*(x-Math::nearbyint(x)));
705  }
706 
707 
709 static inline FXfloat sawtooth(FXfloat x){
710  return x-Math::floor(x);
711  }
712 
714 static inline FXdouble sawtooth(FXdouble x){
715  return x-Math::floor(x);
716  }
717 
718 
720 static inline FXfloat rsawtooth(FXfloat x){
721  return Math::ceil(x)-x;
722  }
723 
725 static inline FXdouble rsawtooth(FXdouble x){
726  return Math::ceil(x)-x;
727  }
728 
729 
731 static inline FXfloat sin(FXfloat x){
732  return ::sinf(x);
733  }
734 
736 static inline FXdouble sin(FXdouble x){
737  return ::sin(x);
738  }
739 
740 
742 static inline FXfloat cos(FXfloat x){
743  return ::cosf(x);
744  }
745 
747 static inline FXdouble cos(FXdouble x){
748  return ::cos(x);
749  }
750 
751 
753 static inline FXfloat tan(FXfloat x){
754  return ::tanf(x);
755  }
756 
758 static inline FXdouble tan(FXdouble x){
759  return ::tan(x);
760  }
761 
762 
764 static inline FXfloat asin(FXfloat x){
765  return ::asinf(x);
766  }
767 
769 static inline FXdouble asin(FXdouble x){
770  return ::asin(x);
771  }
772 
773 
775 static inline FXfloat acos(FXfloat x){
776  return ::acosf(x);
777  }
778 
780 static inline FXdouble acos(FXdouble x){
781  return ::acos(x);
782  }
783 
784 
786 static inline FXfloat atan(FXfloat x){
787  return ::atanf(x);
788  }
789 
791 static inline FXdouble atan(FXdouble x){
792  return ::atan(x);
793  }
794 
795 
797 static inline FXfloat atan2(FXfloat y,FXfloat x){
798  return ::atan2f(y,x);
799  }
800 
802 static inline FXdouble atan2(FXdouble y,FXdouble x){
803  return ::atan2(y,x);
804  }
805 
806 
808 static inline void sincos(FXfloat x,FXfloat& s,FXfloat& c){
809 #if defined(NO_SINCOSF)
810  s=Math::sin(x);
811  c=Math::cos(x);
812 #else
813  ::sincosf(x,&s,&c);
814 #endif
815  }
816 
817 
819 static inline void sincos(FXdouble x,FXdouble& s,FXdouble& c){
820 #if defined(NO_SINCOS)
821  s=Math::sin(x);
822  c=Math::cos(x);
823 #else
824  ::sincos(x,&s,&c);
825 #endif
826  }
827 
828 
830 #if defined(NO_SINHF)
831 extern FXAPI FXfloat sinh(FXfloat x);
832 #else
833 static inline FXfloat sinh(FXfloat x){ return ::sinhf(x); }
834 #endif
835 
837 #if defined(NO_SINH)
838 extern FXAPI FXdouble sinh(FXdouble x);
839 #else
840 static inline FXdouble sinh(FXdouble x){ return ::sinh(x); }
841 #endif
842 
843 
845 #if defined(NO_COSHF)
846 extern FXAPI FXfloat cosh(FXfloat x);
847 #else
848 static inline FXfloat cosh(FXfloat x){ return ::coshf(x); }
849 #endif
850 
852 #if defined(NO_COSH)
853 extern FXAPI FXdouble cosh(FXdouble x);
854 #else
855 static inline FXdouble cosh(FXdouble x){ return ::cosh(x); }
856 #endif
857 
858 
860 #if defined(NO_TANHF)
861 extern FXAPI FXfloat tanh(FXfloat x);
862 #else
863 static inline FXfloat tanh(FXfloat x){ return ::tanhf(x); }
864 #endif
865 
867 #if defined(NO_TANH)
868 extern FXAPI FXdouble tanh(FXdouble x);
869 #else
870 static inline FXdouble tanh(FXdouble x){ return ::tanh(x); }
871 #endif
872 
873 
875 #if defined(NO_ASINHF)
876 extern FXAPI FXfloat asinh(FXfloat x);
877 #else
878 static inline FXfloat asinh(FXfloat x){ return ::asinhf(x); }
879 #endif
880 
882 #if defined(NO_ASINH)
883 extern FXAPI FXdouble asinh(FXdouble x);
884 #else
885 static inline FXdouble asinh(FXdouble x){ return ::asinh(x); }
886 #endif
887 
888 
890 #if defined(NO_ACOSHF)
891 extern FXAPI FXfloat acosh(FXfloat x);
892 #else
893 static inline FXfloat acosh(FXfloat x){ return ::acoshf(x); }
894 #endif
895 
897 #if defined(NO_ACOSH)
898 extern FXAPI FXdouble acosh(FXdouble x);
899 #else
900 static inline FXdouble acosh(FXdouble x){ return ::acosh(x); }
901 #endif
902 
903 
905 #if defined(NO_ATANHF)
906 extern FXAPI FXfloat atanh(FXfloat x);
907 #else
908 static inline FXfloat atanh(FXfloat x){ return ::atanhf(x); }
909 #endif
910 
912 #if defined(NO_ATANH)
913 extern FXAPI FXdouble atanh(FXdouble x);
914 #else
915 static inline FXdouble atanh(FXdouble x){ return ::atanh(x); }
916 #endif
917 
918 
920 static inline FXfloat sqrt(FXfloat x){
921  return ::sqrtf(x);
922  }
923 
925 static inline FXdouble sqrt(FXdouble x){
926  return ::sqrt(x);
927  }
928 
929 
931 static inline FXfloat safesqrt(FXfloat x){
932  return Math::sqrt(Math::fmax(x,0.0f));
933  }
934 
936 static inline FXdouble safesqrt(FXdouble x){
937  return Math::sqrt(Math::fmax(x,0.0));
938  }
939 
940 
942 static inline FXfloat cbrt(FXfloat x){
943 #if defined(NO_CBRTF)
944  return ::powf(x,0.333333333333333333333333333333f);
945 #else
946  return ::cbrtf(x);
947 #endif
948  }
949 
951 static inline FXdouble cbrt(FXdouble x){
952 #if defined(NO_CBRT)
953  return ::pow(x,0.333333333333333333333333333333);
954 #else
955  return ::cbrt(x);
956 #endif
957  }
958 
959 
961 static inline FXfloat sqr(FXfloat x){
962  return x*x;
963  }
964 
966 static inline FXdouble sqr(FXdouble x){
967  return x*x;
968  }
969 
970 
972 static inline FXfloat cub(FXfloat x){
973  return x*x*x;
974  }
975 
977 static inline FXdouble cub(FXdouble x){
978  return x*x*x;
979  }
980 
981 
983 static inline FXfloat rsqrt(FXfloat x){
984  return 1.0f/Math::sqrt(x);
985  }
986 
987 
989 static inline FXdouble rsqrt(FXdouble x){
990  return 1.0/Math::sqrt(x);
991  }
992 
993 
995 static inline FXfloat hypot(FXfloat x,FXfloat y){
996 #if defined(NO_HYPOTF)
997  return Math::sqrt(Math::sqr(x)+Math::sqr(y));
998 #else
999  return ::hypotf(x,y);
1000 #endif
1001  }
1002 
1004 static inline FXdouble hypot(FXdouble x,FXdouble y){
1005 #if defined(NO_HYPOT)
1006  return Math::sqrt(Math::sqr(x)+Math::sqr(y));
1007 #else
1008  return ::hypot(x,y);
1009 #endif
1010  }
1011 
1012 
1014 static inline FXfloat lerp(FXfloat u,FXfloat v,FXfloat f){
1015  return (v-u)*f+u;
1016  }
1017 
1018 
1020 static inline FXdouble lerp(FXdouble u,FXdouble v,FXdouble f){
1021  return (v-u)*f+u;
1022  }
1023 
1024 
1026 static inline FXfloat smoothstep(FXfloat f){
1027  return (3.0f-2.0f*f)*f*f;
1028  }
1029 
1031 static inline FXdouble smoothstep(FXdouble f){
1032  return (3.0-2.0*f)*f*f;
1033  }
1034 
1035 
1037 static inline FXfloat exp(FXfloat x){
1038  return ::expf(x);
1039  }
1040 
1042 static inline FXdouble exp(FXdouble x){
1043  return ::exp(x);
1044  }
1045 
1046 
1048 static inline FXfloat expm1(FXfloat x){
1049 #if defined(NO_EXPM1F)
1050  return ::expf(x)-1.0f;
1051 #else
1052  return ::expm1f(x);
1053 #endif
1054  }
1055 
1057 static inline FXdouble expm1(FXdouble x){
1058 #if defined(NO_EXPM1)
1059  return ::exp(x)-1.0;
1060 #else
1061  return ::expm1(x);
1062 #endif
1063  }
1064 
1065 
1067 static inline FXfloat pow(FXfloat x,FXfloat y){
1068  return ::powf(x,y);
1069  }
1070 
1072 static inline FXdouble pow(FXdouble x,FXdouble y){
1073  return ::pow(x,y);
1074  }
1075 
1076 
1078 static inline FXfloat exp2(FXfloat x){
1079 #if defined(NO_EXP2F)
1080  return Math::pow(2.0f,x);
1081 #else
1082  return ::exp2f(x);
1083 #endif
1084  }
1085 
1086 
1088 static inline FXdouble exp2(FXdouble x){
1089 #if defined(NO_EXP2)
1090  return Math::pow(2.0,x);
1091 #else
1092  return ::exp2(x);
1093 #endif
1094  }
1095 
1096 
1098 static inline FXfloat exp10(FXfloat x){
1099 #if defined(NO_EXP10F)
1100  return Math::pow(10.0f,x);
1101 #else
1102  return ::exp10f(x);
1103 #endif
1104  }
1105 
1106 
1108 static inline FXdouble exp10(FXdouble x){
1109 #if defined(NO_EXP10)
1110  return Math::pow(10.0,x);
1111 #else
1112  return ::exp10(x);
1113 #endif
1114  }
1115 
1116 
1118 static inline FXfloat pow10(FXfloat x){
1119  return Math::exp10(x);
1120  }
1121 
1123 static inline FXdouble pow10(FXdouble x){
1124  return Math::exp10(x);
1125  }
1126 
1127 
1129 extern FXAPI FXdouble pow10i(FXint ex);
1130 
1131 
1133 extern FXAPI FXfloat powi(FXfloat base,FXint ex);
1134 
1135 
1137 extern FXAPI FXdouble powi(FXdouble base,FXint ex);
1138 
1139 
1141 static inline FXfloat log(FXfloat x){
1142  return ::logf(x);
1143  }
1144 
1146 static inline FXdouble log(FXdouble x){
1147  return ::log(x);
1148  }
1149 
1150 
1152 static inline FXfloat log1p(FXfloat x){
1153 #if defined(NO_LOG1PF)
1154  return Math::log(1.0f+x);
1155 #else
1156  return ::log1pf(x);
1157 #endif
1158  }
1159 
1160 
1162 static inline FXdouble log1p(FXdouble x){
1163 #if defined(NO_LOG1P)
1164  return Math::log(1.0+x);
1165 #else
1166  return ::log1p(x);
1167 #endif
1168  }
1169 
1170 
1172 static inline FXfloat log2(FXfloat x){
1173 #if defined(NO_LOG2F)
1174  return Math::log(x)*1.442695040888963407359924681001892137f;
1175 #else
1176  return ::log2f(x);
1177 #endif
1178  }
1179 
1180 
1182 static inline FXdouble log2(FXdouble x){
1183 #if defined(NO_LOG2)
1184  return Math::log(x)*1.442695040888963407359924681001892137;
1185 #else
1186  return ::log2(x);
1187 #endif
1188  }
1189 
1190 
1192 static inline FXfloat log10(FXfloat x){
1193  return ::log10f(x);
1194  }
1195 
1197 static inline FXdouble log10(FXdouble x){
1198  return ::log10(x);
1199  }
1200 
1201 
1203 static inline FXuint hash32(FXuint x){
1204  x=((x>>16)^x)*0x21F0AAAD;
1205  x=((x>>15)^x)*0x735A2D97;
1206  x=((x>>15)^x);
1207  return x;
1208  }
1209 
1211 static inline FXuint unhash32(FXuint x){
1212  x=((x>>15)^(x>>30)^x)*0x97132227;
1213  x=((x>>15)^(x>>30)^x)*0x333C4925;
1214  x=((x>>16)^x);
1215  return x;
1216  }
1217 
1218 
1220 static inline FXulong hash64(FXulong x){
1221  x=(x^(x>>30))*FXULONG(0xBF58476D1CE4E5B9);
1222  x=(x^(x>>27))*FXULONG(0x94D049BB133111EB);
1223  x=x^(x>>31);
1224  return x;
1225  }
1226 
1228 static inline FXulong unhash64(FXulong x){
1229  x=(x^(x>>31)^(x>>62))*FXULONG(0x319642B2D24D8EC3);
1230  x=(x^(x>>27)^(x>>54))*FXULONG(0x96DE1B173F119089);
1231  x=x^(x>>30)^(x>>60);
1232  return x;
1233  }
1234 
1235 
1237 static inline FXuint hash32(FXfloat x){
1238  return x!=0.0f ? hash32(fpBits(x)) : 0;
1239  }
1240 
1241 
1243 static inline FXulong hash64(FXdouble x){
1244  return x!=0.0 ? hash64(fpBits(x)) : 0;
1245  }
1246 
1247 
1249 extern FXAPI FXfloat erf(FXfloat x);
1250 
1252 extern FXAPI FXdouble erf(FXdouble x);
1253 
1254 
1256 extern FXAPI FXfloat erfc(FXfloat x);
1257 
1259 extern FXAPI FXdouble erfc(FXdouble x);
1260 
1261 
1263 extern FXAPI FXfloat inverf(FXfloat x);
1264 
1266 extern FXAPI FXdouble inverf(FXdouble x);
1267 
1268 
1270 extern FXAPI FXfloat inverfc(FXfloat x);
1271 
1273 extern FXAPI FXdouble inverfc(FXdouble x);
1274 
1275 
1276 }
1277 
1278 }
1279 
1280 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp