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

(Re)introduce simd feature to pulldown-cmark-escape #880

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions pulldown-cmark-escape/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ categories = ["text-processing"]
edition = "2021"
rust-version = "1.70" # Update README.md and azure-pipelines.yml if this changes.
readme = "../README.md"

[features]
simd = []
13 changes: 6 additions & 7 deletions pulldown-cmark-escape/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ fn escape_html_scalar<W: StrWrite>(
mod simd {
use super::StrWrite;
use std::arch::x86_64::*;
use std::io;
use std::mem::size_of;

const VECTOR_SIZE: usize = size_of::<__m128i>();
Expand All @@ -280,7 +279,7 @@ mod simd {
mut w: W,
s: &str,
table: &'static [u8; 256],
) -> io::Result<()> {
) -> Result<(), W::Error> {
// The SIMD accelerated code uses the PSHUFB instruction, which is part
// of the SSSE3 instruction set. Further, we can only use this code if
// the buffer is at least one VECTOR_SIZE in length to prevent reading
Expand Down Expand Up @@ -361,13 +360,13 @@ mod simd {
/// Make sure to only call this when `bytes.len() >= 16`, undefined behaviour may
/// occur otherwise.
#[target_feature(enable = "ssse3")]
unsafe fn foreach_special_simd<F>(
unsafe fn foreach_special_simd<E, F>(
bytes: &[u8],
mut offset: usize,
mut callback: F,
) -> io::Result<()>
) -> Result<(), E>
where
F: FnMut(usize) -> io::Result<()>,
F: FnMut(usize) -> Result<(), E>,
{
// The strategy here is to walk the byte buffer in chunks of VECTOR_SIZE (16)
// bytes at a time starting at the given offset. For each chunk, we compute a
Expand Down Expand Up @@ -410,7 +409,7 @@ mod simd {
unsafe {
super::foreach_special_simd("&aXaaaa.a'aa9a<>aab&".as_bytes(), 0, |ix| {
#[allow(clippy::unit_arg)]
Ok(vec.push(ix))
Ok::<_, std::fmt::Error>(vec.push(ix))
})
.unwrap();
}
Expand All @@ -427,7 +426,7 @@ mod simd {
unsafe {
super::foreach_special_simd(&vek, 0, |_| {
match_count += 1;
Ok(())
Ok::<_, std::fmt::Error>(())
})
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion pulldown-cmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ bincode = "1.3.1"
[features]
default = ["getopts", "html"]
gen-tests = []
simd = []
simd = ["pulldown-cmark-escape?/simd"]
html = ["pulldown-cmark-escape"]