/* * Total degree check on p: check whether the full degree after * expansion is not more than TOTAL_LIMIT */ static bool total_degree_test64(bvexp_table_t *table, bv_vartable_t *vtbl, pp_buffer_t *p) { bvmlist64_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_const64(vtbl, x)) { q = bvexp_def64(table, x); if (q != NULL && mlist64_is_short(q, &e) && d * e <= BVEXP_DEGREE_LIMIT) { // x will be expanded to q^e of so x^d has degree d * e d *= e; } total += d; if (total > BVEXP_TOTAL_LIMIT) { return false; } } } return true; }
/* * Test: check whether b is present in the table * if not add it */ static void test_buffer64(bvexp_table_t *table, bvarith64_buffer_t *b) { bvmlist64_t *p; thvar_t x; uint32_t h, n; printf("=== test64 ===\n"); n = b->bitsize; printf("poly = "); print_bvexp64(stdout, b->list, n); printf("\n"); h = hash_bvmlist64(b->list, n); printf("hash code = %"PRIu32"\n", h); x = bvexp_table_find64(table, b, h); if (x < 0) { printf("not in table\n"); x = make_bvvar(table->vtbl, n); bvexp_table_add64(table, x, b, h); printf("adding variable: "); print_def(stdout, table, x); } else { printf("found matching variable: "); print_def(stdout, table, x); p = bvexp_def64(table, x); if (p == NULL || !equal_bvmlists64(b->list, p) || bvvar_bitsize(table->vtbl, x) != n) { printf("BUG\n"); exit(1); } } printf("\n"); }
/* * Expanded form for a product c * p * - c is a normalized bitvector constant * - p is a power product stored in a pp_buffer object * - n = bitsize of p * - the expansion is returned in a bvarith_buffer or bvarith64_buffer object * - the result is normalized */ void expand_bvpprod64(bvexp_table_t *table, bvarith64_buffer_t *buffer, pp_buffer_t *p, uint32_t n, uint64_t c) { bv_vartable_t *vtbl; bvmlist64_t *q; pp_buffer_t *aux; pprod_t *r; uint64_t a; uint32_t i, m, e, d; thvar_t x; assert(buffer->store == &table->store64 && buffer->ptbl == &table->pprods); bvarith64_buffer_prepare(buffer, n); bvarith64_buffer_set_one(buffer); aux = p; vtbl = table->vtbl; if (total_degree_test64(table, vtbl, p)) { /* * Expansion of c * x_1^d_1 ... x_n^ d_n: * - for a constant x_i, update c to c * a^d_i (where a = value of c) * - if x_i is expanded to q_i, update buffer to buffer * q_i ^ d_i * - otherwise, x_i^d_i is copied into the aux buffer */ aux = &table->pp; pp_buffer_reset(aux); m = p->len; for (i=0; i<m; i++) { x = p->prod[i].var; d = p->prod[i].exp; if (bvvar_is_const64(vtbl, x)) { a = bvvar_val64(vtbl, x); c *= upower64(a, d); } else { q = bvexp_def64(table, x); if (q != NULL && mlist64_is_short(q, &e) && d * e <= BVEXP_DEGREE_LIMIT) { // replace x^d by q^d in buffer bvarith64_buffer_mul_mlist_power(buffer, q, d, &table->aux64); } else { // copy x^d into aux pp_buffer_mul_varexp(aux, x, d); } } } c = norm64(c, n); pp_buffer_normalize(aux); } /* * The result is c * aux * buffer */ r = pprod_from_buffer(&table->pprods, aux); bvarith64_buffer_mul_mono(buffer, c, r); bvarith64_buffer_normalize(buffer); }
/* * Expanded form of a bitvector polynomial p * - p is stored in a bvpoly_buffer object * - the expansion is returned in a bvarith_buffer or bvarith64_buffer object * - the result is normalized */ void expand_bvpoly64(bvexp_table_t *table, bvarith64_buffer_t *buffer, bvpoly_buffer_t *p) { bv_vartable_t *vtbl; bvmlist64_t *q; uint64_t c; uint32_t i, n; thvar_t x; assert(buffer->store == &table->store64 && buffer->ptbl == &table->pprods); bvarith64_buffer_prepare(buffer, bvpoly_buffer_bitsize(p)); n = bvpoly_buffer_num_terms(p); if (n > 0) { vtbl = table->vtbl; i = 0; // deal with the constant term if any if (bvpoly_buffer_var(p, 0) == const_idx) { bvarith64_buffer_add_const(buffer, bvpoly_buffer_coeff64(p, 0)); i ++; } /* * non-constant terms of p are of the form a * x * we replace x by its value if x has tag BVTAG_CONST64 * we replace x by its definition if x has a definition in table * otherwise, we keep x as is */ while (i < n) { x = bvpoly_buffer_var(p, i); c = bvpoly_buffer_coeff64(p, i); i ++; if (bvvar_is_const64(vtbl, x)) { c *= bvvar_val64(vtbl, x); bvarith64_buffer_add_const(buffer, c); } else { q = bvexp_def64(table, x); if (q != NULL) { bvarith64_buffer_add_const_times_mlist(buffer, q, c); } else { bvarith64_buffer_add_varmono(buffer, c, x); } } } bvarith64_buffer_normalize(buffer); } }