From 2b4d85950109492a375c8359814b3e5fde81f8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Thu, 24 Oct 2019 20:52:51 +0200 Subject: [PATCH 1/5] Add the ability to run on no_std targets --- Cargo.toml | 6 ++++-- src/cfft1d.rs | 3 +++ src/cfft2d.rs | 3 +++ src/chirpz.rs | 3 +++ src/dct1d.rs | 3 +++ src/lib.rs | 5 +++++ src/mdct1d.rs | 3 +++ src/mixed_radix.rs | 3 +++ src/precompute_utils.rs | 6 ++++++ src/prime_factorization.rs | 3 +++ src/rfft1d.rs | 3 +++ 11 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 219c5f9..37e52f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,8 @@ bench = false test = true [dependencies] -num-traits = ">=0.0.0" -num-complex = ">=0.0.0" +num-traits = { version = ">=0.0.0", default-features = false } +num-complex = { version = ">=0.0.0", default-features = false } [dev-dependencies] rand = ">=0.7.0" @@ -28,4 +28,6 @@ version=">=0.0.0" features=["complex"] [features] +default = ["std"] docs = [] +std = ["num-traits/std", "num-complex/std"] diff --git a/src/cfft1d.rs b/src/cfft1d.rs index 65f971b..2efc673 100644 --- a/src/cfft1d.rs +++ b/src/cfft1d.rs @@ -15,6 +15,9 @@ use num_traits::float::{Float, FloatConst}; use num_traits::identities::{one, zero}; use num_traits::{cast, NumAssign}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + enum WorkData { MixedRadix(mixed_radix::MixedRadixData), ChirpZ(chirpz::ChirpzData), diff --git a/src/cfft2d.rs b/src/cfft2d.rs index d92bfd9..a64df5d 100644 --- a/src/cfft2d.rs +++ b/src/cfft2d.rs @@ -11,6 +11,9 @@ use num_traits::float::{Float, FloatConst}; use num_traits::identities::{one, zero}; use num_traits::{cast, NumAssign}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Perform a complex-to-complex two-dimensional Fourier transform /// /// diff --git a/src/chirpz.rs b/src/chirpz.rs index 860c104..0221514 100644 --- a/src/chirpz.rs +++ b/src/chirpz.rs @@ -12,6 +12,9 @@ use num_traits::float::Float; use num_traits::identities::{one, zero}; use num_traits::NumAssign; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + pub struct ChirpzData { pub level: usize, pub ids: Vec, diff --git a/src/dct1d.rs b/src/dct1d.rs index bf53fe8..6ba5d1c 100644 --- a/src/dct1d.rs +++ b/src/dct1d.rs @@ -12,6 +12,9 @@ use num_traits::float::{Float, FloatConst}; use num_traits::identities::{one, zero}; use num_traits::{cast, NumAssign}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Perform a discrete cosine transform /// /// # Example diff --git a/src/lib.rs b/src/lib.rs index e583e27..b4db95e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![crate_type = "lib"] +#![cfg_attr(not(feature = "std"), no_std)] //! Chalharu's Fastest Fourier Transform. //! @@ -7,6 +8,10 @@ //! version 2.0 (the "License"). You can obtain a copy of the License at //! http://mozilla.org/MPL/2.0/ . +#[cfg(not(feature = "std"))] +#[macro_use] +extern crate alloc; + mod chirpz; mod mixed_radix; mod precompute_utils; diff --git a/src/mdct1d.rs b/src/mdct1d.rs index 197efab..f02c510 100644 --- a/src/mdct1d.rs +++ b/src/mdct1d.rs @@ -12,6 +12,9 @@ use num_traits::float::{Float, FloatConst}; use num_traits::identities::zero; use num_traits::{cast, one, NumAssign}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Perform a Modified discrete cosine transform /// /// diff --git a/src/mixed_radix.rs b/src/mixed_radix.rs index 44dbfbb..28a65a8 100644 --- a/src/mixed_radix.rs +++ b/src/mixed_radix.rs @@ -12,6 +12,9 @@ use num_traits::float::{Float, FloatConst}; use num_traits::identities::one; use num_traits::{cast, NumAssign}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + pub struct MixedRadixData { pub ids: Vec, pub omega: Vec>, diff --git a/src/precompute_utils.rs b/src/precompute_utils.rs index f776b44..b8aa52c 100644 --- a/src/precompute_utils.rs +++ b/src/precompute_utils.rs @@ -10,7 +10,13 @@ use num_complex::Complex; use num_traits::cast; use num_traits::float::{Float, FloatConst}; use num_traits::identities::one; + +#[cfg(feature = "std")] use std::cmp; +#[cfg(not(feature = "std"))] +use core::cmp; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; #[inline] pub fn calc_omega_item(len: usize, position: usize) -> Complex { diff --git a/src/prime_factorization.rs b/src/prime_factorization.rs index 8dd966e..6fb63e3 100644 --- a/src/prime_factorization.rs +++ b/src/prime_factorization.rs @@ -5,6 +5,9 @@ //! version 2.0 (the "License"). You can obtain a copy of the License at //! http://mozilla.org/MPL/2.0/ . +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + pub struct Factor { pub value: usize, pub count: usize, diff --git a/src/rfft1d.rs b/src/rfft1d.rs index 5fa0a94..ff3e6d7 100644 --- a/src/rfft1d.rs +++ b/src/rfft1d.rs @@ -12,6 +12,9 @@ use num_traits::float::{Float, FloatConst}; use num_traits::identities::{one, zero}; use num_traits::{cast, NumAssign}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Perform a real-to-complex one-dimensional Fourier transform /// /// From 66f2a7c3c552a1d169f09c117e94f1c610435c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Thu, 24 Oct 2019 22:05:13 +0200 Subject: [PATCH 2/5] Temporarily use git version of num-traits until next major release --- Cargo.toml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 37e52f4..ef8037a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,8 @@ bench = false test = true [dependencies] -num-traits = { version = ">=0.0.0", default-features = false } -num-complex = { version = ">=0.0.0", default-features = false } +num-traits = { version = "0.2.8", default-features = false, features = ["libm"] } +num-complex = { version = "0.2.3", default-features = false } [dev-dependencies] rand = ">=0.7.0" @@ -31,3 +31,6 @@ features=["complex"] default = ["std"] docs = [] std = ["num-traits/std", "num-complex/std"] + +[patch.crates-io] +num-traits = { git = "https://github.com/rust-num/num-traits.git" } From bcfb156bcbf02a97ef291e7084d24c49dc0b808d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Thu, 24 Oct 2019 22:14:15 +0200 Subject: [PATCH 3/5] Fix uses of Complex::from_polar --- src/precompute_utils.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/precompute_utils.rs b/src/precompute_utils.rs index b8aa52c..cb92bf8 100644 --- a/src/precompute_utils.rs +++ b/src/precompute_utils.rs @@ -11,19 +11,20 @@ use num_traits::cast; use num_traits::float::{Float, FloatConst}; use num_traits::identities::one; -#[cfg(feature = "std")] -use std::cmp; -#[cfg(not(feature = "std"))] -use core::cmp; #[cfg(not(feature = "std"))] use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::cmp; +#[cfg(feature = "std")] +use std::cmp; #[inline] pub fn calc_omega_item(len: usize, position: usize) -> Complex { - Complex::from_polar( - &one(), - &(cast::<_, T>(-2.0).unwrap() * T::PI() / cast(len).unwrap() * cast(position).unwrap()), - ) + let r: T = one(); + let theta: T = + cast::<_, T>(-2.0).unwrap() * T::PI() / cast(len).unwrap() * cast(position).unwrap(); + // We can't use Complex::from_polar because it is at the time of writing not no_std compatible + Complex::new(r * theta.cos(), r * theta.sin()) } // ωの事前計算 From 0387606788706b1b5f3ef19881f08387e6ccd21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Sat, 26 Oct 2019 20:57:37 +0200 Subject: [PATCH 4/5] Break out separate libm feature and require either libm or std --- Cargo.toml | 3 ++- src/lib.rs | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index dc6aa9f..1c6a5b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ bench = false test = true [dependencies] -num-traits = { version = "0.2.8", default-features = false, features = ["libm"] } +num-traits = { version = "0.2.8", default-features = false } num-complex = { version = "0.2.3", default-features = false } [dev-dependencies] @@ -31,6 +31,7 @@ features=["complex"] default = ["std"] docs = [] std = ["num-traits/std", "num-complex/std"] +libm = ["num-traits/libm"] [patch.crates-io] num-traits = { git = "https://github.com/rust-num/num-traits.git" } diff --git a/src/lib.rs b/src/lib.rs index d7838d7..ce4828e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,9 @@ //! version 2.0 (the "License"). You can obtain a copy of the License at //! http://mozilla.org/MPL/2.0/ . +#[cfg(not(any(feature = "std", feature = "libm")))] +compile_error!("Either feature 'std' or feature 'libm' must be enabled for 'chfft' to work"); + #[cfg(not(feature = "std"))] #[macro_use] extern crate alloc; From de272fa6666128ced21f468face13f82f35e9e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Sun, 17 Nov 2019 18:30:58 +0100 Subject: [PATCH 5/5] Use released num-traits version with libm feature --- Cargo.toml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c6a5b7..b08a780 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,15 +16,15 @@ bench = false test = true [dependencies] -num-traits = { version = "0.2.8", default-features = false } +num-traits = { version = "0.2.9", default-features = false } num-complex = { version = "0.2.3", default-features = false } [dev-dependencies] -rand = ">=0.7.0" -rand_xorshift = ">=0.2.0" +rand = "0.7.2" +rand_xorshift = "0.2.0" [dev-dependencies.appro-eq] -version=">=0.0.0" +version= "0.3.0" features=["complex"] [features] @@ -32,6 +32,3 @@ default = ["std"] docs = [] std = ["num-traits/std", "num-complex/std"] libm = ["num-traits/libm"] - -[patch.crates-io] -num-traits = { git = "https://github.com/rust-num/num-traits.git" }