static String tryToPrintVarToStringWithDiv(Conjunct *C, Variable_ID v) { String s; bool seen = false; // This assumes that there is one EQ involving v, that v cannot be // substituted and hence has a non-unit coefficient. for(EQ_Iterator e(C); e; e++) { if ((*e).get_coef(v) != 0) { assert(!seen); // This asserts just one EQ with v coef_t v_coef = (*e).get_coef(v); int v_sign = v_coef > 0 ? 1 : -1; v_coef *= v_sign; int sign_adj = -v_sign; s += "intDiv("; bool first=true; for(Constr_Vars_Iter i(*e,false); i; i++) { if ((*i).var != v && (*i).coef != 0) { coef_t this_coef = sign_adj*(*i).coef; if(!first && this_coef > 0) s+= "+"; if (this_coef == 1) s += (*i).var->name(); else if (this_coef == -1) { s += "-"; s += (*i).var->name(); } else { s += itoS(this_coef) + "*" + (*i).var->name(); } first = false; } } coef_t the_const = (*e).get_const()* sign_adj; if (the_const > 0 && !first) s+= "+"; if (the_const != 0) s += itoS(the_const); s += "," + itoS(v_coef) + ")"; seen = true; } } return s; }
String elimination_info::print_eliminated_to_string() { String res; for(int i = 1; i <= eliminated.size(); i++) res += eliminated[i]->name() + (eliminated[i]->kind() == Input_Var? "(input " : "(output ") + itoS(eliminated[i]->get_position()) + ") "; return res; }
String get_sub(Relation &R, int o, Substitutions &subs) { // R must be a single conjunct, // and s must be substitutions for that conjunct assert(R.query_DNF()->length() == 1); Variable_ID ov = R.output_var(o); modinfo m; if (subs.substituted(ov)) { return subs.get_sub(ov).print_term_to_string(); } else if (m = check_mod(R, o, true), m.modbase) { return (String) "intMod(" + itoS(m.input_var_coef) + "*" + R.input_var(m.input_var)->char_name() + "+" + itoS(m.offset) + "," + itoS(m.modbase) + ")"; } else { String result = ""; if (R.n_out() >= 3) result = desperate_check_mod_kludge_for_time_skewing(R, o); if (result != (String) "") { return result; } else { if (DONT_QUIT_FOR_FAILED_SUBSTITUTION) { if (tcodegen_debug) { fprintf(DebugFile, "%s%s\n", debug_mark_cp, "failed to find substitution.\n"); if (tcodegen_debug > 1) { fprintf(DebugFile, "%s%s\n", debug_mark_cp, "In prefix_print form, R is:"); R.prefix_print(DebugFile); } } return "In tcodegen, no substitution was possible for the expression that was needed here so this won't compile - for details, run with -Dg1 and look for \"failed to find substitution\" in oc.out."; } else { cerr << "The current tcodegen implementation needs to find a subsitution for each\n"; cerr << "output variable, but can't do so for output variable #" << o << " in\n"; cerr << R.print_with_subs_to_string(); Exit(1); return "Shut up little man"; // make the compiler quit complaining } } } }
void writeSetting(const char* fileName) { CString tempElecName[16] = {L"ml", L"cd1", L"cd2", L"tf1", L"f2", L"tf2", L"tf3", L"ld", L"ss", L"ss2", L"se3", L"af", L"af1", L"af2", L"af3", L"af4"}; TiXmlDocument doc; TiXmlElement* root = new TiXmlElement("root"); doc.LinkEndChild(root); TiXmlElement* element = new TiXmlElement("Lead"); element->SetAttribute("Name", "lead"); root->LinkEndChild(element); for (int i = 0; i < 16; i++) { TiXmlElement* element1 = new TiXmlElement((LPCSTR)(CStringA)itoS(i+1)); element->LinkEndChild(element1); element1->SetAttribute("Name", (LPCSTR)(CStringA)tempElecName[i]); } bool success = doc.SaveFile(fileName); doc.Clear(); }
static String desperate_check_mod_kludge_for_time_skewing(Relation &R, int o) { Variable_ID Out_1, Out_3, In_4, In_8, alpha, v, k1, k2, k3, k4; int Out_1_coef, Out_3_coef, v_coef, const_term; int sign; F_Exists *exists_k1, *exists_k1k2, *exists_k3, *exists_k4; F_And *st_k1, *st_k1k2, *st_k3, *st_k4; GEQ_Handle geq_L_k1, geq_U_k1, geq_L_k3, geq_U_k3, geq_L_k4, geq_U_k4; EQ_Handle eq_k1, eq_k3, eq_k4; // R must be a single conjunct // PROBLEM: FOR SOME REASON, THE RELEVANT EQUALITY SHOWS UP AS // t8 == 7+8*xbmod2+8*t4+Out_3+16*alpha // note Out_1 is xbmod2 // WHEN WE DO PRINT_WITH SUBS, BUT THE "TRUE" FORM IS // 8*xbmod2+8*t4+t8 == 7+Out_3+16*alpha // AS EVIDENCED BY THIS prefix_print. // // IS THIS CORRECT? PRESUMABLY SO... // BASED ON AN UNDERSTANDING OF HOW ITS CORRECT, WE NEED TO // GENERALIZE OR CHANGE THE TEST BELOW. // R.prefix_print(stdout); if (tcodegen_debug) { fprintf(DebugFile, "%s%s\n", debug_mark_cp, "desperately looking for special case mod and div substitution."); } // check that the only EQ constraint using Out_1 and Out_3 is of the form // exists alpha : 8*Out_1 + 8*In_4 + In_8 == 7 + Out_3 + 16*alpha Out_1 = R.output_var(1); Out_3 = R.output_var(3); In_4=0, In_8=0, alpha=0; // look for these vars in the loop -- // In_4 must play the appropriate role // in the equality above, but may not // actually be the 4th input variable. DNF_Iterator di(R.query_DNF()); // This loop looks for any one equality of the right form. // We we find it, we break out of the loop with alpha, In_4, and In_8 set; // When we discover we've got the wrong form, we continue the loop. for (EQ_Iterator ei = (*di)->EQs(); ei; ei++, alpha = In_4 = In_8 = 0) { Out_1_coef = (*ei).get_coef(Out_1); Out_3_coef = (*ei).get_coef(Out_3); if (abs(Out_3_coef) == 1 && Out_1_coef * Out_3_coef == -8) { sign = Out_3_coef; const_term = -sign * (*ei).get_const(); for (Constr_Vars_Iter cvi(*ei); cvi; cvi++) { v = (*cvi).var; v_coef = (*cvi).coef; if (v->kind() == Wildcard_Var || v->kind() == Exists_Var) // must be alpha { if (alpha || v_coef != 16) goto continue_ei; alpha = v; } else if (v_coef % 16 == 0) // anything else doesn't matter continue; else if (v->kind() == Output_Var) // must be Out_3 or Out_1 { if (v != Out_1 && v != Out_3) goto continue_ei; } else if (v->kind() == Input_Var) // must be In_4 or In_8 { if (v_coef * sign == -1) { if (In_8) goto continue_ei; In_8 = v; } else if (v_coef * sign == -8) { if (In_4) goto continue_ei; In_4 = v; } else goto continue_ei; } else // some other kind of variable I haven't considered goto continue_ei; } // for all vars in constraint if (alpha && In_4 && In_8) break; } // end of "if Out and In have reasonable coefficients" continue_ei: ; } // end of equality iteration if (! (alpha && In_4 && In_8)) return ""; // if o=3, assert (and return) substitution (In_8-8In_4-7) mod 8 if (o == 3) { #if ! defined NDEBUG // DO AN ASSERTION THAT THIS IS CORRECT Relation checkmod(R.n_inp(),R.n_out()); assert(Out_1->kind() == Output_Var && Out_1->get_position() == 1); assert(Out_3->kind() == Output_Var && Out_3->get_position() == 3); assert(In_4->kind() == Input_Var); // Note then In_4 may not actually be input var #4. assert(In_8->kind() == Input_Var); //Variable_ID cm_out_1 = checkmod.output_var(Out_1->get_position()); Variable_ID cm_out_3 = checkmod.output_var(Out_3->get_position()); Variable_ID cm_in_4 = checkmod.input_var (In_4->get_position()); Variable_ID cm_in_8 = checkmod.input_var (In_8->get_position()); exists_k1=checkmod.add_exists(); k1=(*exists_k1).declare("k1_modbase"); st_k1=(*exists_k1).add_and(); geq_L_k1=(*st_k1).add_GEQ(); geq_L_k1.update_coef(cm_out_3,1); geq_U_k1=(*st_k1).add_GEQ(); geq_U_k1.update_coef(cm_out_3,-1); geq_U_k1.update_const(7); eq_k1=(*st_k1).add_EQ(); eq_k1.update_coef(cm_out_3,-1); eq_k1.update_coef(cm_in_8,1); eq_k1.update_coef(cm_in_4,-8); eq_k1.update_coef(k1,8); eq_k1.update_const(-7); checkmod.finalize(); checkmod = Intersection(checkmod, copy(R)); // by definition, checkmod is a subset of R; // if the above mod constraint is redundant, R is a subset of checkmod assert(Must_Be_Subset(copy(checkmod),copy(R))); // the obvious one assert(Must_Be_Subset(copy(R),checkmod)); // mod constraint is redundant #endif return (String) "intMod(" + In_8->char_name() + "-8*" + In_4->char_name() + "+" + itoS(const_term) + ",8)"; // return "intMod(t8-8*t4-7,8)"; } // if o=1, assert&return the sub ( (8*((In_8-8In_4-7) div 8)) mod 16 ) div 8 else if (o == 1) { #if ! defined NDEBUG Relation checkmod(R.n_inp(),R.n_out()); assert(Out_1->kind() == Output_Var && Out_1->get_position() == 1); assert(Out_3->kind() == Output_Var && Out_3->get_position() == 3); assert(In_4->kind() == Input_Var); // Note then In_4 may not actually be input var #4. assert(In_8->kind() == Input_Var); Variable_ID cm_out_1 = checkmod.output_var(Out_1->get_position()); //Variable_ID cm_out_3 = checkmod.output_var(Out_3->get_position()); Variable_ID cm_in_4 = checkmod.input_var (In_4->get_position()); Variable_ID cm_in_8 = checkmod.input_var (In_8->get_position()); exists_k1k2=checkmod.add_exists(); k1=(*exists_k1k2).declare("k1_modbase"); k2=(*exists_k1k2).declare("k2_modbase"); st_k1k2=(*exists_k1k2).add_and(); geq_L_k1=(*st_k1k2).add_GEQ(); geq_L_k1.update_coef(k1,1); geq_L_k1.update_coef(cm_in_8,-1); geq_L_k1.update_coef(cm_in_4,8); geq_L_k1.update_const(14); geq_U_k1=(*st_k1k2).add_GEQ(); geq_U_k1.update_coef(k1,-1); geq_U_k1.update_coef(cm_in_8,1); geq_U_k1.update_coef(cm_in_4,-8); geq_U_k1.update_const(-7); exists_k3=(*st_k1k2).add_exists(); k3=(*exists_k3).declare("k3_modbase"); st_k3=(*exists_k3).add_and(); geq_L_k3=(*st_k3).add_GEQ(); geq_L_k3.update_coef(k2,1); geq_U_k3=(*st_k3).add_GEQ(); geq_U_k3.update_coef(k2,-1); geq_U_k3.update_const(15); eq_k3=(*st_k3).add_EQ(); eq_k3.update_coef(k1,1); eq_k3.update_coef(k3,-8); exists_k4=(*st_k3).add_exists(); k4=(*exists_k4).declare("k4_modbase"); st_k4=(*exists_k4).add_and(); geq_L_k4=(*st_k4).add_GEQ(); geq_L_k4.update_coef(k2,1); geq_L_k4.update_coef(cm_out_1,-8); geq_U_k4=(*st_k4).add_GEQ(); geq_U_k4.update_coef(k2,-1); geq_U_k4.update_coef(cm_out_1,8); geq_U_k4.update_const(7); eq_k4=(*st_k4).add_EQ(); eq_k4.update_coef(k1,-1); eq_k4.update_coef(k2,1); eq_k4.update_coef(k4,16); checkmod.finalize(); checkmod = Intersection(checkmod, copy(R)); // by definition, checkmod is a subset of R; // if the above mod constraint is redundant, R is a subset of checkmod assert(Must_Be_Subset(copy(checkmod),copy(R))); // the obvious one assert(Must_Be_Subset(copy(R),checkmod)); // mod constraint is redundant #endif return (String) "intDiv(intMod(8*intDiv(" + (*In_8).char_name() + "-8*" + (*In_4).char_name() + "+" + itoS(const_term) + ",8),16),8)"; // return "intDiv(intMod(8*intDiv(t8-8*t4-7,8),16),8)"; } else return ""; }