-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnand_nor.h
277 lines (265 loc) · 8.9 KB
/
nand_nor.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef VM_NAND_NOR_H
#define VM_NAND_NOR_H
#include "utils.h"
//------------------------------------------------------------------------------
template <typename T>
FORCEINLINE T Not(volatile T a) {
return ~a;
}
template <typename T>
FORCEINLINE T Or(volatile T a, volatile T b) {
return a | b;
}
template <typename T>
FORCEINLINE T And(volatile T a, volatile T b) {
return a & b;
}
//------------------------------------------------------------------------------
//! Not (a Or b)
template <typename T>
FORCEINLINE volatile T Nor(volatile T a, volatile T b) {
return Not<T>(Or<T>(a, b));
}
//! Not (a And b)
template <typename T>
FORCEINLINE volatile T Nand(volatile T a, volatile T b) {
return Not<T>(And<T>(a, b));
}
//! (a Nand (a Nand b)) Nand (b Nand (a Nand b))
template <typename T>
FORCEINLINE T Xor(volatile T a, volatile T b) {
return Nand<T>(Nand<T>(a, Nand<T>(a, b)), (Nand<T>(b, Nand<T>(a, b))));
}
//------------------------------------------------------------------------------
//! (Not a) Or (Not b)
template <typename T>
FORCEINLINE T Nand_1(volatile T a, volatile T b) {
return Or<T>(Not<T>(a), Not<T>(b));
}
//! Not(a And b)
template <typename T>
FORCEINLINE T Nand_2(volatile T a, volatile T b) {
return Not<T>(And<T>(a, b));
}
//! Not ((Not a) Nor (Not b))
template <typename T>
FORCEINLINE T Nand_3(volatile T a, volatile T b) {
return Not<T>(Nor<T>(Not<T>(a), Not<T>(b)));
}
//! (Not a) Or (Not b)
template <typename T>
FORCEINLINE T Nand_4(volatile T a, volatile T b) {
return Or<T>(Not<T>(a), Not<T>(b));
}
//! ((Not a) And b) Xor (Not b)
template <typename T>
FORCEINLINE T Nand_5(volatile T a, volatile T b) {
return Xor<T>(And<T>(Not<T>(a), b), Not<T>(b));
}
//------------------------------------------------------------------------------
//! (Not a) And (Not b)
template <typename T>
FORCEINLINE T Nor_1(volatile T a, volatile T b) {
return And<T>(Not<T>(a), Not<T>(b));
}
//! Not(a Xor b Xor (a And b))
template <typename T>
FORCEINLINE T Nor_2(volatile T a, volatile T b) {
return Not<T>(Xor<T>(Xor<T>(a, b), And<T>(a, b)));
}
//! Not((Not a) Nand (Not b))
template <typename T>
FORCEINLINE T Nor_3(volatile T a, volatile T b) {
return Not<T>(Nand<T>(Not<T>(a), Not<T>(b)));
}
//! (Not a) And (Not b)
template <typename T>
FORCEINLINE T Nor_4(volatile T a, volatile T b) {
return And<T>(Not<T>(a), Not<T>(b));
}
//! Not(a Or b)
template <typename T>
FORCEINLINE T Nor_5(volatile T a, volatile T b) {
return Not<T>(Or<T>(a, b));
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T NandR(volatile T a, volatile T b) {
switch (n % 5) {
case 0:
return Nand_1<T>(a, b);
case 1:
return Nand_2<T>(a, b);
case 2:
return Nand_3<T>(a, b);
case 3:
return Nand_4<T>(a, b);
case 4:
return Nand_5<T>(a, b);
default:
return Nand_1<T>(a, b);
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T NorR(volatile T a, volatile T b) {
switch (n % 5) {
case 0:
return Nor_1<T>(a, b);
case 1:
return Nor_2<T>(a, b);
case 2:
return Nor_3<T>(a, b);
case 3:
return Nor_4<T>(a, b);
case 4:
return Nor_5<T>(a, b);
default:
return Nor_1<T>(a, b);
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T NotR(volatile uint64_t a) {
switch (n % 10) {
case 0:
return Nand_1<T>(a, a);
case 1:
return Nand_2<T>(a, a);
case 2:
return Nand_3<T>(a, a);
case 3:
return Nand_4<T>(a, a);
case 4:
return Nand_5<T>(a, a);
case 5:
return Nor_1<T>(a, a);
case 6:
return Nor_2<T>(a, a);
case 7:
return Nor_3<T>(a, a);
case 8:
return Nor_4<T>(a, a);
case 9:
return Nor_5<T>(a, a);
default:
return Nand_1<T>(a, a);
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T AndR(volatile T a, volatile T b) {
switch (n % 5) {
// (Not a) Nor (Not b)
case 0:
return NorR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a),
NotR<T, n + __COUNTER__>(b));
// Not (a Nand b)
case 1:
return NotR<T, n + __COUNTER__>(NandR<T, n + __COUNTER__>(a, b));
// Not((Not a) Or (Not b))
case 2:
return NotR<T, n + __COUNTER__>(
Or(NotR<T, n + __COUNTER__>(a), NotR<T, n + __COUNTER__>(b)));
// (a Xor (Not b)) And a
case 3:
return And<T>(a, Xor<T>(a, NotR<T, n + __COUNTER__>(b)));
// Not((Not a) Or (Not b))
case 4:
return Not<T>(
Or<T>(NotR<T, n + __COUNTER__>(a), NotR<T, n + __COUNTER__>(b)));
default:
return NorR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a),
NotR<T, n + __COUNTER__>(b));
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T OrR(volatile T a, volatile T b) {
switch (n % 5) {
// a Xor b Xor (a And b)
case 0:
return Xor<T>(Xor<T>(a, b), AndR<T, n + __COUNTER__>(a, b));
// Not (a Nor b)
case 1:
return NotR<T, n + __COUNTER__>(NorR<T, n + __COUNTER__>(a, b));
// (Not a) Nand (Not b)
case 2:
return NandR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a),
NotR<T, n + __COUNTER__>(b));
// Not((Not a) And (Not b))
case 3:
return NotR<T, n + __COUNTER__>(AndR<T, n + __COUNTER__>(
NotR<T, n + __COUNTER__>(a), NotR<T, n + __COUNTER__>(b)));
// b Xor (a And (Not b))
case 4:
return Xor<T>(b,
AndR<T, n + __COUNTER__>(a, NotR<T, n + __COUNTER__>(b)));
default:
return Xor<T>(Xor<T>(a, b), AndR<T, n + __COUNTER__>(a, b));
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T XorR(volatile T a, volatile T b) {
switch (n % 6) {
// (a And (Not b)) Or ((Not a) And b)
case 0:
return OrR<T, n + __COUNTER__>(
AndR<T, n + __COUNTER__>(a, (NotR<T, n + __COUNTER__>(b))),
AndR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a), b));
// ((Not a) Or (Not b)) And (a Or b)
case 1:
return And(OrR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a),
NotR<T, n + __COUNTER__>(b)),
OrR<T, n + __COUNTER__>(a, b));
// ((Not a) Nor (Not b)) Nor (a Nor b)
case 2:
return NorR<T, n + __COUNTER__>(
NorR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a),
NotR<T, n + __COUNTER__>(b)),
NorR<T, n + __COUNTER__>(a, b));
// (a Nand (a Nand b)) Nand (b Nand (a Nand b))
case 3:
return NandR<T, n + __COUNTER__>(
NandR<T, n + __COUNTER__>(a, NandR<T, n + __COUNTER__>(a, b)),
NandR<T, n + __COUNTER__>(b, NandR<T, n + __COUNTER__>(a, b)));
// (Not (a And b)) And (Not ((Not a) And (Not b)))
case 4:
return AndR<T, n + __COUNTER__>(
(NotR<T, n + __COUNTER__>(AndR<T, n + __COUNTER__>(a, b))),
NotR<T, n + __COUNTER__>(AndR<T, n + __COUNTER__>(
NotR<T, n + __COUNTER__>(a), NotR<T, n + __COUNTER__>(b))));
// (Not ((Not a) Or b)) Or (Not (a Or (Not b)))
case 5:
return OrR<T, n + __COUNTER__>(
NotR<T, n + __COUNTER__>(
OrR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a), b)),
(NotR<T, n + __COUNTER__>(
OrR<T, n + __COUNTER__>(a, NotR<T, n + __COUNTER__>(b)))));
default:
return OrR<T, n + __COUNTER__>(
AndR<T, n + __COUNTER__>(a, (NotR<T, n + __COUNTER__>(b))),
AndR<T, n + __COUNTER__>(NotR<T, n + __COUNTER__>(a), b));
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T IncR(volatile T a) {
switch (n % 1) {
case 0: return -NotR<T, n + __COUNTER__>(a);
default:
return -NotR<T, n + __COUNTER__>(a);
}
}
//------------------------------------------------------------------------------
template <typename T, const uint64_t n>
FORCEINLINE volatile T DecR(volatile T a) {
switch (n % 1) {
case 0: return NotR<T, n + __COUNTER__>(-a);
default:
return NotR<T, n + __COUNTER__>(-a);
}
}
// TODO Add classic full adder/Kogge stone/Ripple carry/STA, LSHIFT, RSHIFT, CMP emulation (for comparison operators)
#endif