Added rcpSafe.

This commit is contained in:
Бранимир Караџић
2024-10-07 18:57:57 -07:00
parent 296dfb202b
commit b8741eaf6f
3 changed files with 113 additions and 51 deletions

View File

@@ -411,7 +411,7 @@ namespace bx
inline BX_CONST_FUNC float rsqrtRef(float _a)
{
if (_a < kNearZero)
if (_a < kFloatSmallest)
{
return kFloatInfinity;
}
@@ -421,7 +421,7 @@ namespace bx
inline BX_CONST_FUNC float rsqrtSimd(float _a)
{
if (_a < kNearZero)
if (_a < kFloatSmallest)
{
return kFloatInfinity;
}
@@ -441,7 +441,7 @@ namespace bx
inline BX_CONST_FUNC float sqrtRef(float _a)
{
if (_a < 0.0F)
if (_a < 0.0f)
{
return bitsToFloat(kFloatExponentMask | kFloatMantissaMask);
}
@@ -451,11 +451,11 @@ namespace bx
inline BX_CONST_FUNC float sqrtSimd(float _a)
{
if (_a < 0.0F)
if (_a < 0.0f)
{
return bitsToFloat(kFloatExponentMask | kFloatMantissaMask);
}
else if (_a < kNearZero)
else if (_a < kFloatSmallest)
{
return 0.0f;
}
@@ -527,9 +527,14 @@ namespace bx
return 1.0f / _a;
}
inline BX_CONSTEXPR_FUNC float rcpSafe(float _a)
{
return rcp(copySign(max(kFloatSmallest, abs(_a) ), _a) );
}
inline BX_CONSTEXPR_FUNC float mod(float _a, float _b)
{
return _a - _b * floor(_a / _b);
return _a - _b * floor(mul(_a, rcp(_b) ) );
}
inline BX_CONSTEXPR_FUNC bool isEqual(float _a, float _b, float _epsilon)
@@ -803,11 +808,21 @@ namespace bx
return mul(_a, rcp(_b) );
}
inline BX_CONSTEXPR_FUNC Vec3 divSafe(const Vec3 _a, const Vec3 _b)
{
return mul(_a, rcpSafe(_b) );
}
inline BX_CONSTEXPR_FUNC Vec3 div(const Vec3 _a, float _b)
{
return mul(_a, rcp(_b) );
}
inline BX_CONSTEXPR_FUNC Vec3 divSafe(const Vec3 _a, float _b)
{
return mul(_a, rcpSafe(_b) );
}
inline BX_CONSTEXPR_FUNC Vec3 nms(const Vec3 _a, const float _b, const Vec3 _c)
{
return sub(_c, mul(_a, _b) );
@@ -910,9 +925,19 @@ namespace bx
{
return
{
1.0f / _a.x,
1.0f / _a.y,
1.0f / _a.z,
rcp(_a.x),
rcp(_a.y),
rcp(_a.z),
};
}
inline BX_CONSTEXPR_FUNC Vec3 rcpSafe(const Vec3 _a)
{
return
{
rcpSafe(_a.x),
rcpSafe(_a.y),
rcpSafe(_a.z),
};
}

View File

@@ -355,6 +355,10 @@ namespace bx
///
BX_CONSTEXPR_FUNC float rcp(float _a);
/// Returns reciprocal of _a.
///
BX_CONSTEXPR_FUNC float rcpSafe(float _a);
/// Returns the floating-point remainder of the division operation _a/_b.
///
BX_CONSTEXPR_FUNC float mod(float _a, float _b);
@@ -431,9 +435,15 @@ namespace bx
///
BX_CONSTEXPR_FUNC Vec3 div(const Vec3 _a, const Vec3 _b);
///
BX_CONSTEXPR_FUNC Vec3 divSafe(const Vec3 _a, const Vec3 _b);
///
BX_CONSTEXPR_FUNC Vec3 div(const Vec3 _a, float _b);
///
BX_CONSTEXPR_FUNC Vec3 divSafe(const Vec3 _a, float _b);
/// Returns result of negated multiply-sub operation -(_a * _b - _c) -> _c - _a * _b.
///
BX_CONSTEXPR_FUNC Vec3 nms(const Vec3 _a, const float _b, const Vec3 _c);
@@ -482,6 +492,10 @@ namespace bx
///
BX_CONSTEXPR_FUNC Vec3 rcp(const Vec3 _a);
/// Returns component wise reciprocal of _a.
///
BX_CONSTEXPR_FUNC Vec3 rcpSafe(const Vec3 _a);
///
BX_CONSTEXPR_FUNC bool isEqual(const Vec3 _a, const Vec3 _b, float _epsilon);