Skip to content

Commit

Permalink
Support isfinite/isinf for std::complex<X>
Browse files Browse the repository at this point in the history
  • Loading branch information
mnijhuis-tos authored and serge-sans-paille committed May 26, 2023
1 parent 86c01ba commit c23d503
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
12 changes: 12 additions & 0 deletions include/xsimd/arch/generic/xsimd_generic_complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ namespace xsimd
{
return batch_bool<T, A>(isnan(self.real()) || isnan(self.imag()));
}

template <class A, class T>
inline batch_bool<T, A> isinf(batch<std::complex<T>, A> const& self, requires_arch<generic>) noexcept
{
return batch_bool<T, A>(isinf(self.real()) || isinf(self.imag()));
}

template <class A, class T>
inline batch_bool<T, A> isfinite(batch<std::complex<T>, A> const& self, requires_arch<generic>) noexcept
{
return batch_bool<T, A>(isfinite(self.real()) && isfinite(self.imag()));
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion include/xsimd/arch/generic/xsimd_generic_details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ namespace xsimd
template <class T, class A>
inline batch_bool<T, A> is_odd(batch<T, A> const& self) noexcept;
template <class T, class A>
inline batch_bool<T, A> isinf(batch<T, A> const& self) noexcept;
inline typename batch<T, A>::batch_bool_type isinf(batch<T, A> const& self) noexcept;
template <class T, class A>
inline typename batch<T, A>::batch_bool_type isfinite(batch<T, A> const& self) noexcept;
template <class T, class A>
inline typename batch<T, A>::batch_bool_type isnan(batch<T, A> const& self) noexcept;
template <class T, class A>
Expand Down
8 changes: 6 additions & 2 deletions include/xsimd/arch/xsimd_scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ namespace xsimd
return 1. / x;
}

#ifdef XSIMD_ENABLE_NUMPY_COMPLEX
template <class T>
inline bool isnan(std::complex<T> var) noexcept
{
Expand All @@ -360,7 +359,12 @@ namespace xsimd
{
return std::isinf(std::real(var)) || std::isinf(std::imag(var));
}
#endif

template <class T>
inline bool isfinite(std::complex<T> var) noexcept
{
return std::isfinite(std::real(var)) && std::isfinite(std::imag(var));
}

#ifdef XSIMD_ENABLE_XTL_COMPLEX
using xtl::abs;
Expand Down
4 changes: 2 additions & 2 deletions include/xsimd/types/xsimd_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ namespace xsimd
* @return a batch of booleans.
*/
template <class T, class A>
inline batch_bool<T, A> isinf(batch<T, A> const& x) noexcept
inline typename batch<T, A>::batch_bool_type isinf(batch<T, A> const& x) noexcept
{
detail::static_check_supported_config<T, A>();
return kernel::isinf<A>(x, A {});
Expand All @@ -1079,7 +1079,7 @@ namespace xsimd
* @return a batch of booleans.
*/
template <class T, class A>
inline batch_bool<T, A> isfinite(batch<T, A> const& x) noexcept
inline typename batch<T, A>::batch_bool_type isfinite(batch<T, A> const& x) noexcept
{
detail::static_check_supported_config<T, A>();
return kernel::isfinite<A>(x, A {});
Expand Down
60 changes: 33 additions & 27 deletions test/test_xsimd_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,6 @@ struct xsimd_api_float_types_functions
value_type val(4);
CHECK_EQ(extract(xsimd::is_odd(T(val))), (val == long(val)) && (long(val) % 2 == 1));
}
void test_isinf()
{
value_type val(4);
CHECK_EQ(extract(xsimd::isinf(T(val))), std::isinf(val));
}
void test_isfinite()
{
value_type val(4);
CHECK_EQ(extract(xsimd::isfinite(T(val))), std::isfinite(val));
}
void test_isnan()
{
value_type val(4);
CHECK_EQ(extract(xsimd::isnan(T(val))), std::isnan(val));
}
void test_ldexp()
{
value_type val0(4);
Expand Down Expand Up @@ -835,18 +820,6 @@ TEST_CASE_TEMPLATE("[xsimd api | float types functions]", B, FLOAT_TYPES)
{
Test.test_is_odd();
}
SUBCASE("isinf")
{
Test.test_isinf();
}
SUBCASE("isfinite")
{
Test.test_isfinite();
}
SUBCASE("isnan")
{
Test.test_isnan();
}
SUBCASE("ldexp")
{
Test.test_ldexp();
Expand Down Expand Up @@ -994,6 +967,24 @@ struct xsimd_api_complex_types_functions
value_type val(1);
CHECK_EQ(extract(xsimd::proj(T(val))), std::proj(val));
}

void test_isinf()
{
value_type val(4);
CHECK_EQ(extract(xsimd::isinf(T(val))), std::isinf(std::real(val)));
}

void test_isfinite()
{
value_type val(4);
CHECK_EQ(extract(xsimd::isfinite(T(val))), std::isfinite(std::real(val)));
}

void test_isnan()
{
value_type val(4);
CHECK_EQ(extract(xsimd::isnan(T(val))), std::isnan(std::real(val)));
}
};

TEST_CASE_TEMPLATE("[xsimd api | complex types functions]", B, COMPLEX_TYPES)
Expand All @@ -1018,6 +1009,21 @@ TEST_CASE_TEMPLATE("[xsimd api | complex types functions]", B, COMPLEX_TYPES)
{
Test.test_proj();
}

SUBCASE("isinf")
{
Test.test_isinf();
}

SUBCASE("isfinite")
{
Test.test_isfinite();
}

SUBCASE("isnan")
{
Test.test_isnan();
}
}

/*
Expand Down

0 comments on commit c23d503

Please sign in to comment.