Skip to content

Commit eed0a87

Browse files
committed
Merge branch 'master' into feature/gnn
2 parents 9f953d9 + 56e46b5 commit eed0a87

File tree

2 files changed

+180
-17
lines changed

2 files changed

+180
-17
lines changed

src/netlist/boolean_function.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,9 +1240,8 @@ namespace hal
12401240
{
12411241
if (node.has_variable_name(name) && node.size != value.size())
12421242
{
1243-
// TODO the error message does not reflect what is being checked
1244-
return ERR("could not evaluate Boolean function '" + this->to_string() + "': as the number of variables (" + std::to_string(node.size)
1245-
+ ") does not match the number of provided inputs (" + std::to_string(value.size()) + ")");
1243+
return ERR("could not evaluate Boolean function '" + this->to_string() + "': as the size of vairbale " + name + " with size " + std::to_string(node.size)
1244+
+ " does not match the size of the provided input (" + std::to_string(value.size()) + ")");
12461245
}
12471246
}
12481247
}

src/netlist/boolean_function/symbolic_execution.cpp

Lines changed: 178 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "hal_core/netlist/boolean_function/symbolic_execution.h"
2-
32
#include "hal_core/utilities/log.h"
43

54
namespace hal
@@ -235,6 +234,141 @@ namespace hal
235234
return BooleanFunction::Const(simplified);
236235
}
237236

237+
/**
238+
* Helper function to simplify a constant SHL operation.
239+
*
240+
* @param[in] p0 - Boolean function parameter 0.
241+
* @param[in] p1 - Boolean function parameter 1.
242+
* @returns Boolean function with a simplified constant value.
243+
*/
244+
BooleanFunction Shl(const std::vector<BooleanFunction::Value>& p0, const u16 p1)
245+
{
246+
if (p1 >= p0.size())
247+
{
248+
// Shift amount is too large, result is all zeros
249+
return BooleanFunction::Const(std::vector<BooleanFunction::Value>(p0.size(), BooleanFunction::Value::ZERO));
250+
}
251+
252+
std::vector<BooleanFunction::Value> result(p0.size(), BooleanFunction::Value::ZERO);
253+
254+
// Copy bits from original position to shifted position
255+
for (auto i = p1; i < p0.size(); i++)
256+
{
257+
result[i] = p0[i - p1];
258+
}
259+
260+
return BooleanFunction::Const(result);
261+
}
262+
263+
/**
264+
* Helper function to simplify a constant LSHR operation.
265+
*
266+
* @param[in] p0 - Boolean function parameter 0.
267+
* @param[in] p1 - Boolean function parameter 1.
268+
* @returns Boolean function with a simplified constant value.
269+
*/
270+
BooleanFunction Lshr(const std::vector<BooleanFunction::Value>& p0, const u16 p1)
271+
{
272+
if (p1 >= p0.size())
273+
{
274+
// Shift amount is too large, result is all zeros
275+
return BooleanFunction::Const(std::vector<BooleanFunction::Value>(p0.size(), BooleanFunction::Value::ZERO));
276+
}
277+
278+
std::vector<BooleanFunction::Value> result(p0.size(), BooleanFunction::Value::ZERO);
279+
280+
// Copy bits from original position to shifted position
281+
for (auto i = 0u; i < p0.size() - p1; i++)
282+
{
283+
result[i] = p0[i + p1];
284+
}
285+
286+
return BooleanFunction::Const(result);
287+
}
288+
289+
/**
290+
* Helper function to simplify a constant ASHR operation.
291+
*
292+
* @param[in] p0 - Boolean function parameter 0.
293+
* @param[in] p1 - Boolean function parameter 1.
294+
* @returns Boolean function with a simplified constant value.
295+
*/
296+
BooleanFunction Ashr(const std::vector<BooleanFunction::Value>& p0, const u16 p1)
297+
{
298+
auto sign_bit = p0.back(); // MSB is the sign bit
299+
300+
if (p1 >= p0.size())
301+
{
302+
// Shift amount is too large, result is all sign bits
303+
return BooleanFunction::Const(std::vector<BooleanFunction::Value>(p0.size(), sign_bit));
304+
}
305+
306+
std::vector<BooleanFunction::Value> result(p0.size(), sign_bit);
307+
308+
// Copy bits from original position to shifted position
309+
for (auto i = 0u; i < p0.size() - p1; i++)
310+
{
311+
result[i] = p0[i + p1];
312+
}
313+
314+
return BooleanFunction::Const(result);
315+
}
316+
317+
/**
318+
* Helper function to simplify a constant ROL operation.
319+
*
320+
* @param[in] p0 - Boolean function parameter 0.
321+
* @param[in] p1 - Boolean function parameter 1.
322+
* @returns Boolean function with a simplified constant value.
323+
*/
324+
BooleanFunction Rol(const std::vector<BooleanFunction::Value>& p0, const u16 p1)
325+
{
326+
auto rotate_amount = p1 % p0.size(); // Modulo for rotation
327+
328+
if (rotate_amount == 0)
329+
{
330+
return BooleanFunction::Const(p0); // No rotation needed
331+
}
332+
333+
std::vector<BooleanFunction::Value> result(p0.size());
334+
335+
// Perform the rotation
336+
for (auto i = 0u; i < p0.size(); i++)
337+
{
338+
auto new_pos = (i + rotate_amount) % p0.size();
339+
result[new_pos] = p0[i];
340+
}
341+
342+
return BooleanFunction::Const(result);
343+
}
344+
345+
/**
346+
* Helper function to simplify a constant ROR operation.
347+
*
348+
* @param[in] p0 - Boolean function parameter 0.
349+
* @param[in] p1 - Boolean function parameter 1.
350+
* @returns Boolean function with a simplified constant value.
351+
*/
352+
BooleanFunction Ror(const std::vector<BooleanFunction::Value>& p0, const u16 p1)
353+
{
354+
auto rotate_amount = p1 % p0.size(); // Modulo for rotation
355+
356+
if (rotate_amount == 0)
357+
{
358+
return BooleanFunction::Const(p0); // No rotation needed
359+
}
360+
361+
std::vector<BooleanFunction::Value> result(p0.size());
362+
363+
// Perform the rotation
364+
for (auto i = 0u; i < p0.size(); i++)
365+
{
366+
auto new_pos = (i + p0.size() - rotate_amount) % p0.size();
367+
result[new_pos] = p0[i];
368+
}
369+
370+
return BooleanFunction::Const(result);
371+
}
238372
/**
239373
* Helper function to simplify a constant SLE operation.
240374
*
@@ -1288,9 +1422,19 @@ namespace hal
12881422
}
12891423

12901424
std::vector<std::vector<BooleanFunction::Value>> values;
1425+
std::vector<u16> indices;
1426+
12911427
for (const auto& parameter : p)
12921428
{
1293-
values.emplace_back(parameter.get_top_level_node().constant);
1429+
if (parameter.is_index())
1430+
{
1431+
indices.push_back(parameter.get_index_value().get());
1432+
}
1433+
else
1434+
{
1435+
const auto v = parameter.get_top_level_node().constant;
1436+
values.emplace_back(v);
1437+
}
12941438
}
12951439

12961440
switch (node.type)
@@ -1311,15 +1455,32 @@ namespace hal
13111455
case BooleanFunction::NodeType::Mul:
13121456
return OK(ConstantPropagation::Mul(values[0], values[1]));
13131457

1314-
case BooleanFunction::NodeType::Slice: {
1315-
auto start = p[1].get_index_value().get();
1316-
auto end = p[2].get_index_value().get();
1317-
return OK(BooleanFunction::Const(std::vector<BooleanFunction::Value>(values[0].begin() + start, values[0].begin() + end + 1)));
1458+
case BooleanFunction::NodeType::Sdiv: {
1459+
// TODO implement
1460+
return ERR("could not propagate constants: not implemented for given node type");
1461+
}
1462+
case BooleanFunction::NodeType::Udiv: {
1463+
// TODO implement
1464+
return ERR("could not propagate constants: not implemented for given node type");
1465+
}
1466+
case BooleanFunction::NodeType::Srem: {
1467+
// TODO implement
1468+
return ERR("could not propagate constants: not implemented for given node type");
13181469
}
1470+
case BooleanFunction::NodeType::Urem: {
1471+
// TODO implement
1472+
return ERR("could not propagate constants: not implemented for given node type");
1473+
}
1474+
13191475
case BooleanFunction::NodeType::Concat: {
13201476
values[1].insert(values[1].end(), values[0].begin(), values[0].end());
13211477
return OK(BooleanFunction::Const(values[1]));
13221478
}
1479+
case BooleanFunction::NodeType::Slice: {
1480+
auto start = p[1].get_index_value().get();
1481+
auto end = p[2].get_index_value().get();
1482+
return OK(BooleanFunction::Const(std::vector<BooleanFunction::Value>(values[0].begin() + start, values[0].begin() + end + 1)));
1483+
}
13231484
case BooleanFunction::NodeType::Zext: {
13241485
values[0].resize(node.size, BooleanFunction::Value::ZERO);
13251486
return OK(BooleanFunction::Const(values[0]));
@@ -1329,6 +1490,17 @@ namespace hal
13291490
return OK(BooleanFunction::Const(values[0]));
13301491
}
13311492

1493+
case BooleanFunction::NodeType::Shl:
1494+
return OK(ConstantPropagation::Shl(values[0], indices[0]));
1495+
case BooleanFunction::NodeType::Lshr:
1496+
return OK(ConstantPropagation::Lshr(values[0], indices[0]));
1497+
case BooleanFunction::NodeType::Ashr:
1498+
return OK(ConstantPropagation::Ashr(values[0], indices[0]));
1499+
case BooleanFunction::NodeType::Rol:
1500+
return OK(ConstantPropagation::Rol(values[0], indices[0]));
1501+
case BooleanFunction::NodeType::Ror:
1502+
return OK(ConstantPropagation::Ror(values[0], indices[0]));
1503+
13321504
case BooleanFunction::NodeType::Eq:
13331505
return OK((values[0] == values[1]) ? BooleanFunction::Const(1, 1) : BooleanFunction::Const(0, 1));
13341506
case BooleanFunction::NodeType::Sle:
@@ -1342,14 +1514,6 @@ namespace hal
13421514
case BooleanFunction::NodeType::Ite:
13431515
return OK(ConstantPropagation::Ite(values[0], values[1], values[2]));
13441516

1345-
case BooleanFunction::NodeType::Sdiv:
1346-
// TODO implement
1347-
case BooleanFunction::NodeType::Udiv:
1348-
// TODO implement
1349-
case BooleanFunction::NodeType::Srem:
1350-
// TODO implement
1351-
case BooleanFunction::NodeType::Urem:
1352-
// TODO implement
13531517
default:
13541518
return ERR("could not propagate constants: not implemented for given node type");
13551519
}

0 commit comments

Comments
 (0)