Skip to content

Commit

Permalink
Merge pull request #4008 from randombit/jack/mp-shift-right
Browse files Browse the repository at this point in the history
Add constexpr shift_right mp helper
  • Loading branch information
randombit authored Apr 12, 2024
2 parents d184e78 + 89d0d1b commit 7113fb4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/lib/math/mp/mp_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -845,15 +845,29 @@ inline constexpr W shift_left(std::array<W, N>& x) {
static_assert(S < WordInfo<W>::bits, "Shift too large");

W carry = 0;
for(size_t j = 0; j != N; ++j) {
const W w = x[j];
x[j] = (w << S) | carry;
for(size_t i = 0; i != N; ++i) {
const W w = x[i];
x[i] = (w << S) | carry;
carry = w >> (WordInfo<W>::bits - S);
}

return carry;
}

template <size_t S, WordType W, size_t N>
inline consteval W shift_right(std::array<W, N>& x) {
static_assert(S < WordInfo<W>::bits, "Shift too large");

W carry = 0;
for(size_t i = 0; i != N; ++i) {
const W w = x[N - 1 - i];
x[N - 1 - i] = (w >> S) | carry;
carry = w << (WordInfo<W>::bits - S);
}

return carry;
}

template <WordType W, size_t N>
consteval auto hex_to_words(const char (&s)[N]) {
// Char count includes null terminator which we ignore
Expand Down

0 comments on commit 7113fb4

Please sign in to comment.