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

Fix x86 compilation #376

Merged
merged 8 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ codegen-units = 1
lto = true
opt-level = 3
panic = "abort"

[lints.rust]
unexpected_cfgs = { level = "allow", check-cfg = [
# Legacy code
'cfg(portable)',
Comment on lines +152 to +153
Copy link
Contributor Author

@PigeonF PigeonF Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not too sure about the portable config (i.e. is the config supposed to be "hidden", or is this just remainder from previous code), so I have elected to ignore the warning about it here.

# Tool specific configurations
'cfg(tarpaulin_include)',
] }
24 changes: 8 additions & 16 deletions src/impls/avx2/deser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(target_arch = "x86")]
use std::arch::x86::{
__m256i, _mm256_cmpeq_epi8, _mm256_loadu_si256, _mm256_movemask_epi8, _mm256_set1_epi8,
_mm256_storeu_si256,
};
use std::arch::x86 as arch;

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::{
use std::arch::x86_64 as arch;

use arch::{
__m256i, _mm256_cmpeq_epi8, _mm256_loadu_si256, _mm256_movemask_epi8, _mm256_set1_epi8,
_mm256_storeu_si256,
};
Expand Down Expand Up @@ -46,8 +46,7 @@ pub(crate) unsafe fn parse_str<'invoke, 'de>(
loop {
// _mm256_loadu_si256 does not require alignment
#[allow(clippy::cast_ptr_alignment)]
let v: __m256i =
_mm256_loadu_si256(src.as_ptr().add(src_i).cast::<std::arch::x86_64::__m256i>());
let v: __m256i = _mm256_loadu_si256(src.as_ptr().add(src_i).cast::<__m256i>());

// store to dest unconditionally - we can overwrite the bits we don't like
// later
Expand Down Expand Up @@ -97,18 +96,11 @@ pub(crate) unsafe fn parse_str<'invoke, 'de>(
loop {
// _mm256_loadu_si256 does not require alignment
#[allow(clippy::cast_ptr_alignment)]
let v: __m256i =
_mm256_loadu_si256(src.as_ptr().add(src_i).cast::<std::arch::x86_64::__m256i>());
let v: __m256i = _mm256_loadu_si256(src.as_ptr().add(src_i).cast::<__m256i>());

// _mm256_storeu_si256 does not require alignment
#[allow(clippy::cast_ptr_alignment)]
_mm256_storeu_si256(
buffer
.as_mut_ptr()
.add(dst_i)
.cast::<std::arch::x86_64::__m256i>(),
v,
);
_mm256_storeu_si256(buffer.as_mut_ptr().add(dst_i).cast::<__m256i>(), v);

// store to dest unconditionally - we can overwrite the bits we don't like
// later
Expand Down
38 changes: 23 additions & 15 deletions src/impls/avx2/stage1.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#![allow(dead_code)]
use crate::{static_cast_i32, static_cast_i64, static_cast_u32, Stage1Parse};
#[cfg(target_arch = "x86")]
use std::arch::x86::{
__m256i, _mm256_add_epi32, _mm256_and_si256, _mm256_cmpeq_epi8, _mm256_loadu_si256,
_mm256_max_epu8, _mm256_movemask_epi8, _mm256_set1_epi8, _mm256_set_epi32, _mm256_setr_epi8,
_mm256_setzero_si256, _mm256_shuffle_epi8, _mm256_srli_epi32, _mm256_storeu_si256,
_mm_clmulepi64_si128, _mm_cvtsi128_si64, _mm_set1_epi8, _mm_set_epi64x,
};
use std::arch::x86 as arch;

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::{
use std::arch::x86_64 as arch;

use arch::{
__m256i, _mm256_add_epi32, _mm256_and_si256, _mm256_cmpeq_epi8, _mm256_loadu_si256,
_mm256_max_epu8, _mm256_movemask_epi8, _mm256_set1_epi8, _mm256_set_epi32, _mm256_setr_epi8,
_mm256_setzero_si256, _mm256_shuffle_epi8, _mm256_srli_epi32, _mm256_storeu_si256,
_mm_clmulepi64_si128, _mm_cvtsi128_si64, _mm_set1_epi8, _mm_set_epi64x,
_mm_clmulepi64_si128, _mm_set1_epi8, _mm_set_epi64x,
};

macro_rules! low_nibble_mask {
Expand Down Expand Up @@ -56,14 +54,29 @@ impl Stage1Parse for SimdInput {
#[cfg_attr(not(feature = "no-inline"), inline)]
#[allow(clippy::cast_sign_loss)]
#[target_feature(enable = "avx2")]
#[cfg(target_arch = "x86_64")]
unsafe fn compute_quote_mask(quote_bits: u64) -> u64 {
_mm_cvtsi128_si64(_mm_clmulepi64_si128(
std::arch::x86_64::_mm_cvtsi128_si64(_mm_clmulepi64_si128(
_mm_set_epi64x(0, static_cast_i64!(quote_bits)),
_mm_set1_epi8(-1_i8 /* 0xFF */),
0,
)) as u64
}

#[cfg_attr(not(feature = "no-inline"), inline)]
#[allow(clippy::cast_sign_loss)]
#[target_feature(enable = "avx2")]
#[cfg(target_arch = "x86")]
unsafe fn compute_quote_mask(quote_bits: u64) -> u64 {
let mut quote_mask: u64 = quote_bits ^ (quote_bits << 1);
quote_mask = quote_mask ^ (quote_mask << 2);
quote_mask = quote_mask ^ (quote_mask << 4);
quote_mask = quote_mask ^ (quote_mask << 8);
quote_mask = quote_mask ^ (quote_mask << 16);
quote_mask = quote_mask ^ (quote_mask << 32);
quote_mask
}

/// a straightforward comparison of a mask against input
#[cfg_attr(not(feature = "no-inline"), inline)]
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
Expand Down Expand Up @@ -215,12 +228,7 @@ impl Stage1Parse for SimdInput {

let v: __m256i = _mm256_set_epi32(v7, v6, v5, v4, v3, v2, v1, v0);
let v: __m256i = _mm256_add_epi32(idx_64_v, v);
_mm256_storeu_si256(
base.as_mut_ptr()
.add(l)
.cast::<std::arch::x86_64::__m256i>(),
v,
);
_mm256_storeu_si256(base.as_mut_ptr().add(l).cast::<__m256i>(), v);
l += 8;
}
// We have written all the data
Expand Down
52 changes: 39 additions & 13 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,48 +628,62 @@ mod test {

#[test]
fn option_field_absent() {
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Person {
pub name: String,
pub middle_name: Option<String>,
pub friends: Vec<String>,
}
let mut raw_json = r#"{"name":"bob","friends":[]}"#.to_string();
let result: Result<Person, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(
result,
Ok(Person {
name: "bob".to_string(),
middle_name: None,
friends: vec![],
})
);
}
#[test]
fn option_field_present() {
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Person {
pub name: String,
pub middle_name: Option<String>,
pub friends: Vec<String>,
}
let mut raw_json = r#"{"name":"bob","middle_name": "frank", "friends":[]}"#.to_string();
let result: Result<Person, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(
result,
Ok(Person {
name: "bob".to_string(),
middle_name: Some("frank".to_string()),
friends: vec![],
})
);
}

#[test]
fn convert_enum() {
#[allow(dead_code)]
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "type")]
enum Message {
Request { id: usize, method: String },
Response { id: String, result: String },
}

#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "type", content = "v")]
pub enum Color {
Red(String), // TODO: If `content` flag is present, `Red` works and `Green` doesn't
Green { v: bool },
Blue,
}

#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "type")]
pub enum Color1 {
Red(String),
Expand All @@ -679,27 +693,39 @@ mod test {

let mut raw_json = r#"{"type": "Request", "id": 1, "method": "..."}"#.to_string();
let result: Result<Message, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(
result,
Ok(Message::Request {
id: 1,
method: "...".to_string()
})
);

let mut raw_json = r#"{"type": "Response", "id": "1", "result": "..."}"#.to_string();
let result: Result<Message, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(
result,
Ok(Message::Response {
id: "1".to_string(),
result: "...".to_string()
})
);

let mut raw_json = r#"{"type": "Red", "v": "1"}"#.to_string();
let result: Result<Color, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(result, Ok(Color::Red("1".to_string())));

let mut raw_json = r#"{"type": "Blue"}"#.to_string();
let result: Result<Color, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(result, Ok(Color::Blue));

let mut raw_json = r#"{"type": "Green", "v": false}"#.to_string();
let result: Result<Color1, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(result, Ok(Color1::Green { v: false }));

let mut raw_json = r#"{"type": "Blue"}"#.to_string();
let result: Result<Color1, _> = super::from_slice(unsafe { raw_json.as_bytes_mut() });
assert!(result.is_ok());
assert_eq!(result, Ok(Color1::Blue));
}

#[derive(serde_ext::Deserialize)]
Expand Down
25 changes: 20 additions & 5 deletions src/serde/value/borrowed/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ mod test {

#[test]
fn option_field_absent_owned() {
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Person {
pub name: String,
pub middle_name: Option<String>,
Expand All @@ -835,16 +835,23 @@ mod test {
let result: Result<Person, _> =
crate::to_borrowed_value(unsafe { raw_json.as_bytes_mut() })
.and_then(super::super::from_value);
assert!(result.is_ok());
assert_eq!(
result,
Ok(Person {
name: "bob".to_string(),
middle_name: None,
friends: vec![]
})
);
}
#[test]
fn option_field_present_owned() {
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Point {
pub x: u64,
pub y: u64,
}
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Person {
pub name: String,
pub middle_name: Option<String>,
Expand All @@ -857,7 +864,15 @@ mod test {
let result: Result<Person, _> =
crate::to_borrowed_value(unsafe { raw_json.as_bytes_mut() })
.and_then(super::super::from_value);
assert!(result.is_ok());
assert_eq!(
result,
Ok(Person {
name: "bob".to_string(),
middle_name: Some("frank".to_string()),
friends: vec![],
pos: Point { x: 0, y: 1 },
})
);
}

#[test]
Expand Down
25 changes: 20 additions & 5 deletions src/serde/value/owned/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ mod test {

#[test]
fn option_field_absent_owned() {
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Person {
pub name: String,
pub middle_name: Option<String>,
Expand All @@ -810,16 +810,23 @@ mod test {
let mut raw_json = r#"{"name":"bob","friends":[]}"#.to_string();
let result: Result<Person, _> = crate::to_owned_value(unsafe { raw_json.as_bytes_mut() })
.and_then(super::super::from_value);
assert!(result.is_ok());
assert_eq!(
result,
Ok(Person {
name: "bob".to_string(),
middle_name: None,
friends: vec![]
})
);
}
#[test]
fn option_field_present_owned() {
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Point {
pub x: u64,
pub y: u64,
}
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub struct Person {
pub name: String,
pub middle_name: Option<String>,
Expand All @@ -830,7 +837,15 @@ mod test {
r#"{"name":"bob","middle_name": "frank", "friends":[], "pos": [1,2]}"#.to_string();
let result: Result<Person, _> = crate::to_owned_value(unsafe { raw_json.as_bytes_mut() })
.and_then(super::super::from_value);
assert!(result.is_ok());
assert_eq!(
result,
Ok(Person {
name: "bob".to_string(),
middle_name: Some("frank".to_string()),
friends: vec![],
pos: Point { x: 1, y: 2 }
})
);
}

#[test]
Expand Down