Skip to content

Commit

Permalink
avoid repeated function evaluations and force compile-time evaluation
Browse files Browse the repository at this point in the history
improves code performance by an order of magnitude when compiled with Clang-based compilers.
  • Loading branch information
rabauke committed May 9, 2024
1 parent ab92829 commit d5afd97
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions trng/lagfib2plus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ namespace trng {
status_type S;

void step() {
constexpr auto mask_B{int_math::mask(static_cast<decltype(S.index)>(B))};
++S.index;
S.index &= int_math::mask(B);
S.r[S.index] =
S.r[(S.index - A) & int_math::mask(B)] + S.r[(S.index - B) & int_math::mask(B)];
S.index &= mask_B;
S.r[S.index] = S.r[(S.index - A) & mask_B] + S.r[(S.index - B) & mask_B];
}
};

Expand Down
6 changes: 3 additions & 3 deletions trng/lagfib2xor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ namespace trng {
status_type S;

void step() {
constexpr auto mask_B{int_math::mask(static_cast<decltype(S.index)>(B))};
++S.index;
S.index &= int_math::mask(B);
S.r[S.index] =
S.r[(S.index - A) & int_math::mask(B)] ^ S.r[(S.index - B) & int_math::mask(B)];
S.index &= mask_B;
S.r[S.index] = S.r[(S.index - A) & mask_B] ^ S.r[(S.index - B) & mask_B];
}
};

Expand Down
8 changes: 4 additions & 4 deletions trng/lagfib4plus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ namespace trng {
status_type S;

void step() {
constexpr auto mask_D{int_math::mask(static_cast<decltype(S.index)>(D))};
++S.index;
S.index &= int_math::mask(D);
S.r[S.index] =
S.r[(S.index - A) & int_math::mask(D)] + S.r[(S.index - B) & int_math::mask(D)] +
S.r[(S.index - C) & int_math::mask(D)] + S.r[(S.index - D) & int_math::mask(D)];
S.index &= mask_D;
S.r[S.index] = S.r[(S.index - A) & mask_D] + S.r[(S.index - B) & mask_D] +
S.r[(S.index - C) & mask_D] + S.r[(S.index - D) & mask_D];
}
};

Expand Down
8 changes: 4 additions & 4 deletions trng/lagfib4xor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ namespace trng {
status_type S;

void step() {
constexpr auto mask_D{int_math::mask(static_cast<decltype(S.index)>(D))};
++S.index;
S.index &= int_math::mask(D);
S.r[S.index] =
S.r[(S.index - A) & int_math::mask(D)] ^ S.r[(S.index - B) & int_math::mask(D)] ^
S.r[(S.index - C) & int_math::mask(D)] ^ S.r[(S.index - D) & int_math::mask(D)];
S.index &= mask_D;
S.r[S.index] = S.r[(S.index - A) & mask_D] ^ S.r[(S.index - B) & mask_D] ^
S.r[(S.index - C) & mask_D] ^ S.r[(S.index - D) & mask_D];
}
};

Expand Down

0 comments on commit d5afd97

Please sign in to comment.