Example #1
0
static void alloc_constants() {
  uint32_t i;

  init_bvconstants();
  //  a = (uint32_t *) safe_malloc(4 * sizeof(uint32_t));
  //  b = (uint32_t *) safe_malloc(4 * sizeof(uint32_t));
  //  c = (uint32_t *) safe_malloc(4 * sizeof(uint32_t));
  //  d = (uint32_t *) safe_malloc(4 * sizeof(uint32_t));
  //  e = (uint32_t *) safe_malloc(4 * sizeof(uint32_t));
  a = bvconst_alloc(4);
  b = bvconst_alloc(4);
  c = bvconst_alloc(4);
  d = bvconst_alloc(4);
  e = bvconst_alloc(4);

  for (i=0; i<4; i++) {
    a[i] = 0;
    b[i] = 0xffffffff;
    c[i] = 0;
    d[i] = 0;
    e[i] = 0;
  }

  c[0] = 1;
  d[0] = 0x80000000;
  e[1] = 1;
}
Example #2
0
/*
 * Initialize table and store
 */
static void init_globals(void) {
  init_bvconstants();
  init_bvmlist_store(&store);
  init_pprod_table(&prod_table, 0);
}
Example #3
0
int main() {
  int32_t x, y;
  uint32_t a, b, n;
  char c;
  string_buffer_t *s;

  s = &buffer;
  init_string_buffer(s, 0);
  show_test("empty buffer", s);

  string_buffer_reset(s);
  for (c = 'a'; c <= 'z'; c++) {
    string_buffer_append_char(s, c);
  }
  show_test("alphabet", s);

  string_buffer_reset(s);
  for (c = 'a'; c <= 'z'; c++) {
    string_buffer_append_char(s, c);
  }
  string_buffer_append_string(s, "au898ue2bcc90219");
  show_test("alphabet+au898ue2bcc90219", s);

  x = INT32_MIN;
  for (;;){
    sprintf(aux, "signed number: %" PRId32, x);
    string_buffer_reset(s);
    string_buffer_append_int32(s, x);
    show_test(aux, s);
    y = x >> 1;
    if (y == x) break;
    x = y;
  }

  x = INT32_MAX;
  for (;;) {
    sprintf(aux, "signed number: %" PRId32, x);
    string_buffer_reset(s);
    string_buffer_append_int32(s, x);
    show_test(aux, s);
    y = x>>1;
    if (y == x) break;
    x = y;
  }

  a = UINT32_MAX;
  for (;;){
    sprintf(aux, "unsigned number: %" PRIu32, a);
    string_buffer_reset(s);
    string_buffer_append_uint32(s, a);
    show_test(aux, s);
    b = a >> 1;
    if (b == a) break;
    a = b;
  }

  mpz_init(z0);
  mpz_init(z1);
  mpq_init(q0);

  mpz_set_str(z0, "111102222033330123456789", 10);
  string_buffer_reset(s);
  string_buffer_append_mpz(s, z0);
  show_test("mpz: 111102222033330123456789", s);

  mpz_set_str(z0, "-111102222033330123456789", 10);
  string_buffer_reset(s);
  string_buffer_append_mpz(s, z0);
  show_test("mpz: -111102222033330123456789", s);

  string_buffer_reset(s);
  string_buffer_append_mpz(s, z1);
  show_test("mpz: 0", s);

  mpq_set_str(q0, "-98765432109876543210", 10);
  string_buffer_reset(s);
  string_buffer_append_mpq(s, q0);
  show_test("mpq: -98765432109876543210", s);

  mpq_set_str(q0, "-98765432109876543210/38192839777", 10);
  string_buffer_reset(s);
  string_buffer_append_mpq(s, q0);
  show_test("mpq: -98765432109876543210/38192839777", s);

  init_rationals();
  rational_t r0;
  q_init(&r0);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: 0", s);

  q_set_int32(&r0, -12, 73);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: -12/73", s);

  q_set_mpq(&r0, q0);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: -98765432109876543210/38192839777", s);

  q_set_mpz(&r0, z0);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: -111102222033330123456789", s);


  printf("\nBit Vectors\n");
  init_bvconstants();
  bv0 = bvconst_alloc(1);
  bvconst_clear(bv0, 1);
  for (n=1; n<= 32; n++) {
    string_buffer_reset(s);
    string_buffer_append_bvconst(s, bv0, n);
    sprintf(aux, "bv[%" PRIu32"]: 0b000...", n);
    show_test(aux, s);
  }

  for (n=1; n <= 32; n++) {
    bvconst_clear(bv0, 1);
    bvconst_set_bit(bv0, n-1);
    string_buffer_reset(s);
    string_buffer_append_bvconst(s, bv0, n);
    sprintf(aux, "bv[%" PRIu32"]: 0b100...", n);
    show_test(aux, s);
  }


  bvconst_free(bv0, 1);

  cleanup_bvconstants();

  cleanup_rationals();

  mpz_clear(z0);
  mpz_clear(z1);
  mpq_clear(q0);

  delete_string_buffer(s);

  return 0;
}
Example #4
0
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;
}