1 #include "satencoder.h"
8 #include "tableentry.h"
12 #include "satfuncopencoder.h"
14 Edge encodeOperatorPredicateSATEncoder(SATEncoder *This, BooleanPredicate *constraint) {
15 switch (constraint->encoding.type) {
16 case ENUMERATEIMPLICATIONS:
17 return encodeEnumOperatorPredicateSATEncoder(This, constraint);
19 return encodeCircuitOperatorPredicateEncoder(This, constraint);
26 Edge encodeEnumOperatorPredicateSATEncoder(SATEncoder *This, BooleanPredicate *constraint) {
27 PredicateOperator *predicate = (PredicateOperator *)constraint->predicate;
28 uint numDomains = predicate->domains.getSize();
30 FunctionEncodingType encType = constraint->encoding.type;
31 bool generateNegation = encType == ENUMERATEIMPLICATIONSNEGATE;
33 /* Call base encoders for children */
34 for (uint i = 0; i < numDomains; i++) {
35 Element *elem = constraint->inputs.get(i);
36 encodeElementSATEncoder(This, elem);
38 VectorEdge *clauses = allocDefVectorEdge(); // Setup array of clauses
40 uint indices[numDomains]; //setup indices
41 bzero(indices, sizeof(uint) * numDomains);
43 uint64_t vals[numDomains];//setup value array
44 for (uint i = 0; i < numDomains; i++) {
45 Set *set = predicate->domains.get(i);
46 vals[i] = set->getElement(indices[i]);
49 bool notfinished = true;
51 Edge carray[numDomains];
53 if (predicate->evalPredicateOperator(vals) ^ generateNegation) {
54 //Include this in the set of terms
55 for (uint i = 0; i < numDomains; i++) {
56 Element *elem = constraint->inputs.get(i);
57 carray[i] = getElementValueConstraint(This, elem, vals[i]);
59 Edge term = constraintAND(This->cnf, numDomains, carray);
60 pushVectorEdge(clauses, term);
64 for (uint i = 0; i < numDomains; i++) {
65 uint index = ++indices[i];
66 Set *set = predicate->domains.get(i);
68 if (index < set->getSize()) {
69 vals[i] = set->getElement(index);
74 vals[i] = set->getElement(0);
78 if (getSizeVectorEdge(clauses) == 0) {
79 deleteVectorEdge(clauses);
82 Edge cor = constraintOR(This->cnf, getSizeVectorEdge(clauses), exposeArrayEdge(clauses));
83 deleteVectorEdge(clauses);
84 return generateNegation ? constraintNegate(cor) : cor;
88 void encodeOperatorElementFunctionSATEncoder(SATEncoder *This, ElementFunction *func) {
90 model_print("Operator Function ...\n");
92 FunctionOperator *function = (FunctionOperator *) func->function;
93 uint numDomains = func->inputs.getSize();
95 /* Call base encoders for children */
96 for (uint i = 0; i < numDomains; i++) {
97 Element *elem = func->inputs.get(i);
98 encodeElementSATEncoder(This, elem);
101 VectorEdge *clauses = allocDefVectorEdge(); // Setup array of clauses
103 uint indices[numDomains]; //setup indices
104 bzero(indices, sizeof(uint) * numDomains);
106 uint64_t vals[numDomains];//setup value array
107 for (uint i = 0; i < numDomains; i++) {
108 Set *set = getElementSet(func->inputs.get(i));
109 vals[i] = set->getElement(indices[i]);
112 Edge overFlowConstraint = ((BooleanVar *) func->overflowstatus)->var;
114 bool notfinished = true;
115 while (notfinished) {
116 Edge carray[numDomains + 1];
118 uint64_t result = function->applyFunctionOperator(numDomains, vals);
119 bool isInRange = ((FunctionOperator *)func->function)->isInRangeFunction(result);
120 bool needClause = isInRange;
121 if (function->overflowbehavior == OVERFLOWSETSFLAG || function->overflowbehavior == FLAGIFFOVERFLOW) {
126 //Include this in the set of terms
127 for (uint i = 0; i < numDomains; i++) {
128 Element *elem = func->inputs.get(i);
129 carray[i] = getElementValueConstraint(This, elem, vals[i]);
132 carray[numDomains] = getElementValueConstraint(This, func, result);
136 switch (function->overflowbehavior) {
140 clause = constraintIMPLIES(This->cnf,constraintAND(This->cnf, numDomains, carray), carray[numDomains]);
143 case FLAGFORCESOVERFLOW: {
144 clause = constraintIMPLIES(This->cnf,constraintAND(This->cnf, numDomains, carray), constraintAND2(This->cnf, carray[numDomains], constraintNegate(overFlowConstraint)));
147 case OVERFLOWSETSFLAG: {
149 clause = constraintIMPLIES(This->cnf,constraintAND(This->cnf, numDomains, carray), carray[numDomains]);
151 clause = constraintIMPLIES(This->cnf,constraintAND(This->cnf, numDomains, carray), overFlowConstraint);
155 case FLAGIFFOVERFLOW: {
157 clause = constraintIMPLIES(This->cnf,constraintAND(This->cnf, numDomains, carray), constraintAND2(This->cnf, carray[numDomains], constraintNegate(overFlowConstraint)));
159 clause = constraintIMPLIES(This->cnf,constraintAND(This->cnf, numDomains, carray), overFlowConstraint);
167 model_print("added clause in operator function\n");
171 pushVectorEdge(clauses, clause);
175 for (uint i = 0; i < numDomains; i++) {
176 uint index = ++indices[i];
177 Set *set = getElementSet(func->inputs.get(i));
179 if (index < set->getSize()) {
180 vals[i] = set->getElement(index);
185 vals[i] = set->getElement(0);
189 if (getSizeVectorEdge(clauses) == 0) {
190 deleteVectorEdge(clauses);
193 Edge cor = constraintAND(This->cnf, getSizeVectorEdge(clauses), exposeArrayEdge(clauses));
194 addConstraintCNF(This->cnf, cor);
195 deleteVectorEdge(clauses);
198 Edge encodeCircuitOperatorPredicateEncoder(SATEncoder *This, BooleanPredicate *constraint) {
199 PredicateOperator *predicate = (PredicateOperator *) constraint->predicate;
200 Element *elem0 = constraint->inputs.get(0);
201 encodeElementSATEncoder(This, elem0);
202 Element *elem1 = constraint->inputs.get(1);
203 encodeElementSATEncoder(This, elem1);
204 ElementEncoding *ee0 = getElementEncoding(elem0);
205 ElementEncoding *ee1 = getElementEncoding(elem1);
206 ASSERT(ee0->numVars == ee1->numVars);
207 uint numVars = ee0->numVars;
208 switch (predicate->op) {
210 return generateEquivNVConstraint(This->cnf, numVars, ee0->variables, ee1->variables);
212 return generateLTConstraint(This->cnf, numVars, ee0->variables, ee1->variables);
214 return generateLTConstraint(This->cnf, numVars, ee1->variables, ee0->variables);