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

Eric Decker cire831 at gmail.com
Thu Jun 25 20:54:18 PDT 2009


On Thu, Jun 25, 2009 at 3:28 PM, Philip Levis <pal at cs.stanford.edu> wrote:

>
> On Jun 25, 2009, at 2:54 PM, Michael Newman wrote:
>
>
>>
>> 2) Phil's strategy is ONLY a good idea if idx is an 8 bit integer. If the
>> data type is a 16 bit or 32 bit integer this code could result in a loop
>> that takes an unexpectedly long time to execute.
>>
>>>
>>> or
>>>
>>> while (idx > QUEUE_SIZE)
>>>  idx -= QUEUE_SIZE;
>>>
>>>
> Right -- I'm sorry, I read the code so quickly I didn't actually read it. I
> assumed
>
> if (idx >= QUEUE_SIZE)
>  idx -= QUEUE_SIZE;
>
> If you look, that's what I committed.
>
> Phil
>

If idx is out in the weeds this will still do something not very nice.  Its
why
I put the check at the front.  Or perhaps safe would catch that:

command queue_t Queue.element(uint8_t idx) {
   if (idx >= QUEUE_SIZE)
     return NULL;
   idx += head;
   if (idx >= QUEUE_SIZE)
      idx -= QUEUE_SIZE;
   return queue[idx];
}




-- 
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/00afdcaa/attachment.htm 


More information about the Tinyos-devel mailing list