Skip to content

Commit

Permalink
Added nan to the DaCe math namespace (#1437)
Browse files Browse the repository at this point in the history
Before this was generating an error, because there was no object `nan`
inside the `dace::math` namespace. This commit adds a `nan` object to
the namespace, the implementation is based on `typeless_pi`.
  • Loading branch information
philip-paul-mueller authored Dec 12, 2023
1 parent b6e1c9d commit ae378e1
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
8 changes: 5 additions & 3 deletions dace/runtime/include/dace/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
#ifndef __DACE_MATH_H
#define __DACE_MATH_H

#include "pi.h"
#include "types.h"

#include <complex>
#include <numeric>
#include <cmath>
#include <cfloat>
#include <type_traits>

#include "pi.h"
#include "nan.h"
#include "types.h"

#ifdef __CUDACC__
#include <thrust/complex.h>
#endif
Expand Down Expand Up @@ -457,6 +458,7 @@ namespace dace
namespace math
{
static DACE_CONSTEXPR typeless_pi pi{};
static DACE_CONSTEXPR typeless_nan nan{};
//////////////////////////////////////////////////////
template<typename T>
DACE_CONSTEXPR DACE_HDFI T exp(const T& a)
Expand Down
113 changes: 113 additions & 0 deletions dace/runtime/include/dace/nan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2019-2021 ETH Zurich and the DaCe authors. All rights reserved.
#ifndef __DACE_NAN_H
#define __DACE_NAN_H

// Class to define a stateless NAN and related operators.
#include <limits>

namespace dace
{
namespace math
{
//////////////////////////////////////////////////////
// Defines a typeless Pi
struct typeless_nan
{
operator int() const = delete;
operator float() const
{
return std::numeric_limits<float>::quiet_NaN();
}
operator double() const
{
return std::numeric_limits<double>::quiet_NaN();
}
operator long double() const
{
return std::numeric_limits<long double>::quiet_NaN();
}
typeless_nan operator+() const
{
return typeless_nan{};
}
typeless_nan operator-() const
{
return typeless_nan{};
}
};

template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator*(const T&, const typeless_nan&) { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator*(const typeless_nan&, const T&) { return typeless_nan{}; }

inline typeless_nan
operator*(const typeless_nan&, const typeless_nan&) { return typeless_nan{}; }


template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator+(const T&, const typeless_nan&) { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator+(const typeless_nan&, const T&) { return typeless_nan{}; }

inline typeless_nan
operator+(const typeless_nan&, const typeless_nan&) { return typeless_nan{}; }


template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator-(const T&, const typeless_nan&) { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator-(const typeless_nan&, const T&) { return typeless_nan{}; }

inline typeless_nan
operator-(const typeless_nan&, const typeless_nan&) { return typeless_nan{}; }


template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator/(const T&, const typeless_nan&) { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator/(const typeless_nan&, const T&) { return typeless_nan{}; }

inline typeless_nan
operator/(const typeless_nan&, const typeless_nan&) { return typeless_nan{}; }


template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator%(const T&, const typeless_nan&) { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR typename std::enable_if<std::is_floating_point<T>::value, typeless_nan>::type
operator%(const typeless_nan&, const T&) { return typeless_nan{}; }

inline typeless_nan
operator%(const typeless_nan&, const typeless_nan&) { return typeless_nan{}; }

}
}

//These functions allows to perfrom operations with `typeless_nan` instances.
# define FADAPT(F) DACE_CONSTEXPR ::dace::math::typeless_nan F (::dace::math::typeless_nan) { return ::dace::math::typeless_nan{}; }
# define FADAPT2(F) template<typename T1> DACE_CONSTEXPR dace::math::typeless_nan F (T1&&, dace::math::typeless_nan) { return ::dace::math::typeless_nan{}; }; \
template<typename T2> DACE_CONSTEXPR dace::math::typeless_nan F (dace::math::typeless_nan, T2&&) { return ::dace::math::typeless_nan{}; }; \
DACE_CONSTEXPR ::dace::math::typeless_nan F (dace::math::typeless_nan, dace::math::typeless_nan) { return ::dace::math::typeless_nan{}; }
FADAPT(tanh); FADAPT(cos); FADAPT(sin); FADAPT(sqrt); FADAPT(tan);
FADAPT(acos); FADAPT(asin); FADAPT(atan); FADAPT(log); FADAPT(exp);
FADAPT(floor); FADAPT(ceil); FADAPT(round); FADAPT(abs);
FADAPT2(max); FADAPT2(min);
# undef FADAPT2
# undef FADAPT

#endif // __DACE_NAN_H

0 comments on commit ae378e1

Please sign in to comment.