chTimeIsWithin()
Posted: Sun Oct 27, 2013 9:09 pm
chTimeIsWithin() is defined in kernel/src/chvt.c as:
Could not this function be more efficiently to redefined as (a simple macro)?:
Subtracting start from the chTimeNow() deals with the possible time wrap around without conditional branching and multiple tests. Moreover, usually, one is just trying to determine whether more than a certain number of limitTics has elapsed since a given start time. The test for that is even simpler:
The test becomes:
and the original chTimeIsWithin() test becomes:
Just wondering what I'm missing.
Is there some case where this simpler logic is not equivalent to the existing chTimeIsWithin()?
Code: Select all
bool_t chTimeIsWithin(systime_t start, systime_t end) {
systime_t time = chTimeNow();
return end > start ? (time >= start) && (time < end) :
(time >= start) || (time < end);
}
Could not this function be more efficiently to redefined as (a simple macro)?:
Code: Select all
#define chTimeIsWithin(start, end) \
(chTimeNow()-(start) < ((end) - (start)))
Subtracting start from the chTimeNow() deals with the possible time wrap around without conditional branching and multiple tests. Moreover, usually, one is just trying to determine whether more than a certain number of limitTics has elapsed since a given start time. The test for that is even simpler:
Code: Select all
//return the number of tics since start time
#define chTimeElapsedSince(start) (chTimeNow()-(start))
The test becomes:
Code: Select all
chTimeElapsedSince(start) < limitTics
and the original chTimeIsWithin() test becomes:
Code: Select all
#define chTimeIsWithin(start, end) chTimeElapsedSince(start) < ((end)-(start))
Just wondering what I'm missing.
Is there some case where this simpler logic is not equivalent to the existing chTimeIsWithin()?