Exemplo n.º 1
0
/*
 * Initialize table
 * - the table is initially empty (size = 0)
 * - the array def is allocated on the first addition
 * - vtbl = associated variable table
 */
void init_bvexp_table(bvexp_table_t *table, bv_vartable_t *vtbl) {
  table->nvars = 0;
  table->size = 0;
  table->def = NULL;
  table->vtbl = vtbl;
  init_bvmlist_store(&table->store);
  init_bvmlist64_store(&table->store64);
  init_pprod_table(&table->pprods, 32);
  init_int_htbl(&table->htbl, 0);

  init_bvarith_buffer(&table->aux, &table->pprods, &table->store);
  init_bvarith64_buffer(&table->aux64, &table->pprods, &table->store64);
  init_pp_buffer(&table->pp, 10);
  init_bvconstant(&table->bvconst);
}
Exemplo n.º 2
0
/*
 * 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;
  }
}
Exemplo n.º 3
0
/*
 * 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);
}
Exemplo n.º 4
0
/*
 * Initialize the buffers:
 * - n = bitsize
 */
static void init_test2(uint32_t n) {
  uint32_t q0[4];
  uint32_t i;

  assert(0 < n && n <= 128);

  for (i=0; i<8; i++) {
    init_bvarith_buffer(aux + i, &prod_table, &store);
    bvarith_buffer_prepare(aux + i, n);
  }

  bvarith_buffer_add_var(&aux[0], 3); // x_3

  bvconst_set32(q0, 4, 2);
  bvarith_buffer_add_const(&aux[1], q0); // 2

  bvarith_buffer_add_var(&aux[2], 1);
  bvarith_buffer_sub_var(&aux[2], 2); // x_1 - x_2

  bvarith_buffer_add_var(&aux[3], 0);
  bvarith_buffer_sub_const(&aux[3], q0); // x_0 - 2

  bvarith_buffer_add_pp(&aux[4], pprod_mul(&prod_table, var_pp(1), var_pp(1))); // x_1^2

  bvarith_buffer_add_var(&aux[5], 0);
  bvarith_buffer_mul_const(&aux[5], q0); // 2 * x_0

  bvarith_buffer_add_varmono(&aux[6], q0, 1); // 2 * x_1

  bvarith_buffer_sub_var(&aux[7], 3);
  bvarith_buffer_sub_var(&aux[7], 3);
  bvarith_buffer_add_var(&aux[7], 4);

  for (i=0; i<8; i++) {
    bvarith_buffer_normalize(aux + i);
  }
}
Exemplo n.º 5
0
/*
 * 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");
}
Exemplo n.º 6
0
/*
 * 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);
}