Skip to content

Commit 579ef47

Browse files
committed
+ Improved handling of binary logical operations on boolean arrays (fixed #2286).
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16715 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent a79ba42 commit 579ef47

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

Compiler/Template/CodegenC.tpl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7239,8 +7239,14 @@ case LBINARY(__) then
72397239
let e1 = daeExp(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
72407240
let e2 = daeExp(exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
72417241
match operator
7242-
case AND(__) then '(<%e1%> && <%e2%>)'
7243-
case OR(__) then '(<%e1%> || <%e2%>)'
7242+
case AND(__) then
7243+
let var = tempDecl("boolean_array", &varDecls)
7244+
let &preExp += 'and_boolean_array(&<%e1%>,&<%e2%>,&<%var%>);<%\n%>'
7245+
'<%var%>'
7246+
case OR(__) then
7247+
let var = tempDecl("boolean_array", &varDecls)
7248+
let &preExp += 'or_boolean_array(&<%e1%>,&<%e2%>,&<%var%>);<%\n%>'
7249+
'<%var%>'
72447250
else error(sourceInfo(),"daeExpLbinary:ERR")
72457251
end daeExpLbinary;
72467252

SimulationRuntime/c/util/boolean_array.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,48 @@ void copy_boolean_array_data(const boolean_array_t *source, boolean_array_t *des
131131
}
132132
}
133133

134+
void and_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest)
135+
{
136+
size_t i, nr_of_elements;
137+
138+
assert(base_array_ok(source1));
139+
assert(base_array_ok(source2));
140+
assert(base_array_shape_eq(source1, source2));
141+
142+
clone_base_array_spec(source1, dest);
143+
// assert(base_array_ok(dest));
144+
// assert(base_array_shape_eq(source1, dest));
145+
alloc_boolean_array_data(dest);
146+
147+
148+
nr_of_elements = base_array_nr_of_elements(source1);
149+
150+
for(i = 0; i < nr_of_elements; ++i) {
151+
boolean_set(dest, i, boolean_get(source1, i) && boolean_get(source2, i));
152+
}
153+
}
154+
155+
void or_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest)
156+
{
157+
size_t i, nr_of_elements;
158+
159+
assert(base_array_ok(source1));
160+
assert(base_array_ok(source2));
161+
assert(base_array_shape_eq(source1, source2));
162+
163+
clone_base_array_spec(source1, dest);
164+
// assert(base_array_ok(dest));
165+
// assert(base_array_shape_eq(source1, dest));
166+
alloc_boolean_array_data(dest);
167+
168+
169+
nr_of_elements = base_array_nr_of_elements(source1);
170+
171+
for(i = 0; i < nr_of_elements; ++i) {
172+
boolean_set(dest, i, boolean_get(source1, i) || boolean_get(source2, i));
173+
}
174+
}
175+
134176
void copy_boolean_array_data_mem(const boolean_array_t *source, modelica_boolean *dest)
135177
{
136178
size_t i, nr_of_elements;

SimulationRuntime/c/util/boolean_array.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ extern void copy_boolean_array_data_mem(const boolean_array_t* source, modelica_
7777
/* Copy boolean array*/
7878
extern void copy_boolean_array(const boolean_array_t* source, boolean_array_t* dest);
7979

80+
/* 'and' two boolean arrays*/
81+
void and_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest);
82+
83+
/* 'or' two boolean arrays*/
84+
void or_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest);
85+
8086
extern modelica_boolean* calc_boolean_index(int ndims, const _index_t* idx_vec, const boolean_array_t* arr);
8187
extern modelica_boolean* calc_boolean_index_va(const boolean_array_t* source,int ndims,va_list ap);
8288

0 commit comments

Comments
 (0)