Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add force-scalar feature #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ default = ["std", "const-generics"]
std = []
nightly = []
const-generics = []
force-scalar = []

[dev-dependencies]
rand = "0.8"
Expand Down
12 changes: 10 additions & 2 deletions src/imp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#[cfg(not(feature = "force-scalar"))]
pub mod avx2;
#[cfg(not(feature = "force-scalar"))]
pub mod avx512;
pub mod scalar;
#[cfg(not(feature = "force-scalar"))]
pub mod sse2;
#[cfg(not(feature = "force-scalar"))]
pub mod ssse3;
#[cfg(not(feature = "force-scalar"))]
pub mod wasm;

pub type Adler32Imp = fn(u16, u16, &[u8]) -> (u16, u16);
Expand All @@ -14,10 +19,13 @@ pub const fn _MM_SHUFFLE(z: u32, y: u32, x: u32, w: u32) -> i32 {
}

pub fn get_imp() -> Adler32Imp {
avx512::get_imp()
#[cfg(feature = "force-scalar")]
return scalar::update;
#[cfg(not(feature = "force-scalar"))]
return avx512::get_imp()
.or_else(avx2::get_imp)
.or_else(ssse3::get_imp)
.or_else(sse2::get_imp)
.or_else(wasm::get_imp)
.unwrap_or(scalar::update)
.unwrap_or(scalar::update);
}
19 changes: 12 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
//! adler.write(b"rust is pretty cool, man");
//! let hash = adler.finish();
//!
//! println!("{}", hash);
//! // 1921255656
//! println!("{}", hash); // 1921255656
//! # assert_eq!(hash, 1921255656);
//! ```
//!
//! ## Feature flags
Expand Down Expand Up @@ -164,7 +164,8 @@ impl Adler32 {
/// use simd_adler32::adler32;
///
/// let hash = adler32(b"Adler-32");
/// println!("{}", hash); // 800813569
/// println!("{}", hash); // 204735099
/// # assert_eq!(hash, 204735099);
/// ```
pub fn adler32<H: Adler32Hash>(hash: &H) -> u32 {
hash.hash()
Expand Down Expand Up @@ -198,7 +199,8 @@ pub mod read {
//! let mut reader = Cursor::new(b"Hello there");
//! let hash = adler32(&mut reader).unwrap();
//!
//! println!("{}", hash) // 800813569
//! println!("{}", hash); // 409338925
//! # assert_eq!(hash, 409338925);
//! ```
use crate::Adler32;
use std::io::{Read, Result};
Expand All @@ -213,7 +215,8 @@ pub mod read {
/// let mut reader = Cursor::new(b"Hello there");
/// let hash = adler32(&mut reader).unwrap();
///
/// println!("{}", hash) // 800813569
/// println!("{}", hash); // 409338925
/// # assert_eq!(hash, 409338925);
/// ```
pub fn adler32<R: Read>(reader: &mut R) -> Result<u32> {
let mut hash = Adler32::new();
Expand Down Expand Up @@ -246,7 +249,8 @@ pub mod bufread {
//! let mut reader = BufReader::new(reader);
//! let hash = adler32(&mut reader).unwrap();
//!
//! println!("{}", hash) // 800813569
//! println!("{}", hash); // 409338925
//! # assert_eq!(hash, 409338925);
//! ```
use crate::Adler32;
use std::io::{BufRead, ErrorKind, Result};
Expand All @@ -262,7 +266,8 @@ pub mod bufread {
/// let mut reader = BufReader::new(reader);
/// let hash = adler32(&mut reader).unwrap();
///
/// println!("{}", hash) // 800813569
/// println!("{}", hash); // 409338925
/// # assert_eq!(hash, 409338925);
/// ```
pub fn adler32<R: BufRead>(reader: &mut R) -> Result<u32> {
let mut hash = Adler32::new();
Expand Down