32 uint32_t t =
x ^ (
x << 11);
34 w ^= (
w >> 19) ^ t ^ (t >> 8);
41 std::vector<int> modelExpressions;
42 std::vector<bool> modelValues;
46 modelExpressions.push_back(
id);
48 if (sat.
solve(modelExpressions, modelValues, assumption)) {
49 printf(
"satisfiable:");
50 for (
int i = 0; i < int(modelExpressions.size()); i++)
51 printf(
" %s=%d", sat.
to_string(modelExpressions[i]).c_str(), int(modelValues[i]));
55 printf(
"not satisfiable.\n\n");
64 printf(
"==== %s ====\n\n", __PRETTY_FUNCTION__);
77 uint32_t output_pattern = input_pattern;
78 output_pattern ^= output_pattern << 13;
79 output_pattern ^= output_pattern >> 17;
80 output_pattern ^= output_pattern << 5;
82 std::vector<int> modelExpressions;
83 std::vector<int> forwardAssumptions, backwardAssumptions;
84 std::vector<bool> forwardModel, backwardModel;
92 if (!sat.
solve(modelExpressions, backwardModel, backwardAssumptions)) {
93 printf(
"backward solving failed!\n");
97 if (!sat.
solve(modelExpressions, forwardModel, forwardAssumptions)) {
98 printf(
"forward solving failed!\n");
102 printf(
"xorshift32 test with input pattern 0x%08x:\n", input_pattern);
104 printf(
"forward solution: input=0x%08x output=0x%08x\n",
108 printf(
"backward solution: input=0x%08x output=0x%08x\n",
112 if (forwardModel != backwardModel) {
113 printf(
"forward and backward results are inconsistend!\n");
117 printf(
"passed.\n\n");
122 printf(
"==== %s ====\n\n", __PRETTY_FUNCTION__);
129 std::vector<int> bits = sat.
vec_var(
"i", 32);
150 #define CHECK(_expr1, _expr2) check(#_expr1, _expr1, #_expr2, _expr2)
152 void check(
const char *expr1_str,
bool expr1,
const char *expr2_str,
bool expr2)
154 if (expr1 == expr2) {
155 printf(
"[ %s ] == [ %s ] .. ok (%s == %s)\n", expr1_str, expr2_str, expr1 ?
"true" :
"false", expr2 ?
"true" :
"false");
157 printf(
"[ %s ] != [ %s ] .. ERROR (%s != %s)\n", expr1_str, expr2_str, expr1 ?
"true" :
"false", expr2 ?
"true" :
"false");
170 printf(
"Testing signed arithmetic using: a=%+d, b=%+d, c=%+d\n",
int(a),
int(b),
int(c));
186 b ^= c, c ^= b, b ^= c;
192 printf(
"Testing unsigned arithmetic using: a=%d, b=%d, c=%d\n",
int(a),
int(b),
int(c));
208 for (
int i = 0; i < 32; i++)
209 if (((x >> i) & 1) != 0)
212 printf(
"Testing bit counting using x=0x%08x (%d set bits) .. ", x, count);
220 fprintf(stderr,
"FAILED 6bit-no-clipping test!\n");
225 fprintf(stderr,
"FAILED 4bit-clipping test!\n");
234 printf(
"==== %s ====\n\n", __PRETTY_FUNCTION__);
238 for (
int i = 0; i < 100; i++)
239 test_signed(rng() % 19 - 10, rng() % 19 - 10, rng() % 19 - 10);
241 for (
int i = 0; i < 100; i++)
246 for (
int i = 0; i < 30; i++)
256 printf(
"==== %s ====\n\n", __PRETTY_FUNCTION__);
264 std::vector<int> abcd;
272 int solution_counter = 0;
275 std::vector<bool> modelValues;
276 bool ok = ez.
solve(abcd, modelValues);
281 printf(
"Solution: %d %d %d %d\n",
int(modelValues[0]),
int(modelValues[1]),
int(modelValues[2]),
int(modelValues[3]));
284 std::vector<int> sol;
285 for (
int i = 0; i < 4; i++) {
288 sol.push_back(modelValues[i] ? abcd[i] : ez.
NOT(abcd[i]));
292 if (count_hot != 1) {
293 fprintf(stderr,
"Wrong number of hot bits!\n");
300 if (solution_counter != 4) {
301 fprintf(stderr,
"Wrong number of one-hot solutions!\n");
310 printf(
"==== %s ====\n\n", __PRETTY_FUNCTION__);
318 std::vector<int> abcd;
326 int solution_counter = 0;
329 std::vector<bool> modelValues;
330 bool ok = ez.
solve(abcd, modelValues);
335 printf(
"Solution: %d %d %d %d\n",
int(modelValues[0]),
int(modelValues[1]),
int(modelValues[2]),
int(modelValues[3]));
338 std::vector<int> sol;
339 for (
int i = 0; i < 4; i++) {
342 sol.push_back(modelValues[i] ? abcd[i] : ez.
NOT(abcd[i]));
346 if (count_hot != 1 && count_hot != 2) {
347 fprintf(stderr,
"Wrong number of hot bits!\n");
354 if (solution_counter != 4 + 4*3/2) {
355 fprintf(stderr,
"Wrong number of one-hot solutions!\n");
364 printf(
"==== %s ====\n\n", __PRETTY_FUNCTION__);
375 std::vector<int> abc;
380 std::vector<int> xyz;
387 int solution_counter = 0;
391 std::vector<int> modelVariables;
392 std::vector<bool> modelValues;
394 modelVariables.push_back(a);
395 modelVariables.push_back(b);
396 modelVariables.push_back(c);
398 modelVariables.push_back(x);
399 modelVariables.push_back(y);
400 modelVariables.push_back(z);
402 bool ok = ez.
solve(modelVariables, modelValues);
407 printf(
"Solution: %d %d %d | %d %d %d\n",
408 int(modelValues[0]),
int(modelValues[1]),
int(modelValues[2]),
409 int(modelValues[3]),
int(modelValues[4]),
int(modelValues[5]));
411 std::vector<int> sol;
412 for (
size_t i = 0; i < modelVariables.size(); i++)
413 sol.push_back(modelValues[i] ? modelVariables[i] : ez.
NOT(modelVariables[i]));
419 if (solution_counter != 8+7+6+5+4+3+2+1) {
420 fprintf(stderr,
"Wrong number of solutions!\n");
438 printf(
"Passed all tests.\n\n");
std::vector< int > vec_sub(const std::vector< int > &vec1, const std::vector< int > &vec2)
std::vector< int > vec_const_signed(int64_t value, int numBits)
int vec_ge_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
void test_count(uint32_t x)
std::vector< int > vec_var(int numBits)
int vec_le_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
#define CHECK(_expr1, _expr2)
int vec_lt_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
std::vector< int > vec_count(const std::vector< int > &vec, int numBits, bool clip=true)
void vec_set(const std::vector< int > &vec1, const std::vector< int > &vec2)
std::vector< int > vec_shl(const std::vector< int > &vec1, int shift, bool signExtend=false)
int AND(_V a=0, _V b=0, _V c=0, _V d=0, _V e=0, _V f=0)
int onehot(const std::vector< int > &vec, bool max_only=false)
void test_xorshift32_try(ezSAT &sat, uint32_t input_pattern)
std::string to_string(int id) const
void check(const char *expr1_str, bool expr1, const char *expr2_str, bool expr2)
uint64_t vec_model_get_unsigned(const std::vector< int > &modelExpressions, const std::vector< bool > &modelValues, const std::vector< int > &vec1) const
int vec_gt_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
std::vector< int > vec_const_unsigned(uint64_t value, int numBits)
void vec_append_unsigned(std::vector< int > &vec, const std::vector< int > &vec1, uint64_t value)
std::vector< int > vec_shr(const std::vector< int > &vec1, int shift, bool signExtend=false)
bool solve(const std::vector< int > &modelExpressions, std::vector< bool > &modelValues, const std::vector< int > &assumptions)
void printDIMACS(FILE *f, bool verbose=false) const
bool test(ezSAT &sat, int assumption=0)
int ordered(const std::vector< int > &vec1, const std::vector< int > &vec2, bool allow_equal=true)
std::vector< int > vec_xor(const std::vector< int > &vec1, const std::vector< int > &vec2)
int vec_gt_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
void test_unsigned(uint8_t a, uint8_t b, uint8_t c)
std::vector< int > vec_add(const std::vector< int > &vec1, const std::vector< int > &vec2)
void test_signed(int8_t a, int8_t b, int8_t c)
int manyhot(const std::vector< int > &vec, int min_hot, int max_hot=-1)
int expression(OpId op, int a=0, int b=0, int c=0, int d=0, int e=0, int f=0)
int vec_lt_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
int OR(_V a=0, _V b=0, _V c=0, _V d=0, _V e=0, _V f=0)
int vec_ge_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
void vec_append(std::vector< int > &vec, const std::vector< int > &vec1) const
int vec_le_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)