Skip to content

Commit

Permalink
fix: Avoid possible unspecified behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Mar 13, 2023
1 parent 3136fe6 commit c58d03e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions benchmark/fibonacci_sequence_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ TEST_CASE(
"Three variables",
[]() {
result_t r{};
// clang-format off
for (auto i{0}, j{1}, tmp{0};
i <= f;
tmp = j + i, i = j, j = tmp)
for (auto i{0}, j{1}, tmp{0}; i <= f;)
{
r += i;

tmp = j + i;
i = j;
j = tmp;
}
// clang-format on
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
"Two variables",
[]() {
result_t r{};
for (auto i{0}, j{1}; i <= f; j += i, i = j - i)
for (auto i{0}, j{1}; i <= f;)
{
r += i;

j += i;
i = j - i;
}
ankerl::nanobench::doNotOptimizeAway(r);
})
Expand All @@ -53,9 +56,12 @@ TEST_CASE(
"Two unsigned variables",
[]() {
result_t r{};
for (std::uint_fast32_t i{0}, j{1}; i <= uf; j += i, i = j - i)
for (std::uint_fast32_t i{0}, j{1}; i <= uf;)
{
r += i;

j += i;
i = j - i;
}
ankerl::nanobench::doNotOptimizeAway(r);
})
Expand All @@ -64,10 +70,12 @@ TEST_CASE(
"Two variables by Creel",
[]() {
result_t r{};
// Source: https://youtu.be/IZc4Odd3K2Q?t=949
for (auto i{0}, j{1}; i <= f; j = (i += j) - j)
// Adapted from: https://youtu.be/IZc4Odd3K2Q?t=949
for (auto i{0}, j{1}; i <= f;)
{
r += i;

j = (i += j) - j;
}
ankerl::nanobench::doNotOptimizeAway(r);
})
Expand Down

0 comments on commit c58d03e

Please sign in to comment.