From def4d4291697ad43c96d747fd30ba659fe7d779e Mon Sep 17 00:00:00 2001 From: Philip Mueller Date: Tue, 12 Nov 2024 08:37:58 +0100 Subject: [PATCH] Added a specialization of `ipow` for signed integers. 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. --- dace/runtime/include/dace/math.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/dace/runtime/include/dace/math.h b/dace/runtime/include/dace/math.h index 747be8bf93..61c329ced3 100644 --- a/dace/runtime/include/dace/math.h +++ b/dace/runtime/include/dace/math.h @@ -589,6 +589,22 @@ namespace dace return result; } + + template< + typename T, + typename U, + typename = std::enable_if_t<(std::is_integral::value && std::is_signed::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; + return ipow(a, UnsignedU{b}); + } + + template::value>::type* = nullptr> DACE_CONSTEXPR DACE_HDFI T ifloor(const T& a) {