Skip to content

Commit

Permalink
seec-bitmatrix: remove transmutes
Browse files Browse the repository at this point in the history
Thanks to https://doc.rust-lang.org/std/simd/trait.ToBytes.html
it is possible to remove all transmutes in the portable_transpose
 implementation.
  • Loading branch information
robinhundt committed May 7, 2024
1 parent 238c0fb commit f21114a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
id: toolchain
with:
toolchain: nightly-2024-05-03
# for some reason aws-lc-sys's build script needs rustfmt...
components: "rustfmt"
- name: Override default toolchain
run: rustup override set ${{steps.toolchain.outputs.name}}
- run: cargo --version
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
id: toolchain
with:
toolchain: nightly-2024-05-03
# for some reason aws-lc-sys's build script needs rustfmt...
components: "rustfmt"
- name: Override default toolchain
run: rustup override set ${{steps.toolchain.outputs.name}}
- run: cargo --version
Expand Down
5 changes: 4 additions & 1 deletion crates/seec-bitmatrix/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[rustversion::nightly]
fn main() {
println!("cargo::rustc-check-cfg=cfg(is_nightly)");
println!("cargo:rustc-cfg=is_nightly");
}

#[rustversion::not(nightly)]
fn main() {}
fn main() {
println!("cargo::rustc-check-cfg=cfg(is_nightly)");
}
10 changes: 5 additions & 5 deletions crates/seec-bitmatrix/src/portable_transpose.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Storage;
use std::mem;
use std::simd::num::SimdUint;
use std::simd::{i8x16, u16x8, u64x2, u8x16, Mask, Simd};
use std::simd::{i8x16, u16x8, u64x2, u8x16, Mask, Simd, ToBytes};

#[inline]
#[allow(unused)]
Expand Down Expand Up @@ -100,10 +100,10 @@ pub(crate) fn transpose<T: Storage>(input: &[T], nrows: usize, ncols: usize) ->
*(input.get_unchecked(inp(rr, cc)) as *const _ as *const u16),
]);
for i in (0..8).rev() {
h = _mm_movemask_epi8(mem::transmute(v)).to_le_bytes();
h = _mm_movemask_epi8(v.to_le_bytes()).to_le_bytes();
*byte_output.get_unchecked_mut(out(rr, cc + i)) = h[0];
*byte_output.get_unchecked_mut(out(rr, cc + i + 8)) = h[1];
v = mem::transmute(_mm_slli_epi64::<1>(mem::transmute(v)));
v = Simd::from_ne_bytes(_mm_slli_epi64::<1>(v.to_le_bytes()));
}
cc += 16;
}
Expand Down Expand Up @@ -131,9 +131,9 @@ fn _mm_movemask_epi8(a: Simd<u8, 16>) -> u16 {
}

fn _mm_slli_epi64<const IMM8: i32>(a: Simd<u8, 16>) -> Simd<u8, 16> {
let mut a: u64x2 = unsafe { mem::transmute(a) };
let mut a: u64x2 = Simd::from_le_bytes(a);
a <<= u64x2::splat(IMM8 as u64);
unsafe { mem::transmute(a) }
a.to_le_bytes()
}

#[cfg(test)]
Expand Down

0 comments on commit f21114a

Please sign in to comment.