Shop OBEX P1 Docs P2 Docs Learn Events
Wrapping variables.... — Parallax Forums

Wrapping variables....

HughHugh Posts: 362
edited 2010-07-26 08:44 in Propeller 1
Hi,

If I have an array of bytes, such as:

hist[noparse][[/noparse]0] := 1
hist[noparse][[/noparse]1 := 2
hist[noparse][[/noparse]2 := 3
hist[noparse][[/noparse]3 := 4


Is there any simple way I can shift everything 'leftwards' and update the value of hist?

I've tried bitwise shifts to no avail. Should I structure the data differently to enable an easier method?

Any suggestions gratefully received!

Thanks
Hugh

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-07-24 17:37
    You can use bytemove, wordmove, or longmove, depending on the size of the data entires. But the bigger question is why you need to shift things like this. It's usually more efficient to use a circular buffer and keep track of the head and tail via pointers.

    -Phil
  • HughHugh Posts: 362
    edited 2010-07-24 18:35
    Phil Pilgrim (PhiPi) said...
    You can use bytemove, wordmove, or longmove, depending on the size of the data entires. But the bigger question is why you need to shift things like this. It's usually more efficient to use a circular buffer and keep track of the head and tail via pointers.

    -Phil

    I think you have the advantage of me w.r.t circular buffers!(?) - the only other way I could think to do it was to copy the earliet to the last and move everything on one: that didn't seem too elegant.

    Thanks
    Hugh

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Hugh - the thinking woman's Geoffrey Pyke.
  • localrogerlocalroger Posts: 3,452
    edited 2010-07-24 20:30
    In a more typical environment the circular buffer would be more efficient, but in Spin the bytemove etc. operations are written in PASM and are much, much faster than loop operations in Spin, so shifting the whole array to make room for the new entry might actually be faster, or at least on the same scale with much simpler code.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-07-24 20:53
    The comparative efficiency probably depends on the length of the buffer. Also, if you use the shift method, you will run into problems if queuing and dequeuing are done in separate cogs. Using a lock would solve this, but locks are not needed if a circular buffer with pointers is used. I think it's better to acquaint oneself with circular buffers from the get-go. It's like investing in a good chisel, instead of using a sharpened screwdriver.

    -Phil
  • HughHugh Posts: 362
    edited 2010-07-26 06:09
    Thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Hugh - the thinking woman's Geoffrey Pyke.
  • heaterheater Posts: 3,370
    edited 2010-07-26 08:44
    Have a look in the FullDuplexSerial object for a good example circular buffers in Spin. Of course the tx and rx buffers also have PASM code one end.

    There are a couple of things I'd like to point out:

    1) The wiki pages linked to above has examples in C. There they use the mod operator "%" to wrap the head and tail pointers round the buffer. Better to make your buffers a power of 2 in size, 4, 8, 16, 32, 64 etc. Then you can just "and" the head and tail pointers with a mask to wrap them. Much faster.

    2) As pointed out above but perhaps not obvious is the fact that one process, in a COG say, can fill the buffer and another can read from the same buffer at the same time with no worries about data corruption when using a cyclic buffer and no locks. But remember only one writer and one reader process is allowed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
Sign In or Register to comment.