Skip to content

Commit

Permalink
Merge pull request #128 from chfast/sqr
Browse files Browse the repository at this point in the history
Add sqr(), use it for exp()
  • Loading branch information
chfast authored Jun 17, 2020
2 parents 84be0d0 + 5240545 commit 415ac52
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 415ac52

Please sign in to comment.