diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/bit_field/benches | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/bit_field/benches')
-rw-r--r-- | vendor/bit_field/benches/bench.rs | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/vendor/bit_field/benches/bench.rs b/vendor/bit_field/benches/bench.rs new file mode 100644 index 0000000..676461b --- /dev/null +++ b/vendor/bit_field/benches/bench.rs @@ -0,0 +1,233 @@ +#![feature(test)] + +extern crate bit_field; + +use bit_field::*; + +pub trait BitOper { + const BIT_LEN: usize; + fn get_b(&self, idx: usize) -> bool; + fn set_b(&mut self, idx: usize, val: bool); + fn toggle(&mut self, idx: usize); +} + +pub trait BitArrayOper<T: BitOper> { + fn get_blen(&self) -> usize; + fn get_b(&self, idx: usize) -> bool; + fn set_b(&mut self, idx: usize, val: bool); + fn toggle(&mut self, idx: usize); +} + +impl BitOper for u8 { + const BIT_LEN: usize = std::mem::size_of::<Self>() as usize * 8; + + fn set_b(&mut self, idx: usize, val: bool) { + assert!(idx < Self::BIT_LEN); + if val { + *self |= 1 << idx; + } else { + *self &= !(1 << idx); + } + } + + fn get_b(&self, idx: usize) -> bool { + assert!(idx < Self::BIT_LEN); + (self & 1 << idx) != 0 + } + + fn toggle(&mut self, idx: usize) { + assert!(idx < Self::BIT_LEN); + *self ^= 1 << idx; + } +} + +impl BitOper for u32 { + const BIT_LEN: usize = std::mem::size_of::<Self>() as usize * 8; + fn set_b(&mut self, idx: usize, val: bool) { + assert!(idx < Self::BIT_LEN); + if val { + *self |= 1 << idx; + } else { + *self &= !(1 << idx); + } + } + + fn get_b(&self, idx: usize) -> bool { + assert!(idx < Self::BIT_LEN); + (self & 1 << idx) != 0 + } + + fn toggle(&mut self, idx: usize) { + assert!(idx < Self::BIT_LEN); + *self ^= 1 << idx; + } +} + +impl BitOper for u64 { + const BIT_LEN: usize = std::mem::size_of::<Self>() as usize * 8; + fn set_b(&mut self, idx: usize, val: bool) { + assert!(idx < Self::BIT_LEN); + if val { + *self |= 1 << idx; + } else { + *self &= !(1 << idx); + } + } + + fn get_b(&self, idx: usize) -> bool { + assert!(idx < Self::BIT_LEN); + (self & 1 << idx) != 0 + } + + fn toggle(&mut self, idx: usize) { + assert!(idx < Self::BIT_LEN); + *self ^= 1 << idx; + } +} + +impl<T: BitOper> BitArrayOper<T> for [T] { + fn get_blen(&self) -> usize { + self.len() * T::BIT_LEN + } + + fn get_b(&self, idx: usize) -> bool { + self[idx / T::BIT_LEN].get_b(idx % T::BIT_LEN) + } + + fn set_b(&mut self, idx: usize, val: bool) { + self[idx / T::BIT_LEN].set_b(idx % T::BIT_LEN, val); + } + + fn toggle(&mut self, idx: usize) { + self[idx / T::BIT_LEN].toggle(idx % T::BIT_LEN); + } +} + +extern crate test; + +use test::Bencher; + +const LEN: usize = 256; + +fn set_bitfield<T: BitField>(v: &mut Vec<T>) { + for i in 0..v.len() * T::BIT_LENGTH { + v.as_mut_slice().set_bit(i, true);; + } +} + +fn get_bitfield<T: BitField>(v: &Vec<T>) { + for i in 0..v.len() * T::BIT_LENGTH { + let _b = v.as_slice().get_bit(i); + } +} + +fn set_trivial<T: BitOper>(v: &mut Vec<T>) { + for i in 0..v.len() * T::BIT_LEN { + v.set_b(i, true); + } +} + +fn get_trivial<T: BitOper>(v: &Vec<T>) { + for i in 0..v.len() * T::BIT_LEN { + let _b = v.get_b(i); + } +} + +#[bench] +fn u8_set_bitfield(b: &mut Bencher) { + let mut v = vec![0u8; LEN]; + b.iter(|| { + set_bitfield(&mut v); + }); +} + +#[bench] +fn u8_set_trivial(b: &mut Bencher) { + let mut v = vec![0u8; LEN]; + + b.iter(|| { + set_trivial(&mut v); + }); +} + +#[bench] +fn u8_get_bitfield(b: &mut Bencher) { + let v = vec![1u8; LEN]; + b.iter(|| { + get_bitfield(&v); + }); +} + +#[bench] +fn u8_get_trivial(b: &mut Bencher) { + let v = vec![1u8; LEN]; + b.iter(|| { + get_trivial(&v); + }); +} + +#[bench] +fn u32_set_bitfield(b: &mut Bencher) { + let mut v = vec![0u32; LEN]; + b.iter(|| { + set_bitfield(&mut v); + }); +} + +#[bench] +fn u32_set_trivial(b: &mut Bencher) { + let mut v = vec![0u32; LEN]; + + b.iter(|| { + set_trivial(&mut v); + }); +} + +#[bench] +fn u32_get_bitfield(b: &mut Bencher) { + let v = vec![1u32; LEN]; + b.iter(|| { + get_bitfield(&v); + }); +} + +#[bench] +fn u32_get_trivial(b: &mut Bencher) { + let v = vec![1u32; LEN]; + b.iter(|| { + get_trivial(&v); + }); +} + +#[bench] +fn u64_set_bitfield(b: &mut Bencher) { + let mut v = vec![0u64; LEN]; + b.iter(|| { + set_bitfield(&mut v); + }); +} + +#[bench] +fn u64_set_trivial(b: &mut Bencher) { + let mut v = vec![0u64; LEN]; + + b.iter(|| { + set_trivial(&mut v); + }); +} + +#[bench] +fn u64_get_bitfield(b: &mut Bencher) { + let v = vec![1u64; LEN]; + b.iter(|| { + get_bitfield(&v); + }); +} + +#[bench] +fn u64_get_trivial(b: &mut Bencher) { + let v = vec![1u64; LEN]; + b.iter(|| { + get_trivial(&v); + }); +} |