Opened 8 years ago
Closed 5 years ago
#4035 closed defect (worksforme)
Inefficient Code for 'or'
Reported by: | Vitalij Ruge | Owned by: | somebody |
---|---|---|---|
Priority: | normal | Milestone: | never |
Component: | Code Generation | Version: | |
Keywords: | Cc: |
Description
for the following example
y = if f1(x) < 0 or f2(x) < 0 or f3(x) < 0 then ... else ...;
we create
tmp1 = f1(x) < 0; tmp2 = f2(x) < 0; tmp3 = f3(x) < 0; y = (tmp1 || tmp2) || tmp3 ? .... : ....;
where tmp2
and tmp3
possible never used
Change History (7)
comment:1 by , 8 years ago
Priority: | high → normal |
---|
comment:2 by , 8 years ago
comment:3 by , 7 years ago
From Modelica Spec 3.3R1 I read in chapter "3.3 Evaluation Order" that A tool is free to ... not evaluate expressions if their values do not influence the result.
So Vitalij's point seems to be correct.
comment:4 by , 7 years ago
That would require you to analyze the side-effects of each of the above functions; we don't.
comment:5 by , 7 years ago
@sjoelund The spec states that (irrespectively of side effects!) the "tool" is free to not evaluate the expressions in short-cut logic. The tool is even allowed to reorder expressions.
comment:6 by , 7 years ago
if their values do not influence the result
Means you can only do that if you know that it will not influence the result. See also the 3.3.1 example:
x=v[I] and (I>=1 and I<=n); // Invalid (Because the tool is not allowed to short-circuit evaluate that)
comment:7 by , 5 years ago
Milestone: | Future → never |
---|---|
Resolution: | → worksforme |
Status: | new → closed |
This is not an error. Modelica by design requires all of the expressions to be evaluated. If you want f2 to be evaluated only if
f1(x) < 0
is false, you need to use an if-expression. Function calls can throw assertions, etc. and must be checked at run-time. So we need to evaluate tmp2 and tmp3 even if the C-code does not use the values.