/* * 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); } }
int main(void) { thvar_t x, y, z; init_bvconstants(); init_bv_vartable(&vtbl); init_bvexp_table(&table, &vtbl); bvexp_init_buffer64(&table, &b1); bvexp_init_buffer64(&table, &b2); bvexp_init_buffer(&table, &c1); bvexp_init_buffer(&table, &c2); printf("=== Initial table ===\n"); print_bvexp_table(stdout, &table); printf("\n"); x = make_bvvar(&vtbl, 10); y = make_bvvar(&vtbl, 10); z = make_bvvar(&vtbl, 10); // 2 + x + y bvarith64_buffer_prepare(&b1, 10); bvarith64_buffer_add_const(&b1, 2); bvarith64_buffer_add_var(&b1, x); bvarith64_buffer_add_var(&b1, y); bvarith64_buffer_normalize(&b1); test_buffer64(&table, &b1); bvarith64_buffer_prepare(&b1, 10); bvarith64_buffer_add_const(&b1, 2); bvarith64_buffer_add_var(&b1, x); bvarith64_buffer_add_var(&b1, y); bvarith64_buffer_normalize(&b1); test_buffer64(&table, &b1); // (x + z) * (y - z) bvarith64_buffer_prepare(&b1, 10); bvarith64_buffer_add_var(&b1, x); bvarith64_buffer_add_var(&b1, z); bvarith64_buffer_prepare(&b2, 10); bvarith64_buffer_add_var(&b2, y); bvarith64_buffer_sub_var(&b2, z); bvarith64_buffer_mul_buffer(&b1, &b2); bvarith64_buffer_normalize(&b1); test_buffer64(&table, &b1); bvarith64_buffer_prepare(&b1, 10); bvarith64_buffer_add_var(&b1, x); bvarith64_buffer_add_var(&b1, z); bvarith64_buffer_prepare(&b2, 10); bvarith64_buffer_add_var(&b2, y); bvarith64_buffer_sub_var(&b2, z); bvarith64_buffer_mul_buffer(&b1, &b2); bvarith64_buffer_normalize(&b1); test_buffer64(&table, &b1); // x * y * z^2 bvarith64_buffer_prepare(&b1, 10); bvarith64_buffer_add_var(&b1, z); bvarith64_buffer_square(&b1); bvarith64_buffer_mul_var(&b1, y); bvarith64_buffer_mul_var(&b1, x); bvarith64_buffer_normalize(&b1); test_buffer64(&table, &b1); bvarith64_buffer_prepare(&b1, 10); bvarith64_buffer_add_var(&b1, z); bvarith64_buffer_square(&b1); bvarith64_buffer_mul_var(&b1, y); bvarith64_buffer_mul_var(&b1, x); bvarith64_buffer_normalize(&b1); test_buffer64(&table, &b1); // Large coefficients x = make_bvvar(&vtbl, 100); y = make_bvvar(&vtbl, 100); z = make_bvvar(&vtbl, 100); // 1 + x + y bvarith_buffer_prepare(&c1, 100); bvarith_buffer_set_one(&c1); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, y); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); bvarith_buffer_prepare(&c1, 100); bvarith_buffer_set_one(&c1); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, y); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); // (x + z) * (y - z) bvarith_buffer_prepare(&c1, 100); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, z); bvarith_buffer_prepare(&c2, 100); bvarith_buffer_add_var(&c2, y); bvarith_buffer_sub_var(&c2, z); bvarith_buffer_mul_buffer(&c1, &c2); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); bvarith_buffer_prepare(&c1, 100); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, z); bvarith_buffer_prepare(&c2, 100); bvarith_buffer_add_var(&c2, y); bvarith_buffer_sub_var(&c2, z); bvarith_buffer_mul_buffer(&c1, &c2); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); // x * y * z^2 bvarith_buffer_prepare(&c1, 100); bvarith_buffer_add_var(&c1, z); bvarith_buffer_square(&c1); bvarith_buffer_mul_var(&c1, y); bvarith_buffer_mul_var(&c1, x); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); bvarith_buffer_prepare(&c1, 100); bvarith_buffer_add_var(&c1, z); bvarith_buffer_square(&c1); bvarith_buffer_mul_var(&c1, y); bvarith_buffer_mul_var(&c1, x); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); printf("=== Final table ===\n"); print_bvexp_table(stdout, &table); printf("\n"); // remove two variables bvexp_table_remove_vars(&table, 11); printf("=== After removing two variables ===\n"); print_bvexp_table(stdout, &table); printf("\n"); // recheck: 1 + x + y bvarith_buffer_prepare(&c1, 100); bvarith_buffer_set_one(&c1); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, y); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); // recheck: (x + z) * (y - z) bvarith_buffer_prepare(&c1, 100); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, z); bvarith_buffer_prepare(&c2, 100); bvarith_buffer_add_var(&c2, y); bvarith_buffer_sub_var(&c2, z); bvarith_buffer_mul_buffer(&c1, &c2); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); bvarith_buffer_prepare(&c1, 100); bvarith_buffer_add_var(&c1, x); bvarith_buffer_add_var(&c1, z); bvarith_buffer_prepare(&c2, 100); bvarith_buffer_add_var(&c2, y); bvarith_buffer_sub_var(&c2, z); bvarith_buffer_mul_buffer(&c1, &c2); bvarith_buffer_normalize(&c1); test_buffer(&table, &c1); printf("=== Final table ===\n"); print_bvexp_table(stdout, &table); printf("\n"); // cleanup delete_bvarith64_buffer(&b1); delete_bvarith64_buffer(&b2); delete_bvarith_buffer(&c1); delete_bvarith_buffer(&c2); delete_bvexp_table(&table); delete_bv_vartable(&vtbl); cleanup_bvconstants(); return 0; }