CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Logic shift with signed integer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kalo



Joined: 02 Dec 2003
Posts: 0

View user's profile Send private message

Logic shift with signed integer
PostPosted: Thu Mar 04, 2004 7:21 am     Reply with quote

In my program I have only divisions by x². I would like to shorten them by using right shift. Unfortunately the compiler shifts always 0 in. Is there any workaround instead of using a real division routine provided by CCS??? Rolling Eyes
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

/2?
PostPosted: Thu Mar 04, 2004 8:12 am     Reply with quote

I assume you mean /2?

If so try shift_right(). Beware the syntax - the address of the value must be sent, and it returns the 'remainder'; so use it like:

signed int8 value = -100;
int8 remainder;
remainder = shift_right( &value, 1, 1 );

will give value = -50;

Nice thing is it works for any number of bytes (int16, etc) and allows dividing by any power of 2.

- SteveS
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Logic shift with signed integer
PostPosted: Thu Mar 04, 2004 8:30 am     Reply with quote

kalo wrote:
In my program I have only divisions by x². I would like to shorten them by using right shift. Unfortunately the compiler shifts always 0 in. Is there any workaround instead of using a real division routine provided by CCS??? Rolling Eyes


Read ths list file and you will note that the compiler does shifts for power of 2 division. No need to specify rotation. Just make the divisior a power of 2 that is a constant.
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

PostPosted: Thu Mar 04, 2004 9:12 am     Reply with quote

Well, I thought the same and compiled code to check it.

s = -100;
s2 = s/2;

A call is made to DIVS88. I didn't look into that function - perhaps a test is made there.

I know I have seen it compile the shift in other programs I have done. That's why I cautioned someone the other day about trying to outsmart the compiler. It can make some pretty amazing optimizations.

Ahha - just tried something. An unsigned int will get the shift, signed int forces a call to DIVS88.

- SteveS
Ttelmah
Guest







PostPosted: Thu Mar 04, 2004 9:42 am     Reply with quote

SteveS wrote:
Well, I thought the same and compiled code to check it.

s = -100;
s2 = s/2;

A call is made to DIVS88. I didn't look into that function - perhaps a test is made there.

I know I have seen it compile the shift in other programs I have done. That's why I cautioned someone the other day about trying to outsmart the compiler. It can make some pretty amazing optimizations.

Ahha - just tried something. An unsigned int will get the shift, signed int forces a call to DIVS88.

- SteveS

For a single shift I wouldn't worry about it. However the obvious 'solution', if wanting to do multiple shifts, is to test for the number being negative. If it is, set a flag, and change it's sign. Perform the rotations required, and then change the sign back if required.
Something like:
Code:

signed int16 shiftsignedright(int16 val, int numbits){
   int1 sign;
   if (val & 0x8000l) {
      sign=true;
      val &=0x7FFFl;
  }
  else sign=false;
  //Rotate as required here
  shift_right(&val,2,numbits);
  if (sign) val |=0x8000l;
  return(val);
}


If you call this with a signed int16 number, this will be handed to the routine as an unsigned number (but with the sign bit still there). This is then tested, and removed if needed. The word is then rotated the number of bits specified, and the sign bit put back.

I think it ought to work....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group