Managing integer divide by zero checks in MATLAB R2016b codegen output

When generating C code using codegen in at least MATLAB R2016b that contains integer divisions like:
out = uint32(a)/uint32(b)
an elaborate scheme to check for division by zero is implemented as follows in C:
if (b == 0U) {
if (a == 0U) {
out = 0U;
} else {
out = MAX_uint32_T;
}
} else {
// the actual division regular case
}
I would like to be able to tell codegen that these zero checks are not necessary and only output the commented line but I am not able to find a configuration option for this functionality.
I would like so see an option like SaturateOnIntegerOverflow that is able to turn off adding code for Inf special cases but for zero division.
A different option would be the ability to tell codegen what range of values to expect for various input parameters or values.
Are either of the two options above available?
I am using the regular MATLAB Coder Toolbox from the command line in MATLAB R2016b as follows:
configstruct = coder.config('mex'); % can be exe
configstruct.SomeOption1 = SomeValue1;
configstruct.SomeOption2 = SomeValue2;
codegen(...
'SomeFunction.m',...
'-config',configstruct,...
'args',{...},...
);
The Embedded Coder Toolbox and Simulink are available to me if absolutely required.

4 件のコメント

Birdman
Birdman 2017 年 11 月 13 日
What is the question?
JM
JM 2017 年 11 月 13 日
I accidentaly hit enter in the tag box that caused the whole question to be submitted. That's a bug if you ask me.
Birdman
Birdman 2017 年 11 月 13 日
Why do you want to disable that check?
JM
JM 2017 年 11 月 13 日
I would like to reduce ROM size and computation time using the knowlegde that the values to be divided by are always nonzero.

サインインしてコメントする。

回答 (1 件)

Birdman
Birdman 2017 年 11 月 13 日

1 投票

Go to Configuration Parameters->All Parameters and write Remove code that protects against division arithmetic exceptions. Its default value is off(unchecked). Make that on(checked).

10 件のコメント

JM
JM 2017 年 11 月 13 日
Thank you. Is this functionality also available in the regular MATLAB Coder toolbox (without the Embedded part)?
Birdman
Birdman 2017 年 11 月 13 日
It should be because this is independent from Coder part. This is a parameter of Configuration Parameters tab.
JM
JM 2017 年 11 月 13 日
編集済み: JM 2017 年 11 月 13 日
I updated the original question for this important detail: I am using the command line Coder functions like codegen() and coder.config() and honestly do not know where to look for a "Configuration Parameters" tab in some GUI. I have searched all over the place but no luck. Excuse my ignorance on this subject.
Birdman
Birdman 2017 年 11 月 13 日
Configuration Parameters tab takes place in Simulink and I thought you were on a Simulink model. From command line, try to insert the function in codegen command as follows:
codegen(...,'NoFixptDivByZeroProtection','Value','on',...)
I haven't done this from command line, so this is the thing that I can suggest.
JM
JM 2017 年 11 月 13 日
Ah you are talking about this option: https://nl.mathworks.com/help/simulink/gui/remove-code-that-protects-against-division-arithmetic-exceptions.html
That is exactly what I need but I cannot find it outside of the Simulink environment. :(
Birdman
Birdman 2017 年 11 月 13 日
This is the page that we should be looking but unfortunately there is no property for removing protection against division by zero for mex configuration.
JM
JM 2017 年 11 月 13 日
I have looked at
https://nl.mathworks.com/help/coder/ref/coder.mexcodeconfig.html https://nl.mathworks.com/help/coder/ref/coder.codeconfig.html https://nl.mathworks.com/help/coder/ref/coder.embeddedcodeconfig.html
but none of these classes have the required option. :(
Birdman
Birdman 2017 年 11 月 13 日
Can't you implement this in Simulink?
JM
JM 2017 年 11 月 24 日
To continue this story I could not find a way to use Simulink Coder to generate mex files from an .m file and still support this option. The option in question is only available when generating ERT files. :(
As a side note I could use many more of the code generation options presented in Simulink but not available in the command line code generator. :(
Birdman
Birdman 2017 年 11 月 26 日
Don't you have a way to implement what you are doing right now in Simulink? Because it will be a lot easier for you.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSimulink Coder についてさらに検索

質問済み:

JM
2017 年 11 月 13 日

コメント済み:

2017 年 11 月 26 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by