From 82d22578f9e6da52b7be2b616b3f9552256b33b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 31 Jan 2018 19:48:46 -0800 Subject: [PATCH] Added BX_CONST_FUNC macro. --- include/bx/inline/math.inl | 98 +++++++++++++++++----------------- include/bx/macros.h | 8 +++ include/bx/math.h | 106 ++++++++++++++++++------------------- src/math.cpp | 16 +++--- 4 files changed, 118 insertions(+), 110 deletions(-) diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index f582363..26c4cd2 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -13,41 +13,41 @@ namespace bx { - inline float toRad(float _deg) + inline BX_CONST_FUNC float toRad(float _deg) { return _deg * kPi / 180.0f; } - inline float toDeg(float _rad) + inline BX_CONST_FUNC float toDeg(float _rad) { return _rad * 180.0f / kPi; } - inline uint32_t floatToBits(float _a) + inline BX_CONST_FUNC uint32_t floatToBits(float _a) { union { float f; uint32_t ui; } u = { _a }; return u.ui; } - inline float bitsToFloat(uint32_t _a) + inline BX_CONST_FUNC float bitsToFloat(uint32_t _a) { union { uint32_t ui; float f; } u = { _a }; return u.f; } - inline uint64_t doubleToBits(double _a) + inline BX_CONST_FUNC uint64_t doubleToBits(double _a) { union { double f; uint64_t ui; } u = { _a }; return u.ui; } - inline double bitsToDouble(uint64_t _a) + inline BX_CONST_FUNC double bitsToDouble(uint64_t _a) { union { uint64_t ui; double f; } u = { _a }; return u.f; } - inline uint32_t floatFlip(uint32_t _value) + inline BX_CONST_FUNC uint32_t floatFlip(uint32_t _value) { // Reference: // http://archive.fo/2012.12.08-212402/http://stereopsis.com/radix.html @@ -58,98 +58,98 @@ namespace bx return result; } - inline bool isNan(float _f) + inline BX_CONST_FUNC bool isNan(float _f) { const uint32_t tmp = floatToBits(_f) & INT32_MAX; return tmp > UINT32_C(0x7f800000); } - inline bool isNan(double _f) + inline BX_CONST_FUNC bool isNan(double _f) { const uint64_t tmp = doubleToBits(_f) & INT64_MAX; return tmp > UINT64_C(0x7ff0000000000000); } - inline bool isFinite(float _f) + inline BX_CONST_FUNC bool isFinite(float _f) { const uint32_t tmp = floatToBits(_f) & INT32_MAX; return tmp < UINT32_C(0x7f800000); } - inline bool isFinite(double _f) + inline BX_CONST_FUNC bool isFinite(double _f) { const uint64_t tmp = doubleToBits(_f) & INT64_MAX; return tmp < UINT64_C(0x7ff0000000000000); } - inline bool isInfinite(float _f) + inline BX_CONST_FUNC bool isInfinite(float _f) { const uint32_t tmp = floatToBits(_f) & INT32_MAX; return tmp == UINT32_C(0x7f800000); } - inline bool isInfinite(double _f) + inline BX_CONST_FUNC bool isInfinite(double _f) { const uint64_t tmp = doubleToBits(_f) & INT64_MAX; return tmp == UINT64_C(0x7ff0000000000000); } - inline float round(float _f) + inline BX_CONST_FUNC float round(float _f) { return floor(_f + 0.5f); } - inline float ceil(float _a) + inline BX_CONST_FUNC float ceil(float _a) { return -floor(-_a); } - inline float lerp(float _a, float _b, float _t) + inline BX_CONST_FUNC float lerp(float _a, float _b, float _t) { return _a + (_b - _a) * _t; } - inline float abs(float _a) + inline BX_CONST_FUNC float abs(float _a) { return _a < 0.0f ? -_a : _a; } - inline float sign(float _a) + inline BX_CONST_FUNC float sign(float _a) { return _a < 0.0f ? -1.0f : 1.0f; } - inline float square(float _a) + inline BX_CONST_FUNC float square(float _a) { return _a * _a; } - inline float sin(float _a) + inline BX_CONST_FUNC float sin(float _a) { return cos(_a - kPiHalf); } - inline float sinh(float _a) + inline BX_CONST_FUNC float sinh(float _a) { return 0.5f*(exp(_a) - exp(-_a) ); } - inline float asin(float _a) + inline BX_CONST_FUNC float asin(float _a) { return kPiHalf - acos(_a); } - inline float cosh(float _a) + inline BX_CONST_FUNC float cosh(float _a) { return 0.5f*(exp(_a) + exp(-_a) ); } - inline float tan(float _a) + inline BX_CONST_FUNC float tan(float _a) { return sin(_a) / cos(_a); } - inline float tanh(float _a) + inline BX_CONST_FUNC float tanh(float _a) { const float tmp0 = exp(2.0f*_a); const float tmp1 = tmp0 - 1.0f; @@ -159,27 +159,27 @@ namespace bx return result; } - inline float atan(float _a) + inline BX_CONST_FUNC float atan(float _a) { return atan2(_a, 1.0f); } - inline float pow(float _a, float _b) + inline BX_CONST_FUNC float pow(float _a, float _b) { return exp(_b * log(_a) ); } - inline float exp2(float _a) + inline BX_CONST_FUNC float exp2(float _a) { return pow(2.0f, _a); } - inline float log2(float _a) + inline BX_CONST_FUNC float log2(float _a) { return log(_a) * kInvLogNat2; } - inline float sqrtRef(float _a) + inline BX_CONST_FUNC float sqrtRef(float _a) { if (_a < kNearZero) { @@ -189,7 +189,7 @@ namespace bx return 1.0f/rsqrt(_a); } - inline float sqrtSimd(float _a) + inline BX_CONST_FUNC float sqrtSimd(float _a) { const simd128_t aa = simd_splat(_a); const simd128_t sqrta = simd_sqrt(aa); @@ -199,7 +199,7 @@ namespace bx return result; } - inline float sqrt(float _a) + inline BX_CONST_FUNC float sqrt(float _a) { #if BX_CONFIG_SUPPORTS_SIMD return sqrtSimd(_a); @@ -208,12 +208,12 @@ namespace bx #endif // BX_CONFIG_SUPPORTS_SIMD } - inline float rsqrtRef(float _a) + inline BX_CONST_FUNC float rsqrtRef(float _a) { return pow(_a, -0.5f); } - inline float rsqrtSimd(float _a) + inline BX_CONST_FUNC float rsqrtSimd(float _a) { if (_a < kNearZero) { @@ -228,7 +228,7 @@ namespace bx return result; } - inline float rsqrt(float _a) + inline BX_CONST_FUNC float rsqrt(float _a) { #if BX_CONFIG_SUPPORTS_SIMD return rsqrtSimd(_a); @@ -237,27 +237,27 @@ namespace bx #endif // BX_CONFIG_SUPPORTS_SIMD } - inline float trunc(float _a) + inline BX_CONST_FUNC float trunc(float _a) { return float(int(_a) ); } - inline float fract(float _a) + inline BX_CONST_FUNC float fract(float _a) { return _a - trunc(_a); } - inline float mad(float _a, float _b, float _c) + inline BX_CONST_FUNC float mad(float _a, float _b, float _c) { return _a * _b + _c; } - inline float mod(float _a, float _b) + inline BX_CONST_FUNC float mod(float _a, float _b) { return _a - _b * floor(_a / _b); } - inline bool equal(float _a, float _b, float _epsilon) + inline BX_CONST_FUNC bool equal(float _a, float _b, float _epsilon) { // http://realtimecollisiondetection.net/blog/?p=89 const float lhs = abs(_a - _b); @@ -265,7 +265,7 @@ namespace bx return lhs <= rhs; } - inline bool equal(const float* _a, const float* _b, uint32_t _num, float _epsilon) + inline BX_CONST_FUNC bool equal(const float* _a, const float* _b, uint32_t _num, float _epsilon) { bool result = equal(_a[0], _b[0], _epsilon); for (uint32_t ii = 1; result && ii < _num; ++ii) @@ -275,34 +275,34 @@ namespace bx return result; } - inline float wrap(float _a, float _wrap) + inline BX_CONST_FUNC float wrap(float _a, float _wrap) { const float tmp0 = mod(_a, _wrap); const float result = tmp0 < 0.0f ? _wrap + tmp0 : tmp0; return result; } - inline float step(float _edge, float _a) + inline BX_CONST_FUNC float step(float _edge, float _a) { return _a < _edge ? 0.0f : 1.0f; } - inline float pulse(float _a, float _start, float _end) + inline BX_CONST_FUNC float pulse(float _a, float _start, float _end) { return step(_a, _start) - step(_a, _end); } - inline float smoothStep(float _a) + inline BX_CONST_FUNC float smoothStep(float _a) { return square(_a)*(3.0f - 2.0f*_a); } - inline float bias(float _time, float _bias) + inline BX_CONST_FUNC float bias(float _time, float _bias) { return _time / ( ( (1.0f/_bias - 2.0f)*(1.0f - _time) ) + 1.0f); } - inline float gain(float _time, float _gain) + inline BX_CONST_FUNC float gain(float _time, float _gain) { if (_time < 0.5f) { @@ -312,13 +312,13 @@ namespace bx return bias(_time * 2.0f - 1.0f, 1.0f - _gain) * 0.5f + 0.5f; } - inline float angleDiff(float _a, float _b) + inline BX_CONST_FUNC float angleDiff(float _a, float _b) { const float dist = wrap(_b - _a, kPi2); return wrap(dist*2.0f, kPi2) - dist; } - inline float angleLerp(float _a, float _b, float _t) + inline BX_CONST_FUNC float angleLerp(float _a, float _b, float _t) { return _a + angleDiff(_a, _b) * _t; } diff --git a/include/bx/macros.h b/include/bx/macros.h index cdb4bc3..f52f4d3 100644 --- a/include/bx/macros.h +++ b/include/bx/macros.h @@ -70,20 +70,27 @@ # define BX_UNLIKELY(_x) __builtin_expect(!!(_x), 0) # define BX_NO_INLINE __attribute__( (noinline) ) # define BX_NO_RETURN __attribute__( (noreturn) ) +# define BX_CONST_FUNC __attribute__( (const) ) + # if BX_COMPILER_GCC >= 70000 # define BX_FALLTHROUGH __attribute__( (fallthrough) ) # else # define BX_FALLTHROUGH BX_NOOP() # endif // BX_COMPILER_GCC >= 70000 + # define BX_NO_VTABLE # define BX_PRINTF_ARGS(_format, _args) __attribute__( (format(__printf__, _format, _args) ) ) + # if BX_CLANG_HAS_FEATURE(cxx_thread_local) # define BX_THREAD_LOCAL __thread # endif // BX_COMPILER_CLANG + # if (!BX_PLATFORM_OSX && (BX_COMPILER_GCC >= 40200)) || (BX_COMPILER_GCC >= 40500) # define BX_THREAD_LOCAL __thread # endif // BX_COMPILER_GCC + # define BX_ATTRIBUTE(_x) __attribute__( (_x) ) + # if BX_CRT_MSVC # define __stdcall # endif // BX_CRT_MSVC @@ -96,6 +103,7 @@ # define BX_UNLIKELY(_x) (_x) # define BX_NO_INLINE __declspec(noinline) # define BX_NO_RETURN +# define BX_CONST_FUNC __declspec(noalias) # define BX_FALLTHROUGH BX_NOOP() # define BX_NO_VTABLE __declspec(novtable) # define BX_PRINTF_ARGS(_format, _args) diff --git a/include/bx/math.h b/include/bx/math.h index 432a533..f9d4493 100644 --- a/include/bx/math.h +++ b/include/bx/math.h @@ -54,212 +54,212 @@ namespace bx /// Returns converted the argument _deg to radians. /// - float toRad(float _deg); + BX_CONST_FUNC float toRad(float _deg); /// Returns converted the argument _rad to degrees. /// - float toDeg(float _rad); + BX_CONST_FUNC float toDeg(float _rad); /// Reinterprets the bit pattern of _a as uint32_t. /// - uint32_t floatToBits(float _a); + BX_CONST_FUNC uint32_t floatToBits(float _a); /// Reinterprets the bit pattern of _a as float. /// - float bitsToFloat(uint32_t _a); + BX_CONST_FUNC float bitsToFloat(uint32_t _a); /// Reinterprets the bit pattern of _a as uint64_t. /// - uint64_t doubleToBits(double _a); + BX_CONST_FUNC uint64_t doubleToBits(double _a); /// Reinterprets the bit pattern of _a as double. /// - double bitsToDouble(uint64_t _a); + BX_CONST_FUNC double bitsToDouble(uint64_t _a); /// Returns sortable floating point value. /// - uint32_t floatFlip(uint32_t _value); + BX_CONST_FUNC uint32_t floatFlip(uint32_t _value); /// Returns true if _f is a number that is NaN. /// - bool isNan(float _f); + BX_CONST_FUNC bool isNan(float _f); /// Returns true if _f is a number that is NaN. /// - bool isNan(double _f); + BX_CONST_FUNC bool isNan(double _f); /// Returns true if _f is not infinite and is not a NaN. /// - bool isFinite(float _f); + BX_CONST_FUNC bool isFinite(float _f); /// Returns true if _f is not infinite and is not a NaN. /// - bool isFinite(double _f); + BX_CONST_FUNC bool isFinite(double _f); /// Returns true if _f is infinite and is not a NaN. /// - bool isInfinite(float _f); + BX_CONST_FUNC bool isInfinite(float _f); /// Returns true if _f is infinite and is not a NaN. /// - bool isInfinite(double _f); + BX_CONST_FUNC bool isInfinite(double _f); /// Returns the largest integer value not greater than _f. /// - float floor(float _f); + BX_CONST_FUNC float floor(float _f); /// Returns the smallest integer value not less than _f. /// - float ceil(float _f); + BX_CONST_FUNC float ceil(float _f); /// Returns the nearest integer value to _f, rounding halfway cases away from zero, /// - float round(float _f); + BX_CONST_FUNC float round(float _f); /// Returns linear interpolation between two values _a and _b. /// - float lerp(float _a, float _b, float _t); + BX_CONST_FUNC float lerp(float _a, float _b, float _t); /// Returns the sign of _a. /// - float sign(float _a); + BX_CONST_FUNC float sign(float _a); /// Returns the absolute of _a. /// - float abs(float _a); + BX_CONST_FUNC float abs(float _a); /// Returns the square of _a. /// - float square(float _a); + BX_CONST_FUNC float square(float _a); /// Returns the cosine of the argument _a. /// - float sin(float _a); + BX_CONST_FUNC float sin(float _a); /// Returns hyperbolic sine of the argument _a. /// - float sinh(float _a); + BX_CONST_FUNC float sinh(float _a); /// Returns radian angle between -pi/2 and +pi/2 whose sine is _a. /// - float asin(float _a); + BX_CONST_FUNC float asin(float _a); /// Returns the cosine of the argument _a. /// - float cos(float _a); + BX_CONST_FUNC float cos(float _a); /// Returns hyperbolic cosine of the argument _a. /// - float cosh(float _a); + BX_CONST_FUNC float cosh(float _a); /// Returns radian angle between 0 and pi whose cosine is _a. /// - float acos(float _a); + BX_CONST_FUNC float acos(float _a); /// Returns the circular tangent of the radian argument _a. /// - float tan(float _a); + BX_CONST_FUNC float tan(float _a); /// Returns hyperbolic tangent of the argument _a. /// - float tanh(float _a); + BX_CONST_FUNC float tanh(float _a); /// Returns radian angle between -pi/2 and +pi/2 whose tangent is _a. /// - float atan(float _a); + BX_CONST_FUNC float atan(float _a); /// Retruns the inverse tangent of _y/_x. /// - float atan2(float _y, float _x); + BX_CONST_FUNC float atan2(float _y, float _x); /// Computes _a raised to the _b power. /// - float pow(float _a, float _b); + BX_CONST_FUNC float pow(float _a, float _b); /// Returns the result of multiplying _a by 2 raised to the power of the exponent. /// - float ldexp(float _a, int32_t _b); + BX_CONST_FUNC float ldexp(float _a, int32_t _b); /// Returns decomposed given floating point value _a into a normalized fraction and /// an integral power of two. /// - float frexp(float _a, int32_t* _outExp); + BX_CONST_FUNC float frexp(float _a, int32_t* _outExp); /// Returns e (2.71828...) raised to the _a power. /// - float exp(float _a); + BX_CONST_FUNC float exp(float _a); /// Returns 2 raised to the _a power. /// - float exp2(float _a); + BX_CONST_FUNC float exp2(float _a); /// Returns the base e (2.71828...) logarithm of _a. /// - float log(float _a); + BX_CONST_FUNC float log(float _a); /// Returns the base 2 logarithm of _a. /// - float log2(float _a); + BX_CONST_FUNC float log2(float _a); /// Returns the square root of _a. /// - float sqrt(float _a); + BX_CONST_FUNC float sqrt(float _a); /// Returns reciprocal square root of _a. /// - float rsqrt(float _a); + BX_CONST_FUNC float rsqrt(float _a); /// Returns the nearest integer not greater in magnitude than _a. /// - float trunc(float _a); + BX_CONST_FUNC float trunc(float _a); /// Returns the fractional (or decimal) part of _a, which is greater than or equal to 0 /// and less than 1. /// - float fract(float _a); + BX_CONST_FUNC float fract(float _a); /// Returns result of multipla and add (_a * _b + _c). /// - float mad(float _a, float _b, float _c); + BX_CONST_FUNC float mad(float _a, float _b, float _c); /// Returns the floating-point remainder of the division operation _a/_b. /// - float mod(float _a, float _b); + BX_CONST_FUNC float mod(float _a, float _b); /// - bool equal(float _a, float _b, float _epsilon); + BX_CONST_FUNC bool equal(float _a, float _b, float _epsilon); /// - bool equal(const float* _a, const float* _b, uint32_t _num, float _epsilon); + BX_CONST_FUNC bool equal(const float* _a, const float* _b, uint32_t _num, float _epsilon); /// - float wrap(float _a, float _wrap); + BX_CONST_FUNC float wrap(float _a, float _wrap); /// - float step(float _edge, float _a); + BX_CONST_FUNC float step(float _edge, float _a); /// - float pulse(float _a, float _start, float _end); + BX_CONST_FUNC float pulse(float _a, float _start, float _end); /// - float smoothStep(float _a); + BX_CONST_FUNC float smoothStep(float _a); // References: // - Bias And Gain Are Your Friend // http://blog.demofox.org/2012/09/24/bias-and-gain-are-your-friend/ // - http://demofox.org/biasgain.html /// - float bias(float _time, float _bias); + BX_CONST_FUNC float bias(float _time, float _bias); /// - float gain(float _time, float _gain); + BX_CONST_FUNC float gain(float _time, float _gain); /// - float angleDiff(float _a, float _b); + BX_CONST_FUNC float angleDiff(float _a, float _b); /// Returns shortest distance linear interpolation between two angles. /// - float angleLerp(float _a, float _b, float _t); + BX_CONST_FUNC float angleLerp(float _a, float _b, float _t); /// void vec3Move(float* _result, const float* _a); diff --git a/src/math.cpp b/src/math.cpp index 87948f6..874b9d4 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -41,7 +41,7 @@ namespace bx } // namespace - float cos(float _a) + BX_CONST_FUNC float cos(float _a) { const float scaled = _a * 2.0f*kInvPi; const float real = floor(scaled); @@ -93,7 +93,7 @@ namespace bx } // namespace - float acos(float _a) + BX_CONST_FUNC float acos(float _a) { const float absa = abs(_a); const float tmp0 = mad(kAcosC3, absa, kAcosC2); @@ -118,7 +118,7 @@ namespace bx } // namespace - float atan2(float _y, float _x) + BX_CONST_FUNC float atan2(float _y, float _x) { const float ax = abs(_x); const float ay = abs(_y); @@ -139,7 +139,7 @@ namespace bx return result; } - float ldexp(float _a, int32_t _b) + BX_CONST_FUNC float ldexp(float _a, int32_t _b) { const uint32_t ftob = floatToBits(_a); const uint32_t masked = uint32_and(ftob, UINT32_C(0xff800000) ); @@ -153,7 +153,7 @@ namespace bx return result; } - float frexp(float _a, int32_t* _outExp) + BX_CONST_FUNC float frexp(float _a, int32_t* _outExp) { const uint32_t ftob = floatToBits(_a); const uint32_t masked0 = uint32_and(ftob, UINT32_C(0x7f800000) ); @@ -177,7 +177,7 @@ namespace bx } // namespace - float exp(float _a) + BX_CONST_FUNC float exp(float _a) { if (abs(_a) <= kNearZero) { @@ -213,7 +213,7 @@ namespace bx } // namespace - float log(float _a) + BX_CONST_FUNC float log(float _a) { int32_t exp; float ff = frexp(_a, &exp); @@ -247,7 +247,7 @@ namespace bx return result; } - float floor(float _a) + BX_CONST_FUNC float floor(float _a) { if (_a < 0.0f) {