/*
 * Add t to buffer b
 * - t must be defined in table and be a bitvector term of same bitsize as b
 * - b->ptbl must be the same as table->pprods
 */
void bvarith_buffer_add_term(bvarith_buffer_t *b, term_table_t *table, term_t t) {
  pprod_t **v;
  bvpoly_t *p;
  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:
    bvarith_buffer_add_pp(b, pprod_for_idx(table, i));
    break;

  case BV_CONSTANT:
    bvarith_buffer_add_const(b, bvconst_for_idx(table, i)->data);
    break;

  case BV_POLY:
    p = bvpoly_for_idx(table, i);
    v = pprods_for_bvpoly(table, p);
    bvarith_buffer_add_bvpoly(b, p, v);
    term_table_reset_pbuffer(table);
    break;

  default:
    bvarith_buffer_add_var(b, t);
    break;
  }
}
Beispiel #2
0
void expand_bvpoly(bvexp_table_t *table, bvarith_buffer_t *buffer, bvpoly_buffer_t *p) {
  bv_vartable_t *vtbl;
  bvmlist_t *q;
  uint32_t *c;
  uint32_t i, n;
  thvar_t x;

  assert(buffer->store == &table->store && buffer->ptbl == &table->pprods);

  bvarith_buffer_prepare(buffer, bvpoly_buffer_bitsize(p));

  n = bvpoly_buffer_num_terms(p);
  if (n > 0) {
    vtbl = table->vtbl;
    i = 0;

    // constant term of p
    if (bvpoly_buffer_var(p, 0) == const_idx) {
      bvarith_buffer_add_const(buffer, bvpoly_buffer_coeff(p, 0));
      i ++;
    }

    // non-constant terms
    while (i < n) {
      x = bvpoly_buffer_var(p, i);
      c = bvpoly_buffer_coeff(p, i);
      i ++;
      if (bvvar_is_const(vtbl, x)) {
        bvarith_buffer_add_const_times_const(buffer, c, bvvar_val(vtbl, x));
      } else {
        q = bvexp_def(table, x);
        if (q != NULL) {
          bvarith_buffer_add_const_times_mlist(buffer, q, c);
        } else {
          bvarith_buffer_add_varmono(buffer, c, x);
        }
      }
    }

    bvarith_buffer_normalize(buffer);
  }
}
Beispiel #3
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);
  }
}
/*
 * Add a * t to b
 * - t must be defined in table and be a bitvector term of same bitsize as b
 * - a must be have the same bitsize as b (as many words a b->width)
 * - b->ptbl must be the same as table->pprods
 */
void bvarith_buffer_add_const_times_term(bvarith_buffer_t *b, term_table_t *table, uint32_t *a, term_t t) {
  bvconstant_t c;
  pprod_t **v;
  bvpoly_t *p;
  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:
    bvarith_buffer_add_mono(b, a, pprod_for_idx(table, i));
    break;

  case BV_CONSTANT:
    init_bvconstant(&c);
    bvconstant_copy(&c, b->bitsize, bvconst_for_idx(table, i)->data);
    bvconst_mul(c.data, b->width, a);
    bvarith_buffer_add_const(b, c.data);
    delete_bvconstant(&c);
    break;

  case BV_POLY:
    p = bvpoly_for_idx(table, i);
    v = pprods_for_bvpoly(table, p);
    bvarith_buffer_add_const_times_bvpoly(b, p, v, a);
    term_table_reset_pbuffer(table);
    break;

  default:
    bvarith_buffer_add_varmono(b, a, t);
    break;
  }
}
Beispiel #5
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);
}