-
Notifications
You must be signed in to change notification settings - Fork 14
✨ Add rollover_t
#171
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
Merged
Merged
✨ Add rollover_t
#171
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
== `rollover.hpp` | ||
|
||
https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/rollover.hpp[`rollover.hpp`] | ||
provides a class template `rollover_t` that is intended to act like an unsigned | ||
integral type, but with semantics that include the ability to roll-over on overflow. | ||
|
||
A `rollover_t` can be instantiated with any unsigned integral type: | ||
|
||
[source,cpp] | ||
---- | ||
// explicit type | ||
auto x = stdx::rollover_t<std::uint8_t>{}; | ||
|
||
// deduced type: must be unsigned | ||
auto y = stdx::rollover_t{1u}; // rollover_t<unsigned int> | ||
---- | ||
|
||
It supports all the usual arithmetic operations (`+` `-` `*` `/` `%`) and | ||
behaves much like an unsigned integral type, with defined overflow and underflow | ||
semantics. | ||
|
||
=== Comparison semantics | ||
|
||
`rollover_t` supports equality, but the comparison operators (`<` `<=` `>` `>=`) | ||
are deleted. Instead, `cmp_less` is provided, with different semantics. A | ||
`rollover_t` considers itself to be in the middle of a rolling window where half | ||
the bit-range is always lower and half is higher. | ||
|
||
For instance, imagine a 3-bit unsigned integral type. There are eight values of | ||
this type: `0` `1` `2` `3` `4` `5` `6` `7`. Let's call the `rollover_t` over | ||
this type `R`. | ||
|
||
CAUTION: `operator<` on `rollover_t` is not antisymmetric! | ||
|
||
For any value, there are always four values (half the bit-space) less than it, | ||
and four values greater than it. And of course it is equal to itself. e.g. for | ||
the `R` value `5`: | ||
|
||
- `1`, `2`, `3`, `4` are all less than `5` | ||
- `6`, `7`, `0`, `1` are all greater than `5` | ||
|
||
i.e. `cmp_less(R{1u}, R{5u})` is `true`. And `cmp_less(R{5u}, R{1u})` is true. | ||
|
||
Effectively any value partitions the cyclic space in this way. | ||
|
||
CAUTION: `operator<` on `rollover_t` is not transitive! | ||
|
||
Also, the following are all true for `R`: | ||
|
||
- `1` < `3` | ||
- `3` < `5` | ||
- `5` < `7` | ||
- `7` < `1` | ||
|
||
The cyclic nature of the space means that `operator<` is neither antisymmetric | ||
nor transitive! (Lack of antisymmetry might be viewed as a special case of | ||
non-transitivity.) | ||
|
||
This means we need to take care with operations that assume the antisymmetric | ||
and transitive nature of the less-than operation. In particular `cmp_less` does | ||
_not_ define a | ||
https://en.cppreference.com/w/cpp/concepts/strict_weak_order[strict weak order] | ||
-- which is why `operator<` and friends are deleted. In the absence of data | ||
constraints, `rollover_t` cannot be sorted with `std::sort`. | ||
|
||
NOTE: A suitable constraint might be that the data lies completely within half | ||
the bit-range: in that case, `cmp_less` _would_ have the correct properties and | ||
_could_ be used as a | ||
https://en.cppreference.com/w/cpp/named_req/LessThanComparable[comparator] | ||
argument to `std::sort`. As always in C++, we protect against Murphy, not | ||
Machiavelli. | ||
|
||
=== Use with `std::chrono` | ||
|
||
`rollover_t` is intended for use in applications like timers which may be | ||
modelled as a 32-bit counter that rolls over. In that case, it makes sense to | ||
consider a sliding window centred around "now" where half the bit-space is in | ||
the past, and half is in the future. Under such a scheme, in general it is | ||
undefined to schedule an event more than 31 bits-worth in the future. | ||
|
||
[source,cpp] | ||
---- | ||
// 32-bit rollover type | ||
using ro_t = stdx::rollover_t<std::uint32_t>; | ||
// Used with a microsecond resolution | ||
using ro_duration_t = std::chrono::duration<ro_t, std::micro>; | ||
using ro_time_point_t = std::chrono::time_point<std::chrono::local_t, ro_duration_t>; | ||
---- | ||
|
||
This allows us to benefit from the typed time handling of `std::chrono`, and use | ||
`cmp_less` for specialist applications like keeping a sorted list of timer | ||
tasks, where we have the constraint that we never schedule an event beyond a | ||
certain point in the future. |
elbeno marked this conversation as resolved.
Show resolved
Hide resolved
|
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,131 @@ | ||
#pragma once | ||
|
||
#include <stdx/concepts.hpp> | ||
|
||
#include <type_traits> | ||
|
||
namespace stdx { | ||
inline namespace v1 { | ||
template <typename T> struct rollover_t { | ||
static_assert(unsigned_integral<T>, | ||
"Argument to rollover_t must be an unsigned integral type."); | ||
using underlying_t = T; | ||
elbeno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constexpr rollover_t() = default; | ||
template <typename U, | ||
typename = std::enable_if_t<std::is_convertible_v<U, T>>> | ||
constexpr explicit rollover_t(U u) : value{static_cast<underlying_t>(u)} {} | ||
template <typename U, | ||
typename = std::enable_if_t<std::is_convertible_v<U, T>>> | ||
constexpr explicit rollover_t(rollover_t<U> u) | ||
: rollover_t{static_cast<U>(u)} {} | ||
|
||
[[nodiscard]] constexpr auto as_underlying() const -> underlying_t { | ||
return value; | ||
} | ||
constexpr explicit operator underlying_t() const { return value; } | ||
|
||
[[nodiscard]] constexpr auto operator+() const -> rollover_t { | ||
return *this; | ||
} | ||
[[nodiscard]] constexpr auto operator-() const -> rollover_t { | ||
return rollover_t{static_cast<underlying_t>(-value)}; | ||
} | ||
|
||
constexpr auto operator++() -> rollover_t & { | ||
++value; | ||
return *this; | ||
} | ||
constexpr auto operator++(int) -> rollover_t { return rollover_t{value++}; } | ||
|
||
constexpr auto operator--() -> rollover_t & { | ||
--value; | ||
return *this; | ||
} | ||
constexpr auto operator--(int) -> rollover_t { return rollover_t{value--}; } | ||
|
||
constexpr auto operator+=(rollover_t other) -> rollover_t & { | ||
value += other.value; | ||
return *this; | ||
} | ||
constexpr auto operator-=(rollover_t other) -> rollover_t & { | ||
value -= other.value; | ||
return *this; | ||
} | ||
constexpr auto operator*=(rollover_t other) -> rollover_t & { | ||
value *= other.value; | ||
return *this; | ||
} | ||
constexpr auto operator/=(rollover_t other) -> rollover_t & { | ||
value /= other.value; | ||
return *this; | ||
} | ||
constexpr auto operator%=(rollover_t other) -> rollover_t & { | ||
value %= other.value; | ||
return *this; | ||
} | ||
|
||
private: | ||
[[nodiscard]] constexpr friend auto operator==(rollover_t lhs, | ||
rollover_t rhs) -> bool { | ||
return lhs.value == rhs.value; | ||
} | ||
[[nodiscard]] constexpr friend auto operator!=(rollover_t lhs, | ||
rollover_t rhs) -> bool { | ||
return not(lhs == rhs); | ||
} | ||
|
||
constexpr friend auto operator<(rollover_t, rollover_t) -> bool = delete; | ||
constexpr friend auto operator<=(rollover_t, rollover_t) -> bool = delete; | ||
constexpr friend auto operator>(rollover_t, rollover_t) -> bool = delete; | ||
constexpr friend auto operator>=(rollover_t, rollover_t) -> bool = delete; | ||
|
||
[[nodiscard]] constexpr friend auto cmp_less(rollover_t lhs, | ||
rollover_t rhs) -> bool { | ||
constexpr auto mid = static_cast<underlying_t>(~underlying_t{}) / 2; | ||
return static_cast<underlying_t>(lhs.value - rhs.value) > mid; | ||
} | ||
|
||
[[nodiscard]] constexpr friend auto | ||
operator+(rollover_t lhs, rollover_t rhs) -> rollover_t { | ||
lhs += rhs; | ||
return lhs; | ||
} | ||
[[nodiscard]] constexpr friend auto | ||
operator-(rollover_t lhs, rollover_t rhs) -> rollover_t { | ||
lhs -= rhs; | ||
return lhs; | ||
} | ||
[[nodiscard]] constexpr friend auto | ||
operator*(rollover_t lhs, rollover_t rhs) -> rollover_t { | ||
lhs *= rhs; | ||
return lhs; | ||
} | ||
[[nodiscard]] constexpr friend auto | ||
operator/(rollover_t lhs, rollover_t rhs) -> rollover_t { | ||
lhs /= rhs; | ||
return lhs; | ||
} | ||
[[nodiscard]] constexpr friend auto | ||
operator%(rollover_t lhs, rollover_t rhs) -> rollover_t { | ||
lhs %= rhs; | ||
return lhs; | ||
} | ||
|
||
underlying_t value{}; | ||
}; | ||
|
||
template <typename T> rollover_t(T) -> rollover_t<T>; | ||
} // namespace v1 | ||
} // namespace stdx | ||
|
||
template <typename T, typename U> | ||
struct std::common_type<stdx::rollover_t<T>, stdx::rollover_t<U>> { | ||
using type = stdx::rollover_t<std::common_type_t<T, U>>; | ||
}; | ||
|
||
template <typename T, typename I> | ||
struct std::common_type<stdx::rollover_t<T>, I> { | ||
using type = | ||
stdx::rollover_t<std::common_type_t<T, std::make_unsigned_t<I>>>; | ||
}; |
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 |
---|---|---|
|
@@ -55,6 +55,7 @@ add_tests( | |
priority | ||
ranges | ||
remove_cvref | ||
rollover | ||
span | ||
to_underlying | ||
type_map | ||
|
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <stdx/rollover.hpp> | ||
|
||
// EXPECT: deleted (operator|function) | ||
|
||
auto main() -> int { | ||
using X = stdx::rollover_t<unsigned int>; | ||
[[maybe_unused]] auto cmp = X{} < X{1u}; | ||
} |
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,8 @@ | ||
#include <stdx/rollover.hpp> | ||
|
||
// EXPECT: Argument to rollover_t must be an unsigned integral type | ||
|
||
auto main() -> int { | ||
using X = stdx::rollover_t<int>; | ||
[[maybe_unused]] X x{}; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.