I Did It, But I Cannot Remember Why.... (32-bit Math Issue)
JonnyMac
Posts: 9,105
I have a lot of projects that require simple asynchronous timing, so I encapsulated everything into an object for ease-of-use. It's simple: start the object to create a sync point (snapshot of cnt register). To update the timer one uses mark(). The timer will return the current milliseconds and seconds. Again, all very simple and has been very useful to me.
Now I'm looking at it and cannot justify a bit of code -- here's the code in question.
What I am unable to re-justify to myself is the +2 in the marked line. I put it there for a reason, but cannot remember. Please have sympathy, friends, I haven't been sleeping well and I'm working my guts out in prep for a huge convention next week (with lots of Propeller goodies for the Halloween industry).
Thanks for helping me jog my tired brain, or set me straight if I'm just dead wrong.
Now I'm looking at it and cannot justify a bit of code -- here's the code in question.
pub mark | now, delta '' Marks the current point '' -- updates ticks and ms accumulators '' -- returns current milliseconds now := cnt ' capture cnt delta := now - sync ' delta since last capture if (delta < 0) ' rollover past posx? msecs += posx / mstix ' add posx millis [COLOR="#FF0000"]delta += posx + (posx // mstix) + 2 ' correct delta[/color] rawtix += delta ' increment ticks msecs += rawtix / mstix ' update millis rawtix //= mstix ' udpate ticks sync := now ' reset sync point return msecs ' return millis
What I am unable to re-justify to myself is the +2 in the marked line. I put it there for a reason, but cannot remember. Please have sympathy, friends, I haven't been sleeping well and I'm working my guts out in prep for a huge convention next week (with lots of Propeller goodies for the Halloween industry).
Thanks for helping me jog my tired brain, or set me straight if I'm just dead wrong.
spin
5K
Comments
To get back to 1 (or any other positive number) after adding posx, you also need to add 2.
That explanation didn't make any sense but hopefully it's enough to jog your memory.
Another way to see it is to walk through the case of when the time interval was $8000_0000 clock cycles.
Yes. I should have wrote negx + posx = -1
Or $8000_0000 + posx = -1
In the case of $8000_0000 you're adding posx to force another rollover but the rollover is 2 short of the correct value.