[][src]Struct swar::Bits2

pub struct Bits2<N>(pub N);

This is used when every 2 bits is a number stored in parallel.

Methods

impl Bits2<u128>[src]

pub fn from_element(e: u128) -> Self[src]

Spread a single value out to each element. Must be able to fit.

use swar::*;

assert_eq!(Bits2::from_element(0b10), Bits2(0xAAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA));

pub fn any(self) -> Self[src]

If any bit is set in each element, sets the element to 1, else 0.

use swar::*;

let input = Bits2(0b01101100u128);
let output = Bits2(0b01010100u128);
assert_eq!(input.any(), output);

pub fn union(left: Bits4<u128>, right: Bits4<u128>) -> Self[src]

Takes two inputs that have half-sized elements and compresses them into half the space and puts them in the left and right sides of this.

use swar::*;

let input = Bits2(0xFEED_FACE_CAFE_BEEF_FEED_FACE_CAFE_BEEF);
let (left, right) = input.halve();
let output = Bits2::union(left, right);
assert_eq!(input, output);

pub fn squash(self) -> Bits1<u128>[src]

Squashes half-sized elements into half-sized spaces to the right and fills the left half of the number with zeros.

This is the same as union(0, n).

pub fn count_ones(self) -> u32[src]

pub fn pack_ones(self) -> Bits4<u128>[src]

Sqishes all the bits to the right in each 4-bit segment.

use swar::*;

let input = Bits2(0b11_01_01_00);
let out = Bits4(0b0111_0001);
assert_eq!(input.pack_ones(), out, "got {:08b} expected {:08b}", input.pack_ones().0, out.0);

let input = Bits2(0b11_11_00_00);
let out = Bits4(0b1111_0000);
assert_eq!(input.pack_ones(), out, "got {:08b} expected {:08b}", input.pack_ones().0, out.0);

let input = Bits2(0b01_01_00_01);
let out = Bits4(0b0011_0001);
assert_eq!(input.pack_ones(), out, "got {:08b} expected {:08b}", input.pack_ones().0, out.0);

let input = Bits2(0b11_00_00_11);
let out = Bits4(0b0011_0011);
assert_eq!(input.pack_ones(), out, "got {:08b} expected {:08b}", input.pack_ones().0, out.0);

pub fn sum_weight(self) -> u128[src]

pub fn sum_weight2(self) -> Bits4<u128>[src]

pub fn minhwd(self, other: Self) -> Self[src]

This computes the minimum hamming weight distance from hamming weights.

use swar::*;

// All combinations of inputs 0-2 (hamming weights)
let a = Bits2(0b00_01_10_00_01_10_00_01_10u128);
let b = Bits2(0b00_00_00_01_01_01_10_10_10u128);
// Expected output weights
let e = Bits2(0b00_01_10_01_00_01_10_01_00u128);

assert_eq!(a.minhwd(b), e, "got hamming distances {:b} expected {:b}", a.minhwd(b).0, e.0);

pub fn maxhwd(self, other: Self) -> Self[src]

This computes the maximum hamming weight distance from hamming weights.

use swar::*;

// All combinations of inputs 0-2 (hamming weights)
let a = Bits2(0b00_01_10_00_01_10_00_01_10u128);
let b = Bits2(0b00_00_00_01_01_01_10_10_10u128);
// Expected output weights
let e = Bits2(0b00_01_10_01_10_01_10_01_00u128);

assert_eq!(a.maxhwd(b), e, "got hamming distances {:b} expected {:b}", a.maxhwd(b).0, e.0);

pub fn split(self) -> (Bits4<u128>, Bits4<u128>)[src]

pub fn halve(self) -> (Bits4<u128>, Bits4<u128>)[src]

Takes the left and right sides and spreads them out so that the bits in each element are spread out into twice the amount of space.

use swar::*;

let input = Bits2(0b1101 << 64 | 0b0101u128);
let (left, right) = input.halve();
assert_eq!(left, Bits4(0b0011_0001));
assert_eq!(right, Bits4(0b0001_0001));

Trait Implementations

impl<N: Copy> Copy for Bits2<N>[src]

impl<N: Debug> Debug for Bits2<N>[src]

impl<N: PartialEq> PartialEq<Bits2<N>> for Bits2<N>[src]

impl<N: Eq> Eq for Bits2<N>[src]

impl Add<Bits2<u128>> for Bits2<u128>[src]

Note that you are responsible for dealing with overflow. Try to avoid overflow or use the split() method to add in two halves. You can use split() on the result to get the carry bits.

type Output = Self

The resulting type after applying the + operator.

impl BitAnd<u128> for Bits2<u128>[src]

type Output = Self

The resulting type after applying the & operator.

impl Shr<u32> for Bits2<u128>[src]

type Output = Self

The resulting type after applying the >> operator.

impl<N: Clone> Clone for Bits2<N>[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

Auto Trait Implementations

impl<N> Send for Bits2<N> where
    N: Send

impl<N> Sync for Bits2<N> where
    N: Sync

Blanket Implementations

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> From for T[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> FromCast for T[src]

impl<T, U> Cast for T where
    U: FromCast<T>, 
[src]