/* * Delete the buffers */ static void delete_test2(void) { uint32_t i; for (i=0; i<8; i++) { delete_bvarith_buffer(aux + i); } }
/* * Delete the table */ void delete_bvexp_table(bvexp_table_t *table) { uint32_t i, n, b; void *p; n = table->nvars; for (i=0; i<n; i++) { p = table->def[i]; if (p != NULL) { b = bvvar_bitsize(table->vtbl, i); if (b > 64) { delete_bvmlist_coeffs(p, b); } } } // aux buffers must be deleted first delete_bvarith_buffer(&table->aux); delete_bvarith64_buffer(&table->aux64); delete_pp_buffer(&table->pp); delete_bvconstant(&table->bvconst); safe_free(table->def); table->def = NULL; delete_bvmlist_store(&table->store); delete_bvmlist64_store(&table->store64); delete_pprod_table(&table->pprods); delete_int_htbl(&table->htbl); }
/* * Multiply b by t^d * - t must be an arithmetic term * - p->ptbl and table->pprods must be equal */ void bvarith_buffer_mul_term_power(bvarith_buffer_t *b, term_table_t *table, term_t t, uint32_t d) { bvarith_buffer_t aux; bvconstant_t c; bvpoly_t *p; pprod_t **v; pprod_t *r; int32_t i; assert(b->ptbl == table->pprods); assert(pos_term(t) && good_term(table, t) && is_bitvector_term(table, t) && term_bitsize(table, t) == b->bitsize); i = index_of(t); switch (table->kind[i]) { case POWER_PRODUCT: r = pprod_exp(b->ptbl, pprod_for_idx(table, i), d); // r = t^d bvarith_buffer_mul_pp(b, r); break; case BV_CONSTANT: init_bvconstant(&c); bvconstant_copy64(&c, b->bitsize, 1); // c := 1 bvconst_mulpower(c.data, b->width, bvconst_for_idx(table, i)->data, d); // c := t^d bvarith_buffer_mul_const(b, c.data); delete_bvconstant(&c); break; case BV_POLY: p = bvpoly_for_idx(table, i); v = pprods_for_bvpoly(table, p); init_bvarith_buffer(&aux, b->ptbl, b->store); bvarith_buffer_mul_bvpoly_power(b, p, v, d, &aux); delete_bvarith_buffer(&aux); term_table_reset_pbuffer(table); break; default: r = pprod_varexp(b->ptbl, t, d); bvarith_buffer_mul_pp(b, r); break; } }
/* * Empty the table */ void reset_bvexp_table(bvexp_table_t *table) { bvexp_table_remove_vars(table, 0); /* * The two aux buffers must be deleted first since their content may become * invalid pointers afer the reset_objstore calls. Just calling * bvarith..._prepare is not enough as it keeps the end_marker in * table->aux/table->aux64. */ delete_bvarith_buffer(&table->aux); delete_bvarith64_buffer(&table->aux64); pp_buffer_reset(&table->pp); reset_objstore(&table->store); reset_objstore(&table->store64); reset_pprod_table(&table->pprods); // Recreate the buffers aux and aux64 init_bvarith_buffer(&table->aux, &table->pprods, &table->store); init_bvarith64_buffer(&table->aux64, &table->pprods, &table->store64); }
/* * Test binary operations with b1 and b2 */ static void test_ops(bvarith_buffer_t *b1, bvarith_buffer_t *b2) { bvarith_buffer_t b; uint32_t n; assert(b1->bitsize == b2->bitsize); printf("b1: "); print_bvarith_buffer(stdout, b1); printf("\nb2: "); print_bvarith_buffer(stdout, b2); printf("\n"); printf("Equality test: "); if (bvarith_buffer_equal(b1, b2)) { printf("yes\n"); } else { printf("no\n"); } n = b1->bitsize; init_bvarith_buffer(&b, &prod_table, &store); bvarith_buffer_prepare(&b, n); bvarith_buffer_add_buffer(&b, b1); bvarith_buffer_add_buffer(&b, b2); bvarith_buffer_normalize(&b); printf(" b1 + b2: "); print_bvarith_buffer(stdout, &b); printf("\n"); bvarith_buffer_prepare(&b, n); bvarith_buffer_add_buffer(&b, b1); bvarith_buffer_sub_buffer(&b, b2); bvarith_buffer_normalize(&b); printf(" b1 - b2: "); print_bvarith_buffer(stdout, &b); printf("\n"); bvarith_buffer_prepare(&b, n); bvarith_buffer_add_buffer(&b, b2); bvarith_buffer_sub_buffer(&b, b1); bvarith_buffer_normalize(&b); printf(" b2 - b1: "); print_bvarith_buffer(stdout, &b); printf("\n"); bvarith_buffer_prepare(&b, n); bvarith_buffer_add_buffer(&b, b1); bvarith_buffer_mul_buffer(&b, b2); bvarith_buffer_normalize(&b); printf(" b1 * b2: "); print_bvarith_buffer(stdout, &b); printf("\n"); bvarith_buffer_prepare(&b, n); bvarith_buffer_add_buffer(&b, b2); bvarith_buffer_mul_buffer(&b, b1); bvarith_buffer_normalize(&b); printf(" b2 * b1: "); print_bvarith_buffer(stdout, &b); printf("\n"); bvarith_buffer_prepare(&b, n); bvarith_buffer_add_buffer_times_buffer(&b, b1, b2); bvarith_buffer_normalize(&b); printf(" b1 * b2: "); print_bvarith_buffer(stdout, &b); printf("\n"); bvarith_buffer_prepare(&b, n); bvarith_buffer_sub_buffer_times_buffer(&b, b1, b2); bvarith_buffer_normalize(&b); printf("- b1 * b2: "); print_bvarith_buffer(stdout, &b); printf("\n"); delete_bvarith_buffer(&b); printf("----\n"); }
/* * Tests: one buffer * - n = bitsize */ static void test1(uint32_t n) { bvarith_buffer_t buffer; uint32_t q0[4]; assert(0 < n && n <= 128); init_bvarith_buffer(&buffer, &prod_table, &store); bvarith_buffer_prepare(&buffer, n); printf("Empty buffer\n"); test_buffer(&buffer); printf("x_0 + x_1\n"); bvarith_buffer_add_var(&buffer, 0); bvarith_buffer_add_var(&buffer, 1); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("After reset\n"); bvarith_buffer_prepare(&buffer, n); test_buffer(&buffer); printf("x_2 - x_0\n"); bvarith_buffer_add_var(&buffer, 2); bvarith_buffer_sub_var(&buffer, 0); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("x_2 - x_0 + x_1 + x_0\n"); bvarith_buffer_prepare(&buffer, n); bvarith_buffer_add_var(&buffer, 2); bvarith_buffer_sub_var(&buffer, 0); bvarith_buffer_add_var(&buffer, 1); bvarith_buffer_add_var(&buffer, 0); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Adding 3\n"); bvconst_set32(q0, 4, 3); bvarith_buffer_add_const(&buffer, q0); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Negating\n"); bvarith_buffer_negate(&buffer); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Negating again\n"); bvarith_buffer_negate(&buffer); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Multiplying by 2 x_4\n"); bvconst_set32(q0, 4, 2); bvarith_buffer_mul_varmono(&buffer, q0, 4); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Multiplying by x_1^2\n"); bvarith_buffer_mul_var(&buffer, 1); bvarith_buffer_mul_var(&buffer, 1); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Multiplying by 0\n"); bvconst_clear(q0, 4); bvarith_buffer_mul_const(&buffer, q0); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("x_1 + 1 - x_2\n"); bvarith_buffer_prepare(&buffer, n); bvarith_buffer_add_var(&buffer, 1); bvconst_set32(q0, 4, 1); bvarith_buffer_add_const(&buffer, q0); bvarith_buffer_sub_var(&buffer, 2); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Squaring\n"); bvarith_buffer_square(&buffer); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Squaring\n"); bvarith_buffer_square(&buffer); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); printf("Squaring\n"); bvarith_buffer_square(&buffer); bvarith_buffer_normalize(&buffer); test_buffer(&buffer); delete_bvarith_buffer(&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; }