Why is type conversion int32_t to uint32_t a Sign Change

Code generated for a division operation with 'Floor' rounding and 'int32' output datatype is generating a Polyspace Defect: 'Sign Change Integer Conversion Overflow'. The supposed overflow occurs in the code where the absoute value of the numerator is calculated. When the numerator is negative, the value is cast as an uint32_T before calculating the two's complement to make it positive. I don't understand how this can overflow. Is this a bug with Polyspace?
int32_T div_s32_floor(int32_T numerator, etc ...)
uint32_T absNum;
absNum = (numerator < 0) ? ((~((uint32_T)numerator)) + 1U): ((uint32_T)numerator);
/* function call */
div_s32_floor(int32_t numerator, etc...);


Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

First, two suggestions
Here are two suggestions that can improve the efficiency of your generated code and may make the analysis concern go away.
1) Consider changing the rounding mode specified on the block creating the division from Floor to Simplest. The C99 specification requires signed integer division to round to zero. Round to floor is lean for other operations, but requires extra work for division. Simplest will automatically use Zero rounding for division and Floor for other operations to maximize efficiency.
2) If the target provides a long long type, consider allowing Embedded Coder to use it. It's a simple checkbox setting change that will avoid most use of 32 bit multiplication and division helper functions and give more efficient generated code.
Regarding the analysis
I assume Polyspace is flagging this C expression.

This can trigger a sign change overflow. Consider the case where numerator has value -1.
Let's simulate that behavior using fi objects.

numerator = fi(-1,1,32,0)
unsignedExpression = removefimath( fi(numerator,0,32,0,'OverflowAction','Wrap') )

The change of the value from -1 to 4294967295 is an example of the sign change overflow that Polyspace is flagging.




Popular posts from this blog

Why are Fourier series important? Are there any real life applications of Fourier series?

What are some good alternatives to Simulink?

What is the difference between a reversible machine and a self-locking machine?