Caleb Owen asked . 2021-09-11

Why is type conversion int32_t to uint32_t a Sign Change Integer Conversion Overflow Defect in Polyspace?

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?
 
Example:
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...);

sign change intege... , polyspace , division , floor rounding

Expert Answer

John Michell answered . 2024-03-28 11:52:22

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.
 
((uint32_T)numerator)

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.
 
Even though it is flagged, the behavior of the code is fully correct. The C standard requires that cast from signed 32 to unsigned 32 will wrap modulo 2^32, so this well defined behavior is intentionally and correctly used by the generated C code.
 
Overflow definition is "in the eye of the beholder"
 
Perhaps your question is related to the surprising complexities of the meaning of integer overflow. MathWorks tools classify changes of value in type conversions that are due to modulo 2^NBits wrapping as overflows. Let's call this the "common" meaning of overflow. However, the C specification declares that casts to unsigned types that change value due to modulo 2^NBits wrapping are just providing the defined behavior and are not overflows. So the C standard definition and the "common" meaning of overflow disagree for operations going to unsigned types. Polyspace is giving an alert based on the common meaning of overflow.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.