[Tinyos-devel] One should make limited use of the "%" operator

Eric Decker cire831 at gmail.com
Thu Jun 25 11:30:14 PDT 2009


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.

-- 
Eric B. Decker
Senior (over 50 :-) Researcher
Autonomous Systems Lab
Jack Baskin School of Engineering
UCSC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.millennium.berkeley.edu/pipermail/tinyos-devel/attachments/20090625/2da084d2/attachment.htm 


More information about the Tinyos-devel mailing list