Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constexpr shift_right mp helper #4008

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Comment on lines +861 to +866
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'd let the loop run backwards from i = N - 1 to i >0, would that make the indices easier to understand?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the indices would be clearer going in the other direction. But for whatever reason I find the pattern of consistently mapping from i \in [0..N) to an action on that i easier to follow.


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
Loading