[][src]Crate swar

Computing hamming weights in parallel

use swar::*;
let n = 0xAAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAAu128;
let weight: u128 =
    Bits1(n).sum_weight2()
            .sum_weight2()
            .sum_weight2()
            .sum_weight2()
            .sum_weight2()
            .sum_weight2()
            .sum_weight2()
            .into();
assert_eq!(weight as u32, n.count_ones());

assert_eq!(Bits1(n).sum_weight2().sum_weight2().0,
           0x2222_2222_2222_2222_2222_2222_2222_2222);

assert_eq!(Bits1(n).sum_weight2().sum_weight2().split().0,
           Bits8(0x0202_0202_0202_0202_0202_0202_0202_0202));

Finding hamming weight differences in parallel

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 expected = Bits2(0b00_01_10_01_00_01_10_01_00u128);

assert_eq!(a.minhwd(b), expected);

Modules

u128

Structs

Bits1

This is used when each bit is a number stored in parallel.

Bits2

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

Bits4

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

Bits8

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

Bits16

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

Bits32

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

Bits64

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

Bits128

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