Skip to content

Commit

Permalink
Fix ptr write alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
richarddavison authored and Licenser committed Jan 5, 2024
1 parent c83335a commit b7fb9dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/impls/native/stage1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ impl Stage1Parse for SimdInput {
base.reserve(64);
let final_len = l + cnt;

let is_unaligned = l % 4 != 0;
let write_fn = if is_unaligned {
std::ptr::write_unaligned
} else {
std::ptr::write
};

while bits != 0 {
let v0 = bits.trailing_zeros() as i32;
bits &= bits.wrapping_sub(1);
Expand All @@ -461,7 +468,7 @@ impl Stage1Parse for SimdInput {
idx_64_v[2] + v2,
idx_64_v[3] + v3,
];
std::ptr::write(base.as_mut_ptr().add(l).cast::<[i32; 4]>(), v);
write_fn(base.as_mut_ptr().add(l).cast::<[i32; 4]>(), v);
l += 4;
}
// We have written all the data
Expand Down
9 changes: 8 additions & 1 deletion src/impls/neon/stage1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ impl Stage1Parse for SimdInput {
base.reserve(64);
let final_len = l + cnt;

let is_unaligned = l % 4 != 0;
let write_fn = if is_unaligned {
std::ptr::write_unaligned
} else {
std::ptr::write
};

while bits != 0 {
let v0 = bits.trailing_zeros() as i32;
bits &= bits.wrapping_sub(1);
Expand All @@ -202,7 +209,7 @@ impl Stage1Parse for SimdInput {

let v: int32x4_t = mem::transmute([v0, v1, v2, v3]);
let v: int32x4_t = vaddq_s32(idx_64_v, v);
std::ptr::write(base.as_mut_ptr().add(l).cast::<int32x4_t>(), v);
write_fn(base.as_mut_ptr().add(l).cast::<int32x4_t>(), v);
l += 4;
}
// We have written all the data
Expand Down

0 comments on commit b7fb9dc

Please sign in to comment.