View previous topic :: View next topic |
Author |
Message |
Marco27293
Joined: 09 May 2020 Posts: 126
|
DSPIC33EP512GP502 -- PCD v5.115 compiler |
Posted: Tue Apr 30, 2024 7:48 am |
|
|
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: 19538
|
|
Posted: Tue Apr 30, 2024 8:00 am |
|
|
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: 126
|
|
Posted: Tue Apr 30, 2024 8:06 am |
|
|
Isn't PCD compiler able to handle the 100*uint8 var multiplication without overflow ?? |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Tue Apr 30, 2024 8:22 am |
|
|
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: 126
|
|
Posted: Tue Apr 30, 2024 8:24 am |
|
|
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: 126
|
|
Posted: Tue Apr 30, 2024 10:09 am |
|
|
Please help me, I need to understand if this behaviour is PCD compiler dependant... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Tue Apr 30, 2024 11:43 am |
|
|
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. |
|
|
|