From a543fcd410d737913d78ff8ee39fb8b2df81c0e3 Mon Sep 17 00:00:00 2001 From: Tiago Freire <67021355+tmiguelf@users.noreply.github.com> Date: Fri, 10 Nov 2023 11:09:50 +0100 Subject: [PATCH] Fixed compiler warnings (#1697) * fixed warnings used proper math functions * ran clang format * used a more up-to-date clang-format * space twedling * reveretd CMakeLists.txt --- src/benchmark_runner.cc | 2 +- src/complexity.cc | 20 ++++++++++++-------- src/counter.cc | 4 ++-- src/cycleclock.h | 7 ++++--- src/statistics.cc | 9 ++++++--- src/sysinfo.cc | 2 +- src/timers.cc | 3 ++- test/complexity_test.cc | 2 +- 8 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/benchmark_runner.cc b/src/benchmark_runner.cc index f5cd3e64..d35bc30d 100644 --- a/src/benchmark_runner.cc +++ b/src/benchmark_runner.cc @@ -108,7 +108,7 @@ BenchmarkReporter::Run CreateRunReport( report.memory_result = memory_result; report.allocs_per_iter = memory_iterations ? static_cast(memory_result->num_allocs) / - memory_iterations + static_cast(memory_iterations) : 0; } diff --git a/src/complexity.cc b/src/complexity.cc index 825c5739..bee362d1 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -37,12 +37,14 @@ BigOFunc* FittingCurve(BigO complexity) { return [](IterationCount n) -> double { return std::pow(n, 3); }; case oLogN: /* Note: can't use log2 because Android's GNU STL lacks it */ - return - [](IterationCount n) { return kLog2E * log(static_cast(n)); }; + return [](IterationCount n) { + return kLog2E * std::log(static_cast(n)); + }; case oNLogN: /* Note: can't use log2 because Android's GNU STL lacks it */ return [](IterationCount n) { - return kLog2E * n * log(static_cast(n)); + return kLog2E * static_cast(n) * + std::log(static_cast(n)); }; case o1: default: @@ -105,12 +107,12 @@ LeastSq MinimalLeastSq(const std::vector& n, double rms = 0.0; for (size_t i = 0; i < n.size(); ++i) { double fit = result.coef * fitting_curve(n[i]); - rms += pow((time[i] - fit), 2); + rms += std::pow((time[i] - fit), 2); } // Normalized RMS by the mean of the observed values - double mean = sigma_time / n.size(); - result.rms = sqrt(rms / n.size()) / mean; + double mean = sigma_time / static_cast(n.size()); + result.rms = std::sqrt(rms / static_cast(n.size())) / mean; return result; } @@ -171,8 +173,10 @@ std::vector ComputeBigO( BM_CHECK_GT(run.complexity_n, 0) << "Did you forget to call SetComplexityN?"; n.push_back(run.complexity_n); - real_time.push_back(run.real_accumulated_time / run.iterations); - cpu_time.push_back(run.cpu_accumulated_time / run.iterations); + real_time.push_back(run.real_accumulated_time / + static_cast(run.iterations)); + cpu_time.push_back(run.cpu_accumulated_time / + static_cast(run.iterations)); } LeastSq result_cpu; diff --git a/src/counter.cc b/src/counter.cc index cf5b78ee..aa14cd80 100644 --- a/src/counter.cc +++ b/src/counter.cc @@ -27,10 +27,10 @@ double Finish(Counter const& c, IterationCount iterations, double cpu_time, v /= num_threads; } if (c.flags & Counter::kIsIterationInvariant) { - v *= iterations; + v *= static_cast(iterations); } if (c.flags & Counter::kAvgIterations) { - v /= iterations; + v /= static_cast(iterations); } if (c.flags & Counter::kInvert) { // Invert is *always* last. diff --git a/src/cycleclock.h b/src/cycleclock.h index ae1ef2d2..dfc7ae72 100644 --- a/src/cycleclock.h +++ b/src/cycleclock.h @@ -218,9 +218,10 @@ inline BENCHMARK_ALWAYS_INLINE int64_t Now() { asm volatile("%0 = C15:14" : "=r"(pcycle)); return static_cast(pcycle); #else -// The soft failover to a generic implementation is automatic only for ARM. -// For other platforms the developer is expected to make an attempt to create -// a fast implementation and use generic version if nothing better is available. + // The soft failover to a generic implementation is automatic only for ARM. + // For other platforms the developer is expected to make an attempt to create + // a fast implementation and use generic version if nothing better is + // available. #error You need to define CycleTimer for your OS and CPU #endif } diff --git a/src/statistics.cc b/src/statistics.cc index 844e9268..4a639fd2 100644 --- a/src/statistics.cc +++ b/src/statistics.cc @@ -32,7 +32,7 @@ auto StatisticsSum = [](const std::vector& v) { double StatisticsMean(const std::vector& v) { if (v.empty()) return 0.0; - return StatisticsSum(v) * (1.0 / v.size()); + return StatisticsSum(v) * (1.0 / static_cast(v.size())); } double StatisticsMedian(const std::vector& v) { @@ -71,8 +71,11 @@ double StatisticsStdDev(const std::vector& v) { // Sample standard deviation is undefined for n = 1 if (v.size() == 1) return 0.0; - const double avg_squares = SumSquares(v) * (1.0 / v.size()); - return Sqrt(v.size() / (v.size() - 1.0) * (avg_squares - Sqr(mean))); + const double avg_squares = + SumSquares(v) * (1.0 / static_cast(v.size())); + return Sqrt(static_cast(v.size()) / + (static_cast(v.size()) - 1.0) * + (avg_squares - Sqr(mean))); } double StatisticsCV(const std::vector& v) { diff --git a/src/sysinfo.cc b/src/sysinfo.cc index 64aa15e0..88757282 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -655,7 +655,7 @@ double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) { &freq)) { // The value is in kHz (as the file name suggests). For example, on a // 2GHz warpstation, the file contains the value "2000000". - return freq * 1000.0; + return static_cast(freq) * 1000.0; } const double error_value = -1; diff --git a/src/timers.cc b/src/timers.cc index b23feea8..84f48bc2 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -102,7 +102,8 @@ double MakeTime(thread_basic_info_data_t const& info) { #endif #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID) double MakeTime(struct timespec const& ts) { - return ts.tv_sec + (static_cast(ts.tv_nsec) * 1e-9); + return static_cast(ts.tv_sec) + + (static_cast(ts.tv_nsec) * 1e-9); } #endif diff --git a/test/complexity_test.cc b/test/complexity_test.cc index 76891e07..1248a535 100644 --- a/test/complexity_test.cc +++ b/test/complexity_test.cc @@ -175,7 +175,7 @@ BENCHMARK(BM_Complexity_O_N_log_N) ->RangeMultiplier(2) ->Range(1 << 10, 1 << 16) ->Complexity([](benchmark::IterationCount n) { - return kLog2E * static_cast(n) * log(static_cast(n)); + return kLog2E * static_cast(n) * std::log(static_cast(n)); }); BENCHMARK(BM_Complexity_O_N_log_N) ->RangeMultiplier(2)