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

DSPIC33EP512GP502 -- PCD v5.115 compiler

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



Joined: 09 May 2020
Posts: 115

View user's profile Send private message

DSPIC33EP512GP502 -- PCD v5.115 compiler
PostPosted: Tue Apr 30, 2024 7:48 am     Reply with quote

Hi, I have this function:
Code:

unsigned int8  THS_array[7];
[...]
int Burst_Selector_GNPs(float ArmI)
{
   if(THS_array[0]==0&&THS_array[1]==0&&THS_array[2]==0&&THS_array[3]==0)   
      return False;
   
   if(THS_array[3]>0)
   {
      if(Sum_low_freq_power(x_data)>100*THS_array[3])   
         return False;
   }
    if(THS_array[5]>0)
   {
      if(Sum_high_freq_power(x_data)>200*THS_array[5])   
         return False;
   }
   
   if(THS_array[0]>0)
   {
      if (Arm_Value(x_data, ArmI,1)<=THS_array[0]*Arm_Value(x_data, ArmI*1.5,1))
         return False;
   }
   if(THS_array[1]>0)
   {
      if (Arm_Value(x_data, ArmI*2,1)<=THS_array[1]*Arm_Value(x_data, ArmI*2.5,1))
         return False;
   }
   if(THS_array[2]>0)
   {
      if (Arm_Value(x_data, ArmI*3,1)<=THS_array[2]*Arm_Value(x_data, ArmI*3.5,1))
         return False;
   }
    if(THS_array[6]>0)
   {
      if ((Arm_Value(x_data, ArmI,1)+Arm_Value(x_data, ArmI*2,1)+Arm_Value(x_data, ArmI*3,1))<=THS_array[6]*10)
         return False;
   }
   return True;
}


My question is : if I multiply an unsigned int8 for a number (e.g. 100*THS_array[3]) or for an unsigned int32 variable returned from another function (e.g. THS_array[0]*Arm_Value(x_data, ArmI*1.5,1)) the condition in if clause is correctly evaluated ?

I mean the multiplication result is the correct value or I get a value affected by an overflow ?

Regards,

Marco
Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Tue Apr 30, 2024 8:00 am     Reply with quote

The first will overflow, the second won't.

In the first case, '100' is an int8, and the value from THS_array is also an int8.
So int8 multiplication will be used. In the second case,if 'Arm_value' returns an
int32, then int32 arithmetic will be used.
Very much standard C. In C, the arithmetci type used is that of the 'highest'
type of value involved. Highest here is:

float
int32
int16
int8

So int32 is 'above' int16 etc. etc..
Marco27293



Joined: 09 May 2020
Posts: 115

View user's profile Send private message

PostPosted: Tue Apr 30, 2024 8:06 am     Reply with quote

Isn't PCD compiler able to handle the 100*uint8 var multiplication without overflow ??
gaugeguy



Joined: 05 Apr 2011
Posts: 290

View user's profile Send private message

PostPosted: Tue Apr 30, 2024 8:22 am     Reply with quote

If you want to do int8 x int8 with a 16 bit result there are a few ways.
standard C rules:
100L*foo
(uint16_t)100*foo
CCS function to do 8x8 multiply with 16 bit result (fastest)
_mul(100,foo)
Marco27293



Joined: 09 May 2020
Posts: 115

View user's profile Send private message

PostPosted: Tue Apr 30, 2024 8:24 am     Reply with quote

I put this snippet in the code:

Code:

THS_array[0]=200; //unsigned int8
   if(THS_array[0]*20==4000)
      output_high(LED_ONBOARD);


The led turns on, so it seems no overflow... why ?
Marco27293



Joined: 09 May 2020
Posts: 115

View user's profile Send private message

PostPosted: Tue Apr 30, 2024 10:09 am     Reply with quote

Please help me, I need to understand if this behaviour is PCD compiler dependant...
Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Tue Apr 30, 2024 11:43 am     Reply with quote

The point is that '100' by default in PCD, is a signed int16. PCD's default
integer type is signed int16. A 'value' without a type defaults to the
default compiler type. However _beware_. The default in PCD is always
signed. So if any value in THS_array is over 127, then you will get an
overflow.
So it is not really compiler dependant, it is 'default type' dependant.
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