void insert(coeff_t c, cost_t cost, cfset_t &where) { if(c > MAX_NUM) return; c = fundamental(c); /* was this coefficient previously generated with lesser cost? */ /*if(GEN[c] >= 0 && GEN[c] < cost) return; else*/ { GEN[c] = cost; where.insert(c); } }
int main(int argc, char **argv) { int len = 10; if (argc > 1) len = atoi(argv[1]); if (len < 10) len = 10; char *t = malloc(len + 1); if (len == 10) memcpy(t, "aabaaabcaab", 10); else random_fill(t, len); t[len] = '\0'; int *z; fundamental(t, len, &z); inspect(z, len); inspect_c(t, len); free(z); return 0; }
void acm_solve(oplist_t &l, const vector<coeff_t> &coeffs, const vector<reg_t> &dests, reg_t src, reg_t (*tmpreg)()) { ASSERT(dests.size() == coeffs.size()); cfset_t problem; regmap_t * regs = new regmap_t; regs->clear(); (*regs)[1] = src; for(int i = coeffs.size()-1; i >= 0; --i) { if(coeffs[i]!=1) (*regs)[coeffs[i]] = dests[i]; } /* compute registers for all fundamentals ('reduced' constants) */ for(size_t i = 0; i < coeffs.size(); ++i) { coeff_t full = coeffs[i]; coeff_t reduced = fundamental(cfabs(full)); if(reduced!=1) ADD(problem, reduced); if(!CONTAINS(*regs, reduced)) (*regs)[reduced] = tmpreg(); } if_verbose(1) cerr<<"synthesizing "<<problem.size()<<" unique fundamentals\n"; create_problem(problem); GEN_CODE = true; solve(&l, regs, tmpreg); for(size_t i = 0; i < coeffs.size(); ++i) { coeff_t full = coeffs[i]; reg_t dest = dests[i]; coeff_t reduced = fundamental(cfabs(full)); if(full == 1) l.push_back(op::shl(dest, src, 0, full)); /* move dest <- src */ else if(full == -1) l.push_back(op::neg(dest, src, full)); /* move dest <- -1*src */ /* last op */ else if(dest == (*regs)[full]) { if(full != reduced) { int t = (*regs)[reduced]; /* see if we already computed the negative of it */ int found_neg = -1; for(size_t j=0; j<i; ++j) { if(coeffs[j] == -full) { found_neg = j; break; } } /* if not */ if(found_neg == -1) { /* this is the first instance */ int shr = compute_shr(cfabs(full)); if(shr!=0) { reg_t tmp = dest; if(full < 0) { tmp = tmpreg(); for(size_t j = i+1; j < coeffs.size(); ++j) { if(coeffs[j] == -full) { tmp = dests[j]; break; } } } l.push_back(op::shl(tmp, (*regs)[reduced], shr, cfabs(full))); t = tmp; } /* negate if necessary */ if(full<0) l.push_back(op::neg(dest,t, full)); } else /* negative was already computed, just negate */ if(full < 0) l.push_back(op::neg(dest,dests[found_neg],full)); } } else /* we already computed it */ l.push_back(op::shl(dest, (*regs)[full], 0, full)); } delete regs; }