Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ library with support for 3-valued logic: 0,1,X

Tags:

c++

tribool

I am working on a simulator for a microprocessor, written in C++.

I am looking for a way to model state elements in the hardware that have just been powered up and have not yet been reset in any way. A real state element would have an unknown value that is either 0 or 1, but in software models this is commonly modeled as an X, meaning unknown.

I am looking for a library in C++ that can model these X values, including their propagation. That is, it would have to know how to handle logical and arithmetical operations with Xes:

1 AND X = X
0 AND X = 0
1  +  X = X

etc...

Is there any such library that is both stable and fast?

Edit:

I neglected to mention that my current code works with bitvectors. More accurately, I use the standard uint_*t data types, and these are the ones I want to replace. Whatever library I use, it must support arithmetic, shifts and logical operators for it to be useful.

like image 705
Nathan Fellman Avatar asked Oct 06 '10 13:10

Nathan Fellman


1 Answers

Try Boost.Tribool.

The tribool class acts like the built-in bool type, but for 3-state boolean logic. The three states are true, false, and indeterminate, where the first two states are equivalent to those of the C++ bool type and the last state represents an unknown boolean value (that may be true or false, we don't know).

You can see the test suit and the header documentation for the rules this class supports.

Boost libraries are pretty high quality and well maintained, so you don't need to worry about its stability. And "fast"... well it's hard to be slow for simple classes like this :). The operations are implemented with 2 to 3 integer comparison with 1 or 2 if clauses so it should be efficient enough.

like image 153
kennytm Avatar answered Oct 17 '22 10:10

kennytm