Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom implementation of isfinite #1383

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -49,6 +50,7 @@ namespace abs

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand Down Expand Up @@ -106,16 +108,16 @@ template <typename argT, typename resT> struct AbsFunctor
constexpr realT q_nan = std::numeric_limits<realT>::quiet_NaN();
constexpr realT p_inf = std::numeric_limits<realT>::infinity();

if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
return p_inf;
}
else if (std::isinf(y)) {
else if (mu_ns::isinf(y)) {
return p_inf;
}
else if (std::isnan(x)) {
else if (mu_ns::isnan(x)) {
return q_nan;
}
else if (std::isnan(y)) {
else if (mu_ns::isnan(y)) {
return q_nan;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -47,6 +48,7 @@ namespace acos

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand All @@ -73,18 +75,18 @@ template <typename argT, typename resT> struct AcosFunctor
const realT x = std::real(in);
const realT y = std::imag(in);

if (std::isnan(x)) {
if (mu_ns::isnan(x)) {
/* acos(NaN + I*+-Inf) = NaN + I*-+Inf */
if (std::isinf(y)) {
if (mu_ns::isinf(y)) {
return resT{q_nan, -y};
}

/* all other cases involving NaN return NaN + I*NaN. */
return resT{q_nan, q_nan};
}
if (std::isnan(y)) {
if (mu_ns::isnan(y)) {
/* acos(+-Inf + I*NaN) = NaN + I*opt(-)Inf */
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
return resT{q_nan, -std::numeric_limits<realT>::infinity()};
}
/* acos(0 + I*NaN) = PI/2 + I*NaN with inexact */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -47,6 +48,7 @@ namespace acosh

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand Down Expand Up @@ -78,20 +80,20 @@ template <typename argT, typename resT> struct AcoshFunctor
const realT y = std::imag(in);

resT acos_in;
if (std::isnan(x)) {
if (mu_ns::isnan(x)) {
/* acos(NaN + I*+-Inf) = NaN + I*-+Inf */
if (std::isinf(y)) {
if (mu_ns::isinf(y)) {
acos_in = resT{q_nan, -y};
}
else {
acos_in = resT{q_nan, q_nan};
}
}
else if (std::isnan(y)) {
else if (mu_ns::isnan(y)) {
/* acos(+-Inf + I*NaN) = NaN + I*opt(-)Inf */
constexpr realT inf = std::numeric_limits<realT>::infinity();

if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
acos_in = resT{q_nan, -inf};
}
/* acos(0 + I*NaN) = Pi/2 + I*NaN with inexact */
Expand Down Expand Up @@ -126,16 +128,16 @@ template <typename argT, typename resT> struct AcoshFunctor
const realT ry = std::imag(acos_in);

/* acosh(NaN + I*NaN) = NaN + I*NaN */
if (std::isnan(rx) && std::isnan(ry)) {
if (mu_ns::isnan(rx) && mu_ns::isnan(ry)) {
return resT{ry, rx};
}
/* acosh(NaN + I*+-Inf) = +Inf + I*NaN */
/* acosh(+-Inf + I*NaN) = +Inf + I*NaN */
if (std::isnan(rx)) {
if (mu_ns::isnan(rx)) {
return resT{std::abs(ry), rx};
}
/* acosh(0 + I*NaN) = NaN + I*NaN */
if (std::isnan(ry)) {
if (mu_ns::isnan(ry)) {
return resT{ry, ry};
}
/* ordinary cases */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -47,6 +48,7 @@ namespace asin

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand Down Expand Up @@ -80,9 +82,9 @@ template <typename argT, typename resT> struct AsinFunctor
const realT x = std::imag(in);
const realT y = std::real(in);

if (std::isnan(x)) {
if (mu_ns::isnan(x)) {
/* asinh(NaN + I*+-Inf) = opt(+-)Inf + I*NaN */
if (std::isinf(y)) {
if (mu_ns::isinf(y)) {
const realT asinh_re = y;
const realT asinh_im = q_nan;
return resT{asinh_im, asinh_re};
Expand All @@ -96,9 +98,9 @@ template <typename argT, typename resT> struct AsinFunctor
/* All other cases involving NaN return NaN + I*NaN. */
return resT{q_nan, q_nan};
}
else if (std::isnan(y)) {
else if (mu_ns::isnan(y)) {
/* asinh(+-Inf + I*NaN) = +-Inf + I*NaN */
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
const realT asinh_re = x;
const realT asinh_im = q_nan;
return resT{asinh_im, asinh_re};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -47,6 +48,7 @@ namespace asinh

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand All @@ -73,9 +75,9 @@ template <typename argT, typename resT> struct AsinhFunctor
const realT x = std::real(in);
const realT y = std::imag(in);

if (std::isnan(x)) {
if (mu_ns::isnan(x)) {
/* asinh(NaN + I*+-Inf) = opt(+-)Inf + I*NaN */
if (std::isinf(y)) {
if (mu_ns::isinf(y)) {
return resT{y, q_nan};
}
/* asinh(NaN + I*0) = NaN + I*0 */
Expand All @@ -86,9 +88,9 @@ template <typename argT, typename resT> struct AsinhFunctor
return resT{q_nan, q_nan};
}

if (std::isnan(y)) {
if (mu_ns::isnan(y)) {
/* asinh(+-Inf + I*NaN) = +-Inf + I*NaN */
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
return resT{x, q_nan};
}
/* All other cases involving NaN return NaN + I*NaN. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -48,6 +49,7 @@ namespace atan

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand Down Expand Up @@ -79,9 +81,9 @@ template <typename argT, typename resT> struct AtanFunctor
*/
const realT x = std::imag(in);
const realT y = std::real(in);
if (std::isnan(x)) {
if (mu_ns::isnan(x)) {
/* atanh(NaN + I*+-Inf) = sign(NaN)*0 + I*+-Pi/2 */
if (std::isinf(y)) {
if (mu_ns::isinf(y)) {
const realT pi_half = std::atan(realT(1)) * 2;

const realT atanh_re = std::copysign(realT(0), x);
Expand All @@ -93,9 +95,9 @@ template <typename argT, typename resT> struct AtanFunctor
*/
return resT{q_nan, q_nan};
}
else if (std::isnan(y)) {
else if (mu_ns::isnan(y)) {
/* atanh(+-Inf + I*NaN) = +-0 + I*NaN */
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
const realT atanh_re = std::copysign(realT(0), x);
const realT atanh_im = q_nan;
return resT{atanh_im, atanh_re};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -48,6 +49,7 @@ namespace atan2
namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace tu_ns = dpctl::tensor::type_utils;
namespace mu_ns = dpctl::tensor::math_utils;

template <typename argT1, typename argT2, typename resT> struct Atan2Functor
{
Expand All @@ -57,8 +59,8 @@ template <typename argT1, typename argT2, typename resT> struct Atan2Functor

resT operator()(const argT1 &in1, const argT2 &in2)
{
if (std::isinf(in2) && !std::signbit(in2)) {
if (std::isfinite(in1)) {
if (mu_ns::isinf(in2) && !std::signbit(in2)) {
if (mu_ns::isfinite(in1)) {
return std::copysign(resT(0), in1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -48,6 +49,7 @@ namespace atanh

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand All @@ -73,9 +75,9 @@ template <typename argT, typename resT> struct AtanhFunctor
const realT x = std::real(in);
const realT y = std::imag(in);

if (std::isnan(x)) {
if (mu_ns::isnan(x)) {
/* atanh(NaN + I*+-Inf) = sign(NaN)0 + I*+-PI/2 */
if (std::isinf(y)) {
if (mu_ns::isinf(y)) {
const realT pi_half = std::atan(realT(1)) * 2;

const realT res_re = std::copysign(realT(0), x);
Expand All @@ -87,9 +89,9 @@ template <typename argT, typename resT> struct AtanhFunctor
*/
return resT{q_nan, q_nan};
}
else if (std::isnan(y)) {
else if (mu_ns::isnan(y)) {
/* atanh(+-Inf + I*NaN) = +-0 + I*NaN */
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
const realT res_re = std::copysign(realT(0), x);
return resT{res_re, q_nan};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -47,6 +48,7 @@ namespace cos

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand All @@ -73,8 +75,8 @@ template <typename argT, typename resT> struct CosFunctor
realT const &in_re = std::real(in);
realT const &in_im = std::imag(in);

const bool in_re_finite = std::isfinite(in_re);
const bool in_im_finite = std::isfinite(in_im);
const bool in_re_finite = mu_ns::isfinite(in_re);
const bool in_im_finite = mu_ns::isfinite(in_im);

/*
* Handle the nearly-non-exceptional cases where
Expand Down Expand Up @@ -137,7 +139,7 @@ template <typename argT, typename resT> struct CosFunctor
*
* cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
*/
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
if (!yfinite) {
return resT{x * x, std::copysign(q_nan, x)};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "kernels/elementwise_functions/common.hpp"

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand All @@ -47,6 +48,7 @@ namespace cosh

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace mu_ns = dpctl::tensor::math_utils;

using dpctl::tensor::type_utils::is_complex;

Expand All @@ -73,8 +75,8 @@ template <typename argT, typename resT> struct CoshFunctor
const realT x = std::real(in);
const realT y = std::imag(in);

const bool xfinite = std::isfinite(x);
const bool yfinite = std::isfinite(y);
const bool xfinite = mu_ns::isfinite(x);
const bool yfinite = mu_ns::isfinite(y);

/*
* Handle the nearly-non-exceptional cases where
Expand Down Expand Up @@ -126,7 +128,7 @@ template <typename argT, typename resT> struct CoshFunctor
*
* cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
*/
if (std::isinf(x)) {
if (mu_ns::isinf(x)) {
if (!yfinite) {
return resT{x * x, x * q_nan};
}
Expand Down
Loading
Loading