This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem about intrisic of "_shr2"

I have a question,I use "_shr2"

 

EX: int a = _shr2(0x0000FFF3,1);

 

      a = 0xFFFFFFF9  why?  I think the answer is 0x0000FFF9.

 

Can any tell me why??   

 

Thanks All

  • Lin Kuo-Wei said:

    I have a question,I use "_shr2"

     

    EX: int a = _shr2(0x0000FFF3,1);

     

          a = 0xFFFFFFF9  why?  I think the answer is 0x0000FFF9.

     

    Can any tell me why??   

    It looks like your expected inputs to the _shr2 intrinsic are swapped, the first value src1 should contain the number of bits to shift, and the second value should contain the packed signed 16 bit values as per table 7-4 of SPRU187. In other words if a = _shr2(1, 0x0000FFF3); than the resulting a should be 0x0000FFF9.

    The SHR2 instruction only uses the lower 5 bits of the src1 value, so your 0x0000FFF3 means it is being told to shift by 19 which is invalid meaning it defaults back to a shift by 15 as per page 389 of SPRU732. However I would have expected a 0x00000001 with a 15 shift right to come out as 0x00000000 so there may still be something going wrong here, it could be that the interpretation of the intrinsic by the compiler gets messed up if you give it an invalid value for the right shift count.

     

     

  • I try this a = _shr2(1, 0x0000FFF3);  but a is all zero

    This is so strange. The output value is still wrong 

     

  • Just to be sure there are no type problems it may be worth trying to set up the intrinsic more specifically, something like:

    int a;
    int shift_amount = 1;
    unsigned value = 0x0000FFF3;
    a = _shr2(shift_amount, value);

    If that does not work than I am curious what tools versions you are running (i.e. CGT, CCS, device, etc.)?

  • I use linux environment "cg6x_6_0_11".

    Thanks all

  • The documentation that Bernie mentioned above is incorrect.  The right order for the shr2 and shru2 instructions is:

    _shr2(value, shift);

    Try this simple program:

    #include <stdio.h>

    void main(void) {
       printf(" _shru2(src2, src1) = %x\n", _shru2(0x83c072a0, 8));   //the right way
       printf(" _shru2(src1, src2) = %x\n", _shru2(8,0x83c072a0));   //they way the documentation says to do it
    }

    and the result is this:

     _shru2(src2, src1) = 830072
     _shru2(src1, src2) = 8

     

    So I believe that the documentation in SPRU198 and SPRU187 is incorrect.  As to the original question, you have the order of parameters correct, but it looks like sign extension is happening after the _shr2 instruction.  Are you treating the result as a short at any time?