Merge pull request #385 from OttoKuennecke/WarningsFixed

Warnings fixed
This commit is contained in:
Mateusz Pusz
2022-08-10 22:33:27 +02:00
committed by GitHub
6 changed files with 34 additions and 15 deletions

View File

@@ -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

View File

@@ -23,6 +23,7 @@
#pragma once
#include <gsl/gsl-lite.hpp>
#include <units/bits/external/hacks.h>
#include <units/bits/math_concepts.h>
#include <units/bits/pow.h>
#include <units/bits/ratio_maths.h>
@@ -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;

View File

@@ -45,22 +45,26 @@
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")
#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))
#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
#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

View File

@@ -25,15 +25,16 @@
#include <units/bits/algorithm.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <optional>
#include <tuple>
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<std::size_t> first_factor_maybe(std::size_t n, std::size_t k)
constexpr std::optional<std::uintmax_t> first_factor_maybe(std::uintmax_t n, std::uintmax_t k)
{
if (n % k == 0) {
return k;
@@ -56,9 +57,9 @@ constexpr std::optional<std::size_t> first_factor_maybe(std::size_t n, std::size
}
template<std::size_t N>
constexpr std::array<std::size_t, N> first_n_primes()
constexpr std::array<std::uintmax_t, N> first_n_primes()
{
std::array<std::size_t, N> primes;
std::array<std::uintmax_t, N> 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<std::size_t, N> first_n_primes()
}
template<std::size_t N, typename Callable>
constexpr void call_for_coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis, Callable&& call)
constexpr void call_for_coprimes_up_to(std::uintmax_t n, const std::array<std::uintmax_t, N>& 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<std::size
}
template<std::size_t N>
constexpr std::size_t num_coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis)
constexpr std::size_t num_coprimes_up_to(std::uintmax_t n, const std::array<std::uintmax_t, N>& 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<std::si
}
template<std::size_t ResultSize, std::size_t N>
constexpr auto coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis)
constexpr auto coprimes_up_to(std::size_t n, const std::array<std::uintmax_t, N>& basis)
{
std::array<std::size_t, ResultSize> coprimes;
std::array<std::uintmax_t, ResultSize> 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<std::size_t N>
constexpr std::size_t product(const std::array<std::size_t, N>& values)
constexpr std::uintmax_t product(const std::array<std::uintmax_t, N>& 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<std::size_t, N>& values)
template<std::size_t BasisSize>
struct wheel_factorizer {
static constexpr auto basis = first_n_primes<BasisSize>();
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<num_coprimes_up_to(wheel_size, basis)>(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;

View File

@@ -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;
};

View File

@@ -24,6 +24,7 @@
// IWYU pragma: begin_exports
#include <units/bits/external/fixed_string.h>
#include <units/bits/external/hacks.h>
#include <compare>
#include <cstddef>
#include <cstdint>
@@ -146,7 +147,10 @@ struct basic_symbol_text {
[[nodiscard]] friend constexpr auto operator<=>(const basic_symbol_text& lhs,
const basic_symbol_text<StandardCharT2, N2, M2>& 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();
}