/* * 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); }
/* * 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); }
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; }