From c58d03e6a0702901fe3dbec7deef24a8bbdf3e5e Mon Sep 17 00:00:00 2001 From: Omar Boukli-Hacene Date: Wed, 13 Sep 2023 22:10:17 +0200 Subject: [PATCH] fix: Avoid possible unspecified behavior --- benchmark/fibonacci_sequence_benchmark.cpp | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/benchmark/fibonacci_sequence_benchmark.cpp b/benchmark/fibonacci_sequence_benchmark.cpp index 1b66b02..a855f9c 100644 --- a/benchmark/fibonacci_sequence_benchmark.cpp +++ b/benchmark/fibonacci_sequence_benchmark.cpp @@ -27,14 +27,14 @@ 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); }) @@ -42,9 +42,12 @@ TEST_CASE( "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); }) @@ -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); }) @@ -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); })