Shop OBEX P1 Docs P2 Docs Learn Events
Fast way of checking block for non-zero in SPIN — Parallax Forums

Fast way of checking block for non-zero in SPIN

MacTuxLinMacTuxLin Posts: 821
edited 2014-02-24 16:02 in Propeller 1
Kinda having a brain-freeze now. Is there a way to && %111...111 (long) and left shift to see if a long contains a bit in SPIN? Needed to quickly check a block of 3,074 longs for any bit set.

Thanks

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-02-24 07:54
    There's no super fast way to do this in Spin. You'll need a loop to check one long at a time for zero (no bits set). The first non-zero long has a 1 bit somewhere. You can then use a separate loop to check 1 bit at a time for the first set bit.

    If you have a spare cog, you could use a short assembly routine that is running all the time, waiting for its shared memory long to get cleared to zero, then it scans the block and sets the shared long to the address of the first non-zero long found (and even the first bit number in the high order word of the long), then goes back to wait for the shared long to be cleared.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-24 09:19
    As Mike points out, Spin is not very fast. I have this method in my standard template; perhaps it will be of some use.
    pub bit_pos(value, mode)
    
    '' Returns position of 1st "1" bit
    '' -- mode 0 (LSBFIRST) to scan from lsb, mode 1 (MSBFIRST) to scan from msb 
    '' -- -1 = no bits set
    
      if (value == 0)                                               ' if no bits                                   
        return -1                                                   '  return -1
    
      else
        if (mode == LSBFIRST)                                       ' check from LSB
          value ><= 32                                              '  flip for >|
          return (32 - >|value)
        else
          return (>|value - 1)
    
  • MacTuxLinMacTuxLin Posts: 821
    edited 2014-02-24 16:02
    Thanks Mike.

    Thanks Jon. Cool, I'll give this a shot & if it is still not fast enough, I'll change my code to make way for a spare cog for PASM.


    Thanks
Sign In or Register to comment.