Why do I receive a div_s32_floor function instead of '/' symbol in the generated code when I have a Divide block in Embedded Coder 6.2 (R2013a)?
4 views (last 30 days)
Show older comments
MathWorks Support Team
on 25 Oct 2013
Answered: MathWorks Support Team
on 25 Oct 2013
I use a Divide block (Input data type "int32", Output data type "int32", Integer rounding mode "floor") in the model. After code generation I get a function "div_s32_floor" in the code. Why does the code generator include this additional function? It would be easier to use a "/".
Accepted Answer
MathWorks Support Team
on 24 Oct 2013
The 'div_s32_floor' function is actually expected behaviour. For signed integers, when you do integer division with 'Integer rounding mode' set to 'Floor', the division operation result is different from the one given by the simple '/' division symbol. So 'div_s32_floor' is generated to do the division properly. This is a simple example to illustrate the behaviour.
Suppose I have :
a = int32(1);
b = int32(4);
If I execute :
a/b
I get "0" as the result, and if I execute :
div_s32_floor(a,b)
I also get "0" as the result. The answer is correct in both cases because they have been rounded to the flooring number.
But if I have :
a = int32(-1);
b = int32(4);
If I execute :
a/b
I get "0" as the result, and if I execute :
div_s32_floor(a,b)
I get "-1" as the result. The answer given by 'div_s32_floor(a,b)' is correct because of the rounding to the floor requirement.
The workaround to replace the helper function that protects against zero division is to do the following:
1. Open the Configuration Parameters and navigate to the Hardware Implementation pane
2. Set "Signed integer division rounds to:" to "Zero" and Apply changes.
3. Go to the Optimization pane in Configuration Parameters
4. Select "Remove code that protects against division arithmetic exceptions".
5. Locate the Divide block in the Simulink model and open the block parameters.
6. On the "Signal Attributes" tab, set "Integer rounding mode" to "Zero" and Apply changes.
After rebuilding, the generated code should produce a single slash for the division. Please note that removing code that protects against division by zero may not be ideal for the customer's application, but this step may be necessary to ensure that the generated code for division is just a slash instead of a helper function.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!