Skip to content

Commit

Permalink
fakesimd is now also verified with clippy.
Browse files Browse the repository at this point in the history
  • Loading branch information
yotarok committed Sep 29, 2023
1 parent 07ee750 commit c6e5eeb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ jobs:
command: clippy
args: --tests -- -D warnings
directory: flacenc-bin
- name: Clippy (with fakesimd feature)
uses: ClementTsang/[email protected]
with:
command: clippy
args: --tests --features "fakesimd" -- -D warnings
14 changes: 10 additions & 4 deletions codehealth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@

set -e

rustup default nightly
cargo rustc --release -- -D warnings
cargo build --release
cargo test
cargo fmt --check
cargo clippy --tests
cargo clippy --tests -- -D warnings
cargo doc

pushd flacenc-bin
cargo build --release
cargo test
cargo fmt --check
cargo clippy --tests
cargo doc
cargo clippy --tests -- -D warnings
popd

rustup default stable
cargo rustc --release --features "fakesimd" -- -D warnings
cargo build --release --features "fakesimd"
cargo clippy --tests --features "fakesimd"
cargo test --features "fakesimd"
rustup default nightly
63 changes: 33 additions & 30 deletions src/fakesimd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Fake SIMD module is a minimal subset of std::simd of "portable_simd"
//! feature in a nightly rust. This module only implement the functions
//! that are used in flacenc.
//! Fake SIMD module is a minimal subset of `std::simd` of `portable_simd`
//! feature in a nightly rust. This module only implement the functions that
//! are used in flacenc.

use std::array;

// ===
// TYPE DEFINITION
// ===
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
pub struct Simd<T: SimdElement, const LANES: usize>([T; LANES]);

#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
pub struct Mask<T: SimdElement, const LANES: usize> {
mask: [bool; LANES],
phantom_data: std::marker::PhantomData<T>,
Expand All @@ -38,11 +38,11 @@ pub trait SimdElement: Copy + std::fmt::Debug {
}

impl SimdElement for i32 {
type Mask = i32;
type Mask = Self;
}

impl SimdElement for u32 {
type Mask = u32;
type Mask = Self;
}

pub trait SupportedLaneCount {}
Expand All @@ -61,7 +61,9 @@ pub trait SimdUint {

pub trait SimdInt {
type Scalar;
#[allow(clippy::return_self_not_must_use)]
fn abs(self) -> Self;
#[allow(clippy::return_self_not_must_use)]
fn signum(self) -> Self;
fn reduce_sum(self) -> Self::Scalar;
}
Expand Down Expand Up @@ -112,6 +114,7 @@ where
Self([v; N])
}

#[allow(clippy::return_self_not_must_use)]
#[inline]
pub fn rotate_lanes_right<const OFFSET: usize>(self) -> Self {
Self(array::from_fn(|i| self.0[(i + N - OFFSET) % N]))
Expand Down Expand Up @@ -147,12 +150,12 @@ where
type Scalar = T;
#[inline]
fn abs(self) -> Self {
Simd(array::from_fn(|i| num_traits::sign::abs(self.0[i])))
Self(array::from_fn(|i| num_traits::sign::abs(self.0[i])))
}

#[inline]
fn signum(self) -> Self {
Simd(array::from_fn(|i| num_traits::sign::signum(self.0[i])))
Self(array::from_fn(|i| num_traits::sign::signum(self.0[i])))
}

#[inline]
Expand Down Expand Up @@ -256,65 +259,65 @@ where
}
}

impl<T, const N: usize> std::ops::Add<Simd<T, N>> for Simd<T, N>
impl<T, const N: usize> std::ops::Add<Self> for Simd<T, N>
where
T: SimdElement + std::ops::Add<T, Output = T>,
{
type Output = Simd<T, N>;
type Output = Self;
#[inline]
fn add(self, rhs: Simd<T, N>) -> Self::Output {
Simd(array::from_fn(|i| self.0[i] + rhs.0[i]))
fn add(self, rhs: Self) -> Self::Output {
Self(array::from_fn(|i| self.0[i] + rhs.0[i]))
}
}

impl<T, const N: usize> std::ops::Sub<Simd<T, N>> for Simd<T, N>
impl<T, const N: usize> std::ops::Sub<Self> for Simd<T, N>
where
T: SimdElement + std::ops::Sub<T, Output = T>,
{
type Output = Simd<T, N>;
type Output = Self;
#[inline]
fn sub(self, rhs: Simd<T, N>) -> Self::Output {
Simd(array::from_fn(|i| self.0[i] - rhs.0[i]))
fn sub(self, rhs: Self) -> Self::Output {
Self(array::from_fn(|i| self.0[i] - rhs.0[i]))
}
}

impl<T, const N: usize> std::ops::Mul<Simd<T, N>> for Simd<T, N>
impl<T, const N: usize> std::ops::Mul<Self> for Simd<T, N>
where
T: SimdElement + std::ops::Mul<T, Output = T>,
{
type Output = Simd<T, N>;
type Output = Self;
#[inline]
fn mul(self, rhs: Simd<T, N>) -> Self::Output {
Simd(array::from_fn(|i| self.0[i] * rhs.0[i]))
fn mul(self, rhs: Self) -> Self::Output {
Self(array::from_fn(|i| self.0[i] * rhs.0[i]))
}
}

impl<T, const N: usize> std::ops::Div<Simd<T, N>> for Simd<T, N>
impl<T, const N: usize> std::ops::Div<Self> for Simd<T, N>
where
T: SimdElement + std::ops::Div<T, Output = T>,
{
type Output = Simd<T, N>;
type Output = Self;
#[inline]
fn div(self, rhs: Simd<T, N>) -> Self::Output {
Simd(array::from_fn(|i| self.0[i] / rhs.0[i]))
fn div(self, rhs: Self) -> Self::Output {
Self(array::from_fn(|i| self.0[i] / rhs.0[i]))
}
}

impl<T, const N: usize> std::ops::BitAnd<Simd<T, N>> for Simd<T, N>
impl<T, const N: usize> std::ops::BitAnd<Self> for Simd<T, N>
where
T: SimdElement + std::ops::BitAnd<T, Output = T>,
{
type Output = Simd<T, N>;
type Output = Self;
#[inline]
fn bitand(self, rhs: Simd<T, N>) -> Self::Output {
Simd(array::from_fn(|i| self.0[i] & rhs.0[i]))
fn bitand(self, rhs: Self) -> Self::Output {
Self(array::from_fn(|i| self.0[i] & rhs.0[i]))
}
}

impl<U, T, const N: usize> std::ops::SubAssign<U> for Simd<T, N>
where
T: SimdElement,
Simd<T, N>: std::ops::Sub<U, Output = Simd<T, N>>,
Self: std::ops::Sub<U, Output = Self>,
{
#[inline]
fn sub_assign(&mut self, rhs: U) {
Expand Down
4 changes: 2 additions & 2 deletions src/lpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ where
F: Fn(usize) -> f32,
{
assert!(dest.len() >= order);
for p in dest.iter_mut() {
for p in &mut *dest {
*p = 0.0;
}
for t in (order - 1)..signal.len() {
Expand Down Expand Up @@ -416,7 +416,7 @@ pub fn symmetric_levinson_recursion(coefs: &[f32], ys: &[f32], dest: &mut [f32])
assert!(dest.len() >= ys.len());
assert!(coefs.len() >= ys.len());

for p in dest.iter_mut() {
for p in &mut *dest {
*p = 0.0;
}

Expand Down

0 comments on commit c6e5eeb

Please sign in to comment.