[Tinyos-devel] One should make limited use of the "%" operator
Philip Levis
pal at cs.stanford.edu
Thu Jun 25 11:36:07 PDT 2009
On Jun 25, 2009, at 11:30 AM, Eric Decker wrote:
> While perusing the code and paying attention to the kind of code
> generated I noticed constructs of the form:
>
> command queue_t Queue.element(uint8_t idx) {
> idx += head;
> idx %= QUEUE_SIZE;
> return queue[idx];
> }
>
> I'm referring to "idx %= QUEUE_SIZE"
>
>
> The use of the % operator can be expensive and more often than not
> is. How is it expensive? Well what
> it does it generates a call to the math library (which pull that
> piece in which chews precious rom space) and
> the math code take a bit of time to execute.
>
> The above could might be rewritten as:
>
> command queue_t Queue.element(uint8_t idx) {
> if (idx > QUEUE_SIZE)
> return NULL;
> return queue[idx];
> }
>
> or
>
> while (idx > QUEUE_SIZE)
> idx -= QUEUE_SIZE;
>
>
> I prefer the former because it is well bounded in time execution and
> detects odd requests.
Agreed. Except that the former is buggy. The latter is the correct
approach. I'll check in the fix.
Phil
More information about the Tinyos-devel
mailing list