Skip to content

Commit

Permalink
Add constexpr shift_right mp helper
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Apr 12, 2024
1 parent 3801b0a commit 89d0d1b
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 89d0d1b

Please sign in to comment.