13#ifndef CIRCT_SUPPORT_TRUTHTABLE_H
14#define CIRCT_SUPPORT_TRUTHTABLE_H
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Support/raw_ostream.h"
51 const llvm::APInt &
table)
54 "Truth table size mismatch");
63 llvm::APInt
getOutput(
const llvm::APInt &input)
const;
66 void setOutput(
const llvm::APInt &input,
const llvm::APInt &output);
88 void dump(llvm::raw_ostream &os = llvm::errs())
const;
119 unsigned inputNeg,
unsigned outputNeg)
146 llvm::SmallVectorImpl<unsigned> &permutation)
const;
164 void dump(llvm::raw_ostream &os = llvm::errs())
const;
189 llvm::SmallVectorImpl<uint16_t> &representatives);
214llvm::APInt
createVarMask(
unsigned numVars,
unsigned varIndex,
bool positive);
221 ArrayRef<unsigned> inputMapping,
222 unsigned numExpandedInputs);
241std::pair<llvm::APInt, llvm::APInt>
260 unsigned size()
const {
return llvm::popcount(
mask); }
264 return mask & (1ULL << varIndex);
268 uint64_t varMask = 1ULL << varIndex;
274 uint64_t varMask = 1ULL << varIndex;
280 return inverted & (1ULL << varIndex);
296 void dump(llvm::raw_ostream &os = llvm::errs())
const;
assert(baseType &&"element must be base type")
std::array< Entry4, 1u<< 16 > entries4
bool lookup(const BinaryTruthTable &tt, NPNClass &result) const
Returns false if the given truth table shape is unsupported.
llvm::APInt expandTruthTableToInputSpace(const llvm::APInt &tt, ArrayRef< unsigned > inputMapping, unsigned numExpandedInputs)
Expand a truth table to a larger input space using the given input mapping.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
SOPForm extractISOP(const llvm::APInt &truthTable, unsigned numVars)
Extract ISOP (Irredundant Sum-of-Products) from a truth table.
std::pair< llvm::APInt, llvm::APInt > computeCofactors(const llvm::APInt &f, unsigned numVars, unsigned varIndex)
Compute cofactor of a Boolean function for a given variable.
llvm::APInt createVarMask(unsigned numVars, unsigned varIndex, bool positive)
Create a mask for a variable in the truth table.
void collectCanonicalNPN4Representatives(llvm::SmallVectorImpl< uint16_t > &representatives)
Collect all canonical 4-input single-output NPN representatives in ascending truth-table order.
Represents a boolean function as a truth table.
BinaryTruthTable()
Default constructor creates an empty truth table.
BinaryTruthTable(unsigned numInputs, unsigned numOutputs)
Constructor for a truth table with given dimensions, initialized to zero.
BinaryTruthTable applyOutputNegation(unsigned negation) const
Apply output negation to create a new truth table.
llvm::APInt table
Truth table data as a packed bit vector.
BinaryTruthTable applyInputNegation(unsigned mask) const
Apply input negation to create a new truth table.
unsigned numInputs
Number of inputs for this boolean function.
BinaryTruthTable(unsigned numInputs, unsigned numOutputs, const llvm::APInt &table)
Constructor for a truth table with given dimensions and evaluation data.
void dump(llvm::raw_ostream &os=llvm::errs()) const
Debug dump method for truth tables.
bool isLexicographicallySmaller(const BinaryTruthTable &other) const
Check if this truth table is lexicographically smaller than another.
bool operator==(const BinaryTruthTable &other) const
Equality comparison for truth tables.
llvm::APInt getOutput(const llvm::APInt &input) const
Get the output value for a given input combination.
BinaryTruthTable applyPermutation(ArrayRef< unsigned > permutation) const
Apply input permutation to create a new truth table.
void setOutput(const llvm::APInt &input, const llvm::APInt &output)
Set the output value for a given input combination.
unsigned numOutputs
Number of outputs for this boolean function.
bool hasLiteral(unsigned varIndex) const
Has literal for variable at index varIndex.
unsigned size() const
Get number of literals in this cube.
void removeLiteral(unsigned varIndex)
uint64_t inverted
Bitmask indicating which variables are negated.
bool isLiteralInverted(unsigned varIndex) const
void setLiteral(unsigned varIndex, bool isInverted)
uint64_t mask
Bitmask indicating which variables appear in this cube.
Represents the canonical form of a boolean function under NPN equivalence.
NPNClass(const BinaryTruthTable &tt)
Constructor from a truth table.
unsigned outputNegation
Output negation mask.
static NPNClass computeNPNCanonicalForm(const BinaryTruthTable &tt)
Compute the canonical NPN form for a given truth table.
void getInputPermutation(const NPNClass &targetNPN, llvm::SmallVectorImpl< unsigned > &permutation) const
Get input permutation from this NPN class to another equivalent NPN class.
BinaryTruthTable truthTable
Canonical truth table.
void dump(llvm::raw_ostream &os=llvm::errs()) const
Debug dump method for NPN classes.
NPNClass()=default
Default constructor creates an empty NPN class.
bool equivalentOtherThanPermutation(const NPNClass &other) const
Equality comparison for NPN classes.
llvm::SmallVector< unsigned > inputPermutation
Input permutation applied.
unsigned inputNegation
Input negation mask.
bool isLexicographicallySmaller(const NPNClass &other) const
NPNClass(const BinaryTruthTable &tt, llvm::SmallVector< unsigned > inputPerm, unsigned inputNeg, unsigned outputNeg)
std::array< uint8_t, 4 > inputPermutation