From 82abfced05b30b7c145b937aa5143c0a58a94aca Mon Sep 17 00:00:00 2001 From: Andrea Costamagna Date: Wed, 30 Jul 2025 15:52:40 +0200 Subject: [PATCH] feat: update ternary truth tables to handle don't care conditions --- include/kitty/bit_operations.hpp | 6 ++++++ include/kitty/operations.hpp | 36 +++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/include/kitty/bit_operations.hpp b/include/kitty/bit_operations.hpp index 51a089a..e43e310 100644 --- a/include/kitty/bit_operations.hpp +++ b/include/kitty/bit_operations.hpp @@ -354,6 +354,12 @@ inline uint64_t count_ones( const static_truth_table& tt ) { return __builtin_popcountll( tt._bits ); } + +template +inline uint64_t count_ones( const ternary_truth_table& tt ) +{ + return count_ones( tt._bits & tt._care ); +} /*! \endcond */ /*! \brief Count zeros in truth table diff --git a/include/kitty/operations.hpp b/include/kitty/operations.hpp index 1faeee3..312120d 100644 --- a/include/kitty/operations.hpp +++ b/include/kitty/operations.hpp @@ -531,10 +531,28 @@ inline bool equal( const partial_truth_table& first, const partial_truth_table& return binary_predicate( first, second, std::equal_to<>() ); } /*! \endcond */ -template +/*! \brief Checks whether two incompletely specified truth tables are equal + + The template parameter UseDCs allows us to decide if to check for possible assignment + of the don't cares to achieve equality: + - UseDCs = false : Checks if both the careset and the onset coincide + - UseDCs = true : Checks if there is an assignment of the don't cares making the functions equal. + + \param first First truth table + \param second Second truth table +*/ +template inline bool equal( const ternary_truth_table& first, const ternary_truth_table& second ) { - return equal( first._bits, second._bits ) && equal( first._care, second._care ); + if constexpr ( UseDCs ) + { + const auto care_mask = first._care & second._care; + return equal( first._bits & care_mask, second._bits & care_mask ); + } + else + { + return equal( first._bits, second._bits ) && equal( first._care, second._care ); + } } template @@ -645,12 +663,20 @@ inline bool is_const0( const static_truth_table& tt ) \param tt Truth table */ -template +template inline bool is_const0( const ternary_truth_table& tt ) { - return is_const0( tt._bits | ~tt._care ); + if constexpr ( UseDCs ) + { + return is_const0( tt._bits & tt._care ); + } + else + { + return is_const0( tt._bits | ~tt._care ); + } } + /*! \brief Checks whether a quaternary truth table is constant composed by only - and 0. \param tt Truth table @@ -2526,4 +2552,4 @@ inline TT shift_with_mask( const TT& f, uint8_t mask ) return copy; } -} // namespace kitty +} // namespace kitty \ No newline at end of file