static bool total_degree_test(bvexp_table_t *table, bv_vartable_t *vtbl, pp_buffer_t *p) { bvmlist_t *q; uint32_t i, n, e, d, total; thvar_t x; assert(vtbl == table->vtbl); total = 0; n = p->len; for (i=0; i<n; i++) { x = p->prod[i].var; d = p->prod[i].exp; if (! bvvar_is_const(vtbl, x)) { q = bvexp_def(table, x); if (q != NULL && mlist_is_short(q, &e) && d * e <= BVEXP_DEGREE_LIMIT) { d *= e; } total += d; if (total > BVEXP_TOTAL_LIMIT) { return false; } } } return true; }
static void test_buffer(bvexp_table_t *table, bvarith_buffer_t *b) { bvmlist_t *p; thvar_t x; uint32_t h, n; printf("=== test ===\n"); n = b->bitsize; printf("poly = "); print_bvexp(stdout, b->list, n); printf("\n"); h = hash_bvmlist(b->list, n); printf("hash code = %"PRIu32"\n", h); x = bvexp_table_find(table, b, h); if (x < 0) { printf("not in table\n"); x = make_bvvar(table->vtbl, n); bvexp_table_add(table, x, b, h); printf("adding variable: "); print_def(stdout, table, x); } else { printf("found matching variable: "); print_def(stdout, table, x); p = bvexp_def(table, x); if (p == NULL || !equal_bvmlists(b->list, p, n) || bvvar_bitsize(table->vtbl, x) != n) { printf("BUG\n"); exit(1); } } printf("\n"); }
void expand_bvpprod(bvexp_table_t *table, bvarith_buffer_t *buffer, pp_buffer_t *p, uint32_t n, uint32_t *c) { bv_vartable_t *vtbl; bvmlist_t *q; pp_buffer_t *aux; pprod_t *r; uint32_t *a; uint32_t i, m, d, e, k; thvar_t x; assert(buffer->store == &table->store && buffer->ptbl == &table->pprods); bvarith_buffer_prepare(buffer, n); bvarith_buffer_set_one(buffer); aux = p; vtbl = table->vtbl; if (total_degree_test(table, vtbl, p)) { aux = &table->pp; pp_buffer_reset(aux); // make a copy of c in the internal bvconst buffer bvconstant_copy(&table->bvconst, n, c); c = table->bvconst.data; k = (n + 31) >> 5; m = p->len; for (i=0; i<m; i++) { x = p->prod[i].var; d = p->prod[i].exp; if (bvvar_is_const(vtbl, x)) { a = bvvar_val(vtbl, x); bvconst_mulpower(c, k, a, d); } else { q = bvexp_def(table, x); if (q != NULL && mlist_is_short(q, &e) && d * e <= BVEXP_DEGREE_LIMIT) { bvarith_buffer_mul_mlist_power(buffer, q, d, &table->aux); } else { pp_buffer_mul_varexp(aux, x, d); } } } // normalize bvconst_normalize(c, n); pp_buffer_normalize(aux); } /* * The result is c * aux * buffer */ r = pprod_from_buffer(&table->pprods, aux); bvarith_buffer_mul_mono(buffer, c, r); bvarith_buffer_normalize(buffer); }
void expand_bvpoly(bvexp_table_t *table, bvarith_buffer_t *buffer, bvpoly_buffer_t *p) { bv_vartable_t *vtbl; bvmlist_t *q; uint32_t *c; uint32_t i, n; thvar_t x; assert(buffer->store == &table->store && buffer->ptbl == &table->pprods); bvarith_buffer_prepare(buffer, bvpoly_buffer_bitsize(p)); n = bvpoly_buffer_num_terms(p); if (n > 0) { vtbl = table->vtbl; i = 0; // constant term of p if (bvpoly_buffer_var(p, 0) == const_idx) { bvarith_buffer_add_const(buffer, bvpoly_buffer_coeff(p, 0)); i ++; } // non-constant terms while (i < n) { x = bvpoly_buffer_var(p, i); c = bvpoly_buffer_coeff(p, i); i ++; if (bvvar_is_const(vtbl, x)) { bvarith_buffer_add_const_times_const(buffer, c, bvvar_val(vtbl, x)); } else { q = bvexp_def(table, x); if (q != NULL) { bvarith_buffer_add_const_times_mlist(buffer, q, c); } else { bvarith_buffer_add_varmono(buffer, c, x); } } } bvarith_buffer_normalize(buffer); } }