Skip to content

Commit

Permalink
+ Improved handling of binary logical operations on boolean arrays (f…
Browse files Browse the repository at this point in the history
…ixed #2286).

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16715 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Aug 1, 2013
1 parent a79ba42 commit 579ef47
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -7239,8 +7239,14 @@ case LBINARY(__) then
let e1 = daeExp(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let e2 = daeExp(exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
match operator
case AND(__) then '(<%e1%> && <%e2%>)'
case OR(__) then '(<%e1%> || <%e2%>)'
case AND(__) then
let var = tempDecl("boolean_array", &varDecls)
let &preExp += 'and_boolean_array(&<%e1%>,&<%e2%>,&<%var%>);<%\n%>'
'<%var%>'
case OR(__) then
let var = tempDecl("boolean_array", &varDecls)
let &preExp += 'or_boolean_array(&<%e1%>,&<%e2%>,&<%var%>);<%\n%>'
'<%var%>'
else error(sourceInfo(),"daeExpLbinary:ERR")
end daeExpLbinary;

Expand Down
42 changes: 42 additions & 0 deletions SimulationRuntime/c/util/boolean_array.c
Expand Up @@ -131,6 +131,48 @@ void copy_boolean_array_data(const boolean_array_t *source, boolean_array_t *des
}
}

void and_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest)
{
size_t i, nr_of_elements;

assert(base_array_ok(source1));
assert(base_array_ok(source2));
assert(base_array_shape_eq(source1, source2));

clone_base_array_spec(source1, dest);
// assert(base_array_ok(dest));
// assert(base_array_shape_eq(source1, dest));
alloc_boolean_array_data(dest);


nr_of_elements = base_array_nr_of_elements(source1);

for(i = 0; i < nr_of_elements; ++i) {
boolean_set(dest, i, boolean_get(source1, i) && boolean_get(source2, i));
}
}

void or_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest)
{
size_t i, nr_of_elements;

assert(base_array_ok(source1));
assert(base_array_ok(source2));
assert(base_array_shape_eq(source1, source2));

clone_base_array_spec(source1, dest);
// assert(base_array_ok(dest));
// assert(base_array_shape_eq(source1, dest));
alloc_boolean_array_data(dest);


nr_of_elements = base_array_nr_of_elements(source1);

for(i = 0; i < nr_of_elements; ++i) {
boolean_set(dest, i, boolean_get(source1, i) || boolean_get(source2, i));
}
}

void copy_boolean_array_data_mem(const boolean_array_t *source, modelica_boolean *dest)
{
size_t i, nr_of_elements;
Expand Down
6 changes: 6 additions & 0 deletions SimulationRuntime/c/util/boolean_array.h
Expand Up @@ -77,6 +77,12 @@ extern void copy_boolean_array_data_mem(const boolean_array_t* source, modelica_
/* Copy boolean array*/
extern void copy_boolean_array(const boolean_array_t* source, boolean_array_t* dest);

/* 'and' two boolean arrays*/
void and_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest);

/* 'or' two boolean arrays*/
void or_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest);

extern modelica_boolean* calc_boolean_index(int ndims, const _index_t* idx_vec, const boolean_array_t* arr);
extern modelica_boolean* calc_boolean_index_va(const boolean_array_t* source,int ndims,va_list ap);

Expand Down

0 comments on commit 579ef47

Please sign in to comment.