shift operation in C language for microcontrollers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

shift operation in C language for microcontrollers

849 Views
admin
Specialist II

Hello ,

 

My question is generic to all controllers. The question is whether we can use shift  operation with negative counts.

ie

 

a =  b << (-4):

 

Does it do a right shift 4 bit position ? When checked in internet ,the comments are like it is not possible ?

But when checked with one of my friend he told it's possible and is controller specific.

 

1. Does this possible with (in C context) with any compilers of  freescale controllers?

Is this controller specific operation?

 

Thanks and Regards

Syam

Labels (1)
0 Kudos
8 Replies

575 Views
JimDon
Senior Contributor III

The answer is it will shift left by 0xfffffffc in the V2 compiler, so no it will not shift right by 4.

If there is some compiler out there that chose some non standard way of looking at the sign and reversing the shift direction, good luck with that.

 

0 Kudos

575 Views
kef
Specialist I

According to ISO/IEC 9899:1999 if the value of shift count is negative or is greater than or equal to the width of the promoted left operand (b), the behavior is undefined. Thus it is compiler specific and you should assume that it is not possible if you are going to code well and reuse your code in future.

0 Kudos

575 Views
admin
Specialist II

HEllo,

 

Thank you very much for the reply....

 

I have checked with one of my friend .He is working on tricore controllers with hightec compiler and he is doing the shift with negative count. We had a good word fight each other regarding this.Finally I had to give up byseeing his code working.

 

Is this operation compiler specific or controller specific ? Does it depends on the asembly shift instruction available for controllers.

 

Does freescale controllers support such shifting?

 

Thanks and Regards

Syam

0 Kudos

575 Views
JimDon
Senior Contributor III

 

In assembly language, it will still not work. You still can't shift left by a negative number and get a shift right , it make no sense, and that is why the "C" standard makes it undefined.

 

How about this:

if( sftcnt < 0 )

  a = b >>  (-1 * sfcnt);

else

 a = b<<sftcnt;

0 Kudos

575 Views
kef
Specialist I

TriCore shift instructions do left shift on positive shift count and right shift on negative shift count.

Probably TriCore C compiler just doesn't care to check if shift count is negative or not and it is a hack to use << not just for left shift. To make code portable and more readable by new TriCore guys, TriCore users should do something like this:

 

#if defined(TRICORE) // /* or whatever TRICORE specific predefined define*/

    a = b << sftcnt;

#else  // other architectures

   if( sftcnt < 0 )

      a = b >>  (-1 * sfcnt);

   else

      a = b<<sftcnt;

#endif

0 Kudos

575 Views
FridgeFreezer
Senior Contributor I

Instead of this:

  a = b >>  (-1 * sfcnt);

 

I usually do this:

  a = b >>  (0 - sfcnt);

 

x-y is faster (in machine instructions) than x*y, this may not hold true for values of 1/-1 but I thought I'd offer the other point of view.

 

There is a document somewhere detailing CPU cycles for certain operations and as a result we replaced a lot of code that was:

a = X mod Y

with:

if(X > Y)

{

X -= Y

}

a = X

As it's many times faster to test & subtract than to do the modulus. Of course you need to be sure you won't need to subtract Y more than once, but you can easily make it a WHILE loop to handle that.

0 Kudos

575 Views
scifi
Senior Contributor I

FridgeFreezer wrote:
x-y is faster (in machine instructions) than x*y

You could just write -sfcnt (unary minus). Anyway, this is the compiler's job. This optimization is called 'strength reduction'. You should not have to do this in your source code with the compilers of the 21st century. If you do, maybe you should pick a different compiler.

0 Kudos

575 Views
scifi
Senior Contributor I

I have checked with one of my friend .He is working on tricore controllers with hightec compiler and he is doing the shift with negative count. We had a good word fight each other regarding this.Finally I had to give up byseeing his code working.

You friend likes to produce some sloppy code. The fact that it works here and now is somewhat relevant, but there are numerous reasons why it should not be done this way.


Is this operation compiler specific or controller specific ? Does it depends on the asembly shift instruction available for controllers.

Yes, yes and yes.

 

If you like to go low-level, just write the piece of code in assembly language and be done with it. There will be no complaints from me, provided the assembly code is decent, anyway :smileyhappy:

0 Kudos