// compare sequence numbers with correct wrapping
// semantically: return a < b
constexprboolcomp(uint16 a, uint16 b){//return (a < b && b - a < 32768) || (a > b && a - b > 32768);
return(sint16)(b - a)>0;}static_assert(comp(5,10));static_assert(comp(50000,60000));static_assert(comp(60000,5));static_assert(!comp(10,5));static_assert(!comp(60000,50000));static_assert(!comp(5,60000));static_assert(!comp(5,5));static_assert(!comp(60000,60000));
When I read the above article, I recalled this compare function from the libtorrent source code which can be optimized too.
// compare if lhs is less than rhs, taking wrapping
// into account. if lhs is close to UINT_MAX and rhs
// is close to 0, lhs is assumed to have wrapped and
// considered smaller
bool compare_less_wrap(std::uint32_t lhs
, std::uint32_t rhs, std::uint32_t mask)
{
// distance walking from lhs to rhs, downwards
std::uint32_t dist_down = (lhs - rhs) & mask;
// distance walking from lhs to rhs, upwards
std::uint32_t dist_up = (rhs - lhs) & mask;
// if the distance walking up is shorter, lhs
// is less than rhs. If the distance walking down
// is shorter, then rhs is less than lhs
return dist_up < dist_down;
}
I have a function that sorts timestamps in a scheduler, taking wrapping into account.
It works by subtracting each timestamp from "now" and then comparing them. Essentially sorting them by "closest to now" rather than absolute value.
It compiles into three subtractions (because a less-than is a subtraction):
bool compare_timestamps(uint32 a, uint32 b)
{
return (a-now)<(b-now);
}
The timestamps do move fast enough that 32 bit wrapping is likely. I could use 64-bit which wouldn't wrap within the lifespan of our civilization but where's the fun in that?
The more idiomatic way for comparing timestamps is just:
(int32)(a-b) < 0
Provided they each differ by less than half the unsigned range it produces correct results. And if they differ by more than that, you really need a wider type :)
Doesn't this only work if two's complement wrapping is guaranteed for uint -> int conversions? Which would mean it's only guaranteed since C23 or C++20.
Prior to that it was implementation defined, so you'd have to realistically be compiling for a non 2s complement platform for it to ever be a problem. If that's the case you've probably got bigger issues, like getting invaded by the normans and scurvy
Detection of two’s complement is something you can assert in constexpr (by essentially writing a unit test for this function for some know values), so if you were concerned about relying on the assumption you could always SFNAE select a different less efficient solution on unsupported platforms.
There’s a difference between relying on undefined and implementation defined behavior.
For the purposes of this token conversion and evaluation, all signed integer types and all unsigned integer types act as if they have the same representation as, respectively, the types intmax_t and uintmax_t defined in the header <stdint.h>.
3
u/trad_emark Mar 16 '26
thanks. i have updated my function ;)
13
u/HommeMusical Mar 16 '26
Hah, I laughed at the punchline... the implementation of the function.
The funny part was I initially thought of that near-trivial method and then thought, "No, the overflow, etc, this won't work."
Accept an upvote!
9
u/[deleted] Mar 16 '26
Doesn't this only work if two's complement wrapping is guaranteed for uint -> int conversions? Which would mean it's only guaranteed since C23 or C++20.
5
u/Nolia_X Mar 16 '26
Nice! I would just add an explicit static_cast to avoid people from wanting to fix "the obvious narrowing"
3
u/Jcsq6 Mar 16 '26
I’m actually proud of myself, since my first thought was “doesn’t signed to unsigned conversion already handle this?”.
2
u/phi_rus Mar 16 '26
A perfect example of keeping it simple.