Shop OBEX P1 Docs P2 Docs Learn Events
Shift & Rotate with count > 31 - in Spin and assembly language — Parallax Forums

Shift & Rotate with count > 31 - in Spin and assembly language

ksltdksltd Posts: 163
edited 2014-08-25 10:39 in Propeller 1
There's no documentation regarding the effect of any shift or rotate instruction when the shift amount is greater than 31.

The instructions are SHL, SHR, SAR, ROL, ROR

The corresponding Spin operators are <<, >>, ~>, <- and ->.

The assembly language documentation suggests that if the source operand is an immediate form, that only the five least significant bits are used. This would imply that a shift of 33 bits is really a shift of 1 bit when using the immediate form.

Is the same true of the dynamic form of the instructions or does a shift of more than 31 bits shift away the entire contents of Dest?

What about the semantics of the corresponding Spin operators?

For what its worth, the assembler doesn't generate an error for:
shr <any address>,#<value bigger than 31>

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-08-24 15:45
    ksltd wrote: »
    There's no documentation regarding the effect of any shift or rotate instruction when the shift amount is greater than 31.
    datasheet: shift amount is src[4..0], that covers both immediate and register sources
    ksltd wrote: »
    What about the semantics of the corresponding Spin operators?
    SPIN uses PASM equivalents where appropriate. IOW same effect for these operators.
    ksltd wrote: »
    For what its worth, the assembler doesn't generate an error for:
    shr <any address>,#<value bigger than 31>
    Why would that be desirable? Imagine e.g. a packed pin value long. Do you really want to copy it to a temporary for each value and apply a mask instead of just using it? Besides, using a register makes it rather tricky to know what's in there at runtime, so why bother treating immediates differently?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-08-24 21:19
    In Spin you can do it this way:
    x := (y >> z) & (z < 32)
    

    The "z< 32" results if $ffff_ffff if true; 0, otherwise. 'Works for positive z only, BTW.

    If z can be negative,
    x := (y >> z) & (z == z & $1f)
    

    -Phil
  • ksltdksltd Posts: 163
    edited 2014-08-25 07:11
    kuroneko,

    Uggh, it never occurs to me to look at the datasheet for the implementation to glean information about the architecture.

    My point about the assembler is that it would be good for it to generate an assembly-time error for something like:
    shr some_register, #47

    Phil,

    My question wasn't about how to program, it was about the definition of the behavior.
  • Mike GreenMike Green Posts: 23,101
    edited 2014-08-25 08:04
    There's nothing wrong with writing "shr some_register, #47". It's a perfectly valid instruction even though the result of executing it may be different from what you expect. For example, for some strange reason, you may be using that instruction as a variable to store some flags in the otherwise unused source field bits and this is the initial value of that variable. It's not the job of an assembler to catch bizarre uses of an instruction set and treat them as errors. I could see an assembler producing optional warnings about non-standard usage, but neither Spin nor its assembler produce warnings.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-08-25 09:15
    BTW, ROL and ROR work the way you'd expect for any size rotate, since the net change in the result is modulo 32 anyway.

    -Phil
  • Mark_TMark_T Posts: 1,981
    edited 2014-08-25 10:35
    AFAICR all the arhitectures I've seen with a barrel-shifter just route the low N bits to the shifter ignoring the rest.

    Actually, I've a nagging suspicion that there was one architecture that had a signed-shift (ie the direction
    of shift was encoded in the shift-amount register), but can't remember for sure.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-08-25 10:39
    The only real anomaly with these instructions is that the carry represents the first bit shifted out, rather than the last one.

    -Phil
Sign In or Register to comment.