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

Vlado Handziski handzisk at tkn.tu-berlin.de
Thu Jun 25 11:42:59 PDT 2009


Speaking about optimization, and looking at your example snippet: For the
msp430 platforms, it is always much more efficient to use word size array
indices. The code is a lot simpler and some nasty gcc bugs can be avoided
more easy.

Vlado


On Thu, Jun 25, 2009 at 20:30, Eric Decker <cire831 at gmail.com> 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.
>
> --
> Eric B. Decker
> Senior (over 50 :-) Researcher
> Autonomous Systems Lab
> Jack Baskin School of Engineering
> UCSC
>
>
> _______________________________________________
> Tinyos-devel mailing list
> Tinyos-devel at millennium.berkeley.edu
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.millennium.berkeley.edu/pipermail/tinyos-devel/attachments/20090625/2f6d728d/attachment-0001.htm 


More information about the Tinyos-devel mailing list