Shop OBEX P1 Docs P2 Docs Learn Events
I Did It, But I Cannot Remember Why.... (32-bit Math Issue) — Parallax Forums

I Did It, But I Cannot Remember Why.... (32-bit Math Issue)

JonnyMacJonnyMac Posts: 9,105
edited 2015-03-12 12:25 in Propeller 1
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.
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.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-03-12 11:55
    I think it's to correct for the fact: posx + 1 = -1

    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.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-03-12 11:58
    Actually, posx + 1 = negx. Still, I think the +2 deals with the zero digits that have to be counted. Thanks for the feedback.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-03-12 12:11
    JonnyMac wrote: »
    Actually, posx + 1 = negx.

    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.
  • RickBRickB Posts: 395
    edited 2015-03-12 12:25
    There's no such thing as to many comments...
Sign In or Register to comment.