Why a condition like If (~Input) changes to a complex condition with ternary operator ?

7 Ansichten (letzte 30 Tage)
When I am trying to generate C code with Embedded Coder, the following condition
if (~Input)
is converted to the following ugly condition
if ((boolean_T)((int32_T)((Input ? ((int32_T)1) : ((int32_T)0)) ^ 1))) {
Is there any option to change in the setting to avoid this type of code being generated?
Is there a reason why this code would be generated instead of a simpler alternative?
The same code is generated if I change the condition to:
if (false == Input)
  4 Kommentare
Walter Roberson
Walter Roberson am 1 Dez. 2022
Bleh, that code is equivalent to
xor(1, Input ~= 0)
which is pretty ugly.
What happens if you change the condition to
if (Input == 0)
?
Miguel
Miguel am 1 Dez. 2022
I tried this different condition and the generated code was the same.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Miguel
Miguel am 1 Dez. 2022
Thanks everybody to take the time to review the question.
After reviewing all the possible options in the Code Generator and comparing one configuration that generated the ugly code vs a new one that didn't generate it, It was found that the parameter responsible for the ugly code is:
The ugly code is generated when it is set to "Bitwise operator"

Weitere Antworten (1)

Les Beckham
Les Beckham am 1 Dez. 2022
So, the original condition if (~Input) is C code and not Matlab code? If so, you should use the logical not operator which is ! in C instead of ~ (as it is in Matlab).
~false in C will be 0xFFFFFFFE (or similar) and the ugly ternary expression coerces that back to a 1.
BTW - please post comments as comments rather than answers.
  3 Kommentare
Les Beckham
Les Beckham am 1 Dez. 2022
I thought that too until OP said "The class of variable "Input" is "boolean_T". Perhaps he meant in the generated code. I meant in the original Matlab code when I asked that. Probably a misunderstanding.
@Miguel - can you tell us the class of Input in the Matlab code instead of in the generated C code?
Miguel
Miguel am 1 Dez. 2022
The original code was Matlab code. The "Input" variable type was inherited from simulink and it was of type boolean. The code is in a Matlab function inside of a simulink model.
In the generated C code, the "Input" variable was of type "boolean_T".

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Deployment, Integration, and Supported Hardware finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by