diff --git a/Cargo.toml b/Cargo.toml index 1b78d45..f5bbeac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "shallow_water", "gridgeneration", "heat-equation", + "utils/float", ] default-members = ["sbp", "euler", "maxwell", "shallow_water"] diff --git a/sbp/Cargo.toml b/sbp/Cargo.toml index ac71d9a..552b73d 100644 --- a/sbp/Cargo.toml +++ b/sbp/Cargo.toml @@ -12,11 +12,12 @@ rayon = { version = "1.3.0", optional = true } sprs = { version = "0.10.0", optional = true, default-features = false } serde = { version = "1.0.115", optional = true, default-features = false, features = ["derive"] } num-traits = "0.2.14" +float = { path = "../utils/float" } [features] # Use f32 as precision, default is f64 -f32 = [] -fast-float = [] +f32 = ["float/f32"] +fast-float = ["float/fast-float"] sparse = ["sprs"] serde1 = ["serde", "ndarray/serde"] diff --git a/sbp/src/lib.rs b/sbp/src/lib.rs index 52a1afa..63a58d7 100644 --- a/sbp/src/lib.rs +++ b/sbp/src/lib.rs @@ -3,20 +3,7 @@ #![feature(array_chunks)] #![feature(const_fn_floating_point_arithmetic)] -/// Type used for floats, configure with the `f32` feature -#[cfg(feature = "f32")] -pub type Float = f32; -#[cfg(not(feature = "f32"))] -/// Type used for floats, configure with the `f32` feature -pub type Float = f64; - -/// Associated constants for [`Float`] -pub mod consts { - #[cfg(feature = "f32")] - pub use std::f32::consts::*; - #[cfg(not(feature = "f32"))] - pub use std::f64::consts::*; -} +pub use float::{consts, Float}; /// Grid and grid metrics pub mod grid; diff --git a/sbp/src/operators/algos.rs b/sbp/src/operators/algos.rs index 9ed593d..23a4e82 100644 --- a/sbp/src/operators/algos.rs +++ b/sbp/src/operators/algos.rs @@ -6,9 +6,7 @@ pub(crate) mod constmatrix; pub(crate) use constmatrix::{ColVector, Matrix, RowVector}; #[cfg(feature = "fast-float")] -mod fastfloat; -#[cfg(feature = "fast-float")] -use fastfloat::FastFloat; +use float::FastFloat; #[derive(Clone, Debug, PartialEq)] pub(crate) struct DiagonalMatrix { diff --git a/utils/float/Cargo.toml b/utils/float/Cargo.toml new file mode 100644 index 0000000..a5fb980 --- /dev/null +++ b/utils/float/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "float" +version = "0.1.0" +authors = ["Magnus Ulimoen "] +edition = "2018" + +[features] +f32 = [] +fast-float = [] + +[dependencies] +num-traits = "0.2.14" diff --git a/sbp/src/operators/algos/fastfloat.rs b/utils/float/src/fastfloat.rs similarity index 98% rename from sbp/src/operators/algos/fastfloat.rs rename to utils/float/src/fastfloat.rs index f4e4e8b..64bf434 100644 --- a/sbp/src/operators/algos/fastfloat.rs +++ b/utils/float/src/fastfloat.rs @@ -1,8 +1,8 @@ -use super::*; +use super::Float; #[repr(transparent)] #[derive(Copy, Clone, Debug, PartialEq, Default)] -pub(crate) struct FastFloat(Float); +pub struct FastFloat(Float); use core::intrinsics::{fadd_fast, fdiv_fast, fmul_fast, fsub_fast}; diff --git a/utils/float/src/lib.rs b/utils/float/src/lib.rs new file mode 100644 index 0000000..92cb5c2 --- /dev/null +++ b/utils/float/src/lib.rs @@ -0,0 +1,23 @@ +//! The Float type used throughout the ecosystem + +#![cfg_attr(feature = "fast-float", feature(core_intrinsics))] + +/// Type used for floats, configure with the `f32` feature +#[cfg(feature = "f32")] +pub type Float = f32; +#[cfg(not(feature = "f32"))] +/// Type used for floats, configure with the `f32` feature +pub type Float = f64; + +/// Associated constants for [`Float`] +pub mod consts { + #[cfg(feature = "f32")] + pub use std::f32::consts::*; + #[cfg(not(feature = "f32"))] + pub use std::f64::consts::*; +} + +#[cfg(feature = "fast-float")] +mod fastfloat; +#[cfg(feature = "fast-float")] +pub use fastfloat::FastFloat;