From 11e811858913cb2d03249d0a7239da10f4af5a7c Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Mon, 25 Nov 2013 15:10:32 +0100 Subject: [PATCH] Added ezsat vec_const() api --- libs/ezsat/ezsat.cc | 32 ++++++++++++++++++++------------ libs/ezsat/ezsat.h | 11 ++++++----- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/libs/ezsat/ezsat.cc b/libs/ezsat/ezsat.cc index 27e74e010..dccc00555 100644 --- a/libs/ezsat/ezsat.cc +++ b/libs/ezsat/ezsat.cc @@ -651,34 +651,42 @@ bool ezSAT::solver(const std::vector &modelExpressions, std::vector & return false; } -std::vector ezSAT::vec_const_signed(int64_t value, int bits) +std::vector ezSAT::vec_const(const std::vector &bits) { std::vector vec; - for (int i = 0; i < bits; i++) + for (auto bit : bits) + vec.push_back(bit ? TRUE : FALSE); + return vec; +} + +std::vector ezSAT::vec_const_signed(int64_t value, int numBits) +{ + std::vector vec; + for (int i = 0; i < numBits; i++) vec.push_back(((value >> i) & 1) != 0 ? TRUE : FALSE); return vec; } -std::vector ezSAT::vec_const_unsigned(uint64_t value, int bits) +std::vector ezSAT::vec_const_unsigned(uint64_t value, int numBits) { std::vector vec; - for (int i = 0; i < bits; i++) + for (int i = 0; i < numBits; i++) vec.push_back(((value >> i) & 1) != 0 ? TRUE : FALSE); return vec; } -std::vector ezSAT::vec_var(int bits) +std::vector ezSAT::vec_var(int numBits) { std::vector vec; - for (int i = 0; i < bits; i++) + for (int i = 0; i < numBits; i++) vec.push_back(literal()); return vec; } -std::vector ezSAT::vec_var(std::string name, int bits) +std::vector ezSAT::vec_var(std::string name, int numBits) { std::vector vec; - for (int i = 0; i < bits; i++) + for (int i = 0; i < numBits; i++) vec.push_back(VAR(name + "[" + std::to_string(i) + "]")); return vec; } @@ -782,21 +790,21 @@ static void halfadder(ezSAT *that, int a, int b, int &y, int &x) x = new_x, y = new_y; } -std::vector ezSAT::vec_count(const std::vector &vec, int bits, bool clip) +std::vector ezSAT::vec_count(const std::vector &vec, int numBits, bool clip) { - std::vector sum = vec_const_unsigned(0, bits); + std::vector sum = vec_const_unsigned(0, numBits); std::vector carry_vector; for (auto bit : vec) { int carry = bit; - for (int i = 0; i < bits; i++) + for (int i = 0; i < numBits; i++) halfadder(this, carry, sum[i], carry, sum[i]); carry_vector.push_back(carry); } if (clip) { int overflow = vec_reduce_or(carry_vector); - sum = vec_ite(overflow, vec_const_unsigned(~0, bits), sum); + sum = vec_ite(overflow, vec_const_unsigned(~0, numBits), sum); } #if 0 diff --git a/libs/ezsat/ezsat.h b/libs/ezsat/ezsat.h index abec0b239..547edb93b 100644 --- a/libs/ezsat/ezsat.h +++ b/libs/ezsat/ezsat.h @@ -203,10 +203,11 @@ public: // simple helpers for building expressions with bit vectors - std::vector vec_const_signed(int64_t value, int bits); - std::vector vec_const_unsigned(uint64_t value, int bits); - std::vector vec_var(int bits); - std::vector vec_var(std::string name, int bits); + std::vector vec_const(const std::vector &bits); + std::vector vec_const_signed(int64_t value, int numBits); + std::vector vec_const_unsigned(uint64_t value, int numBits); + std::vector vec_var(int numBits); + std::vector vec_var(std::string name, int numBits); std::vector vec_cast(const std::vector &vec1, int toBits, bool signExtend = false); std::vector vec_not(const std::vector &vec1); @@ -218,7 +219,7 @@ public: std::vector vec_ite(const std::vector &vec1, const std::vector &vec2, const std::vector &vec3); std::vector vec_ite(int sel, const std::vector &vec2, const std::vector &vec3); - std::vector vec_count(const std::vector &vec, int bits, bool clip = true); + std::vector vec_count(const std::vector &vec, int numBits, bool clip = true); std::vector vec_add(const std::vector &vec1, const std::vector &vec2); std::vector vec_sub(const std::vector &vec1, const std::vector &vec2); std::vector vec_neg(const std::vector &vec); -- 2.30.2