Shop OBEX P1 Docs P2 Docs Learn Events
Clearing an array of bytes — Parallax Forums

Clearing an array of bytes

Vega256Vega256 Posts: 197
edited 2014-12-20 18:04 in Propeller 1
Hello all,

Suppose I have declared an array of bytes.
var

byte myByte [6]

Is there some immediate operator that I can use to clear all of the bytes in the array, or do I have to use bytefill?

Comments

  • dgatelydgately Posts: 1,630
    edited 2014-12-20 10:48
    Vega256 wrote: »
    Hello all,

    Suppose I have declared an array of bytes.
    var
    
    byte myByte [6]
    

    Is there some immediate operator that I can use to clear all of the bytes in the array, or do I have to use bytefill?


    From the Propeller Manual (Web-PropellerManual-v1.2.pdf):
    VAR
      byte   Buff[100]
    PUB Main
      bytefill(@Buff, 0, 100)    'Clear Buff to 0
    


    dgately
  • ElectrodudeElectrodude Posts: 1,658
    edited 2014-12-20 12:18
    Byte/word/long fill/move is always way faster than any other way in SPIN (unless it's just a single byte, word, or long, in which case just setting it to 0 is the fastest way).

    Note, though - you have an array of 6 bytes, which is divisible by 2 bytes (= 1 word), so you could use wordfill in your case, which would be slightly faster. If it was divisible by 4, longfill would be even faster than wordfill or bytefill. However, with such a small array, the speed difference is probably be insignificant. There's no code size difference AFAIK.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-12-20 12:44
    Note, though - you have an array of 6 bytes, which is divisible by 2 bytes (= 1 word), so you could use wordfill in your case, which would be slightly faster. If it was divisible by 4, longfill would be even faster than wordfill or bytefill. However, with such a small array, the speed difference is probably be insignificant. There's no code size difference AFAIK.

    When possible, I define byte arrays as longs; this gives me access as longs, words, or bytes, and the best speed. You an do longfill on a byte array that is misaligned, but it's a tad slower than if the array is long-aligned.

    In a recent project I did this:
    long buffer[64/4]
    

    Again, this allows clean access as bytes, words, or longs, and using longfill with a long-aligned array is faster than if the array is misaligned (not on an even long boundary).
  • ElectrodudeElectrodude Posts: 1,658
    edited 2014-12-20 16:48
    JonnyMac wrote: »
    When possible, I define byte arrays as longs; this gives me access as longs, words, or bytes, and the best speed. You an do longfill on a byte array that is misaligned, but it's a tad slower than if the array is long-aligned.

    In a recent project I did this:
    long buffer[64/4]
    

    Again, this allows clean access as bytes, words, or longs, and using longfill with a long-aligned array is faster than if the array is misaligned (not on an even long boundary).

    I just looked at the code for byte/word/longfill in the spin interpreter. byte/word/longfill doesn't care whether what you feed it a pointer to is a byte, word, or long array. It just takes a pointer and fills stuff, using wrbyte, wrword, or wrlong and a stride of 1, 2, or 4 depending on which flavor you use (they're all in the same opcode group). If you try doing a longfill on a misaligned array (of bytes, words, or longs), the address will just get rounded down to the nearest long, like how it always happens when you try reading misaligned longs. rdlong and wrlong cannot read or write a misaligned long - they will zero the bottom two bits. It's just as fast as filling aligned longs, except that it doesn't fill starting at the pointer you feed it. It makes no difference whatsoever to longfill what type you define things as - pointers in spin don't carry type information. It's perfectly safe to longfill a byte array if it's long aligned.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-12-20 17:06
    It's perfectly safe to longfill a byte array if it's long aligned.

    Agreed. I do all the time.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-12-20 18:04
    From the Prop manual
    In Spin, all variables defined in VAR blocks are initialized to zero.
    So the array myByte will be zero initially since it is defined in a VAR block.
Sign In or Register to comment.