-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombLogics.cpp
88 lines (83 loc) · 3.12 KB
/
CombLogics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Created by unknown on 2024/11/3.
//
#include "CombLogics.h"
int CombLogic::counter = 0;
void CombLogic::checkInputDataSlicing(CircuitData *s1, CircuitData *s2) {
auto slicing1 = s1->getBitWidth();
auto slicing2 = s2->getBitWidth();
if (slicing1 != slicing2) {
throw CircuitException("Width mismatched");
}
}
std::tuple<bool, std::size_t> CombLogic::generateUnsignedIntegerFromData(CircuitData *data) {
std::size_t answer = 0;
for (long int i = (long int) data->getBitWidth() - 1; i >= 0; i--) {
auto bit = data->bits[i];
if (bit == 1) {
answer |= 1;
} else if (bit == 0) {
answer |= 0;
} else {
return {false, 0};
}
if (i) {
answer <<= 1;
}
}
return {true, answer};
}
std::unique_ptr<CombLogic> CombLogicFactory::create(VeriPythonTokens token) {
switch (token) {
case TOKEN_logical_or:
return std::make_unique<CombLogicLogicalOr>();
case TOKEN_logical_and:
return std::make_unique<CombLogicLogicalAnd>();
case TOKEN_bitwise_or:
return std::make_unique<CombLogicBitwiseOr>();
case TOKEN_bitwise_xor:
return std::make_unique<CombLogicBitwiseXor>();
case TOKEN_bitwise_and:
return std::make_unique<CombLogicBitwiseAnd>();
case TOKEN_cond_eq:
return std::make_unique<CombLogicCompareEqual>();
case TOKEN_cond_ne:
return std::make_unique<CombLogicCompareNonEqual>();
case TOKEN_cond_lt:
return std::make_unique<CombLogicCompareLessThan>();
case TOKEN_cond_le:
return std::make_unique<CombLogicCompareLessEqual>();
case TOKEN_cond_gt:
return std::make_unique<CombLogicCompareGreatThan>();
case TOKEN_cond_ge:
return std::make_unique<CombLogicCompareGreatEqual>();
case TOKEN_arith_lshift:
return std::make_unique<CombLogicShiftLeftArith>();
case TOKEN_logical_lshift:
return std::make_unique<CombLogicShiftLeftLogical>();
case TOKEN_arith_rshift:
return std::make_unique<CombLogicShiftRightArith>();
case TOKEN_logical_rshift:
return std::make_unique<CombLogicShiftRightLogical>();
case TOKEN_op_add:
return std::make_unique<CombLogicArithAdd>();
case TOKEN_op_sub:
return std::make_unique<CombLogicArithSub>();
case TOKEN_op_mod:
return std::make_unique<CombLogicArithMod>();
case TOKEN_op_mul:
return std::make_unique<CombLogicArithMul>();
case TOKEN_op_div:
return std::make_unique<CombLogicArithDiv>();
case TOKEN_logical_not:
return std::make_unique<CombLogicLogicalNot>();
case TOKEN_bitwise_not:
return std::make_unique<CombLogicBitwiseNot>();
case TOKEN_question:
return std::make_unique<CombLogicMultiplexer>();
case TOKEN_lbrace:
return std::make_unique<CombLogicConcat>();
default:
throw CircuitException("Unknown operator");
}
}