Example #1
0
/*
 * Test shift a by b: both stored as 64bit integers
 * - n = number of bits in a and b
 */
static void test_shift64(uint64_t a, uint64_t b, uint32_t n) {
  uint64_t lshl, lshr, ashr;

  a = norm64(a, n);
  b = norm64(b, n);

  lshl = bvconst64_lshl(a, b, n);
  assert(lshl == norm64(lshl, n));

  lshr = bvconst64_lshr(a, b, n);
  assert(lshr == norm64(lshr, n));

  ashr = bvconst64_ashr(a, b, n);
  assert(ashr == norm64(ashr, n));

  printf(" a = ");
  bvconst64_print(stdout, a, n);
  printf("\n");
  printf(" b = ");
  bvconst64_print(stdout, b, n);
  printf("\n");
  printf(" lshl(a, b) = ");
  bvconst64_print(stdout, lshl, n);
  printf("\n");
  printf(" lshr(a, b) = ");
  bvconst64_print(stdout, lshr, n);
  printf("\n");
  printf(" ashr(a, b) = ");
  bvconst64_print(stdout, ashr, n);
  printf("\n\n");
}
/*
 * Test x divided by y
 * - both stored as uin64_t, interpreted as n-bit constants
 */
static void test_bv64(uint64_t x, uint64_t y, uint32_t n) {
  uint64_t udiv, urem, sdiv, srem, smod;

  x = norm64(x, n);
  y = norm64(y, n);
  udiv = bvconst64_udiv2z(x, y, n);
  urem = bvconst64_urem2z(x, y, n);
  sdiv = bvconst64_sdiv2z(x, y, n);
  srem = bvconst64_srem2z(x, y, n);
  smod = bvconst64_smod2z(x, y, n);

  printf("udiv(%"PRIu64", %"PRIu64") = %"PRIu64", ", x, y, udiv);
  printf("urem(%"PRIu64", %"PRIu64") = %"PRIu64", ", x, y, urem);
  printf("sdiv(%"PRIu64", %"PRIu64") = %"PRIu64", ", x, y, sdiv);
  printf("srem(%"PRIu64", %"PRIu64") = %"PRIu64", ", x, y, srem);
  printf("smod(%"PRIu64", %"PRIu64") = %"PRIu64"\n", x, y, smod);
}
Example #3
0
/*
 * Expanded form for a product c * p
 * - c is a normalized bitvector constant
 * - p is a power product stored in a pp_buffer object
 * - n = bitsize of p
 * - the expansion is returned in a bvarith_buffer or bvarith64_buffer object
 * - the result is normalized
 */
void expand_bvpprod64(bvexp_table_t *table, bvarith64_buffer_t *buffer, pp_buffer_t *p, uint32_t n, uint64_t c) {
  bv_vartable_t *vtbl;
  bvmlist64_t *q;
  pp_buffer_t *aux;
  pprod_t *r;
  uint64_t a;
  uint32_t i, m, e, d;
  thvar_t x;

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

  bvarith64_buffer_prepare(buffer, n);
  bvarith64_buffer_set_one(buffer);
  aux = p;

  vtbl = table->vtbl;

  if (total_degree_test64(table, vtbl, p)) {
    /*
     * Expansion of c * x_1^d_1 ... x_n^ d_n:
     * - for a constant x_i, update c to c * a^d_i (where a = value of c)
     * - if x_i is expanded to q_i, update buffer to buffer * q_i ^ d_i
     * - otherwise, x_i^d_i is copied into the aux buffer
     */
    aux = &table->pp;
    pp_buffer_reset(aux);

    m = p->len;
    for (i=0; i<m; i++) {
      x = p->prod[i].var;
      d = p->prod[i].exp;
      if (bvvar_is_const64(vtbl, x)) {
        a = bvvar_val64(vtbl, x);
        c *= upower64(a, d);
      } else {
        q = bvexp_def64(table, x);
        if (q != NULL && mlist64_is_short(q, &e) && d * e <= BVEXP_DEGREE_LIMIT) {
          // replace x^d by q^d in buffer
          bvarith64_buffer_mul_mlist_power(buffer, q, d, &table->aux64);
        } else {
          // copy x^d into aux
          pp_buffer_mul_varexp(aux, x, d);
        }
      }
    }

    c = norm64(c, n);
    pp_buffer_normalize(aux);
  }

  /*
   * The result is c * aux * buffer
   */
  r = pprod_from_buffer(&table->pprods, aux);
  bvarith64_buffer_mul_mono(buffer, c, r);
  bvarith64_buffer_normalize(buffer);
}