Skip to content

Commit ec54e43

Browse files
committed
Add isNan() functions
1 parent c2a0af4 commit ec54e43

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

include/math/simd/vector/float.hpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ static f32x4 normalize3(f32x4 v) noexcept
11721172
}
11731173

11741174
/**
1175-
* @brief Returns true if SIMD vector is normalized with specified tolerance.
1175+
* @brief Returns true if 4D SIMD vector is normalized with specified tolerance.
11761176
*
11771177
* @param v target SIMD vector to check
11781178
* @param tolerance floating point precision tolerance
@@ -1182,7 +1182,7 @@ static bool isNormalized4(f32x4 v, float tolerance = 1.0e-6f) noexcept
11821182
return std::abs(lengthSq4(v) - 1.0f) <= tolerance;
11831183
}
11841184
/**
1185-
* @brief Returns true if SIMD vector is normalized with specified tolerance.
1185+
* @brief Returns true if 3D SIMD vector is normalized with specified tolerance.
11861186
*
11871187
* @param v target SIMD vector to check
11881188
* @param tolerance floating point precision tolerance
@@ -1192,6 +1192,36 @@ static bool isNormalized3(f32x4 v, float tolerance = 1.0e-6f) noexcept
11921192
return std::abs(lengthSq3(v) - 1.0f) <= tolerance;
11931193
}
11941194

1195+
/**
1196+
* @brief Returns true if any SIMD vector element is not a number.
1197+
* @param v target SIMD vector to check
1198+
*/
1199+
static bool isNan4(f32x4 v) noexcept
1200+
{
1201+
#if defined(MATH_SIMD_SUPPORT_SSE)
1202+
return _mm_movemask_ps(_mm_cmpunord_ps(v.data, v.data)) != 0;
1203+
#elif defined(MATH_SIMD_SUPPORT_NEON)
1204+
return vaddvq_u32(vshrq_n_u32(vceqq_f32(v.data, v.data), 31)) != 4;
1205+
#else
1206+
return isNan((float4)v);
1207+
#endif
1208+
}
1209+
/**
1210+
* @brief Returns true if any SIMD vector element is not a number.
1211+
* @param v target SIMD vector to check
1212+
*/
1213+
static bool isNan3(f32x4 v) noexcept
1214+
{
1215+
#if defined(MATH_SIMD_SUPPORT_SSE)
1216+
return (_mm_movemask_ps(_mm_cmpunord_ps(v.data, v.data)) & 0x7) != 0;
1217+
#elif defined(MATH_SIMD_SUPPORT_NEON)
1218+
auto mask = (uint32x4_t){ 1, 1, 1, 0 };
1219+
return vaddvq_u32(vandq_u32(vceqq_f32(v.data, v.data), mask)) != 3;
1220+
#else
1221+
return isNan((float3)v);
1222+
#endif
1223+
}
1224+
11951225
/***********************************************************************************************************************
11961226
* @brief Returns float remainder of numer/denom for each element of the SIMD vector.
11971227
*

include/math/vector/float.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,11 @@ static bool isNormalized(float2 v, float tolerance = 1.0e-6f) noexcept
754754
{
755755
return std::abs(lengthSq(v) - 1.0f) <= tolerance;
756756
}
757+
/**
758+
* @brief Returns true if any vector element is not a number.
759+
* @param v target vector to check
760+
*/
761+
static bool isNan(float2 v) noexcept { return isnan(v.x) || isnan(v.y); }
757762

758763
/**
759764
* @brief Remaps each component of vector to the 0.0 - 1.0 range.
@@ -1038,6 +1043,11 @@ static bool isNormalized(float3 v, float tolerance = 1.0e-6f) noexcept
10381043
{
10391044
return std::abs(lengthSq(v) - 1.0f) <= tolerance;
10401045
}
1046+
/**
1047+
* @brief Returns true if any vector element is not a number.
1048+
* @param v target vector to check
1049+
*/
1050+
static bool isNan(float3 v) noexcept { return isnan(v.x) || isnan(v.y) || isnan(v.z); }
10411051

10421052
/**
10431053
* @brief Remaps each component of vector to the 0.0 - 1.0 range.
@@ -1339,6 +1349,11 @@ static bool isNormalized(float4 v, float tolerance = 1.0e-6f) noexcept
13391349
{
13401350
return std::abs(lengthSq(v) - 1.0f) <= tolerance;
13411351
}
1352+
/**
1353+
* @brief Returns true if any vector element is not a number.
1354+
* @param v target vector to check
1355+
*/
1356+
static bool isNan(float4 v) noexcept { return isnan(v.x) || isnan(v.y) || isnan(v.z) || isnan(v.w); }
13421357

13431358
/**
13441359
* @brief Remaps each component of vector to the 0.0 - 1.0 range.

0 commit comments

Comments
 (0)