Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/19/09 17:16
Read: times


 
#166260 - re: unsigned
Responding to: ???'s previous message
Per Westermark said:
With unsigned integers, you don't need to take rollovers into account unless you need long delays in relation to the rollover frequency.


Hmmm ... time for an experiment.

So assume we count down, and my starting timer count is 0x100, and my desired period is 0x1234. I need to compare against 0x100-0x1234, which rolls over. So does the compiler saturate at 0, or does it roll over?

Here's what I'm doing, which handles rollover. I think I added the rollover handling code because I didn't know what the compiler would do in the rollover case. (To me, "undefined operation" may include a black hole swallowing the world, and I do my best to avoid this.)

lastTick = timer_GetCount();
for(;;) {
    thisTick = timer_GetCount();
    if (lastTick > thisTick) {
        // normally count down
        moduloTick = lastTick - thisTick;
    } else {
        // handle rollover:
        moduloTick = lastTick + 0xFFFFFFFF - thisTick;
    }
    // should we process?
    if (moduloTick > INTERVAL) {
        lastTick = thisTick;   // save new count as old
        doWhatNeedsToBeDone(); // process stuff
    }
} // forever 
 


And now that I look at it, a better solution, which saves the compare and subtract each time though the loop, would be something like:

timeout = timer_GetCount();
if (timeout < INTERVAL) {
    // handle rollover:
    timeout = 0xFFFFFFFF - INTERVAL - timeout;
} else {
    timeout -= INTERVAL;
}

for(;;) {
    thisTick = timer_GetCount();
    if (thisTick == timeout) {
        // compute timeout for next interval:
        if (thisTick < INTERVAL) {
            timeout = 0xFFFFFFFF - INTERVAL - timeout;
        } else {
            timeout = thisTick - INTERVAL;
        }
        doWhatNeedsToBeDone(); // process stuff
    }
} // forever 
 


One suspects that the compiler will optimize the subtraction of two constants 0xFFFFFFFF - INTERVAL.

-a


List of 30 messages in thread
TopicAuthorDate
s/w delay function            01/01/70 00:00      
   Software loops can be optimized away            01/01/70 00:00      
   lacks side-effects            01/01/70 00:00      
      First time with LINT?            01/01/70 00:00      
   lacks side-effects            01/01/70 00:00      
      That does not mean it is an error.            01/01/70 00:00      
      It also blocks            01/01/70 00:00      
   How to post legible source code            01/01/70 00:00      
   DELAY_0.1.ZIP Useful?            01/01/70 00:00      
      That doesn't help, and it won't work anyhow!            01/01/70 00:00      
         I stand by it.            01/01/70 00:00      
            Yes a delay function is useful            01/01/70 00:00      
               wrong !!!!            01/01/70 00:00      
               No, that's precisely where you're wrong            01/01/70 00:00      
            How can you say that?            01/01/70 00:00      
               I think you should read Murray's comments            01/01/70 00:00      
                  I have seen ...            01/01/70 00:00      
                     Timers usable without start/stop too            01/01/70 00:00      
                        free-running counter/timer            01/01/70 00:00      
                           Unsigned integers            01/01/70 00:00      
                              re: unsigned            01/01/70 00:00      
                                 Try unsigned subtraction with borrow            01/01/70 00:00      
                                 bug in second (improved!?) code block            01/01/70 00:00      
   Delay Loops in 'C'..!!! NO            01/01/70 00:00      
      Go on. Suggest a SIMPLE alternative            01/01/70 00:00      
         My Methods            01/01/70 00:00      
            So he has a long list of constraints            01/01/70 00:00      
               oh boy what a load who wil have 10 minutes for this            01/01/70 00:00      
                  Ok. I was being naughty.            01/01/70 00:00      
                     you forget the obvious ...            01/01/70 00:00      

Back to Subject List