Skip to content

Commit

Permalink
Split into Rust crate
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn committed Jun 11, 2024
1 parent fcaa93e commit 914e88d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 52 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"bench-vortex",
"build-vortex",
"fastlanes",
"fastlanez",
"fastlanez-sys",
"pyvortex",
Expand Down
27 changes: 27 additions & 0 deletions fastlanes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "fastlanes"
version.workspace = true
homepage.workspace = true
repository.workspace = true
authors.workspace = true
license.workspace = true
keywords.workspace = true
include.workspace = true
edition.workspace = true
rust-version.workspace = true

[dependencies]
arrayref = { workspace = true }
num-traits = { workspace = true }
paste = { workspace = true }
seq-macro = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }

[lints]
workspace = true

[[bench]]
name = "fastlanes_bitpacking"
harness = false
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use std::mem::{size_of, MaybeUninit};
use std::mem::size_of;

use arrayref::{array_mut_ref, array_ref};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use fastlanez::{BitPack, BitPack2};
use fastlanes::BitPacking;

fn bitpacking(c: &mut Criterion) {
{
Expand All @@ -16,7 +16,7 @@ fn bitpacking(c: &mut Criterion) {
let mut packed = vec![0; 128 * WIDTH / size_of::<u16>()];

b.iter(|| {
BitPack2::<WIDTH>::bitpack(
BitPacking::bitpack::<WIDTH>(
array_ref![values, 0, 1024],
array_mut_ref![packed, 0, 192],
);
Expand All @@ -27,17 +27,7 @@ fn bitpacking(c: &mut Criterion) {
const WIDTH: usize = 3;
let values = [3u16; 1024];
let mut packed = [0; 128 * WIDTH / size_of::<u16>()];
b.iter(|| BitPack2::<WIDTH>::bitpack(&values, &mut packed));
});

group.bench_function("old pack 16 -> 3", |b| {
const WIDTH: usize = 3;
let values = [3u16; 1024];
let mut packed = [MaybeUninit::new(0u8); 128 * WIDTH];

b.iter(|| {
black_box(BitPack::<WIDTH>::pack(&values, &mut packed));
});
b.iter(|| BitPacking::bitpack::<WIDTH>(&values, &mut packed));
});
}

Expand All @@ -47,10 +37,10 @@ fn bitpacking(c: &mut Criterion) {
const WIDTH: usize = 3;
let values = [3u16; 1024];
let mut packed = [0; 128 * WIDTH / size_of::<u16>()];
BitPack2::<WIDTH>::bitpack(&values, &mut packed);
BitPacking::bitpack::<WIDTH>(&values, &mut packed);

let mut unpacked = [0u16; 1024];
b.iter(|| BitPack2::<WIDTH>::bitunpack(&packed, &mut unpacked));
b.iter(|| BitPacking::bitunpack::<WIDTH>(&packed, &mut unpacked));
});
}

Expand All @@ -60,23 +50,11 @@ fn bitpacking(c: &mut Criterion) {
const WIDTH: usize = 3;
let values = [3u16; 1024];
let mut packed = [0; 128 * WIDTH / size_of::<u16>()];
BitPack2::<WIDTH>::bitpack(&values, &mut packed);

b.iter(|| {
for i in 0..1024 {
black_box::<u16>(BitPack2::<WIDTH>::bitunpack_single(&packed, i));
}
});
});
group.bench_function("unpack single old 16 <- 3", |b| {
const WIDTH: usize = 3;
let values = [3u16; 1024];
let mut packed = [MaybeUninit::new(0u8); 128 * WIDTH];
let packed = BitPack::<WIDTH>::pack(&values, &mut packed);
BitPacking::bitpack::<WIDTH>(&values, &mut packed);

b.iter(|| {
for i in 0..1024 {
black_box::<u16>(BitPack::<WIDTH>::unpack_single(&packed, i));
black_box::<u16>(BitPacking::bitunpack_single::<WIDTH>(&packed, i));
}
});
});
Expand Down
22 changes: 8 additions & 14 deletions fastlanez/src/fl.rs → fastlanes/src/bitpacking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ use num_traits::{One, PrimInt, Unsigned};
use paste::paste;
use seq_macro::seq;

use crate::FastLanes;
use crate::{Pred, Satisfied};

pub const ORDER: [u8; 8] = [0, 4, 2, 6, 1, 5, 3, 7];

pub trait FastLanes: Sized + Unsigned + PrimInt {
const T: usize = size_of::<Self>() * 8;
const LANES: usize = 1024 / Self::T;
}

pub struct BitPackWidth<const W: usize>;
pub trait SupportedBitPackWidth<T> {}
impl<const W: usize, T> SupportedBitPackWidth<T> for BitPackWidth<W>
Expand All @@ -23,7 +17,7 @@ where
}

/// BitPack into a compile-time known bit-width.
pub trait BitPack2: FastLanes {
pub trait BitPacking: FastLanes {
/// Packs 1024 elements into W bits each.
/// The output is given as Self to ensure correct alignment.
fn bitpack<const W: usize>(
Expand Down Expand Up @@ -56,7 +50,7 @@ macro_rules! seq_type_width {
}

#[inline]
fn mask<T: PrimInt + Unsigned + One>(width: usize) -> T {
pub(crate) fn mask<T: PrimInt + Unsigned + One>(width: usize) -> T {
(T::one() << width) - T::one()
}

Expand All @@ -67,7 +61,7 @@ macro_rules! impl_bitpacking {
paste! {
impl FastLanes for $T {}

impl BitPack2 for $T {
impl BitPacking for $T {
#[inline(never)] // Makes it easier to disassemble and validate ASM.
#[allow(unused_assignments)] // Inlined loop gives unused assignment on final iteration
fn bitpack<const W: usize>(
Expand Down Expand Up @@ -201,10 +195,10 @@ mod test {
}

let mut packed = [0; 128 * $W / size_of::<$T>()];
BitPack2::bitpack::<$W>(&values, &mut packed);
BitPacking::bitpack::<$W>(&values, &mut packed);

let mut unpacked = [0; 1024];
BitPack2::bitunpack::<$W>(&packed, &mut unpacked);
BitPacking::bitunpack::<$W>(&packed, &mut unpacked);

assert_eq!(&unpacked, &values);
}
Expand All @@ -217,10 +211,10 @@ mod test {
}

let mut packed = [0; 128 * $W / size_of::<$T>()];
BitPack2::bitpack::<$W>(&values, &mut packed);
BitPacking::bitpack::<$W>(&values, &mut packed);

for (idx, value) in values.into_iter().enumerate() {
assert_eq!(BitPack2::bitunpack_single::<$W>(&packed, idx), value);
assert_eq!(BitPacking::bitunpack_single::<$W>(&packed, idx), value);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions fastlanes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use std::mem::size_of;

use num_traits::{PrimInt, Unsigned};

mod bitpacking;
pub use bitpacking::*;

pub const ORDER: [u8; 8] = [0, 4, 2, 6, 1, 5, 3, 7];

pub trait FastLanes: Sized + Unsigned + PrimInt {
const T: usize = size_of::<Self>() * 8;
const LANES: usize = 1024 / Self::T;
}

pub struct Pred<const B: bool>;

pub trait Satisfied {}

impl Satisfied for Pred<true> {}
7 changes: 0 additions & 7 deletions fastlanez/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,3 @@ num-traits = { workspace = true }
paste = { workspace = true }
seq-macro = { workspace = true }
uninit = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }

[[bench]]
name = "fastlanes_bitpacking"
harness = false

0 comments on commit 914e88d

Please sign in to comment.