From 238b03a7172cfe66854597e94903acefc49e380a Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 12:45:44 -0700 Subject: [PATCH 01/13] Force no_std --- src/lib.rs | 47 ++++++++++++++++++++--------------------------- src/range.rs | 4 +--- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 460e0c0..6636863 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,21 +12,14 @@ //! This version of fixedbitset requires Rust 1.39 or later. //! #![doc(html_root_url = "https://docs.rs/fixedbitset/0.4.2/")] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![forbid(clippy::undocumented_unsafe_blocks)] -#[cfg(not(feature = "std"))] extern crate alloc; -#[cfg(not(feature = "std"))] use alloc::{ vec, vec::{IntoIter, Vec}, }; -#[cfg(feature = "std")] -use std::vec::IntoIter; - -#[cfg(not(feature = "std"))] -use core as std; mod range; @@ -35,17 +28,17 @@ extern crate serde; #[cfg(feature = "serde")] mod serde_impl; -use std::fmt::Write; -use std::fmt::{Binary, Display, Error, Formatter}; +use core::fmt::Write; +use core::fmt::{Binary, Display, Error, Formatter}; pub use range::IndexRange; -use std::cmp::{Ord, Ordering}; -use std::iter::{Chain, ExactSizeIterator, FromIterator, FusedIterator}; -use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index}; +use core::cmp::{Ord, Ordering}; +use core::iter::{Chain, ExactSizeIterator, FromIterator, FusedIterator}; +use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index}; -pub(crate) const BITS: usize = std::mem::size_of::() * 8; +pub(crate) const BITS: usize = core::mem::size_of::() * 8; #[cfg(feature = "serde")] -pub(crate) const BYTES: usize = std::mem::size_of::(); +pub(crate) const BYTES: usize = core::mem::size_of::(); pub type Block = usize; @@ -620,7 +613,7 @@ impl FixedBitSet { for (x, y) in self.data.iter_mut().zip(other.data.iter()) { *x &= *y; } - let mn = std::cmp::min(self.data.len(), other.data.len()); + let mn = core::cmp::min(self.data.len(), other.data.len()); for wd in &mut self.data[mn..] { *wd = 0; } @@ -927,7 +920,7 @@ pub struct Ones<'a> { bitset_back: Block, block_idx_front: usize, block_idx_back: usize, - remaining_blocks: std::slice::Iter<'a, Block>, + remaining_blocks: core::slice::Iter<'a, Block>, } impl<'a> Ones<'a> { @@ -1039,7 +1032,7 @@ pub struct Zeroes<'a> { bitset: Block, block_idx: usize, len: usize, - remaining_blocks: std::slice::Iter<'a, Block>, + remaining_blocks: core::slice::Iter<'a, Block>, } impl<'a> Iterator for Zeroes<'a> { @@ -1247,7 +1240,7 @@ impl<'a> BitAnd for &'a FixedBitSet { for (data, block) in data.iter_mut().zip(long.iter()) { *data &= *block; } - let len = std::cmp::min(self.len(), other.len()); + let len = core::cmp::min(self.len(), other.len()); FixedBitSet { data, length: len } } } @@ -1278,7 +1271,7 @@ impl<'a> BitOr for &'a FixedBitSet { for (data, block) in data.iter_mut().zip(short.iter()) { *data |= *block; } - let len = std::cmp::max(self.len(), other.len()); + let len = core::cmp::max(self.len(), other.len()); FixedBitSet { data, length: len } } } @@ -1309,7 +1302,7 @@ impl<'a> BitXor for &'a FixedBitSet { for (data, block) in data.iter_mut().zip(short.iter()) { *data ^= *block; } - let len = std::cmp::max(self.len(), other.len()); + let len = core::cmp::max(self.len(), other.len()); FixedBitSet { data, length: len } } } @@ -1761,7 +1754,7 @@ mod tests { fn bitand_first_smaller() { let a_len = 113; let b_len = 137; - let len = std::cmp::min(a_len, b_len); + let len = core::cmp::min(a_len, b_len); let a_end = 97; let b_start = 89; let mut a = FixedBitSet::with_capacity(a_len); @@ -1785,7 +1778,7 @@ mod tests { fn bitand_first_larger() { let a_len = 173; let b_len = 137; - let len = std::cmp::min(a_len, b_len); + let len = core::cmp::min(a_len, b_len); let a_end = 107; let b_start = 43; let mut a = FixedBitSet::with_capacity(a_len); @@ -2017,7 +2010,7 @@ mod tests { fn bitxor_first_smaller() { let a_len = 113; let b_len = 137; - let len = std::cmp::max(a_len, b_len); + let len = core::cmp::max(a_len, b_len); let a_end = 97; let b_start = 89; let mut a = FixedBitSet::with_capacity(a_len); @@ -2041,7 +2034,7 @@ mod tests { fn bitxor_first_larger() { let a_len = 173; let b_len = 137; - let len = std::cmp::max(a_len, b_len); + let len = core::cmp::max(a_len, b_len); let a_end = 107; let b_start = 43; let mut a = FixedBitSet::with_capacity(a_len); @@ -2296,8 +2289,8 @@ mod tests { let items: Vec = vec![1, 5, 7, 10, 14, 15]; let fb = items.iter().cloned().collect::(); - assert_eq!(format!("{:b}", fb), "0100010100100011"); - assert_eq!(format!("{:#b}", fb), "0b0100010100100011"); + assert_eq!(alloc::format!("{:b}", fb), "0100010100100011"); + assert_eq!(alloc::format!("{:#b}", fb), "0b0100010100100011"); } #[cfg(feature = "std")] diff --git a/src/range.rs b/src/range.rs index 738b869..9b385c4 100644 --- a/src/range.rs +++ b/src/range.rs @@ -1,6 +1,4 @@ -#[cfg(not(feature = "std"))] -use core as std; -use std::ops::{Range, RangeFrom, RangeFull, RangeTo}; +use core::ops::{Range, RangeFrom, RangeFull, RangeTo}; // Taken from https://github.com/bluss/odds/blob/master/src/range.rs. From 378a7bf8728c7b38ba9dadfe2e107691b0349eba Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 13:54:37 -0700 Subject: [PATCH 02/13] Use SIMD blocks instead of usize --- src/block/avx2.rs | 214 +++++++++++++++++++++++++++++++++++++++++ src/block/default.rs | 102 ++++++++++++++++++++ src/block/mod.rs | 35 +++++++ src/block/sse2.rs | 221 +++++++++++++++++++++++++++++++++++++++++++ src/block/wasm32.rs | 199 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 183 ++++++++++++++++++++--------------- 6 files changed, 880 insertions(+), 74 deletions(-) create mode 100644 src/block/avx2.rs create mode 100644 src/block/default.rs create mode 100644 src/block/mod.rs create mode 100644 src/block/sse2.rs create mode 100644 src/block/wasm32.rs diff --git a/src/block/avx2.rs b/src/block/avx2.rs new file mode 100644 index 0000000..7f0d84c --- /dev/null +++ b/src/block/avx2.rs @@ -0,0 +1,214 @@ +#[cfg(target_arch = "x86")] +use core::arch::x86::*; +#[cfg(target_arch = "x86_64")] +use core::arch::x86_64::*; +use core::{ + cmp::Ordering, + hash::{Hash, Hasher}, + iter::Iterator, + ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}, +}; + +#[derive(Copy, Clone, Debug)] +#[repr(transparent)] +pub struct Block(__m256i); + +impl Block { + const _ASSERTION: () = { + if core::mem::size_of::() % core::mem::size_of::() != 0 { + panic!("vector is not a multiple size of usize"); + } + }; + + pub const USIZE_COUNT: usize = core::mem::size_of::() / core::mem::size_of::(); + pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); + pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); + pub const BITS: usize = core::mem::size_of::() * 8; + + #[inline] + fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { + unsafe { core::mem::transmute(self.0) } + } + + #[inline] + const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + Self(unsafe { core::mem::transmute(array) }) + } + + #[inline] + pub fn create_buffer(iter: impl Iterator) -> Vec { + let (lower, _) = iter.size_hint(); + let mut output = Vec::with_capacity(lower / Self::USIZE_COUNT); + let mut buffer = [0; Self::USIZE_COUNT]; + let mut index = 0; + for chunk in iter { + buffer[index] = chunk; + index += 1; + if index >= Self::USIZE_COUNT { + output.push(Self::from_usize_array(buffer)); + index = 0; + } + } + if index != 0 { + #[allow(clippy::needless_range_loop)] + for idx in index..Self::USIZE_COUNT { + buffer[idx] = 0; + } + output.push(Self::from_usize_array(buffer)); + } + output + } + + #[inline] + pub fn is_empty(self) -> bool { + unsafe { _mm256_testz_si256(self.0, self.0) == 1 } + } + + #[inline] + pub fn count_ones(self) -> u32 { + unsafe { + let array: [usize; Self::USIZE_COUNT] = core::mem::transmute(self.0); + array.iter().copied().map(usize::count_ones).sum() + } + } + + #[inline] + pub fn upper_mask(mut size: usize) -> Self { + let res = if size >= 128 { + size -= 128; + [0, core::u128::MAX << size] + } else { + [core::u128::MAX << size, core::u128::MAX] + }; + Self(unsafe { core::mem::transmute(res) }) + } + + #[inline] + pub fn lower_mask(mut size: usize) -> Self { + let res = if size >= 128 { + size -= 128; + [core::u128::MAX, (core::u128::MAX >> 1) >> (128 - size - 1)] + } else { + [(core::u128::MAX >> 1) >> (128 - size - 1), 0] + }; + Self(unsafe { core::mem::transmute(res) }) + } + + #[inline] + pub fn andnot(self, other: Self) -> Self { + Self(unsafe { _mm256_andnot_si256(other.0, self.0) }) + } +} + +impl Not for Block { + type Output = Block; + #[inline] + fn not(self) -> Self::Output { + unsafe { Self(_mm256_xor_si256(self.0, Self::ALL.0)) } + } +} + +impl BitAnd for Block { + type Output = Block; + #[inline] + fn bitand(self, other: Self) -> Self::Output { + unsafe { Self(_mm256_and_si256(self.0, other.0)) } + } +} + +impl BitAndAssign for Block { + #[inline] + fn bitand_assign(&mut self, other: Self) { + unsafe { + self.0 = _mm256_and_si256(self.0, other.0); + } + } +} + +impl BitOr for Block { + type Output = Block; + #[inline] + fn bitor(self, other: Self) -> Self::Output { + unsafe { Self(_mm256_or_si256(self.0, other.0)) } + } +} + +impl BitOrAssign for Block { + #[inline] + fn bitor_assign(&mut self, other: Self) { + unsafe { + self.0 = _mm256_or_si256(self.0, other.0); + } + } +} + +impl BitXor for Block { + type Output = Block; + #[inline] + fn bitxor(self, other: Self) -> Self::Output { + unsafe { Self(_mm256_xor_si256(self.0, other.0)) } + } +} + +impl BitXorAssign for Block { + #[inline] + fn bitxor_assign(&mut self, other: Self) { + unsafe { self.0 = _mm256_xor_si256(self.0, other.0) } + } +} + +impl PartialEq for Block { + #[inline] + fn eq(&self, other: &Self) -> bool { + unsafe { + let eq = _mm256_cmpeq_epi8(self.0, other.0); + _mm256_movemask_epi8(eq) == !(0i32) + } + } +} + +impl Eq for Block {} + +impl PartialOrd for Block { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return Some(cmp), + } + } + Some(Ordering::Equal) + } +} + +impl Ord for Block { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return cmp, + } + } + Ordering::Equal + } +} + +impl Default for Block { + #[inline] + fn default() -> Self { + Self::NONE + } +} + +impl Hash for Block { + #[inline] + fn hash(&self, hasher: &mut H) { + self.into_usize_array().hash(hasher) + } +} diff --git a/src/block/default.rs b/src/block/default.rs new file mode 100644 index 0000000..958191b --- /dev/null +++ b/src/block/default.rs @@ -0,0 +1,102 @@ +use core::iter::Iterator; +use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}; + +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] +#[repr(transparent)] +pub struct Block(usize); + +impl Block { + const _ASSERTION: () = { + if core::mem::size_of::() % core::mem::size_of::() != 0 { + panic!("vector is not a multiple size of usize"); + } + }; + + pub const USIZE_COUNT: usize = 1; + pub const NONE: Self = Block(0); + pub const ALL: Self = Block(!0); + pub const BITS: usize = core::mem::size_of::() * 8; + + #[inline] + pub fn create_buffer(iter: impl Iterator) -> Vec { + iter.map(Self).collect() + } + + #[inline] + pub const fn is_empty(self) -> bool { + self.0 == Self::NONE.0 + } + + #[inline] + pub const fn count_ones(self) -> u32 { + self.0.count_ones() + } + + #[inline] + pub const fn upper_mask(size: usize) -> Self { + Self(core::usize::MAX << size) + } + + #[inline] + pub const fn lower_mask(size: usize) -> Self { + Self((core::usize::MAX >> 1) >> (Self::BITS - size - 1)) + } + + #[inline] + pub fn andnot(self, other: Self) -> Self { + Self(!other.0 & self.0) + } +} + +impl Not for Block { + type Output = Block; + #[inline] + fn not(self) -> Self::Output { + Self(self.0.not()) + } +} + +impl BitAnd for Block { + type Output = Block; + #[inline] + fn bitand(self, other: Self) -> Self::Output { + Self(self.0.bitand(other.0)) + } +} + +impl BitAndAssign for Block { + #[inline] + fn bitand_assign(&mut self, other: Self) { + self.0.bitand_assign(other.0); + } +} + +impl BitOr for Block { + type Output = Block; + #[inline] + fn bitor(self, other: Self) -> Self::Output { + Self(self.0.bitor(other.0)) + } +} + +impl BitOrAssign for Block { + #[inline] + fn bitor_assign(&mut self, other: Self) { + self.0.bitor_assign(other.0) + } +} + +impl BitXor for Block { + type Output = Block; + #[inline] + fn bitxor(self, other: Self) -> Self::Output { + Self(self.0.bitxor(other.0)) + } +} + +impl BitXorAssign for Block { + #[inline] + fn bitxor_assign(&mut self, other: Self) { + self.0.bitxor_assign(other.0) + } +} diff --git a/src/block/mod.rs b/src/block/mod.rs new file mode 100644 index 0000000..8f2aba5 --- /dev/null +++ b/src/block/mod.rs @@ -0,0 +1,35 @@ +#[cfg(all( + not(target_arch = "wasm32"), + not(target_feature = "sse2"), + not(target_feature = "avx2"), +))] +mod default; +#[cfg(all( + not(target_arch = "wasm32"), + not(target_feature = "sse2"), + not(target_feature = "avx2"), +))] +pub use self::default::*; + +#[cfg(all( + not(target_arch = "wasm32"), + target_feature = "sse2", + not(target_feature = "avx2"), +))] +mod sse2; +#[cfg(all( + not(target_arch = "wasm32"), + target_feature = "sse2", + not(target_feature = "avx2"), +))] +pub use self::sse2::*; + +#[cfg(all(not(target_arch = "wasm32"), target_feature = "avx2",))] +mod avx2; +#[cfg(all(not(target_arch = "wasm32"), target_feature = "avx2",))] +pub use self::avx2::*; + +#[cfg(target_arch = "wasm32")] +mod wasm32; +#[cfg(target_arch = "wasm32")] +pub use self::wasm32::*; diff --git a/src/block/sse2.rs b/src/block/sse2.rs new file mode 100644 index 0000000..25f7948 --- /dev/null +++ b/src/block/sse2.rs @@ -0,0 +1,221 @@ +use alloc::vec::Vec; +#[cfg(target_arch = "x86")] +use core::arch::x86::*; +#[cfg(target_arch = "x86_64")] +use core::arch::x86_64::*; +use core::{ + cmp::Ordering, + hash::{Hash, Hasher}, + iter::Iterator, + ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}, +}; + +#[derive(Copy, Clone, Debug)] +#[repr(transparent)] +pub struct Block(__m128i); + +impl Block { + const _ASSERTION: () = { + if core::mem::size_of::() % core::mem::size_of::() != 0 { + panic!("vector is not a multiple size of usize"); + } + }; + + pub const USIZE_COUNT: usize = core::mem::size_of::() / core::mem::size_of::(); + pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); + pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); + pub const BITS: usize = core::mem::size_of::() * 8; + + #[inline] + pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { + unsafe { core::mem::transmute(self.0) } + } + + #[inline] + pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + Self(unsafe { core::mem::transmute(array) }) + } + + #[inline] + pub fn create_buffer(iter: impl Iterator) -> Vec { + let (lower, _) = iter.size_hint(); + let mut output = Vec::with_capacity(lower / Self::USIZE_COUNT); + let mut buffer = [0; Self::USIZE_COUNT]; + let mut index = 0; + for chunk in iter { + buffer[index] = chunk; + index += 1; + if index >= Self::USIZE_COUNT { + output.push(Self::from_usize_array(buffer)); + index = 0; + } + } + if index != 0 { + #[allow(clippy::needless_range_loop)] + for idx in index..Self::USIZE_COUNT { + buffer[idx] = 0; + } + output.push(Self::from_usize_array(buffer)); + } + output + } + + #[inline] + pub fn is_empty(self) -> bool { + #[cfg(not(target_feature = "sse4.1"))] + { + self == Self::NONE + } + #[cfg(target_feature = "sse4.1")] + { + unsafe { _mm_test_all_zeros(self.0, Self::ALL.0) == 1 } + } + } + + #[inline] + pub fn count_ones(self) -> u32 { + unsafe { + let array: [usize; Self::USIZE_COUNT] = core::mem::transmute(self.0); + array.iter().copied().map(usize::count_ones).sum() + } + } + + #[inline] + pub const fn upper_mask(size: usize) -> Self { + unsafe { Self(core::mem::transmute(core::u128::MAX << size)) } + } + + #[inline] + pub const fn lower_mask(size: usize) -> Self { + unsafe { + Self(core::mem::transmute( + (core::u128::MAX >> 1) >> (Self::BITS - size - 1), + )) + } + } + + #[inline] + pub fn andnot(self, other: Self) -> Self { + Self(unsafe { _mm_andnot_si128(other.0, self.0) }) + } +} + +impl Not for Block { + type Output = Block; + #[inline] + fn not(self) -> Self::Output { + unsafe { Self(_mm_xor_si128(self.0, Self::ALL.0)) } + } +} + +impl BitAnd for Block { + type Output = Block; + #[inline] + fn bitand(self, other: Self) -> Self::Output { + unsafe { Self(_mm_and_si128(self.0, other.0)) } + } +} + +impl BitAndAssign for Block { + #[inline] + fn bitand_assign(&mut self, other: Self) { + unsafe { + self.0 = _mm_and_si128(self.0, other.0); + } + } +} + +impl BitOr for Block { + type Output = Block; + #[inline] + fn bitor(self, other: Self) -> Self::Output { + unsafe { Self(_mm_or_si128(self.0, other.0)) } + } +} + +impl BitOrAssign for Block { + #[inline] + fn bitor_assign(&mut self, other: Self) { + unsafe { + self.0 = _mm_or_si128(self.0, other.0); + } + } +} + +impl BitXor for Block { + type Output = Block; + #[inline] + fn bitxor(self, other: Self) -> Self::Output { + unsafe { Self(_mm_xor_si128(self.0, other.0)) } + } +} + +impl BitXorAssign for Block { + #[inline] + fn bitxor_assign(&mut self, other: Self) { + unsafe { self.0 = _mm_xor_si128(self.0, other.0) } + } +} + +impl PartialEq for Block { + #[inline] + fn eq(&self, other: &Self) -> bool { + unsafe { + #[cfg(not(target_feature = "sse4.1"))] + { + _mm_movemask_epi8(_mm_cmpeq_epi8(self.0, other.0)) == 0xffff + } + #[cfg(target_feature = "sse4.1")] + { + let neq = _mm_xor_si128(self.0, other.0); + _mm_test_all_zeros(neq, neq) == 1 + } + } + } +} + +impl Eq for Block {} + +impl PartialOrd for Block { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return Some(cmp), + } + } + Some(Ordering::Equal) + } +} + +impl Ord for Block { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return cmp, + } + } + Ordering::Equal + } +} + +impl Default for Block { + #[inline] + fn default() -> Self { + Self::NONE + } +} + +impl Hash for Block { + #[inline] + fn hash(&self, hasher: &mut H) { + self.into_usize_array().hash(hasher) + } +} diff --git a/src/block/wasm32.rs b/src/block/wasm32.rs new file mode 100644 index 0000000..5bdf1ae --- /dev/null +++ b/src/block/wasm32.rs @@ -0,0 +1,199 @@ +use core::{ + arch::wasm32::*, + cmp::Ordering, + hash::{Hash, Hasher}, + iter::Iterator, + ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}, +}; + +#[derive(Copy, Clone, Debug)] +#[repr(transparent)] +pub struct Block(v128); + +impl Block { + const _ASSERTION: () = { + if core::mem::size_of::() % core::mem::size_of::() != 0 { + panic!("vector is not a multiple size of usize"); + } + }; + + pub const USIZE_COUNT: usize = core::mem::size_of::() / core::mem::size_of::(); + pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); + pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); + pub const BITS: usize = core::mem::size_of::() * 8; + + #[inline] + fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { + unsafe { core::mem::transmute(self.0) } + } + + #[inline] + const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + Self(unsafe { core::mem::transmute(array) }) + } + + #[inline] + pub fn create_buffer(iter: impl Iterator) -> Vec { + let (lower, _) = iter.size_hint(); + let mut output = Vec::with_capacity(lower / Self::USIZE_COUNT); + let mut buffer = [0; Self::USIZE_COUNT]; + let mut index = 0; + for chunk in iter { + buffer[index] = chunk; + index += 1; + if index >= Self::USIZE_COUNT { + output.push(Self::from_usize_array(buffer)); + index = 0; + } + } + if index != 0 { + for idx in index..Self::USIZE_COUNT { + buffer[idx] = 0; + } + output.push(Self::from_usize_array(buffer)); + } + output + } + + #[inline] + pub fn is_empty(self) -> bool { + !v128_any_true(self.0) + } + + #[inline] + pub fn count_ones(self) -> u32 { + unsafe { + let array: [usize; Self::USIZE_COUNT] = core::mem::transmute(self.0); + let mut total = 0; + for i in 0..Self::USIZE_COUNT { + total += array[i].count_ones(); + } + total + } + } + + #[inline] + pub const fn upper_mask(size: usize) -> Self { + unsafe { Self(core::mem::transmute(core::u128::MAX << size)) } + } + + #[inline] + pub const fn lower_mask(size: usize) -> Self { + unsafe { + Self(core::mem::transmute( + (core::u128::MAX >> 1) >> (Self::BITS - size - 1), + )) + } + } + + #[inline] + pub fn andnot(self, other: Self) -> Self { + Self(unsafe { v128_andnot(self.0, other.0) }) + } +} + +impl Not for Block { + type Output = Block; + #[inline] + fn not(self) -> Self::Output { + Self(v128_xor(self.0, Self::ALL.0)) + } +} + +impl BitAnd for Block { + type Output = Block; + #[inline] + fn bitand(self, other: Self) -> Self::Output { + Self(v128_and(self.0, other.0)) + } +} + +impl BitAndAssign for Block { + #[inline] + fn bitand_assign(&mut self, other: Self) { + self.0 = v128_and(self.0, other.0); + } +} + +impl BitOr for Block { + type Output = Block; + #[inline] + fn bitor(self, other: Self) -> Self::Output { + Self(v128_or(self.0, other.0)) + } +} + +impl BitOrAssign for Block { + #[inline] + fn bitor_assign(&mut self, other: Self) { + self.0 = v128_or(self.0, other.0); + } +} + +impl BitXor for Block { + type Output = Block; + #[inline] + fn bitxor(self, other: Self) -> Self::Output { + Self(v128_xor(self.0, other.0)) + } +} + +impl BitXorAssign for Block { + #[inline] + fn bitxor_assign(&mut self, other: Self) { + self.0 = v128_xor(self.0, other.0) + } +} + +impl PartialEq for Block { + #[inline] + fn eq(&self, other: &Self) -> bool { + !v128_any_true(v128_xor(self.0, other.0)) + } +} + +impl Eq for Block {} + +impl PartialOrd for Block { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return Some(cmp), + } + } + Some(Ordering::Equal) + } +} + +impl Ord for Block { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return cmp, + } + } + Ordering::Equal + } +} + +impl Default for Block { + #[inline] + fn default() -> Self { + Self::NONE + } +} + +impl Hash for Block { + #[inline] + fn hash(&self, hasher: &mut H) { + self.into_usize_array().hash(hasher) + } +} diff --git a/src/lib.rs b/src/lib.rs index 6636863..7ac6f52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ use alloc::{ vec::{IntoIter, Vec}, }; +mod block; mod range; #[cfg(feature = "serde")] @@ -28,19 +29,19 @@ extern crate serde; #[cfg(feature = "serde")] mod serde_impl; -use core::fmt::Write; use core::fmt::{Binary, Display, Error, Formatter}; +use core::{fmt::Write, mem::ManuallyDrop}; -pub use range::IndexRange; use core::cmp::{Ord, Ordering}; use core::iter::{Chain, ExactSizeIterator, FromIterator, FusedIterator}; use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index}; +pub use range::IndexRange; -pub(crate) const BITS: usize = core::mem::size_of::() * 8; +pub(crate) const BITS: usize = core::mem::size_of::() * 8; #[cfg(feature = "serde")] pub(crate) const BYTES: usize = core::mem::size_of::(); -pub type Block = usize; +pub use block::Block; #[inline] fn div_rem(x: usize) -> (usize, usize) { @@ -77,7 +78,7 @@ impl FixedBitSet { let (mut blocks, rem) = div_rem(bits); blocks += (rem > 0) as usize; FixedBitSet { - data: vec![0; blocks], + data: vec![Block::NONE; blocks], length: bits, } } @@ -95,22 +96,17 @@ impl FixedBitSet { /// let bs = fixedbitset::FixedBitSet::with_capacity_and_blocks(4, data); /// assert_eq!(format!("{:b}", bs), "0010"); /// ``` - pub fn with_capacity_and_blocks>(bits: usize, blocks: I) -> Self { - let (mut n_blocks, rem) = div_rem(bits); + pub fn with_capacity_and_blocks>(bits: usize, blocks: I) -> Self { + let (mut n_blocks, rem) = (bits / Block::BITS, bits % Block::BITS); n_blocks += (rem > 0) as usize; - let mut data: Vec = blocks.into_iter().collect(); - // Pad data with zeros if smaller or truncate if larger - if data.len() != n_blocks { - data.resize(n_blocks, 0); - } - // Disable bits in blocks beyond capacity - let end = data.len() * BITS; - for (block, mask) in Masks::new(bits..end, end) { - // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { data.get_unchecked_mut(block) }; - *block &= !mask; + let mut bitset = FixedBitSet { + data: vec![Block::NONE; n_blocks], + length: bits, + }; + for (subblock, value) in bitset.as_mut_slice().iter_mut().zip(blocks.into_iter()) { + *subblock = value; } - FixedBitSet { data, length: bits } + bitset } /// Grow capacity to **bits**, all new bits initialized to zero @@ -119,10 +115,34 @@ impl FixedBitSet { blocks += (rem > 0) as usize; if bits > self.length { self.length = bits; - self.data.resize(blocks, 0); + self.data.resize(blocks, Block::NONE); } } + unsafe fn get_unchecked(&self, subblock: usize) -> &usize { + self.data + .as_ptr() + .cast::() + .add(subblock) + .as_ref() + .unwrap_unchecked() + } + + unsafe fn get_unchecked_mut(&mut self, subblock: usize) -> &mut usize { + self.data + .as_mut_ptr() + .cast::() + .add(subblock) + .as_mut() + .unwrap_unchecked() + } + + fn usize_len(&self) -> usize { + (self.length != 0) + .then(|| self.length / BITS + 1) + .unwrap_or(0) + } + /// Grows the internal size of the bitset before inserting a bit /// /// Unlike `insert`, this cannot panic, but may allocate if the bit is outside of the existing buffer's range. @@ -135,7 +155,7 @@ impl FixedBitSet { let (blocks, rem) = div_rem(bits); // SAFETY: The above grow ensures that the block is inside the Vec's allocation. unsafe { - *self.data.get_unchecked_mut(blocks) |= 1 << rem; + *self.get_unchecked_mut(blocks) |= 1 << rem; } } @@ -191,7 +211,7 @@ impl FixedBitSet { /// This is equivalent to [`bitset.count_ones(..) == 0`](FixedBitSet::count_ones). #[inline] pub fn is_clear(&self) -> bool { - self.data.iter().all(|block| *block == 0) + self.data.iter().all(|block| *block == Block::NONE) } /// Return **true** if the bit is enabled in the **FixedBitSet**, @@ -202,11 +222,9 @@ impl FixedBitSet { /// Note: Also available with index syntax: `bitset[bit]`. #[inline] pub fn contains(&self, bit: usize) -> bool { - let (block, i) = div_rem(bit); - match self.data.get(block) { - None => false, - Some(b) => (b & (1 << i)) != 0, - } + (bit < self.length) + .then(|| unsafe { self.contains_unchecked(bit) }) + .unwrap_or(false) } /// Return **true** if the bit is enabled in the **FixedBitSet**, @@ -220,14 +238,14 @@ impl FixedBitSet { #[inline] pub unsafe fn contains_unchecked(&self, bit: usize) -> bool { let (block, i) = div_rem(bit); - (self.data.get_unchecked(block) & (1 << i)) != 0 + (self.get_unchecked(block) & (1 << i)) != 0 } /// Clear all bits. #[inline] pub fn clear(&mut self) { for elt in &mut self.data { - *elt = 0 + *elt = Block::NONE } } @@ -257,7 +275,7 @@ impl FixedBitSet { let (block, i) = div_rem(bit); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { - *self.data.get_unchecked_mut(block) |= 1 << i; + *self.get_unchecked_mut(block) |= 1 << i; } } @@ -287,7 +305,7 @@ impl FixedBitSet { let (block, i) = div_rem(bit); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { - *self.data.get_unchecked_mut(block) &= !(1 << i); + *self.get_unchecked_mut(block) &= !(1 << i); } } @@ -315,7 +333,7 @@ impl FixedBitSet { let (block, i) = div_rem(bit); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { - let word = self.data.get_unchecked_mut(block); + let word = self.get_unchecked_mut(block); let prev = *word & (1 << i) != 0; *word |= 1 << i; prev @@ -348,7 +366,7 @@ impl FixedBitSet { let (block, i) = div_rem(bit); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { - *self.data.get_unchecked_mut(block) ^= 1 << i; + *self.get_unchecked_mut(block) ^= 1 << i; } } @@ -377,7 +395,7 @@ impl FixedBitSet { pub unsafe fn set_unchecked(&mut self, bit: usize, enabled: bool) { let (block, i) = div_rem(bit); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. - let elt = unsafe { self.data.get_unchecked_mut(block) }; + let elt = unsafe { self.get_unchecked_mut(block) }; if enabled { *elt |= 1 << i; } else { @@ -428,7 +446,7 @@ impl FixedBitSet { Masks::new(range, self.length) .map(|(block, mask)| { // SAFETY: Masks cannot return a block index that is out of range. - let value = unsafe { *self.data.get_unchecked(block) }; + let value = unsafe { *self.get_unchecked(block) }; (value & mask).count_ones() as usize }) .sum() @@ -443,7 +461,7 @@ impl FixedBitSet { pub fn set_range(&mut self, range: T, enabled: bool) { for (block, mask) in Masks::new(range, self.length) { // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { self.data.get_unchecked_mut(block) }; + let block = unsafe { self.get_unchecked_mut(block) }; if enabled { *block |= mask; } else { @@ -471,22 +489,28 @@ impl FixedBitSet { pub fn toggle_range(&mut self, range: T) { for (block, mask) in Masks::new(range, self.length) { // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { self.data.get_unchecked_mut(block) }; + let block = unsafe { self.get_unchecked_mut(block) }; *block ^= mask; } } /// View the bitset as a slice of `Block` blocks #[inline] - pub fn as_slice(&self) -> &[Block] { - &self.data + pub fn as_slice(&self) -> &[usize] { + unsafe { + let ptr = self.data.as_ptr().cast::(); + core::slice::from_raw_parts(ptr, self.usize_len()) + } } /// View the bitset as a mutable slice of `Block` blocks. Writing past the bitlength in the last /// will cause `contains` to return potentially incorrect results for bits past the bitlength. #[inline] - pub fn as_mut_slice(&mut self) -> &mut [Block] { - &mut self.data + pub fn as_mut_slice(&mut self) -> &mut [usize] { + unsafe { + let ptr = self.data.as_mut_ptr().cast::(); + core::slice::from_raw_parts_mut(ptr, self.usize_len()) + } } /// Iterates over all enabled bits. @@ -519,24 +543,31 @@ impl FixedBitSet { /// /// Iterator element is the index of the `1` bit, type `usize`. /// Unlike `ones`, this function consumes the `FixedBitset`. - pub fn into_ones(mut self) -> IntoOnes { - if self.data.len() == 0 { + pub fn into_ones(self) -> IntoOnes { + let mut data = unsafe { + let mut data = ManuallyDrop::new(self.data); + let ptr = data.as_mut_ptr().cast(); + let len = data.len() * Block::USIZE_COUNT; + let capacity = data.capacity() * Block::USIZE_COUNT; + Vec::from_raw_parts(ptr, len, capacity) + }; + if data.len() == 0 { IntoOnes { bitset_front: 0, bitset_back: 0, block_idx_front: 0, block_idx_back: 0, - remaining_blocks: self.data.into_iter(), + remaining_blocks: data.into_iter(), } } else { - let first_block = self.data.remove(0); - let last_block = self.data.pop().unwrap_or(0); + let first_block = data.remove(0); + let last_block = data.pop().unwrap_or(0); IntoOnes { bitset_front: first_block, bitset_back: last_block, block_idx_front: 0, - block_idx_back: (1 + self.data.len()) * BITS, - remaining_blocks: self.data.into_iter(), + block_idx_back: (1 + data.len()) * BITS, + remaining_blocks: data.into_iter(), } } } @@ -615,7 +646,7 @@ impl FixedBitSet { } let mn = core::cmp::min(self.data.len(), other.data.len()); for wd in &mut self.data[mn..] { - *wd = 0; + *wd = Block::NONE; } } @@ -653,7 +684,7 @@ impl FixedBitSet { self.data .iter() .zip(other.data.iter()) - .all(|(x, y)| x & y == 0) + .all(|(x, y)| *x & *y == Block::NONE) } /// Returns `true` if the set is a subset of another, i.e. `other` contains @@ -662,8 +693,12 @@ impl FixedBitSet { self.data .iter() .zip(other.data.iter()) - .all(|(x, y)| x & !y == 0) - && self.data.iter().skip(other.data.len()).all(|x| *x == 0) + .all(|(x, y)| x.andnot(*y) == Block::NONE) + && self + .data + .iter() + .skip(other.data.len()) + .all(|x| *x == Block::NONE) } /// Returns `true` if the set is a superset of another, i.e. `self` contains @@ -843,9 +878,9 @@ impl<'a> FusedIterator for Union<'a> {} struct Masks { first_block: usize, - first_mask: Block, + first_mask: usize, last_block: usize, - last_mask: Block, + last_mask: usize, } impl Masks { @@ -866,16 +901,16 @@ impl Masks { Masks { first_block, - first_mask: Block::max_value() << first_rem, + first_mask: usize::max_value() << first_rem, last_block, - last_mask: (Block::max_value() >> 1) >> (BITS - last_rem - 1), + last_mask: (usize::max_value() >> 1) >> (BITS - last_rem - 1), // this is equivalent to `MAX >> (BITS - x)` with correct semantics when x == 0. } } } impl Iterator for Masks { - type Item = (usize, Block); + type Item = (usize, usize); #[inline] fn next(&mut self) -> Option { match self.first_block.cmp(&self.last_block) { @@ -916,16 +951,16 @@ impl ExactSizeIterator for Masks {} /// /// This struct is created by the [`FixedBitSet::ones`] method. pub struct Ones<'a> { - bitset_front: Block, - bitset_back: Block, + bitset_front: usize, + bitset_back: usize, block_idx_front: usize, block_idx_back: usize, - remaining_blocks: core::slice::Iter<'a, Block>, + remaining_blocks: core::slice::Iter<'a, usize>, } impl<'a> Ones<'a> { #[inline] - pub fn last_positive_bit_and_unset(n: &mut Block) -> usize { + pub fn last_positive_bit_and_unset(n: &mut usize) -> usize { // Find the last set bit using x & -x let last_bit = *n & n.wrapping_neg(); @@ -939,12 +974,12 @@ impl<'a> Ones<'a> { } #[inline] - fn first_positive_bit_and_unset(n: &mut Block) -> usize { + fn first_positive_bit_and_unset(n: &mut usize) -> usize { /* Identify the first non zero bit */ let bit_idx = n.leading_zeros(); /* set that bit to zero */ - let mask = !((1 as Block) << (BITS as u32 - bit_idx - 1)); + let mask = !((1 as usize) << (BITS as u32 - bit_idx - 1)); n.bitand_assign(mask); bit_idx as usize @@ -1029,10 +1064,10 @@ impl<'a> FusedIterator for Ones<'a> {} /// /// This struct is created by the [`FixedBitSet::ones`] method. pub struct Zeroes<'a> { - bitset: Block, + bitset: usize, block_idx: usize, len: usize, - remaining_blocks: core::slice::Iter<'a, Block>, + remaining_blocks: core::slice::Iter<'a, usize>, } impl<'a> Iterator for Zeroes<'a> { @@ -1044,7 +1079,7 @@ impl<'a> Iterator for Zeroes<'a> { self.bitset = !*self.remaining_blocks.next()?; self.block_idx += BITS; } - let t = self.bitset & (0 as Block).wrapping_sub(self.bitset); + let t = self.bitset & (0 as usize).wrapping_sub(self.bitset); let r = self.bitset.trailing_zeros() as usize; self.bitset ^= t; let bit = self.block_idx + r; @@ -1117,16 +1152,16 @@ impl FromIterator for FixedBitSet { } pub struct IntoOnes { - bitset_front: Block, - bitset_back: Block, + bitset_front: usize, + bitset_back: usize, block_idx_front: usize, block_idx_back: usize, - remaining_blocks: IntoIter, + remaining_blocks: IntoIter, } impl IntoOnes { #[inline] - pub fn last_positive_bit_and_unset(n: &mut Block) -> usize { + pub fn last_positive_bit_and_unset(n: &mut usize) -> usize { // Find the last set bit using x & -x let last_bit = *n & n.wrapping_neg(); @@ -1140,12 +1175,12 @@ impl IntoOnes { } #[inline] - fn first_positive_bit_and_unset(n: &mut Block) -> usize { + fn first_positive_bit_and_unset(n: &mut usize) -> usize { /* Identify the first non zero bit */ let bit_idx = n.leading_zeros(); /* set that bit to zero */ - let mask = !((1 as Block) << (BITS as u32 - bit_idx - 1)); + let mask = !((1 as usize) << (BITS as u32 - bit_idx - 1)); n.bitand_assign(mask); bit_idx as usize @@ -2302,8 +2337,8 @@ mod tests { fb.put(4); fb.put(2); - assert_eq!(format!("{}", fb), "00101000"); - assert_eq!(format!("{:#}", fb), "0b00101000"); + assert_eq!(alloc::format!("{}", fb), "00101000"); + assert_eq!(alloc::format!("{:#}", fb), "0b00101000"); } // TODO: Rewite this test to be platform agnostic. From 648d906e28f52e57bd2001e91692e103c8ad2b1b Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 14:04:17 -0700 Subject: [PATCH 03/13] Use Block::is_empty properly --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7ac6f52..bf6e945 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -684,7 +684,7 @@ impl FixedBitSet { self.data .iter() .zip(other.data.iter()) - .all(|(x, y)| *x & *y == Block::NONE) + .all(|(x, y)| (*x & *y).is_empty()) } /// Returns `true` if the set is a subset of another, i.e. `other` contains @@ -693,12 +693,12 @@ impl FixedBitSet { self.data .iter() .zip(other.data.iter()) - .all(|(x, y)| x.andnot(*y) == Block::NONE) + .all(|(x, y)| x.andnot(*y).is_empty()) && self .data .iter() .skip(other.data.len()) - .all(|x| *x == Block::NONE) + .all(|x| x.is_empty()) } /// Returns `true` if the set is a superset of another, i.e. `self` contains From 069291ce25d3daa3e808ff00ac4ffd8fc3530b2c Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 15:06:45 -0700 Subject: [PATCH 04/13] Cleanup --- src/block/avx2.rs | 60 -------------------------------------------- src/block/default.rs | 26 ------------------- src/block/mod.rs | 6 +++++ src/block/sse2.rs | 56 +---------------------------------------- src/block/wasm32.rs | 49 ------------------------------------ src/lib.rs | 14 ++--------- src/serde_impl.rs | 4 +-- 7 files changed, 11 insertions(+), 204 deletions(-) diff --git a/src/block/avx2.rs b/src/block/avx2.rs index 7f0d84c..287b07f 100644 --- a/src/block/avx2.rs +++ b/src/block/avx2.rs @@ -14,12 +14,6 @@ use core::{ pub struct Block(__m256i); impl Block { - const _ASSERTION: () = { - if core::mem::size_of::() % core::mem::size_of::() != 0 { - panic!("vector is not a multiple size of usize"); - } - }; - pub const USIZE_COUNT: usize = core::mem::size_of::() / core::mem::size_of::(); pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); @@ -35,65 +29,11 @@ impl Block { Self(unsafe { core::mem::transmute(array) }) } - #[inline] - pub fn create_buffer(iter: impl Iterator) -> Vec { - let (lower, _) = iter.size_hint(); - let mut output = Vec::with_capacity(lower / Self::USIZE_COUNT); - let mut buffer = [0; Self::USIZE_COUNT]; - let mut index = 0; - for chunk in iter { - buffer[index] = chunk; - index += 1; - if index >= Self::USIZE_COUNT { - output.push(Self::from_usize_array(buffer)); - index = 0; - } - } - if index != 0 { - #[allow(clippy::needless_range_loop)] - for idx in index..Self::USIZE_COUNT { - buffer[idx] = 0; - } - output.push(Self::from_usize_array(buffer)); - } - output - } - #[inline] pub fn is_empty(self) -> bool { unsafe { _mm256_testz_si256(self.0, self.0) == 1 } } - #[inline] - pub fn count_ones(self) -> u32 { - unsafe { - let array: [usize; Self::USIZE_COUNT] = core::mem::transmute(self.0); - array.iter().copied().map(usize::count_ones).sum() - } - } - - #[inline] - pub fn upper_mask(mut size: usize) -> Self { - let res = if size >= 128 { - size -= 128; - [0, core::u128::MAX << size] - } else { - [core::u128::MAX << size, core::u128::MAX] - }; - Self(unsafe { core::mem::transmute(res) }) - } - - #[inline] - pub fn lower_mask(mut size: usize) -> Self { - let res = if size >= 128 { - size -= 128; - [core::u128::MAX, (core::u128::MAX >> 1) >> (128 - size - 1)] - } else { - [(core::u128::MAX >> 1) >> (128 - size - 1), 0] - }; - Self(unsafe { core::mem::transmute(res) }) - } - #[inline] pub fn andnot(self, other: Self) -> Self { Self(unsafe { _mm256_andnot_si256(other.0, self.0) }) diff --git a/src/block/default.rs b/src/block/default.rs index 958191b..b65c9d2 100644 --- a/src/block/default.rs +++ b/src/block/default.rs @@ -6,42 +6,16 @@ use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, pub struct Block(usize); impl Block { - const _ASSERTION: () = { - if core::mem::size_of::() % core::mem::size_of::() != 0 { - panic!("vector is not a multiple size of usize"); - } - }; - pub const USIZE_COUNT: usize = 1; pub const NONE: Self = Block(0); pub const ALL: Self = Block(!0); pub const BITS: usize = core::mem::size_of::() * 8; - #[inline] - pub fn create_buffer(iter: impl Iterator) -> Vec { - iter.map(Self).collect() - } - #[inline] pub const fn is_empty(self) -> bool { self.0 == Self::NONE.0 } - #[inline] - pub const fn count_ones(self) -> u32 { - self.0.count_ones() - } - - #[inline] - pub const fn upper_mask(size: usize) -> Self { - Self(core::usize::MAX << size) - } - - #[inline] - pub const fn lower_mask(size: usize) -> Self { - Self((core::usize::MAX >> 1) >> (Self::BITS - size - 1)) - } - #[inline] pub fn andnot(self, other: Self) -> Self { Self(!other.0 & self.0) diff --git a/src/block/mod.rs b/src/block/mod.rs index 8f2aba5..3a83767 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -33,3 +33,9 @@ pub use self::avx2::*; mod wasm32; #[cfg(target_arch = "wasm32")] pub use self::wasm32::*; + +const _ASSERTION: () = { + if core::mem::size_of::() % core::mem::size_of::() != 0 { + panic!("vector is not a multiple size of usize"); + } +}; diff --git a/src/block/sse2.rs b/src/block/sse2.rs index 25f7948..6f262b4 100644 --- a/src/block/sse2.rs +++ b/src/block/sse2.rs @@ -1,4 +1,3 @@ -use alloc::vec::Vec; #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] @@ -6,7 +5,6 @@ use core::arch::x86_64::*; use core::{ cmp::Ordering, hash::{Hash, Hasher}, - iter::Iterator, ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}, }; @@ -15,12 +13,6 @@ use core::{ pub struct Block(__m128i); impl Block { - const _ASSERTION: () = { - if core::mem::size_of::() % core::mem::size_of::() != 0 { - panic!("vector is not a multiple size of usize"); - } - }; - pub const USIZE_COUNT: usize = core::mem::size_of::() / core::mem::size_of::(); pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); @@ -32,34 +24,10 @@ impl Block { } #[inline] - pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { Self(unsafe { core::mem::transmute(array) }) } - #[inline] - pub fn create_buffer(iter: impl Iterator) -> Vec { - let (lower, _) = iter.size_hint(); - let mut output = Vec::with_capacity(lower / Self::USIZE_COUNT); - let mut buffer = [0; Self::USIZE_COUNT]; - let mut index = 0; - for chunk in iter { - buffer[index] = chunk; - index += 1; - if index >= Self::USIZE_COUNT { - output.push(Self::from_usize_array(buffer)); - index = 0; - } - } - if index != 0 { - #[allow(clippy::needless_range_loop)] - for idx in index..Self::USIZE_COUNT { - buffer[idx] = 0; - } - output.push(Self::from_usize_array(buffer)); - } - output - } - #[inline] pub fn is_empty(self) -> bool { #[cfg(not(target_feature = "sse4.1"))] @@ -72,28 +40,6 @@ impl Block { } } - #[inline] - pub fn count_ones(self) -> u32 { - unsafe { - let array: [usize; Self::USIZE_COUNT] = core::mem::transmute(self.0); - array.iter().copied().map(usize::count_ones).sum() - } - } - - #[inline] - pub const fn upper_mask(size: usize) -> Self { - unsafe { Self(core::mem::transmute(core::u128::MAX << size)) } - } - - #[inline] - pub const fn lower_mask(size: usize) -> Self { - unsafe { - Self(core::mem::transmute( - (core::u128::MAX >> 1) >> (Self::BITS - size - 1), - )) - } - } - #[inline] pub fn andnot(self, other: Self) -> Self { Self(unsafe { _mm_andnot_si128(other.0, self.0) }) diff --git a/src/block/wasm32.rs b/src/block/wasm32.rs index 5bdf1ae..f5e7d6f 100644 --- a/src/block/wasm32.rs +++ b/src/block/wasm32.rs @@ -32,60 +32,11 @@ impl Block { Self(unsafe { core::mem::transmute(array) }) } - #[inline] - pub fn create_buffer(iter: impl Iterator) -> Vec { - let (lower, _) = iter.size_hint(); - let mut output = Vec::with_capacity(lower / Self::USIZE_COUNT); - let mut buffer = [0; Self::USIZE_COUNT]; - let mut index = 0; - for chunk in iter { - buffer[index] = chunk; - index += 1; - if index >= Self::USIZE_COUNT { - output.push(Self::from_usize_array(buffer)); - index = 0; - } - } - if index != 0 { - for idx in index..Self::USIZE_COUNT { - buffer[idx] = 0; - } - output.push(Self::from_usize_array(buffer)); - } - output - } - #[inline] pub fn is_empty(self) -> bool { !v128_any_true(self.0) } - #[inline] - pub fn count_ones(self) -> u32 { - unsafe { - let array: [usize; Self::USIZE_COUNT] = core::mem::transmute(self.0); - let mut total = 0; - for i in 0..Self::USIZE_COUNT { - total += array[i].count_ones(); - } - total - } - } - - #[inline] - pub const fn upper_mask(size: usize) -> Self { - unsafe { Self(core::mem::transmute(core::u128::MAX << size)) } - } - - #[inline] - pub const fn lower_mask(size: usize) -> Self { - unsafe { - Self(core::mem::transmute( - (core::u128::MAX >> 1) >> (Self::BITS - size - 1), - )) - } - } - #[inline] pub fn andnot(self, other: Self) -> Self { Self(unsafe { v128_andnot(self.0, other.0) }) diff --git a/src/lib.rs b/src/lib.rs index bf6e945..023ea02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,21 +120,11 @@ impl FixedBitSet { } unsafe fn get_unchecked(&self, subblock: usize) -> &usize { - self.data - .as_ptr() - .cast::() - .add(subblock) - .as_ref() - .unwrap_unchecked() + &*self.data.as_ptr().cast::().add(subblock) } unsafe fn get_unchecked_mut(&mut self, subblock: usize) -> &mut usize { - self.data - .as_mut_ptr() - .cast::() - .add(subblock) - .as_mut() - .unwrap_unchecked() + &mut *self.data.as_mut_ptr().cast::().add(subblock) } fn usize_len(&self) -> usize { diff --git a/src/serde_impl.rs b/src/serde_impl.rs index 53d97f4..60e73fa 100644 --- a/src/serde_impl.rs +++ b/src/serde_impl.rs @@ -25,10 +25,10 @@ impl<'a> Serialize for BitSetByteSerializer<'a> { where S: Serializer, { - let len = self.0.data.len() * BYTES; + let len = self.0.as_slice().len() * BYTES; // PERF: Figure out a way to do this without allocating. let mut temp = Vec::with_capacity(len); - for block in &self.0.data { + for block in &self.0.as_slice() { temp.extend(&block.to_le_bytes()); } serializer.serialize_bytes(&temp) From 9c1ae24401ea17c28a1c8ad6ce02aa3499362cbc Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 15:10:30 -0700 Subject: [PATCH 05/13] Remove the assertion due to MSRV --- src/block/mod.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/block/mod.rs b/src/block/mod.rs index 3a83767..8f2aba5 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -33,9 +33,3 @@ pub use self::avx2::*; mod wasm32; #[cfg(target_arch = "wasm32")] pub use self::wasm32::*; - -const _ASSERTION: () = { - if core::mem::size_of::() % core::mem::size_of::() != 0 { - panic!("vector is not a multiple size of usize"); - } -}; From ac87e14d7b4e343d37dda5f6690452e85b549024 Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 15:22:33 -0700 Subject: [PATCH 06/13] Move out the common shared code --- src/block/avx2.rs | 48 +--------------------------------------- src/block/default.rs | 2 +- src/block/mod.rs | 49 +++++++++++++++++++++++++++++++++++++++++ src/block/sse2.rs | 52 +------------------------------------------- src/block/wasm32.rs | 52 -------------------------------------------- 5 files changed, 52 insertions(+), 151 deletions(-) diff --git a/src/block/avx2.rs b/src/block/avx2.rs index 287b07f..8a4f19d 100644 --- a/src/block/avx2.rs +++ b/src/block/avx2.rs @@ -105,50 +105,4 @@ impl PartialEq for Block { _mm256_movemask_epi8(eq) == !(0i32) } } -} - -impl Eq for Block {} - -impl PartialOrd for Block { - #[inline] - fn partial_cmp(&self, other: &Self) -> Option { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return Some(cmp), - } - } - Some(Ordering::Equal) - } -} - -impl Ord for Block { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return cmp, - } - } - Ordering::Equal - } -} - -impl Default for Block { - #[inline] - fn default() -> Self { - Self::NONE - } -} - -impl Hash for Block { - #[inline] - fn hash(&self, hasher: &mut H) { - self.into_usize_array().hash(hasher) - } -} +} \ No newline at end of file diff --git a/src/block/default.rs b/src/block/default.rs index b65c9d2..48d52f9 100644 --- a/src/block/default.rs +++ b/src/block/default.rs @@ -1,7 +1,7 @@ use core::iter::Iterator; use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}; -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] +#[derive(Copy, Clone, PartialEq, Debug)] #[repr(transparent)] pub struct Block(usize); diff --git a/src/block/mod.rs b/src/block/mod.rs index 8f2aba5..3dff6fa 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -1,3 +1,6 @@ +use core::cmp::Ordering; +use core::hash::{Hash, Hasher}; + #[cfg(all( not(target_arch = "wasm32"), not(target_feature = "sse2"), @@ -33,3 +36,49 @@ pub use self::avx2::*; mod wasm32; #[cfg(target_arch = "wasm32")] pub use self::wasm32::*; + +impl Eq for Block {} + +impl PartialOrd for Block { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return Some(cmp), + } + } + Some(Ordering::Equal) + } +} + +impl Ord for Block { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + let a = self.into_usize_array(); + let b = other.into_usize_array(); + for i in 0..Self::USIZE_COUNT { + match a[i].cmp(&b[i]) { + Ordering::Equal => continue, + cmp => return cmp, + } + } + Ordering::Equal + } +} + +impl Default for Block { + #[inline] + fn default() -> Self { + Self::NONE + } +} + +impl Hash for Block { + #[inline] + fn hash(&self, hasher: &mut H) { + self.into_usize_array().hash(hasher) + } +} \ No newline at end of file diff --git a/src/block/sse2.rs b/src/block/sse2.rs index 6f262b4..9d6f3b7 100644 --- a/src/block/sse2.rs +++ b/src/block/sse2.rs @@ -2,11 +2,7 @@ use core::arch::x86::*; #[cfg(target_arch = "x86_64")] use core::arch::x86_64::*; -use core::{ - cmp::Ordering, - hash::{Hash, Hasher}, - ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}, -}; +use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}; #[derive(Copy, Clone, Debug)] #[repr(transparent)] @@ -119,49 +115,3 @@ impl PartialEq for Block { } } } - -impl Eq for Block {} - -impl PartialOrd for Block { - #[inline] - fn partial_cmp(&self, other: &Self) -> Option { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return Some(cmp), - } - } - Some(Ordering::Equal) - } -} - -impl Ord for Block { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return cmp, - } - } - Ordering::Equal - } -} - -impl Default for Block { - #[inline] - fn default() -> Self { - Self::NONE - } -} - -impl Hash for Block { - #[inline] - fn hash(&self, hasher: &mut H) { - self.into_usize_array().hash(hasher) - } -} diff --git a/src/block/wasm32.rs b/src/block/wasm32.rs index f5e7d6f..96ab3f9 100644 --- a/src/block/wasm32.rs +++ b/src/block/wasm32.rs @@ -11,12 +11,6 @@ use core::{ pub struct Block(v128); impl Block { - const _ASSERTION: () = { - if core::mem::size_of::() % core::mem::size_of::() != 0 { - panic!("vector is not a multiple size of usize"); - } - }; - pub const USIZE_COUNT: usize = core::mem::size_of::() / core::mem::size_of::(); pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); @@ -102,49 +96,3 @@ impl PartialEq for Block { !v128_any_true(v128_xor(self.0, other.0)) } } - -impl Eq for Block {} - -impl PartialOrd for Block { - #[inline] - fn partial_cmp(&self, other: &Self) -> Option { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return Some(cmp), - } - } - Some(Ordering::Equal) - } -} - -impl Ord for Block { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return cmp, - } - } - Ordering::Equal - } -} - -impl Default for Block { - #[inline] - fn default() -> Self { - Self::NONE - } -} - -impl Hash for Block { - #[inline] - fn hash(&self, hasher: &mut H) { - self.into_usize_array().hash(hasher) - } -} From afe7acb71d7ff490486a3d2bc2f89a27afd4bf33 Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 16:15:34 -0700 Subject: [PATCH 07/13] Allocate the minimal amount of memory --- src/lib.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 023ea02..d38430a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,8 +44,8 @@ pub(crate) const BYTES: usize = core::mem::size_of::(); pub use block::Block; #[inline] -fn div_rem(x: usize) -> (usize, usize) { - (x / BITS, x % BITS) +fn div_rem(x: usize, denominator: usize) -> (usize, usize) { + (x / denominator, x % denominator) } /// `FixedBitSet` is a simple fixed size set of bits that each can @@ -75,7 +75,7 @@ impl FixedBitSet { /// Create a new **FixedBitSet** with a specific number of bits, /// all initially clear. pub fn with_capacity(bits: usize) -> Self { - let (mut blocks, rem) = div_rem(bits); + let (mut blocks, rem) = div_rem(bits, Block::BITS); blocks += (rem > 0) as usize; FixedBitSet { data: vec![Block::NONE; blocks], @@ -97,7 +97,7 @@ impl FixedBitSet { /// assert_eq!(format!("{:b}", bs), "0010"); /// ``` pub fn with_capacity_and_blocks>(bits: usize, blocks: I) -> Self { - let (mut n_blocks, rem) = (bits / Block::BITS, bits % Block::BITS); + let (mut n_blocks, rem) = div_rem(bits, Block::BITS); n_blocks += (rem > 0) as usize; let mut bitset = FixedBitSet { data: vec![Block::NONE; n_blocks], @@ -111,7 +111,7 @@ impl FixedBitSet { /// Grow capacity to **bits**, all new bits initialized to zero pub fn grow(&mut self, bits: usize) { - let (mut blocks, rem) = div_rem(bits); + let (mut blocks, rem) = div_rem(bits, Block::BITS); blocks += (rem > 0) as usize; if bits > self.length { self.length = bits; @@ -128,9 +128,9 @@ impl FixedBitSet { } fn usize_len(&self) -> usize { - (self.length != 0) - .then(|| self.length / BITS + 1) - .unwrap_or(0) + let (mut blocks, rem) = div_rem(self.length, BITS); + blocks += (rem > 0) as usize; + blocks } /// Grows the internal size of the bitset before inserting a bit @@ -142,7 +142,7 @@ impl FixedBitSet { pub fn grow_and_insert(&mut self, bits: usize) { self.grow(bits + 1); - let (blocks, rem) = div_rem(bits); + let (blocks, rem) = div_rem(bits, BITS); // SAFETY: The above grow ensures that the block is inside the Vec's allocation. unsafe { *self.get_unchecked_mut(blocks) |= 1 << rem; @@ -201,7 +201,7 @@ impl FixedBitSet { /// This is equivalent to [`bitset.count_ones(..) == 0`](FixedBitSet::count_ones). #[inline] pub fn is_clear(&self) -> bool { - self.data.iter().all(|block| *block == Block::NONE) + self.data.iter().all(|block| block.is_empty()) } /// Return **true** if the bit is enabled in the **FixedBitSet**, @@ -227,7 +227,7 @@ impl FixedBitSet { /// `bit` must be less than `self.len()` #[inline] pub unsafe fn contains_unchecked(&self, bit: usize) -> bool { - let (block, i) = div_rem(bit); + let (block, i) = div_rem(bit, BITS); (self.get_unchecked(block) & (1 << i)) != 0 } @@ -262,7 +262,7 @@ impl FixedBitSet { /// `bit` must be less than `self.len()` #[inline] pub unsafe fn insert_unchecked(&mut self, bit: usize) { - let (block, i) = div_rem(bit); + let (block, i) = div_rem(bit, BITS); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { *self.get_unchecked_mut(block) |= 1 << i; @@ -292,7 +292,7 @@ impl FixedBitSet { /// `bit` must be less than `self.len()` #[inline] pub unsafe fn remove_unchecked(&mut self, bit: usize) { - let (block, i) = div_rem(bit); + let (block, i) = div_rem(bit, BITS); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { *self.get_unchecked_mut(block) &= !(1 << i); @@ -320,7 +320,7 @@ impl FixedBitSet { /// `bit` must be less than `self.len()` #[inline] pub unsafe fn put_unchecked(&mut self, bit: usize) -> bool { - let (block, i) = div_rem(bit); + let (block, i) = div_rem(bit, BITS); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { let word = self.get_unchecked_mut(block); @@ -353,7 +353,7 @@ impl FixedBitSet { /// `bit` must be less than `self.len()` #[inline] pub unsafe fn toggle_unchecked(&mut self, bit: usize) { - let (block, i) = div_rem(bit); + let (block, i) = div_rem(bit, BITS); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. unsafe { *self.get_unchecked_mut(block) ^= 1 << i; @@ -383,7 +383,7 @@ impl FixedBitSet { /// `bit` must be less than `self.len()` #[inline] pub unsafe fn set_unchecked(&mut self, bit: usize, enabled: bool) { - let (block, i) = div_rem(bit); + let (block, i) = div_rem(bit, BITS); // SAFETY: The above assertion ensures that the block is inside the Vec's allocation. let elt = unsafe { self.get_unchecked_mut(block) }; if enabled { @@ -886,8 +886,8 @@ impl Masks { length ); - let (first_block, first_rem) = div_rem(start); - let (last_block, last_rem) = div_rem(end); + let (first_block, first_rem) = div_rem(start, BITS); + let (last_block, last_rem) = div_rem(end, BITS); Masks { first_block, @@ -1626,13 +1626,13 @@ mod tests { let mut bitset = FixedBitSet::with_capacity(s); bitset.insert_range(..); let mut t = s; + extern crate std; let mut iter = bitset.ones().alternate(); loop { match iter.next() { None => break, Some(_) => { t -= 1; - //println!("{:?} < {}", iter.size_hint(), t); assert!(iter.size_hint().1.unwrap() >= t); assert!(iter.size_hint().1.unwrap() <= t + 3 * BITS); } From 173cc0c5f33350a39bb852b9cb63b27cec814c21 Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 16:17:31 -0700 Subject: [PATCH 08/13] Formatting --- src/block/avx2.rs | 2 +- src/block/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/block/avx2.rs b/src/block/avx2.rs index 8a4f19d..42ec5df 100644 --- a/src/block/avx2.rs +++ b/src/block/avx2.rs @@ -105,4 +105,4 @@ impl PartialEq for Block { _mm256_movemask_epi8(eq) == !(0i32) } } -} \ No newline at end of file +} diff --git a/src/block/mod.rs b/src/block/mod.rs index 3dff6fa..5c3fbcb 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -81,4 +81,4 @@ impl Hash for Block { fn hash(&self, hasher: &mut H) { self.into_usize_array().hash(hasher) } -} \ No newline at end of file +} From 7e0d84483b1dc0952a1d4848fd66a256fa91d7fe Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 16:26:47 -0700 Subject: [PATCH 09/13] Shut up clippy --- src/block/mod.rs | 10 +--------- src/block/sse2.rs | 2 ++ src/lib.rs | 50 ++++++++++++++++++++++++----------------------- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/block/mod.rs b/src/block/mod.rs index 5c3fbcb..be64af9 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -42,15 +42,7 @@ impl Eq for Block {} impl PartialOrd for Block { #[inline] fn partial_cmp(&self, other: &Self) -> Option { - let a = self.into_usize_array(); - let b = other.into_usize_array(); - for i in 0..Self::USIZE_COUNT { - match a[i].cmp(&b[i]) { - Ordering::Equal => continue, - cmp => return Some(cmp), - } - } - Some(Ordering::Equal) + Some(self.cmp(other)) } } diff --git a/src/block/sse2.rs b/src/block/sse2.rs index 9d6f3b7..8ff86cc 100644 --- a/src/block/sse2.rs +++ b/src/block/sse2.rs @@ -1,3 +1,5 @@ +#![allow(clippy::undocumented_unsafe_blocks)] + #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] diff --git a/src/lib.rs b/src/lib.rs index d38430a..ee7bf55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ //! #![doc(html_root_url = "https://docs.rs/fixedbitset/0.4.2/")] #![no_std] -#![forbid(clippy::undocumented_unsafe_blocks)] +#![deny(clippy::undocumented_unsafe_blocks)] extern crate alloc; use alloc::{ @@ -213,6 +213,7 @@ impl FixedBitSet { #[inline] pub fn contains(&self, bit: usize) -> bool { (bit < self.length) + // SAFETY: The above check ensures that the block and bit are within bounds. .then(|| unsafe { self.contains_unchecked(bit) }) .unwrap_or(false) } @@ -487,6 +488,10 @@ impl FixedBitSet { /// View the bitset as a slice of `Block` blocks #[inline] pub fn as_slice(&self) -> &[usize] { + // SAFETY: The bits from both usize and Block are required to be reinterprettable, and + // neither have any padding or alignment issues. The slice constructed is within bounds + // of the underlying allocation. This function is called with a read-only borrow so + // no other write can happen as long as the returned borrow lives. unsafe { let ptr = self.data.as_ptr().cast::(); core::slice::from_raw_parts(ptr, self.usize_len()) @@ -497,6 +502,10 @@ impl FixedBitSet { /// will cause `contains` to return potentially incorrect results for bits past the bitlength. #[inline] pub fn as_mut_slice(&mut self) -> &mut [usize] { + // SAFETY: The bits from both usize and Block are required to be reinterprettable, and + // neither have any padding or alignment issues. The slice constructed is within bounds + // of the underlying allocation. This function is called with a mutable borrow so + // no other read or write can happen as long as the returned borrow lives. unsafe { let ptr = self.data.as_mut_ptr().cast::(); core::slice::from_raw_parts_mut(ptr, self.usize_len()) @@ -534,6 +543,8 @@ impl FixedBitSet { /// Iterator element is the index of the `1` bit, type `usize`. /// Unlike `ones`, this function consumes the `FixedBitset`. pub fn into_ones(self) -> IntoOnes { + // SAFETY: This is using the exact same allocation pattern, size, and capacity + // making this reconstruction of the Vec safe. let mut data = unsafe { let mut data = ManuallyDrop::new(self.data); let ptr = data.as_mut_ptr().cast(); @@ -541,7 +552,7 @@ impl FixedBitSet { let capacity = data.capacity() * Block::USIZE_COUNT; Vec::from_raw_parts(ptr, len, capacity) }; - if data.len() == 0 { + if data.is_empty() { IntoOnes { bitset_front: 0, bitset_back: 0, @@ -752,12 +763,10 @@ impl<'a> Iterator for Difference<'a> { impl<'a> DoubleEndedIterator for Difference<'a> { fn next_back(&mut self) -> Option { - for nxt in self.iter.by_ref().rev() { - if !self.other.contains(nxt) { - return Some(nxt); - } - } - None + self.iter + .by_ref() + .rev() + .find(|&nxt| !self.other.contains(nxt)) } } @@ -808,12 +817,7 @@ impl<'a> Iterator for Intersection<'a> { #[inline] #[allow(clippy::manual_find)] fn next(&mut self) -> Option { - for nxt in self.iter.by_ref() { - if self.other.contains(nxt) { - return Some(nxt); - } - } - None + self.iter.by_ref().find(|&nxt| self.other.contains(nxt)) } #[inline] @@ -824,12 +828,10 @@ impl<'a> Iterator for Intersection<'a> { impl<'a> DoubleEndedIterator for Intersection<'a> { fn next_back(&mut self) -> Option { - for nxt in self.iter.by_ref().rev() { - if self.other.contains(nxt) { - return Some(nxt); - } - } - None + self.iter + .by_ref() + .rev() + .find(|&nxt| self.other.contains(nxt)) } } @@ -969,7 +971,7 @@ impl<'a> Ones<'a> { let bit_idx = n.leading_zeros(); /* set that bit to zero */ - let mask = !((1 as usize) << (BITS as u32 - bit_idx - 1)); + let mask = !((1_usize) << (BITS as u32 - bit_idx - 1)); n.bitand_assign(mask); bit_idx as usize @@ -1069,7 +1071,7 @@ impl<'a> Iterator for Zeroes<'a> { self.bitset = !*self.remaining_blocks.next()?; self.block_idx += BITS; } - let t = self.bitset & (0 as usize).wrapping_sub(self.bitset); + let t = self.bitset & (0_usize).wrapping_sub(self.bitset); let r = self.bitset.trailing_zeros() as usize; self.bitset ^= t; let bit = self.block_idx + r; @@ -1170,7 +1172,7 @@ impl IntoOnes { let bit_idx = n.leading_zeros(); /* set that bit to zero */ - let mask = !((1 as usize) << (BITS as u32 - bit_idx - 1)); + let mask = !((1_usize) << (BITS as u32 - bit_idx - 1)); n.bitand_assign(mask); bit_idx as usize @@ -1249,7 +1251,7 @@ impl Iterator for IntoOnes { } // Ones will continue to return None once it first returns None. -impl<'a> FusedIterator for IntoOnes {} +impl FusedIterator for IntoOnes {} impl<'a> BitAnd for &'a FixedBitSet { type Output = FixedBitSet; From 5282a4dd88d28ba7cf76d186cbc1108b0c636441 Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 18:11:03 -0700 Subject: [PATCH 10/13] Get Masks working with blocks instead of usize --- src/block/avx2.rs | 6 ++++-- src/block/default.rs | 10 ++++++++++ src/block/mod.rs | 26 ++++++++++++++++++++++++++ src/block/sse2.rs | 2 +- src/block/wasm32.rs | 4 ++-- src/lib.rs | 25 ++++++++++++------------- 6 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/block/avx2.rs b/src/block/avx2.rs index 42ec5df..108872c 100644 --- a/src/block/avx2.rs +++ b/src/block/avx2.rs @@ -1,3 +1,5 @@ +#![allow(clippy::undocumented_unsafe_blocks)] + #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] @@ -20,12 +22,12 @@ impl Block { pub const BITS: usize = core::mem::size_of::() * 8; #[inline] - fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { + pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { unsafe { core::mem::transmute(self.0) } } #[inline] - const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { Self(unsafe { core::mem::transmute(array) }) } diff --git a/src/block/default.rs b/src/block/default.rs index 48d52f9..e1299ef 100644 --- a/src/block/default.rs +++ b/src/block/default.rs @@ -11,6 +11,16 @@ impl Block { pub const ALL: Self = Block(!0); pub const BITS: usize = core::mem::size_of::() * 8; + #[inline] + pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { + [self] + } + + #[inline] + pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + array.0 + } + #[inline] pub const fn is_empty(self) -> bool { self.0 == Self::NONE.0 diff --git a/src/block/mod.rs b/src/block/mod.rs index be64af9..fa77562 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -37,6 +37,32 @@ mod wasm32; #[cfg(target_arch = "wasm32")] pub use self::wasm32::*; +impl Block { + #[inline] + pub fn upper_mask(bits: usize) -> Self { + let mut array = [0_usize; Self::USIZE_COUNT]; + let (idx, rem) = crate::div_rem(bits, crate::BITS); + array[idx] = usize::max_value() << rem; + for value in array.iter_mut().skip(idx + 1) { + *value = usize::MAX; + } + Self::from_usize_array(array) + } + + #[inline] + pub fn lower_mask(bits: usize) -> Self { + !Self::upper_mask(bits) + } + + #[inline] + pub fn count_ones(self) -> u32 { + self.into_usize_array() + .into_iter() + .map(usize::count_ones) + .sum() + } +} + impl Eq for Block {} impl PartialOrd for Block { diff --git a/src/block/sse2.rs b/src/block/sse2.rs index 8ff86cc..6f61948 100644 --- a/src/block/sse2.rs +++ b/src/block/sse2.rs @@ -22,7 +22,7 @@ impl Block { } #[inline] - const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { Self(unsafe { core::mem::transmute(array) }) } diff --git a/src/block/wasm32.rs b/src/block/wasm32.rs index 96ab3f9..2dac999 100644 --- a/src/block/wasm32.rs +++ b/src/block/wasm32.rs @@ -17,12 +17,12 @@ impl Block { pub const BITS: usize = core::mem::size_of::() * 8; #[inline] - fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { + pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { unsafe { core::mem::transmute(self.0) } } #[inline] - const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { + pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { Self(unsafe { core::mem::transmute(array) }) } diff --git a/src/lib.rs b/src/lib.rs index ee7bf55..d8917b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -437,7 +437,7 @@ impl FixedBitSet { Masks::new(range, self.length) .map(|(block, mask)| { // SAFETY: Masks cannot return a block index that is out of range. - let value = unsafe { *self.get_unchecked(block) }; + let value = unsafe { *self.data.get_unchecked(block) }; (value & mask).count_ones() as usize }) .sum() @@ -452,7 +452,7 @@ impl FixedBitSet { pub fn set_range(&mut self, range: T, enabled: bool) { for (block, mask) in Masks::new(range, self.length) { // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { self.get_unchecked_mut(block) }; + let block = unsafe { self.data.get_unchecked_mut(block) }; if enabled { *block |= mask; } else { @@ -480,7 +480,7 @@ impl FixedBitSet { pub fn toggle_range(&mut self, range: T) { for (block, mask) in Masks::new(range, self.length) { // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { self.get_unchecked_mut(block) }; + let block = unsafe { self.data.get_unchecked_mut(block) }; *block ^= mask; } } @@ -870,9 +870,9 @@ impl<'a> FusedIterator for Union<'a> {} struct Masks { first_block: usize, - first_mask: usize, + first_mask: Block, last_block: usize, - last_mask: usize, + last_mask: Block, } impl Masks { @@ -888,33 +888,32 @@ impl Masks { length ); - let (first_block, first_rem) = div_rem(start, BITS); - let (last_block, last_rem) = div_rem(end, BITS); + let (first_block, first_rem) = div_rem(start, Block::BITS); + let (last_block, last_rem) = div_rem(end, Block::BITS); Masks { first_block, - first_mask: usize::max_value() << first_rem, + first_mask: Block::upper_mask(first_rem), last_block, - last_mask: (usize::max_value() >> 1) >> (BITS - last_rem - 1), - // this is equivalent to `MAX >> (BITS - x)` with correct semantics when x == 0. + last_mask: Block::lower_mask(last_rem), } } } impl Iterator for Masks { - type Item = (usize, usize); + type Item = (usize, Block); #[inline] fn next(&mut self) -> Option { match self.first_block.cmp(&self.last_block) { Ordering::Less => { let res = (self.first_block, self.first_mask); self.first_block += 1; - self.first_mask = !0; + self.first_mask = Block::ALL; Some(res) } Ordering::Equal => { let mask = self.first_mask & self.last_mask; - let res = if mask == 0 { + let res = if mask.is_empty() { None } else { Some((self.first_block, mask)) From 930909c837a396362e83e3f479f4cb238c374460 Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 22:13:40 -0700 Subject: [PATCH 11/13] Fix serde implementation --- src/lib.rs | 69 ++++++++++++++++++++++++----------------------- src/serde_impl.rs | 45 ++++++++++++++++++------------- 2 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d8917b1..d3d89c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,11 +37,12 @@ use core::iter::{Chain, ExactSizeIterator, FromIterator, FusedIterator}; use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index}; pub use range::IndexRange; -pub(crate) const BITS: usize = core::mem::size_of::() * 8; +pub(crate) const BITS: usize = core::mem::size_of::() * 8; #[cfg(feature = "serde")] pub(crate) const BYTES: usize = core::mem::size_of::(); -pub use block::Block; +use block::Block as SimdBlock; +pub type Block = usize; #[inline] fn div_rem(x: usize, denominator: usize) -> (usize, usize) { @@ -58,7 +59,7 @@ fn div_rem(x: usize, denominator: usize) -> (usize, usize) { /// [0,1,0]. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct FixedBitSet { - pub(crate) data: Vec, + pub(crate) data: Vec, /// length in bits pub(crate) length: usize, } @@ -75,10 +76,10 @@ impl FixedBitSet { /// Create a new **FixedBitSet** with a specific number of bits, /// all initially clear. pub fn with_capacity(bits: usize) -> Self { - let (mut blocks, rem) = div_rem(bits, Block::BITS); + let (mut blocks, rem) = div_rem(bits, SimdBlock::BITS); blocks += (rem > 0) as usize; FixedBitSet { - data: vec![Block::NONE; blocks], + data: vec![SimdBlock::NONE; blocks], length: bits, } } @@ -96,11 +97,11 @@ impl FixedBitSet { /// let bs = fixedbitset::FixedBitSet::with_capacity_and_blocks(4, data); /// assert_eq!(format!("{:b}", bs), "0010"); /// ``` - pub fn with_capacity_and_blocks>(bits: usize, blocks: I) -> Self { - let (mut n_blocks, rem) = div_rem(bits, Block::BITS); + pub fn with_capacity_and_blocks>(bits: usize, blocks: I) -> Self { + let (mut n_blocks, rem) = div_rem(bits, SimdBlock::BITS); n_blocks += (rem > 0) as usize; let mut bitset = FixedBitSet { - data: vec![Block::NONE; n_blocks], + data: vec![SimdBlock::NONE; n_blocks], length: bits, }; for (subblock, value) in bitset.as_mut_slice().iter_mut().zip(blocks.into_iter()) { @@ -111,20 +112,20 @@ impl FixedBitSet { /// Grow capacity to **bits**, all new bits initialized to zero pub fn grow(&mut self, bits: usize) { - let (mut blocks, rem) = div_rem(bits, Block::BITS); + let (mut blocks, rem) = div_rem(bits, SimdBlock::BITS); blocks += (rem > 0) as usize; if bits > self.length { self.length = bits; - self.data.resize(blocks, Block::NONE); + self.data.resize(blocks, SimdBlock::NONE); } } - unsafe fn get_unchecked(&self, subblock: usize) -> &usize { - &*self.data.as_ptr().cast::().add(subblock) + unsafe fn get_unchecked(&self, subblock: usize) -> &Block { + &*self.data.as_ptr().cast::().add(subblock) } - unsafe fn get_unchecked_mut(&mut self, subblock: usize) -> &mut usize { - &mut *self.data.as_mut_ptr().cast::().add(subblock) + unsafe fn get_unchecked_mut(&mut self, subblock: usize) -> &mut Block { + &mut *self.data.as_mut_ptr().cast::().add(subblock) } fn usize_len(&self) -> usize { @@ -236,7 +237,7 @@ impl FixedBitSet { #[inline] pub fn clear(&mut self) { for elt in &mut self.data { - *elt = Block::NONE + *elt = SimdBlock::NONE } } @@ -487,13 +488,13 @@ impl FixedBitSet { /// View the bitset as a slice of `Block` blocks #[inline] - pub fn as_slice(&self) -> &[usize] { + pub fn as_slice(&self) -> &[Block] { // SAFETY: The bits from both usize and Block are required to be reinterprettable, and // neither have any padding or alignment issues. The slice constructed is within bounds // of the underlying allocation. This function is called with a read-only borrow so // no other write can happen as long as the returned borrow lives. unsafe { - let ptr = self.data.as_ptr().cast::(); + let ptr = self.data.as_ptr().cast::(); core::slice::from_raw_parts(ptr, self.usize_len()) } } @@ -501,13 +502,13 @@ impl FixedBitSet { /// View the bitset as a mutable slice of `Block` blocks. Writing past the bitlength in the last /// will cause `contains` to return potentially incorrect results for bits past the bitlength. #[inline] - pub fn as_mut_slice(&mut self) -> &mut [usize] { + pub fn as_mut_slice(&mut self) -> &mut [Block] { // SAFETY: The bits from both usize and Block are required to be reinterprettable, and // neither have any padding or alignment issues. The slice constructed is within bounds // of the underlying allocation. This function is called with a mutable borrow so // no other read or write can happen as long as the returned borrow lives. unsafe { - let ptr = self.data.as_mut_ptr().cast::(); + let ptr = self.data.as_mut_ptr().cast::(); core::slice::from_raw_parts_mut(ptr, self.usize_len()) } } @@ -548,8 +549,8 @@ impl FixedBitSet { let mut data = unsafe { let mut data = ManuallyDrop::new(self.data); let ptr = data.as_mut_ptr().cast(); - let len = data.len() * Block::USIZE_COUNT; - let capacity = data.capacity() * Block::USIZE_COUNT; + let len = data.len() * SimdBlock::USIZE_COUNT; + let capacity = data.capacity() * SimdBlock::USIZE_COUNT; Vec::from_raw_parts(ptr, len, capacity) }; if data.is_empty() { @@ -647,7 +648,7 @@ impl FixedBitSet { } let mn = core::cmp::min(self.data.len(), other.data.len()); for wd in &mut self.data[mn..] { - *wd = Block::NONE; + *wd = SimdBlock::NONE; } } @@ -870,9 +871,9 @@ impl<'a> FusedIterator for Union<'a> {} struct Masks { first_block: usize, - first_mask: Block, + first_mask: SimdBlock, last_block: usize, - last_mask: Block, + last_mask: SimdBlock, } impl Masks { @@ -888,27 +889,27 @@ impl Masks { length ); - let (first_block, first_rem) = div_rem(start, Block::BITS); - let (last_block, last_rem) = div_rem(end, Block::BITS); + let (first_block, first_rem) = div_rem(start, SimdBlock::BITS); + let (last_block, last_rem) = div_rem(end, SimdBlock::BITS); Masks { first_block, - first_mask: Block::upper_mask(first_rem), + first_mask: SimdBlock::upper_mask(first_rem), last_block, - last_mask: Block::lower_mask(last_rem), + last_mask: SimdBlock::lower_mask(last_rem), } } } impl Iterator for Masks { - type Item = (usize, Block); + type Item = (usize, SimdBlock); #[inline] fn next(&mut self) -> Option { match self.first_block.cmp(&self.last_block) { Ordering::Less => { let res = (self.first_block, self.first_mask); self.first_block += 1; - self.first_mask = Block::ALL; + self.first_mask = SimdBlock::ALL; Some(res) } Ordering::Equal => { @@ -1143,8 +1144,8 @@ impl FromIterator for FixedBitSet { } pub struct IntoOnes { - bitset_front: usize, - bitset_back: usize, + bitset_front: Block, + bitset_back: Block, block_idx_front: usize, block_idx_back: usize, remaining_blocks: IntoIter, @@ -1152,7 +1153,7 @@ pub struct IntoOnes { impl IntoOnes { #[inline] - pub fn last_positive_bit_and_unset(n: &mut usize) -> usize { + pub fn last_positive_bit_and_unset(n: &mut Block) -> usize { // Find the last set bit using x & -x let last_bit = *n & n.wrapping_neg(); @@ -1166,7 +1167,7 @@ impl IntoOnes { } #[inline] - fn first_positive_bit_and_unset(n: &mut usize) -> usize { + fn first_positive_bit_and_unset(n: &mut Block) -> usize { /* Identify the first non zero bit */ let bit_idx = n.leading_zeros(); diff --git a/src/serde_impl.rs b/src/serde_impl.rs index 60e73fa..9823159 100644 --- a/src/serde_impl.rs +++ b/src/serde_impl.rs @@ -1,10 +1,11 @@ #[cfg(not(feature = "std"))] use core as std; -use crate::{FixedBitSet, BYTES}; +use crate::{Block, FixedBitSet, BYTES}; +use alloc::vec::Vec; +use core::{convert::TryFrom, fmt}; use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; use serde::ser::{Serialize, SerializeStruct, Serializer}; -use std::{convert::TryFrom, fmt}; struct BitSetByteSerializer<'a>(&'a FixedBitSet); @@ -28,7 +29,7 @@ impl<'a> Serialize for BitSetByteSerializer<'a> { let len = self.0.as_slice().len() * BYTES; // PERF: Figure out a way to do this without allocating. let mut temp = Vec::with_capacity(len); - for block in &self.0.as_slice() { + for block in self.0.as_slice() { temp.extend(&block.to_le_bytes()); } serializer.serialize_bytes(&temp) @@ -45,6 +46,22 @@ impl<'de> Deserialize<'de> for FixedBitSet { Data, } + fn bytes_to_data(length: usize, input: &[u8]) -> Vec { + let block_len = length / BYTES + 1; + let mut data = Vec::with_capacity(block_len); + for chunk in input.chunks(BYTES) { + match <&[u8; BYTES]>::try_from(chunk) { + Ok(bytes) => data.push(usize::from_le_bytes(*bytes)), + Err(_) => { + let mut bytes = [0u8; BYTES]; + bytes[0..BYTES].copy_from_slice(chunk); + data.push(usize::from_le_bytes(bytes)); + } + } + } + data + } + impl<'de> Deserialize<'de> for Field { fn deserialize(deserializer: D) -> Result where @@ -91,10 +108,11 @@ impl<'de> Deserialize<'de> for FixedBitSet { let length = seq .next_element()? .ok_or_else(|| de::Error::invalid_length(0, &self))?; - let data = seq + let data: &[u8] = seq .next_element()? .ok_or_else(|| de::Error::invalid_length(1, &self))?; - Ok(FixedBitSet { length, data }) + let data = bytes_to_data(length, data); + Ok(FixedBitSet::with_capacity_and_blocks(length, data)) } fn visit_map(self, mut map: V) -> Result @@ -120,20 +138,9 @@ impl<'de> Deserialize<'de> for FixedBitSet { } } let length = length.ok_or_else(|| de::Error::missing_field("length"))?; - let temp = temp.ok_or_else(|| de::Error::missing_field("data"))?; - let block_len = length / BYTES + 1; - let mut data = Vec::with_capacity(block_len); - for chunk in temp.chunks(BYTES) { - match <&[u8; BYTES]>::try_from(chunk) { - Ok(bytes) => data.push(usize::from_le_bytes(*bytes)), - Err(_) => { - let mut bytes = [0u8; BYTES]; - bytes[0..BYTES].copy_from_slice(chunk); - data.push(usize::from_le_bytes(bytes)); - } - } - } - Ok(FixedBitSet { length, data }) + let data = temp.ok_or_else(|| de::Error::missing_field("data"))?; + let data = bytes_to_data(length, data); + Ok(FixedBitSet::with_capacity_and_blocks(length, data)) } } From be79d1607d2d9326cc30040d1d31e847ccd94e4a Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 22:44:17 -0700 Subject: [PATCH 12/13] Revert "Get Masks working with blocks instead of usize" This reverts commit 5282a4dd88d28ba7cf76d186cbc1108b0c636441. --- src/block/avx2.rs | 2 -- src/block/default.rs | 10 ---------- src/block/mod.rs | 26 -------------------------- src/lib.rs | 26 ++++++++++++++------------ 4 files changed, 14 insertions(+), 50 deletions(-) diff --git a/src/block/avx2.rs b/src/block/avx2.rs index 108872c..00b835a 100644 --- a/src/block/avx2.rs +++ b/src/block/avx2.rs @@ -1,5 +1,3 @@ -#![allow(clippy::undocumented_unsafe_blocks)] - #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] diff --git a/src/block/default.rs b/src/block/default.rs index e1299ef..48d52f9 100644 --- a/src/block/default.rs +++ b/src/block/default.rs @@ -11,16 +11,6 @@ impl Block { pub const ALL: Self = Block(!0); pub const BITS: usize = core::mem::size_of::() * 8; - #[inline] - pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { - [self] - } - - #[inline] - pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { - array.0 - } - #[inline] pub const fn is_empty(self) -> bool { self.0 == Self::NONE.0 diff --git a/src/block/mod.rs b/src/block/mod.rs index fa77562..be64af9 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -37,32 +37,6 @@ mod wasm32; #[cfg(target_arch = "wasm32")] pub use self::wasm32::*; -impl Block { - #[inline] - pub fn upper_mask(bits: usize) -> Self { - let mut array = [0_usize; Self::USIZE_COUNT]; - let (idx, rem) = crate::div_rem(bits, crate::BITS); - array[idx] = usize::max_value() << rem; - for value in array.iter_mut().skip(idx + 1) { - *value = usize::MAX; - } - Self::from_usize_array(array) - } - - #[inline] - pub fn lower_mask(bits: usize) -> Self { - !Self::upper_mask(bits) - } - - #[inline] - pub fn count_ones(self) -> u32 { - self.into_usize_array() - .into_iter() - .map(usize::count_ones) - .sum() - } -} - impl Eq for Block {} impl PartialOrd for Block { diff --git a/src/lib.rs b/src/lib.rs index d3d89c7..03dac9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -438,7 +438,7 @@ impl FixedBitSet { Masks::new(range, self.length) .map(|(block, mask)| { // SAFETY: Masks cannot return a block index that is out of range. - let value = unsafe { *self.data.get_unchecked(block) }; + let value = unsafe { *self.get_unchecked(block) }; (value & mask).count_ones() as usize }) .sum() @@ -453,7 +453,7 @@ impl FixedBitSet { pub fn set_range(&mut self, range: T, enabled: bool) { for (block, mask) in Masks::new(range, self.length) { // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { self.data.get_unchecked_mut(block) }; + let block = unsafe { self.get_unchecked_mut(block) }; if enabled { *block |= mask; } else { @@ -481,7 +481,7 @@ impl FixedBitSet { pub fn toggle_range(&mut self, range: T) { for (block, mask) in Masks::new(range, self.length) { // SAFETY: Masks cannot return a block index that is out of range. - let block = unsafe { self.data.get_unchecked_mut(block) }; + let block = unsafe { self.get_unchecked_mut(block) }; *block ^= mask; } } @@ -871,9 +871,9 @@ impl<'a> FusedIterator for Union<'a> {} struct Masks { first_block: usize, - first_mask: SimdBlock, + first_mask: usize, last_block: usize, - last_mask: SimdBlock, + last_mask: usize, } impl Masks { @@ -889,32 +889,34 @@ impl Masks { length ); - let (first_block, first_rem) = div_rem(start, SimdBlock::BITS); - let (last_block, last_rem) = div_rem(end, SimdBlock::BITS); + let (first_block, first_rem) = div_rem(start, BITS); + let (last_block, last_rem) = div_rem(end, BITS); Masks { first_block, - first_mask: SimdBlock::upper_mask(first_rem), + first_mask: usize::max_value() << first_rem, last_block, - last_mask: SimdBlock::lower_mask(last_rem), + last_mask: (usize::max_value() >> 1) >> (BITS - last_rem - 1), + // this is equivalent to `MAX >> (BITS - x)` with correct semantics when x == 0. } } } impl Iterator for Masks { - type Item = (usize, SimdBlock); + type Item = (usize, usize); + #[inline] fn next(&mut self) -> Option { match self.first_block.cmp(&self.last_block) { Ordering::Less => { let res = (self.first_block, self.first_mask); self.first_block += 1; - self.first_mask = SimdBlock::ALL; + self.first_mask = !0; Some(res) } Ordering::Equal => { let mask = self.first_mask & self.last_mask; - let res = if mask.is_empty() { + let res = if mask == 0 { None } else { Some((self.first_block, mask)) From 1a5099bae55d2ee19ada688e75352ec3fb615bf5 Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 18 Mar 2024 22:54:40 -0700 Subject: [PATCH 13/13] Extend CI for wasm and features checking --- .github/workflows/rust.yml | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b259f66..1031050 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,9 +18,11 @@ jobs: strategy: matrix: rust: [1.56.0, stable, nightly] - + features: ["+avx2", "+sse2", "-avx2,-sse2"] + env: + RUSTCFLAGS: "-C target-features={{matrix.features}}" steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: profile: minimal @@ -43,7 +45,7 @@ jobs: rust: [stable] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: profile: minimal @@ -64,7 +66,7 @@ jobs: rust: [stable] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: profile: minimal @@ -85,7 +87,7 @@ jobs: rust: [stable] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: profile: minimal @@ -95,4 +97,16 @@ jobs: - name: Run Clippy run: | cd benches - cargo bench --bench benches --no-run \ No newline at end of file + cargo bench --bench benches --no-run + + build-wasm: + runs-on: ubuntu-latest + timeout-minutes: 30 + needs: build + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + target: wasm32-unknown-unknown + - name: Check wasm + run: cargo check --target wasm32-unknown-unknown \ No newline at end of file