From d1130c9d395d57b035b8738f6e993ca9c9ff8621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Mon, 1 Aug 2022 14:00:14 +0200 Subject: [PATCH 1/3] Suppress float-equal warning --- src/core/include/units/bits/constexpr_math.h | 4 ++++ src/core/include/units/bits/external/hacks.h | 2 ++ src/core/include/units/magnitude.h | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/core/include/units/bits/constexpr_math.h b/src/core/include/units/bits/constexpr_math.h index 1a3e719d..da7fc2cf 100644 --- a/src/core/include/units/bits/constexpr_math.h +++ b/src/core/include/units/bits/constexpr_math.h @@ -23,6 +23,7 @@ #pragma once #include +#include #include #include #include @@ -36,9 +37,12 @@ struct decimal_fp { [[nodiscard]] constexpr decimal_fp to_decimal(double v) noexcept { + UNITS_DIAGNOSTIC_PUSH + UNITS_DIAGNOSTIC_IGNORE_FLOAT_EQUAL if (v == 0) { return {.significant = 0.0, .exponent = 0}; } + UNITS_DIAGNOSTIC_POP double significant = abs(v); std::intmax_t exponent = 0; diff --git a/src/core/include/units/bits/external/hacks.h b/src/core/include/units/bits/external/hacks.h index e08faec9..8d142727 100644 --- a/src/core/include/units/bits/external/hacks.h +++ b/src/core/include/units/bits/external/hacks.h @@ -45,6 +45,7 @@ UNITS_PRAGMA(GCC diagnostic ignored "-Wunknown-warning-option") \ UNITS_PRAGMA(GCC diagnostic ignored X) #define UNITS_DIAGNOSTIC_IGNORE_EXPR_ALWAYS_TF +#define UNITS_DIAGNOSTIC_IGNORE_FLOAT_EQUAL UNITS_DIAGNOSTIC_IGNORE("-Wfloat-equal") #define UNITS_DIAGNOSTIC_IGNORE_LOSS_OF_DATA #define UNITS_DIAGNOSTIC_IGNORE_MISSING_BRACES UNITS_DIAGNOSTIC_IGNORE("-Wmissing-braces") #define UNITS_DIAGNOSTIC_IGNORE_NON_TEMPLATE_FRIEND UNITS_DIAGNOSTIC_IGNORE("-Wnon-template-friend") @@ -56,6 +57,7 @@ #define UNITS_DIAGNOSTIC_IGNORE_PRAGMAS UNITS_PRAGMA(warning(disable : 4068)) #define UNITS_DIAGNOSTIC_IGNORE(X) UNITS_DIAGNOSTIC_IGNORE_PRAGMAS UNITS_PRAGMA(warning(disable : X)) #define UNITS_DIAGNOSTIC_IGNORE_EXPR_ALWAYS_TF UNITS_DIAGNOSTIC_IGNORE(4296) +#define UNITS_DIAGNOSTIC_IGNORE_FLOAT_EQUAL #define UNITS_DIAGNOSTIC_IGNORE_LOSS_OF_DATA UNITS_DIAGNOSTIC_IGNORE(4244) #define UNITS_DIAGNOSTIC_IGNORE_MISSING_BRACES #define UNITS_DIAGNOSTIC_IGNORE_NON_TEMPLATE_FRIEND diff --git a/src/core/include/units/magnitude.h b/src/core/include/units/magnitude.h index c81fb538..a4de36d9 100644 --- a/src/core/include/units/magnitude.h +++ b/src/core/include/units/magnitude.h @@ -155,9 +155,12 @@ constexpr T int_power(T base, std::integral auto exp) constexpr auto checked_multiply = [](auto a, auto b) { const auto result = a * b; + UNITS_DIAGNOSTIC_PUSH + UNITS_DIAGNOSTIC_IGNORE_FLOAT_EQUAL if (result / a != b) { throw std::overflow_error{"Wraparound detected"}; } + UNITS_DIAGNOSTIC_POP return result; }; From cce0d2c12b098a9b17c7f9238b6c2897406df52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Mon, 1 Aug 2022 16:13:19 +0200 Subject: [PATCH 2/3] Supress "zero as null pointer constant" warning --- src/core/include/units/bits/algorithm.h | 3 +++ src/core/include/units/bits/external/hacks.h | 2 ++ src/core/include/units/symbol_text.h | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/src/core/include/units/bits/algorithm.h b/src/core/include/units/bits/algorithm.h index 762bb7bc..1ac80362 100644 --- a/src/core/include/units/bits/algorithm.h +++ b/src/core/include/units/bits/algorithm.h @@ -80,8 +80,11 @@ constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp bool exhaust1 = (f1 == l1); bool exhaust2 = (f2 == l2); + UNITS_DIAGNOSTIC_PUSH + UNITS_DIAGNOSTIC_IGNORE_ZERO_AS_NULLPOINTER_CONSTANT for (; !exhaust1 && !exhaust2; exhaust1 = (++f1 == l1), exhaust2 = (++f2 == l2)) if (auto c = comp(*f1, *f2); c != 0) return c; + UNITS_DIAGNOSTIC_POP return !exhaust1 ? std::strong_ordering::greater : !exhaust2 ? std::strong_ordering::less diff --git a/src/core/include/units/bits/external/hacks.h b/src/core/include/units/bits/external/hacks.h index 8d142727..67c1086c 100644 --- a/src/core/include/units/bits/external/hacks.h +++ b/src/core/include/units/bits/external/hacks.h @@ -51,6 +51,7 @@ #define UNITS_DIAGNOSTIC_IGNORE_NON_TEMPLATE_FRIEND UNITS_DIAGNOSTIC_IGNORE("-Wnon-template-friend") #define UNITS_DIAGNOSTIC_IGNORE_SHADOW UNITS_DIAGNOSTIC_IGNORE("-Wshadow") #define UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE +#define UNITS_DIAGNOSTIC_IGNORE_ZERO_AS_NULLPOINTER_CONSTANT UNITS_DIAGNOSTIC_IGNORE("-Wzero-as-nullpointer-constant") #else #define UNITS_DIAGNOSTIC_PUSH UNITS_PRAGMA(warning(push)) #define UNITS_DIAGNOSTIC_POP UNITS_PRAGMA(warning(pop)) @@ -63,6 +64,7 @@ #define UNITS_DIAGNOSTIC_IGNORE_NON_TEMPLATE_FRIEND #define UNITS_DIAGNOSTIC_IGNORE_SHADOW UNITS_DIAGNOSTIC_IGNORE(4459) #define UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE UNITS_DIAGNOSTIC_IGNORE(4702) +#define UNITS_DIAGNOSTIC_IGNORE_ZERO_AS_NULLPOINTER_CONSTANT #endif #if _LIBCPP_VERSION diff --git a/src/core/include/units/symbol_text.h b/src/core/include/units/symbol_text.h index adf138e5..6eb71e6d 100644 --- a/src/core/include/units/symbol_text.h +++ b/src/core/include/units/symbol_text.h @@ -24,6 +24,7 @@ // IWYU pragma: begin_exports #include +#include #include #include #include @@ -146,7 +147,10 @@ struct basic_symbol_text { [[nodiscard]] friend constexpr auto operator<=>(const basic_symbol_text& lhs, const basic_symbol_text& rhs) noexcept { + UNITS_DIAGNOSTIC_PUSH + UNITS_DIAGNOSTIC_IGNORE_ZERO_AS_NULLPOINTER_CONSTANT if (const auto cmp = lhs.standard() <=> rhs.standard(); cmp != 0) return cmp; + UNITS_DIAGNOSTIC_POP return lhs.ascii() <=> rhs.ascii(); } From 9c514439d18905d3cbffcc842bc17283aa447743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Mon, 1 Aug 2022 19:29:45 +0200 Subject: [PATCH 3/3] Fix: 32 bit overflow ../units/src/core/include/units/magnitude.h: In instantiation of 'static constexpr intmax_t units::detail::prime_factorization::get_or_compute_first_factor() [with long long int N = 149597870700; intmax_t = long long int]': ../units/src/core/include/units/magnitude.h:627:74: required from 'constexpr const intmax_t units::detail::prime_factorization<149597870700>::first_base' ../units/src/core/include/units/magnitude.h:632:54: required from 'constexpr const auto units::detail::prime_factorization<149597870700>::value' ../units/src/core/include/units/magnitude.h:631:25: required from 'struct units::detail::prime_factorization<149597870700>' ../units/src/core/include/units/magnitude.h:642:71: required from 'constexpr const auto units::detail::prime_factorization_v<149597870700>' ../units/src/core/include/units/magnitude.h:655:18: required from 'constexpr auto [requires units::Magnitude<, >] units::mag() [with ratio R = ratio{149597870700, 1}]' ../units/src/systems/si/include/units/isq/si/length.h:59:91: required from here ../units/src/core/include/units/magnitude.h:623:70: error: unsigned conversion from 'long long int' to 'std::size_t' {aka 'unsigned int'} changes value from '149597870700' to '3568982636' [-Werror=overflow] 623 | return static_cast(factorizer::find_first_factor(N)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ --- src/core/include/units/bits/prime.h | 31 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/core/include/units/bits/prime.h b/src/core/include/units/bits/prime.h index f6aa9e48..56a65168 100644 --- a/src/core/include/units/bits/prime.h +++ b/src/core/include/units/bits/prime.h @@ -25,15 +25,16 @@ #include #include #include +#include #include #include #include namespace units::detail { -constexpr bool is_prime_by_trial_division(std::size_t n) +constexpr bool is_prime_by_trial_division(std::uintmax_t n) { - for (std::size_t f = 2; f * f <= n; f += 1 + (f % 2)) { + for (std::uintmax_t f = 2; f * f <= n; f += 1 + (f % 2)) { if (n % f == 0) { return false; } @@ -44,7 +45,7 @@ constexpr bool is_prime_by_trial_division(std::size_t n) // Return the first factor of n, as long as it is either k or n. // // Precondition: no integer smaller than k evenly divides n. -constexpr std::optional first_factor_maybe(std::size_t n, std::size_t k) +constexpr std::optional first_factor_maybe(std::uintmax_t n, std::uintmax_t k) { if (n % k == 0) { return k; @@ -56,9 +57,9 @@ constexpr std::optional first_factor_maybe(std::size_t n, std::size } template -constexpr std::array first_n_primes() +constexpr std::array first_n_primes() { - std::array primes; + std::array primes; primes[0] = 2; for (std::size_t i = 1; i < N; ++i) { primes[i] = primes[i - 1] + 1; @@ -70,9 +71,9 @@ constexpr std::array first_n_primes() } template -constexpr void call_for_coprimes_up_to(std::size_t n, const std::array& basis, Callable&& call) +constexpr void call_for_coprimes_up_to(std::uintmax_t n, const std::array& basis, Callable&& call) { - for (std::size_t i = 0u; i < n; ++i) { + for (std::uintmax_t i = 0u; i < n; ++i) { if (std::apply([&i](auto... primes) { return ((i % primes != 0) && ...); }, basis)) { call(i); } @@ -80,7 +81,7 @@ constexpr void call_for_coprimes_up_to(std::size_t n, const std::array -constexpr std::size_t num_coprimes_up_to(std::size_t n, const std::array& basis) +constexpr std::size_t num_coprimes_up_to(std::uintmax_t n, const std::array& basis) { std::size_t count = 0u; call_for_coprimes_up_to(n, basis, [&count](auto) { ++count; }); @@ -88,20 +89,20 @@ constexpr std::size_t num_coprimes_up_to(std::size_t n, const std::array -constexpr auto coprimes_up_to(std::size_t n, const std::array& basis) +constexpr auto coprimes_up_to(std::size_t n, const std::array& basis) { - std::array coprimes; + std::array coprimes; std::size_t i = 0u; - call_for_coprimes_up_to(n, basis, [&coprimes, &i](std::size_t cp) { coprimes[i++] = cp; }); + call_for_coprimes_up_to(n, basis, [&coprimes, &i](std::uintmax_t cp) { coprimes[i++] = cp; }); return coprimes; } template -constexpr std::size_t product(const std::array& values) +constexpr std::uintmax_t product(const std::array& values) { - std::size_t product = 1; + std::uintmax_t product = 1; for (const auto& v : values) { product *= v; } @@ -130,11 +131,11 @@ constexpr std::size_t product(const std::array& values) template struct wheel_factorizer { static constexpr auto basis = first_n_primes(); - static constexpr std::size_t wheel_size = product(basis); + static constexpr auto wheel_size = product(basis); static constexpr auto coprimes_in_first_wheel = coprimes_up_to(wheel_size, basis); - static constexpr std::size_t find_first_factor(std::size_t n) + static constexpr std::uintmax_t find_first_factor(std::uintmax_t n) { if (const auto k = detail::get_first_of(basis, [&](auto p) { return first_factor_maybe(n, p); })) return *k;