Skip to content

Commit

Permalink
More refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Mar 16, 2024
1 parent 9718de7 commit 17997cf
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 79 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
Checks: '-*,
bugprone-*,
-bugprone-easily-swappable-parameters,
modernize-*,
-modernize-use-trailing-return-type,
performance-*,
Expand Down
5 changes: 5 additions & 0 deletions ccmath_headers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ set(ccmath_internal_helpers_headers
)

set(ccmath_internal_predef_headers
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/assume.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/compiler_warnings_and_errors_def.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/compiler_warnings_and_errors_undef.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/has_attribute.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/likely.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/unlikely.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/no_debug.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ccmath/internal/predef/suppress.hpp
)

set(ccmath_internal_setup_headers
Expand Down
2 changes: 1 addition & 1 deletion include/ccmath/detail/basic/fdim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace ccm
* @param y A floating-point value.
* @return If successful, returns the positive difference between x and y.
*/
template <typename T, typename U, std::enable_if_t<std::is_floating_point_v<T> && std::is_floating_point<U>::value, int> = 0>
template <typename T, typename U, std::enable_if_t<std::is_floating_point_v<T> && std::is_floating_point_v<U>, int> = 0>
inline constexpr auto fdim(T x, U y)
{
// Find the common type of the two arguments
Expand Down
9 changes: 5 additions & 4 deletions include/ccmath/detail/basic/impl/remquo_double_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ccm::internal
{
namespace impl
{
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
inline constexpr double remquo_double_impl(double x, double y, int * quo) noexcept
{
std::int64_t x_i64{};
Expand Down Expand Up @@ -54,7 +55,7 @@ namespace ccm::internal
// clang-format on

// If x is not finite or y is NaN.
if (CCM_UNLIKELY(x_i64 >= 0x7ff0000000000000ULL || y_i64 > 0x7ff0000000000000ULL)) { return (x * y) / (x * y); }
if (CCM_UNLIKELY(x_i64 >= 0x7ff0000000000000ULL || y_i64 > 0x7ff0000000000000ULL)) { return (x * y) / (x * y); } // NOLINT(readability-simplify-boolean-expr)

// b (or bit 54) represents the highest bit we can compare against for both signed and unsigned integers without causing an overflow.
// Here we are checking that if y_i64 is within the range of signed 64-bit integers that can be represented without setting the MSB (i.e.,
Expand All @@ -66,7 +67,7 @@ namespace ccm::internal

if (CCM_UNLIKELY(x_i64 == y_i64))
{
*quo = quotient_sign ? -1 : 1;
*quo = quotient_sign != 0U ? -1 : 1;
return 0.0 * x;
}

Expand Down Expand Up @@ -117,11 +118,11 @@ namespace ccm::internal
CCM_RESTORE_GCC_WARNING()
CCM_RESTORE_CLANG_WARNING()

*quo = quotient_sign ? -computed_quotient : computed_quotient;
*quo = quotient_sign != 0U ? -computed_quotient : computed_quotient;

// Make sure that the correct sign of zero results in round down mode.
if (x == 0.0) { x = 0.0; }
if (x_sign) { x = -x; }
if (x_sign != 0U) { x = -x; }

return x;
}
Expand Down
15 changes: 8 additions & 7 deletions include/ccmath/detail/basic/impl/remquo_float_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace ccm::internal
{
namespace impl
{
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
inline constexpr float remquo_float_impl(float x, float y, int * quo) noexcept
{
std::int32_t x_i32{};
Expand All @@ -45,7 +46,7 @@ namespace ccm::internal
if (CCM_UNLIKELY(y_i32 == 0)) { return (x * y) / (x * y); }

// If x is not finite or y is NaN.
if (CCM_UNLIKELY(x_i32 >= 0x7f800000 || y_i32 > 0x7f800000)) { return (x * y) / (x * y); }
if (CCM_UNLIKELY(x_i32 >= 0x7f800000 || y_i32 > 0x7f800000)) { return (x * y) / (x * y); } // NOLINT(readability-simplify-boolean-expr)

if (y_i32 <= 0x7dffffff)
{
Expand All @@ -54,8 +55,8 @@ namespace ccm::internal

if ((x_i32 - y_i32) == 0)
{
*quo = quotient_sign ? -1 : 1;
return 0.0f * x;
*quo = quotient_sign != 0 ? -1 : 1;
return 0.0F * x;
}

x = ccm::fabsf(x);
Expand Down Expand Up @@ -87,7 +88,7 @@ namespace ccm::internal
}
else
{
float y_half = 0.5f * y;
float y_half = 0.5F * y;
if (x > y_half)
{
x -= y;
Expand All @@ -99,11 +100,11 @@ namespace ccm::internal
}
}
}
*quo = quotient_sign ? -computed_quotient : computed_quotient;
*quo = quotient_sign != 0 ? -computed_quotient : computed_quotient;

// Make sure that the correct sign of zero results in round down mode.
if (x == 0.0f) { x = 0.0f; }
if (x_sign) { x = -x; }
if (x == 0.0F) { x = 0.0F; }
if (x_sign != 0U) { x = -x; }

return x;
}
Expand Down
7 changes: 3 additions & 4 deletions include/ccmath/detail/lerp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ namespace ccm
{
return b < x ? x : b;
}
else
{
return x < b ? x : b;
}

return x < b ? x : b;

}
}
}
Expand Down
4 changes: 3 additions & 1 deletion include/ccmath/internal/helpers/endian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#pragma once

#include <cstdint>

namespace ccm::helpers
{
enum class endian
enum class endian : std::uint16_t
{
// TODO: Add support for other compilers besides just MSVC, GCC, and Clang.

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion include/ccmath/internal/setup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

// GCC (a.k.a. GNUC)
#elif defined(__GNUC__)
#define CCM_COMPILER_GCC 1
#define CCM_COMPILER_GCC 1 // NOLINT
#define CCM_COMPILER_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__)

#elif defined(_MSC_VER)
Expand Down
24 changes: 12 additions & 12 deletions include/ccmath/internal/type_traits/floating_point_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ namespace ccm::helpers

using uint_type = std::uint32_t;

static constexpr std::uint32_t exponent_mask = 0x000000FFu; // (1u << exponent_bits) - 1
static constexpr std::uint32_t normal_mantissa_mask = 0x00FFFFFFu; // (1u << mantissa_bits) - 1
static constexpr std::uint32_t denormal_mantissa_mask = 0x007FFFFFu; // (1u << (mantissa_bits - 1)) - 1
static constexpr std::uint32_t special_nan_mantissa_mask = 0x00400000u; // 1u << (mantissa_bits - 2)
static constexpr std::uint32_t shifted_sign_mask = 0x80000000u; // 1u << sign_shift
static constexpr std::uint32_t shifted_exponent_mask = 0x7F800000u; // exponent_mask << exponent_shift
static constexpr std::uint32_t exponent_mask = 0x000000FFU; // (1u << exponent_bits) - 1
static constexpr std::uint32_t normal_mantissa_mask = 0x00FFFFFFU; // (1u << mantissa_bits) - 1
static constexpr std::uint32_t denormal_mantissa_mask = 0x007FFFFFU; // (1u << (mantissa_bits - 1)) - 1
static constexpr std::uint32_t special_nan_mantissa_mask = 0x00400000U; // 1u << (mantissa_bits - 2)
static constexpr std::uint32_t shifted_sign_mask = 0x80000000U; // 1u << sign_shift
static constexpr std::uint32_t shifted_exponent_mask = 0x7F800000U; // exponent_mask << exponent_shift
};

template <>
Expand All @@ -50,12 +50,12 @@ namespace ccm::helpers

using uint_type = std::uint64_t;

static constexpr std::uint64_t exponent_mask = 0x00000000000007FFu; // (1ULL << exponent_bits) - 1
static constexpr std::uint64_t normal_mantissa_mask = 0x001FFFFFFFFFFFFFu; // (1ULL << mantissa_bits) - 1
static constexpr std::uint64_t denormal_mantissa_mask = 0x000FFFFFFFFFFFFFu; // (1ULL << (mantissa_bits - 1)) - 1
static constexpr std::uint64_t special_nan_mantissa_mask = 0x0008000000000000u; // 1ULL << (mantissa_bits - 2)
static constexpr std::uint64_t shifted_sign_mask = 0x8000000000000000u; // 1ULL << sign_shift
static constexpr std::uint64_t shifted_exponent_mask = 0x7FF0000000000000u; // exponent_mask << exponent_shift
static constexpr std::uint64_t exponent_mask = 0x00000000000007FFU; // (1ULL << exponent_bits) - 1
static constexpr std::uint64_t normal_mantissa_mask = 0x001FFFFFFFFFFFFFU; // (1ULL << mantissa_bits) - 1
static constexpr std::uint64_t denormal_mantissa_mask = 0x000FFFFFFFFFFFFFU; // (1ULL << (mantissa_bits - 1)) - 1
static constexpr std::uint64_t special_nan_mantissa_mask = 0x0008000000000000U; // 1ULL << (mantissa_bits - 2)
static constexpr std::uint64_t shifted_sign_mask = 0x8000000000000000U; // 1ULL << sign_shift
static constexpr std::uint64_t shifted_exponent_mask = 0x7FF0000000000000U; // exponent_mask << exponent_shift
};

template <>
Expand Down
8 changes: 2 additions & 6 deletions include/ccmath/internal/type_traits/is_constant_evaluated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,17 @@
# define CCMATH_HAS_BUILTIN_IS_CONSTANT_EVALUATED
#endif

namespace ccm::type_traits
namespace ccm::traits
{
template <typename T>
inline constexpr bool is_constant_evaluated(T x) noexcept
inline constexpr bool is_constant_evaluated() noexcept
{
#if defined(CCMATH_HAS_IS_CONSTANT_EVALUATED)
return std::is_constant_evaluated();
#elif defined(CCMATH_HAS_BUILTIN_IS_CONSTANT_EVALUATED)
return __builtin_is_constant_evaluated();
#elif defined(__GNUC__) && (__GNUC__ >= 6)
return __builtin_constant_p(x);
#else
return false;
#endif

}
} // namespace ccm::type_traits

Expand Down
2 changes: 1 addition & 1 deletion include/ccmath/internal/utility/unreachable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ccm
{
[[noreturn]] inline void unreachable()
{
// If we can't use the standard library's unreachable function
// If we can't use the standard library's unreachable function,
// Then we'll use the compiler specific extensions if possible.
#if defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L
std::unreachable();
Expand Down
59 changes: 17 additions & 42 deletions include/ccmath/internal/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,41 @@ namespace ccm
struct Version
{
public:
constexpr Version(int major, int minor, int patch)
: m_major(major), m_minor(minor), m_patch(patch)
{
}
constexpr Version(int major, int minor, int patch) : m_major(major), m_minor(minor), m_patch(patch) {}

[[nodiscard]] std::string to_string() const
{
return std::to_string(m_major) + "." + std::to_string(m_minor) + "." + std::to_string(m_patch);
}
[[nodiscard]] std::string to_string() const { return std::to_string(m_major) + "." + std::to_string(m_minor) + "." + std::to_string(m_patch); }

[[nodiscard]] constexpr int major() const { return m_major; }
[[nodiscard]] constexpr int major() const { return m_major; }
[[nodiscard]] constexpr int minor() const { return m_minor; }
[[nodiscard]] constexpr int patch() const { return m_patch; }
[[nodiscard]] constexpr int full() const { return m_major * 10000 + m_minor * 100 + m_patch; }
[[nodiscard]] constexpr int get() const { return full(); }

constexpr bool operator==(const Version& other) const
{
return m_major == other.m_major && m_minor == other.m_minor && m_patch == other.m_patch;
}
constexpr bool operator==(const Version & other) const { return m_major == other.m_major && m_minor == other.m_minor && m_patch == other.m_patch; }

constexpr bool operator!=(const Version& other) const
{
return !(*this == other);
}
constexpr bool operator!=(const Version & other) const { return !(*this == other); }

constexpr bool operator<(const Version& other) const
{
if (m_major < other.m_major)
return true;
if (m_major > other.m_major)
return false;
constexpr bool operator<(const Version & other) const
{
if (m_major < other.m_major) { return true; }
if (m_major > other.m_major) { return false; }

if (m_minor < other.m_minor)
return true;
if (m_minor > other.m_minor)
return false;
if (m_minor < other.m_minor) { return true; }
if (m_minor > other.m_minor) { return false; }

return m_patch < other.m_patch;
}
return m_patch < other.m_patch;
}

constexpr bool operator>(const Version& other) const
{
return other < *this;
}
constexpr bool operator>(const Version & other) const { return other < *this; }

constexpr bool operator<=(const Version& other) const
{
return !(other < *this);
}
constexpr bool operator<=(const Version & other) const { return !(other < *this); }

constexpr bool operator>=(const Version& other) const
{
return !(*this < other);
}
constexpr bool operator>=(const Version & other) const { return !(*this < other); }

private:
int m_major;
int m_minor;
int m_patch;
};

}
} // namespace ccm

0 comments on commit 17997cf

Please sign in to comment.