Skip to content

Commit

Permalink
Added a specialization of ipow for signed integers.
Browse files Browse the repository at this point in the history
The original signature of `ipow` only accepted an unsigned integer as exponent.
However, it seems that it was called using integers as exponents.
Thus there is now a specialization for this case.

For the sake of argument let's consider the following situation `ipow(a, b)`:
- If `b` was zero then the previous implementation would return `a`, the new implementation returns 1.
- If `b` was negative then it would perform a lot of iterations, because a signed integer was converted to an unsigned one.
	In the new implementation, the function would return 0, which is in agreement with what `pow` does.
  • Loading branch information
philip-paul-mueller committed Nov 12, 2024
1 parent d355c12 commit def4d42
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dace/runtime/include/dace/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,22 @@ namespace dace
return result;
}


template<
typename T,
typename U,
typename = std::enable_if_t<(std::is_integral<U>::value && std::is_signed<U>::value)>
>
DACE_CONSTEXPR DACE_HDFI T ipow(const T& a, const U b)
{
if(b < 0) {
return T(0);
};
using UnsignedU = std::make_unsigned_t<U>;
return ipow(a, UnsignedU{b});
}


template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
DACE_CONSTEXPR DACE_HDFI T ifloor(const T& a)
{
Expand Down

0 comments on commit def4d42

Please sign in to comment.