Skip to content

Commit

Permalink
Merge pull request #649 from boostorg/small_int_powers
Browse files Browse the repository at this point in the history
Try and optimize small integer powers.
  • Loading branch information
jzmaddock authored Jan 17, 2025
2 parents ad1ee2a + d606acf commit 5e1db68
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions include/boost/multiprecision/detail/functions/pow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ inline void pow_imp(T& result, const T& t, const U& p, const std::integral_const
return;
}

switch (p)
{
case 0:
result = int_type(1);
return;
case 1:
result = t;
return;
case 2:
eval_multiply(result, t, t);
return;
case 3:
eval_multiply(result, t, t);
eval_multiply(result, t);
return;
case 4:
eval_multiply(result, t, t);
eval_multiply(result, result);
return;
}

// This will store the result.
if (U(p % U(2)) != U(0))
{
Expand Down

0 comments on commit 5e1db68

Please sign in to comment.