Skip to content

Conversation

elbeno
Copy link
Contributor

@elbeno elbeno commented Nov 7, 2024

Problem:

  • When dealing with a timer register that rolls over, it is useful to have a type that knows how to handle that.

Solution:

  • Add rollover_t.
  • rollover_t behaves like an unsigned integral type, with interesting comparison semantics.

@elbeno elbeno force-pushed the add-rollover branch 3 times, most recently from 7e3e891 to b97aec5 Compare November 7, 2024 23:03
Copy link
Contributor

@lukevalenty lukevalenty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm worried about the fact that zero < half and half < zero both evaluate to true. is there a way we can avoid this? it feels very wrong.

@lukevalenty
Copy link
Contributor

@elbeno: I think I remember what I did...it was with signed integers, but let's pretend that signed overflow is defined behavior for a moment:

bool operator<(auto a, auto b) {
    retutrn (a - b) < 0;
}

That implementation takes care of the wraparound case and there is no case in which a < b and b < a holds true. We should be able to implement the equivalent with unsigned integers.

@lukevalenty
Copy link
Contributor

lukevalenty commented Nov 8, 2024

This is how it should work for uint32_t:

bool operator<(uint32_t a, uint32_t b) {
    retutrn (a - b) >> 31;
}

In 2s complement signed integers, a number is negative if the msb is set. This function does the equivalent check for uint32_t.

@elbeno
Copy link
Contributor Author

elbeno commented Nov 8, 2024

You're right - and that's exactly equivalent to what I've written. > mid means the sign bit is set. Bear in mind the pesky integer promotion rules...

@lukevalenty
Copy link
Contributor

You're right - and that's exactly equivalent to what I've written. > mid means the sign bit is set. Bear in mind the pesky integer promotion rules...

You're right! Ok....I need some more work to wrap my brain grapes around this one... 🧠🍇

@elbeno elbeno force-pushed the add-rollover branch 2 times, most recently from 69eb768 to b7e612d Compare November 11, 2024 15:29
Problem:
- When dealing with a timer register that rolls over, it is useful to have a
  type that knows how to handle that.

Solution:
- Add `rollover_t`.
- `rollover_t` behaves like an unsigned integral type, with interesting
  comparison semantics.
@lukevalenty lukevalenty merged commit 242020e into intel:main Nov 12, 2024
32 checks passed
@elbeno elbeno deleted the add-rollover branch November 12, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants