-
Notifications
You must be signed in to change notification settings - Fork 171
Make modm compileable with Clang for tooling support #1349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chris-durand
wants to merge
7
commits into
modm-io:develop
Choose a base branch
from
chris-durand:clang-tooling-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f92e3ba
[math] Implement consteval float rounding functions for clang support
chris-durand 7507164
[ui] Fix circular dependencies in ui:color module
chris-durand e450487
[tools] Allow compilation with Clang version >= 22
chris-durand e068fd2
[math] Deprecate ArithmeticTraits, MakeSigned and MakeUnsigned
chris-durand 635acfe
[utils] Fix aligned_storage_helper for Capacity == 2
chris-durand c650a4d
[utils] Deprecate modm::aligned_storage_t
chris-durand c0dd51c
fixup
chris-durand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |||||
| * Copyright (c) 2012, 2014, Niklas Hauser | ||||||
| * Copyright (c) 2013, 2015, Sascha Schade | ||||||
| * Copyright (c) 2015, Kevin Läufer | ||||||
| * Copyright (c) 2018, Christopher Durand | ||||||
| * Copyright (c) 2018, 2026, Christopher Durand | ||||||
| * Copyright (c) 2022, Raphael Lehmann | ||||||
| * | ||||||
| * This file is part of the modm project. | ||||||
|
|
@@ -18,11 +18,14 @@ | |||||
| #ifndef MODM_ARITHMETIC_TRAITS_HPP | ||||||
| #define MODM_ARITHMETIC_TRAITS_HPP | ||||||
|
|
||||||
| #include <concepts> | ||||||
| #include <cstdint> | ||||||
| #include <limits> | ||||||
| #include <type_traits> | ||||||
| #include <cmath> | ||||||
|
|
||||||
| #include <modm/math/utils/cmath.hpp> | ||||||
|
|
||||||
| namespace modm | ||||||
| { | ||||||
|
|
||||||
|
|
@@ -97,13 +100,9 @@ namespace detail | |||||
| struct WideType<unsigned long long> | ||||||
| { using type = double; }; | ||||||
|
|
||||||
| template<typename T, typename = std::enable_if_t< | ||||||
| std::is_integral_v<T> && !std::is_same_v<std::decay_t<T>, bool> | ||||||
| > > | ||||||
| using enable_if_int = T; | ||||||
|
|
||||||
| template<typename T> | ||||||
| struct WideType<enable_if_int<T>> | ||||||
| template<std::integral T> | ||||||
| requires (!std::is_same_v<std::remove_cv_t<T>, bool>) | ||||||
| struct WideType<T> | ||||||
| { | ||||||
| static constexpr bool isNextIntLarger = | ||||||
| std::numeric_limits<typename NextInt<T>::type>::max() > std::numeric_limits<T>::max(); | ||||||
|
|
@@ -123,8 +122,9 @@ namespace detail | |||||
| using type = T; | ||||||
| }; | ||||||
|
|
||||||
| template<typename T> | ||||||
| struct MakeSigned<enable_if_int<T>> | ||||||
| template<std::integral T> | ||||||
| requires (!std::is_same_v<std::remove_cv_t<T>, bool>) | ||||||
| struct MakeSigned<T> | ||||||
| { | ||||||
| using type = std::make_signed_t<T>; | ||||||
| }; | ||||||
|
|
@@ -135,8 +135,9 @@ namespace detail | |||||
| using type = T; | ||||||
| }; | ||||||
|
|
||||||
| template<typename T> | ||||||
| struct MakeUnsigned<enable_if_int<T>> | ||||||
| template<std::integral T> | ||||||
| requires (!std::is_same_v<std::remove_cv_t<T>, bool>) | ||||||
| struct MakeUnsigned<T> | ||||||
| { | ||||||
| using type = std::make_unsigned_t<T>; | ||||||
| }; | ||||||
|
|
@@ -149,10 +150,10 @@ template<typename T> | |||||
| using WideType = typename detail::WideType<T>::type; | ||||||
|
|
||||||
| template<typename T> | ||||||
| using SignedType = typename detail::MakeSigned<T>::type; | ||||||
| using SignedType [[deprecated("use std::make_signed_t")]] = typename detail::MakeSigned<T>::type; // DEPRECATED: 2026q3 | ||||||
|
|
||||||
| template<typename T> | ||||||
| using UnsignedType = typename detail::MakeUnsigned<T>::type; | ||||||
| using UnsignedType [[deprecated("use std::make_unsigned_t")]] = typename detail::MakeUnsigned<T>::type; // DEPRECATED: 2026q3 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Historically we've given people 1y before we remove deprecation notices, due to them maybe skipping a few releases. |
||||||
|
|
||||||
| /** | ||||||
| * Arithmetic Traits | ||||||
|
|
@@ -176,13 +177,16 @@ using UnsignedType = typename detail::MakeUnsigned<T>::type; | |||||
| * @endcode | ||||||
| */ | ||||||
| template<typename T> | ||||||
| struct ArithmeticTraits | ||||||
| struct [[deprecated("use std::numeric_limits instead")]] ArithmeticTraits // DEPRECATED: 2026q3 | ||||||
| { | ||||||
| static constexpr bool isInteger = std::is_integral_v<T> | ||||||
| && !std::is_same_v<std::decay_t<T>, bool>; | ||||||
|
|
||||||
| // log10(2), not constexpr yet with clang | ||||||
| static constexpr auto log10_2 = 0.3010299956639812; | ||||||
|
|
||||||
| static constexpr unsigned char decimalDigits = | ||||||
| std::ceil(std::numeric_limits<T>::digits * log10(2)) + (std::is_signed_v<T> ? 1 : 0); | ||||||
| modm::ceil(std::numeric_limits<T>::digits * log10_2) + (std::is_signed_v<T> ? 1 : 0); | ||||||
| }; | ||||||
| /// @} | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright (c) 2026, Christopher Durand | ||
| * | ||
| * This file is part of the modm project. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cmath> | ||
| #include <concepts> | ||
| #include <limits> | ||
| #include <modm/architecture/detect.hpp> | ||
|
|
||
| namespace modm::detail | ||
| { | ||
|
|
||
| template<std::floating_point T> | ||
| consteval T | ||
| consteval_ceil(T value) | ||
| { | ||
| static_assert(std::numeric_limits<T>::is_iec559); | ||
|
|
||
| // handle NaN | ||
| if (value != value) return value; | ||
|
|
||
| constexpr auto mantissaDigits = std::numeric_limits<T>::digits; | ||
| static_assert(std::numeric_limits<long long>::digits >= mantissaDigits); | ||
|
|
||
| // calculate magnitude limit outside which all values must be integral | ||
| constexpr auto limit = T{1ll << (mantissaDigits - 1)}; | ||
| if (value >= limit || value <= -limit) { | ||
| return value; | ||
| } | ||
|
|
||
| const auto truncated = static_cast<T>(static_cast<long long>(value)); | ||
| if (value <= T{0}) { | ||
| // preserve sign of zero, e.g. ceil(-0.5f) = -0.0f | ||
| if (truncated == 0) { | ||
| return value * 0; | ||
| } | ||
|
|
||
| return truncated; | ||
| } else { | ||
| return (value > truncated) ? (truncated + T{1}) : truncated; | ||
| } | ||
| } | ||
|
|
||
| template<std::floating_point T> | ||
| consteval T | ||
| consteval_floor(T value) | ||
| { | ||
| // handle NaN | ||
| if (value != value) return value; | ||
|
|
||
| constexpr auto mantissaDigits = std::numeric_limits<T>::digits; | ||
| static_assert(std::numeric_limits<long long>::digits >= mantissaDigits); | ||
|
|
||
| // calculate magnitude limit outside which all values must be integral | ||
| constexpr auto limit = T{1ll << (mantissaDigits - 1)}; | ||
| if (value >= limit || value <= -limit) { | ||
| return value; | ||
| } | ||
|
|
||
| // preserve sign of zero, floor(-0.0f) = -0.0f | ||
| if (value == T{0}) { | ||
| return value; | ||
| } | ||
|
|
||
| const auto truncated = static_cast<T>(static_cast<long long>(value)); | ||
| if (value >= T{0}) { | ||
| return truncated; | ||
| } else { | ||
| return (value < truncated) ? (truncated - T{1}) : truncated; | ||
| } | ||
| } | ||
|
|
||
| template <std::floating_point T> | ||
| consteval T | ||
| consteval_round(T value) | ||
| { | ||
| // handle NaN | ||
| if (value != value) return value; | ||
|
|
||
| constexpr auto mantissaDigits = std::numeric_limits<T>::digits; | ||
| static_assert(std::numeric_limits<long long>::digits >= mantissaDigits); | ||
|
|
||
| // calculate magnitude limit outside which all values must be integral | ||
| constexpr auto limit = T{1ll << (mantissaDigits - 1)}; | ||
| if (value >= limit || value <= -limit) { | ||
| return value; | ||
| } | ||
|
|
||
| const auto truncated = static_cast<T>(static_cast<long long>(value)); | ||
| const auto fraction = value - truncated; | ||
|
|
||
| if (std::signbit(value)) { | ||
| if (fraction <= T{-0.5}) return truncated - T{1}; | ||
| if (truncated == 0) return value * T{0}; | ||
| return truncated; | ||
| } else { | ||
| if (fraction >= T{0.5}) return truncated + T{1}; | ||
| return truncated; | ||
| } | ||
| } | ||
|
|
||
| } // namespace modm::detail | ||
|
|
||
| namespace modm | ||
| { | ||
|
|
||
| template<std::floating_point T> | ||
| constexpr T | ||
| ceil(T value) | ||
| { | ||
| #ifdef MODM_COMPILER_CLANG | ||
| if consteval { | ||
| return detail::consteval_ceil(value); | ||
| } else { | ||
| return std::ceil(value); | ||
| } | ||
| #else | ||
| return std::ceil(value); | ||
| #endif | ||
| } | ||
|
|
||
| template<std::floating_point T> | ||
| constexpr T | ||
| floor(T value) | ||
| { | ||
| #ifdef MODM_COMPILER_CLANG | ||
| if consteval { | ||
| return detail::consteval_floor(value); | ||
| } else { | ||
| return std::floor(value); | ||
| } | ||
| #else | ||
| return std::floor(value); | ||
| #endif | ||
| } | ||
|
|
||
| template<std::floating_point T> | ||
| constexpr T | ||
| round(T value) | ||
| { | ||
| #ifdef MODM_COMPILER_CLANG | ||
| if consteval { | ||
| return detail::consteval_round(value); | ||
| } else { | ||
| return std::round(value); | ||
| } | ||
| #else | ||
| return std::round(value); | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace modm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can add
modm_externally_visibleto the utils file, which is meant to abstract these compiler/platform differences.