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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Andy Peters
06/19/09 12:16
Read: 196 times
Tucson, Az
USA


 
#166260 - re: unsigned
Responding to: Per Westermark'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      Sandeep Gupta      06/02/09 00:50      
   Software loops can be optimized away      Per Westermark      06/02/09 01:07      
   lacks side-effects      Neil Kurzman      06/02/09 01:07      
      First time with LINT?      Andy Neil      06/02/09 01:19      
   lacks side-effects      Andy Neil      06/02/09 01:07      
      That does not mean it is an error.      Neil Kurzman      06/02/09 01:12      
      It also blocks      Jez Smith      06/02/09 01:49      
   How to post legible source code      Andy Neil      06/02/09 01:13      
   DELAY_0.1.ZIP Useful?      Murray R. Van Luyn      06/14/09 22:11      
      That doesn't help, and it won't work anyhow!      Andy Neil      06/15/09 01:54      
         I stand by it.      Murray R. Van Luyn      06/15/09 19:21      
            Yes a delay function is useful      David Prentice      06/16/09 02:39      
               wrong !!!!      Erik Malund      06/16/09 08:46      
               No, that's precisely where you're wrong      Andy Neil      06/16/09 12:35      
            How can you say that?      Andy Neil      06/16/09 12:43      
               I think you should read Murray's comments      David Prentice      06/16/09 15:20      
                  I have seen ...      Erik Malund      06/16/09 15:45      
                     Timers usable without start/stop too        Per Westermark      06/16/09 16:30      
                        free-running counter/timer      Andy Peters      06/18/09 17:30      
                           Unsigned integers      Per Westermark      06/18/09 17:39      
                              re: unsigned      Andy Peters      06/19/09 12:16      
                                 Try unsigned subtraction with borrow      Per Westermark      06/19/09 13:05      
                                 bug in second (improved!?) code block      Andy Peters      06/19/09 16:38      
   Delay Loops in 'C'..!!! NO      Mahesh Joshi      06/16/09 05:44      
      Go on. Suggest a SIMPLE alternative      David Prentice      06/16/09 06:20      
         My Methods      Mahesh Joshi      06/16/09 07:21      
            So he has a long list of constraints      David Prentice      06/16/09 07:46      
               oh boy what a load who wil have 10 minutes for this      Erik Malund      06/16/09 08:55      
                  Ok. I was being naughty.      David Prentice      06/16/09 10:28      
                     you forget the obvious ...      Erik Malund      06/16/09 10:49      

Back to Subject List