yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
demo_cmp.cc File Reference
#include "ezminisat.h"
#include <assert.h>
+ Include dependency graph for demo_cmp.cc:

Go to the source code of this file.

Macros

#define INIT_X   123456789
 
#define INIT_Y   362436069
 
#define INIT_Z   521288629
 
#define INIT_W   88675123
 
#define MONITOR_VARS
 
#define X(_n)   int _n; bool _n ## _master;
 
#define X(_n)   modelExpressions.push_back(_n); modelNames.push_back(#_n); modelMaster.push_back(_n ## _master);
 

Functions

uint32_t xorshift128 ()
 
void test_cmp (uint32_t a, uint32_t b)
 
int main ()
 

Macro Definition Documentation

#define INIT_W   88675123

Definition at line 26 of file demo_cmp.cc.

#define INIT_X   123456789

Definition at line 23 of file demo_cmp.cc.

#define INIT_Y   362436069

Definition at line 24 of file demo_cmp.cc.

#define INIT_Z   521288629

Definition at line 25 of file demo_cmp.cc.

#define MONITOR_VARS
Value:
X(carry) X(overflow) X(sign) X(zero) \
X(lt_signed) X(le_signed) X(ge_signed) X(gt_signed) \
X(lt_unsigned) X(le_unsigned) X(ge_unsigned) X(gt_unsigned)
bool sign(Lit p)
Definition: SolverTypes.h:66
#define X(_n)
#define X (   _n)    int _n; bool _n ## _master;
#define X (   _n)    modelExpressions.push_back(_n); modelNames.push_back(#_n); modelMaster.push_back(_n ## _master);

Function Documentation

int main ( )

Definition at line 133 of file demo_cmp.cc.

134 {
135  printf("\n");
136  for (int i = 0; i < 1024; i++) {
137  printf("************** %d **************\n\n", i);
138  uint32_t a = xorshift128();
139  uint32_t b = xorshift128();
140  if (xorshift128() % 16 == 0)
141  a = b;
142  test_cmp(a, b);
143  }
144  return 0;
145 }
void test_cmp(uint32_t a, uint32_t b)
Definition: demo_cmp.cc:39
uint32_t xorshift128()
Definition: demo_cmp.cc:28

+ Here is the call graph for this function:

void test_cmp ( uint32_t  a,
uint32_t  b 
)

Definition at line 39 of file demo_cmp.cc.

40 {
41  ezMiniSAT sat;
42 
43  printf("A = %10u (%10d)\n", a, int32_t(a));
44  printf("B = %10u (%10d)\n", b, int32_t(b));
45  printf("\n");
46 
47  std::vector<int> va = sat.vec_var("a", 32);
48  std::vector<int> vb = sat.vec_var("b", 32);
49 
50  sat.vec_set_unsigned(va, a);
51  sat.vec_set_unsigned(vb, b);
52 
53 #define MONITOR_VARS \
54  X(carry) X(overflow) X(sign) X(zero) \
55  X(lt_signed) X(le_signed) X(ge_signed) X(gt_signed) \
56  X(lt_unsigned) X(le_unsigned) X(ge_unsigned) X(gt_unsigned)
57 
58 #define X(_n) int _n; bool _n ## _master;
60 #undef X
61 
62  carry_master = ((uint64_t(a) - uint64_t(b)) >> 32) & 1;
63  overflow_master = (int32_t(a) - int32_t(b)) != (int64_t(int32_t(a)) - int64_t(int32_t(b)));
64  sign_master = ((a - b) >> 31) & 1;
65  zero_master = a == b;
66 
67  sat.vec_cmp(va, vb, carry, overflow, sign, zero);
68 
69  lt_signed_master = int32_t(a) < int32_t(b);
70  le_signed_master = int32_t(a) <= int32_t(b);
71  ge_signed_master = int32_t(a) >= int32_t(b);
72  gt_signed_master = int32_t(a) > int32_t(b);
73 
74  lt_unsigned_master = a < b;
75  le_unsigned_master = a <= b;
76  ge_unsigned_master = a >= b;
77  gt_unsigned_master = a > b;
78 
79  lt_signed = sat.vec_lt_signed(va, vb);
80  le_signed = sat.vec_le_signed(va, vb);
81  ge_signed = sat.vec_ge_signed(va, vb);
82  gt_signed = sat.vec_gt_signed(va, vb);
83 
84  lt_unsigned = sat.vec_lt_unsigned(va, vb);
85  le_unsigned = sat.vec_le_unsigned(va, vb);
86  ge_unsigned = sat.vec_ge_unsigned(va, vb);
87  gt_unsigned = sat.vec_gt_unsigned(va, vb);
88 
89  std::vector<int> modelExpressions;
90  std::vector<bool> modelValues, modelMaster;
91  std::vector<std::string> modelNames;
92 
93 #define X(_n) modelExpressions.push_back(_n); modelNames.push_back(#_n); modelMaster.push_back(_n ## _master);
95 #undef X
96 
97  std::vector<int> add_ab = sat.vec_add(va, vb);
98  std::vector<int> sub_ab = sat.vec_sub(va, vb);
99  std::vector<int> sub_ba = sat.vec_sub(vb, va);
100 
101  sat.vec_append(modelExpressions, add_ab);
102  sat.vec_append(modelExpressions, sub_ab);
103  sat.vec_append(modelExpressions, sub_ba);
104 
105  if (!sat.solve(modelExpressions, modelValues)) {
106  fprintf(stderr, "SAT solver failed to find a model!\n");
107  abort();
108  }
109 
110  bool found_error = false;
111 
112  for (size_t i = 0; i < modelMaster.size(); i++) {
113  if (modelMaster.at(i) != int(modelValues.at(i)))
114  found_error = true;
115  printf("%-20s %d%s\n", modelNames.at(i).c_str(), int(modelValues.at(i)),
116  modelMaster.at(i) != modelValues.at(i) ? " !!!" : "");
117  }
118  printf("\n");
119 
120  uint32_t add_ab_value = sat.vec_model_get_unsigned(modelExpressions, modelValues, add_ab);
121  uint32_t sub_ab_value = sat.vec_model_get_unsigned(modelExpressions, modelValues, sub_ab);
122  uint32_t sub_ba_value = sat.vec_model_get_unsigned(modelExpressions, modelValues, sub_ba);
123 
124  printf("%-20s %10u %10u%s\n", "result(a+b)", add_ab_value, a+b, add_ab_value != a+b ? " !!!" : "");
125  printf("%-20s %10u %10u%s\n", "result(a-b)", sub_ab_value, a-b, sub_ab_value != a-b ? " !!!" : "");
126  printf("%-20s %10u %10u%s\n", "result(b-a)", sub_ba_value, b-a, sub_ba_value != b-a ? " !!!" : "");
127  printf("\n");
128 
129  if (found_error || add_ab_value != a+b || sub_ab_value != a-b || sub_ba_value != b-a)
130  abort();
131 }
std::vector< int > vec_sub(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:843
int vec_ge_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:912
std::vector< int > vec_var(int numBits)
Definition: ezsat.cc:673
int vec_le_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:905
void vec_cmp(const std::vector< int > &vec1, const std::vector< int > &vec2, int &carry, int &overflow, int &sign, int &zero)
Definition: ezsat.cc:873
int vec_lt_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:926
void vec_set_unsigned(const std::vector< int > &vec1, uint64_t value)
Definition: ezsat.cc:1167
bool sign(Lit p)
Definition: SolverTypes.h:66
uint64_t vec_model_get_unsigned(const std::vector< int > &modelExpressions, const std::vector< bool > &modelValues, const std::vector< int > &vec1) const
Definition: ezsat.cc:1126
int vec_gt_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:947
#define MONITOR_VARS
bool solve(const std::vector< int > &modelExpressions, std::vector< bool > &modelValues, const std::vector< int > &assumptions)
Definition: ezsat.h:122
int vec_gt_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:919
std::vector< int > vec_add(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:819
int vec_lt_signed(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:898
int vec_ge_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:940
void vec_append(std::vector< int > &vec, const std::vector< int > &vec1) const
Definition: ezsat.cc:1083
int vec_le_unsigned(const std::vector< int > &vec1, const std::vector< int > &vec2)
Definition: ezsat.cc:933

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint32_t xorshift128 ( )

Definition at line 28 of file demo_cmp.cc.

28  {
29  static uint32_t x = INIT_X;
30  static uint32_t y = INIT_Y;
31  static uint32_t z = INIT_Z;
32  static uint32_t w = INIT_W;
33  uint32_t t = x ^ (x << 11);
34  x = y; y = z; z = w;
35  w ^= (w >> 19) ^ t ^ (t >> 8);
36  return w;
37 }
#define INIT_W
Definition: demo_cmp.cc:26
#define INIT_X
Definition: demo_cmp.cc:23
#define INIT_Y
Definition: demo_cmp.cc:24
#define INIT_Z
Definition: demo_cmp.cc:25

+ Here is the caller graph for this function: