Page 1 of 1

chTimeIsWithin()

Posted: Sun Oct 27, 2013 9:09 pm
by genosensor
chTimeIsWithin() is defined in kernel/src/chvt.c as:

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()?

Re: chTimeIsWithin()

Posted: Sun Oct 27, 2013 9:25 pm
by Giovanni
Hi,

I don't see problems, excellent suggestion, it is a pretty critical bit of code.

Giovanni

Re: chTimeIsWithin()

Posted: Wed Oct 30, 2013 2:14 pm
by Giovanni
Hi,

Applied the change to both stable and unstable versions, thanks.

Giovanni