From 165397e2c5d20853385c9540204a35df57505112 Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Mon, 30 Mar 2020 08:21:26 +0200 Subject: [PATCH] Avoid unnecessary integer overflow of Utilities::pow() --- include/deal.II/base/utilities.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index a1db035464..04218bd02b 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -508,6 +508,10 @@ namespace Utilities // if (iexp <= 0) // return 1; // + // // avoid overflow of one additional recursion with pow(base * base, 0) + // if (iexp == 1) + // return base; + // // // if the current exponent is not divisible by two, // // we need to account for that. // const unsigned int prefactor = (iexp % 2 == 1) ? base : 1; @@ -519,9 +523,11 @@ namespace Utilities static_assert(std::is_integral::value, "Only integral types supported"); - return iexp <= 0 ? 1 : - (((iexp % 2 == 1) ? base : 1) * - dealii::Utilities::pow(base * base, iexp / 2)); + return iexp <= 0 ? + 1 : + (iexp == 1 ? base : + (((iexp % 2 == 1) ? base : 1) * + dealii::Utilities::pow(base * base, iexp / 2))); } /** -- 2.39.5