From 4d7834291995a3f7a2e8de5008d2eefa1281769e Mon Sep 17 00:00:00 2001 From: Hamdi Sahloul Date: Thu, 23 Aug 2018 22:50:36 +0900 Subject: [PATCH] Closes #12281 - a bug in cuda::pow with negative base values --- modules/cudaarithm/src/cuda/math.cu | 31 ++++++++--------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/modules/cudaarithm/src/cuda/math.cu b/modules/cudaarithm/src/cuda/math.cu index 41d762f6a6..b885319659 100644 --- a/modules/cudaarithm/src/cuda/math.cu +++ b/modules/cudaarithm/src/cuda/math.cu @@ -278,20 +278,12 @@ namespace { template::is_signed> struct PowOp : unary_function { - float power; + typedef typename LargerType::type LargerType; + LargerType power; __device__ __forceinline__ T operator()(T e) const { - return cudev::saturate_cast(__powf((float)e, power)); - } - }; - template struct PowOp : unary_function - { - float power; - - __device__ __forceinline__ T operator()(T e) const - { - T res = cudev::saturate_cast(__powf((float)e, power)); + T res = cudev::saturate_cast(__powf(e < 0 ? -e : e, power)); if ((e < 0) && (1 & static_cast(power))) res *= -1; @@ -299,22 +291,15 @@ namespace return res; } }; - template<> struct PowOp : unary_function - { - float power; - __device__ __forceinline__ float operator()(float e) const - { - return __powf(::fabs(e), power); - } - }; - template<> struct PowOp : unary_function + template struct PowOp : unary_function { - double power; + typedef typename LargerType::type LargerType; + LargerType power; - __device__ __forceinline__ double operator()(double e) const + __device__ __forceinline__ T operator()(T e) const { - return ::pow(::fabs(e), power); + return cudev::saturate_cast(__powf(e, power)); } };