Skip to content

Commit f3415ce

Browse files
committed
Add ISOP expression printing to print.hpp
SOP expressions are useful when constructing the GENLIB library for technology mapping
1 parent 1b2f49f commit f3415ce

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

include/kitty/print.hpp

+89
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
\brief Implements functions to print truth tables
2929
3030
\author Mathias Soeken
31+
\author Rassul Bairamkulov
3132
*/
3233

3334
#pragma once
@@ -45,6 +46,7 @@
4546
#include "karnaugh_map.hpp"
4647
#include "operations.hpp"
4748
#include "constructors.hpp"
49+
#include "isop.hpp"
4850

4951
namespace kitty
5052
{
@@ -478,4 +480,91 @@ std::string anf_to_expression( const ternary_truth_table<TT>& anf )
478480
return anf_to_expression( anf._bits );
479481
}
480482

483+
/*! \brief Creates an expression in the sum of products format.
484+
485+
Does not perform any optimizations. Useful when constructing GENLIB-compatible expressions
486+
487+
\param tt Truth table
488+
\param os Output stream
489+
*/
490+
template<typename TT, typename = std::enable_if_t<is_completely_specified_truth_table<TT>::value>>
491+
void print_sop_expression( TT tt, std::ostream& os = std::cout )
492+
{
493+
/* compare to constants */
494+
if ( is_const0( tt ) )
495+
{
496+
os << "CONST0";
497+
return;
498+
}
499+
else if ( is_const0( ~tt ) )
500+
{
501+
os << "CONST1";
502+
return;
503+
}
504+
505+
/* extract product terms */
506+
auto cubes = kitty::isop( tt );
507+
508+
bool first_cube = true; // Controls insertion of '|'. It's false for the first cube to avoid leading '|'.
509+
510+
/* write product terms */
511+
for ( auto cube : cubes )
512+
{
513+
auto bits = cube._bits;
514+
auto mask = cube._mask;
515+
516+
bool brackets = __builtin_popcount( mask ) > 1;
517+
518+
if ( !first_cube )
519+
{
520+
os << '|';
521+
}
522+
523+
if ( brackets )
524+
{
525+
os << '(';
526+
}
527+
528+
bool first_literal = true;
529+
for ( auto i = 0u; i < tt.num_vars(); ++i )
530+
{
531+
if ( mask & 1 )
532+
{
533+
if ( !first_literal )
534+
{
535+
os << '&';
536+
}
537+
if ( !( bits & 1 ) )
538+
{
539+
os << '!';
540+
}
541+
os << static_cast<char>( 'a' + i );
542+
first_literal = false;
543+
}
544+
bits >>= 1;
545+
mask >>= 1;
546+
}
547+
548+
if ( brackets )
549+
{
550+
os << ')';
551+
}
552+
first_cube = false;
553+
}
554+
}
555+
556+
/*! \brief Creates an expression in the sum of products format.
557+
558+
Does not perform any optimizations. Useful when constructing GENLIB-compatible expressions
559+
560+
\param tt Truth table
561+
*/
562+
template<typename TT, typename = std::enable_if_t<is_completely_specified_truth_table<TT>::value>>
563+
std::string to_sop_expression( TT tt )
564+
{
565+
std::stringstream os;
566+
print_sop_expression( tt, os ); // Use the function to write into the stringstream
567+
return os.str(); // Convert the stringstream to string and return it
568+
}
569+
481570
} /* namespace kitty */

0 commit comments

Comments
 (0)