Skip to content

Instantly share code, notes, and snippets.

@LeeMetaX
Created October 23, 2025 19:16
Show Gist options
  • Select an option

  • Save LeeMetaX/b5cee8f18e62d0fa2acfa9de16199941 to your computer and use it in GitHub Desktop.

Select an option

Save LeeMetaX/b5cee8f18e62d0fa2acfa9de16199941 to your computer and use it in GitHub Desktop.
Boolean 4 state logic wrapped inside package SIMD vectors
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum State { Z, X, Zero, One }
impl State {
#[inline(always)]
pub fn from_bool(b: Option<bool>) -> Self {
match b {
None => State::X, // undefined / faulted
Some(true) => State::One,
Some(false) => State::Zero,
}
}
#[inline(always)]
pub fn and(self, rhs: Self) -> Self {
use State::*;
match (self, rhs) {
(Z, _) | (_, Z) => Z,
(X, _) | (_, X) => X,
(One, One) => One,
(Zero, _) | (_, Zero) => Zero,
}
}
#[inline(always)]
pub fn or(self, rhs: Self) -> Self {
use State::*;
match (self, rhs) {
(Z, s) | (s, Z) => s,
(X, _) | (_, X) => X,
(One, _) | (_, One) => One,
(Zero, Zero) => Zero,
}
}
#[inline(always)]
pub fn not(self) -> Self {
use State::*;
match self {
Z => Z,
X => X,
One => Zero,
Zero => One,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment