Skip to content

Commit

Permalink
Add sqr(), use it for exp()
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jun 17, 2020
1 parent 84be0d0 commit 5240545
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,17 @@ inline uint<N> mul(const uint<N>& a, const uint<N>& b) noexcept
return {hi, t.lo};
}

template <unsigned N>
inline uint<N> sqr(const uint<N>& a) noexcept
{
// Based on mul() implementation.

const auto t = umul(a.lo, a.lo);
const auto hi = 2 * (a.lo * a.hi) + t.hi;

return {hi, t.lo};
}


template <unsigned N>
constexpr uint<N> constexpr_mul(const uint<N>& a, const uint<N>& b) noexcept
Expand Down Expand Up @@ -600,7 +611,7 @@ constexpr uint<N> exp(uint<N> base, uint<N> exponent) noexcept
{
if ((exponent & 1) != 0)
result *= base;
base *= base;
base = sqr(base);
exponent >>= 1;
}
return result;
Expand Down

0 comments on commit 5240545

Please sign in to comment.