From ce2e8f17ec04fb1e937a2fa7ea37809073d19fef Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Tue, 23 Mar 2021 19:36:33 +0100 Subject: [PATCH] Move FastFloat to separate crate (archival) --- Cargo.toml | 1 + utils/fast-float/Cargo.toml | 11 ++++ utils/fast-float/src/lib.rs | 123 ++++++++++++++++++++++++++++++++++++ utils/float/Cargo.toml | 2 - utils/float/src/lib.rs | 7 +- 5 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 utils/fast-float/Cargo.toml create mode 100644 utils/fast-float/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 99b8083..7b2788b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "gridgeneration", "heat-equation", "utils/float", + "utils/fast-float", "utils/integrate", "utils/constmatrix", ] diff --git a/utils/fast-float/Cargo.toml b/utils/fast-float/Cargo.toml new file mode 100644 index 0000000..45c8e69 --- /dev/null +++ b/utils/fast-float/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "fast-float" +version = "0.1.0" +authors = ["Magnus Ulimoen "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +float = { path = "../float" } +num-traits = "0.2.14" diff --git a/utils/fast-float/src/lib.rs b/utils/fast-float/src/lib.rs new file mode 100644 index 0000000..b6a9573 --- /dev/null +++ b/utils/fast-float/src/lib.rs @@ -0,0 +1,123 @@ +#![no_std] +#![feature(core_intrinsics)] + +use float::Float; + +#[repr(transparent)] +#[derive(Copy, Clone, Debug, PartialEq, Default)] +pub struct FastFloat(Float); + +use core::intrinsics::{fadd_fast, fdiv_fast, fmul_fast, fsub_fast}; + +impl core::ops::Mul for FastFloat { + type Output = Self; + #[inline(always)] + fn mul(self, o: Self) -> Self::Output { + unsafe { Self(fmul_fast(self.0, o.0)) } + } +} + +impl core::ops::Mul for FastFloat { + type Output = FastFloat; + #[inline(always)] + fn mul(self, o: Float) -> Self::Output { + unsafe { Self(fmul_fast(self.0, o)) } + } +} + +impl core::ops::Mul for Float { + type Output = FastFloat; + #[inline(always)] + fn mul(self, o: FastFloat) -> Self::Output { + unsafe { FastFloat(fmul_fast(self, o.0)) } + } +} + +impl core::ops::Add for FastFloat { + type Output = FastFloat; + #[inline(always)] + fn add(self, o: FastFloat) -> Self::Output { + unsafe { Self(fadd_fast(self.0, o.0)) } + } +} + +impl core::ops::Add for FastFloat { + type Output = FastFloat; + #[inline(always)] + fn add(self, o: Float) -> Self::Output { + unsafe { Self(fadd_fast(self.0, o)) } + } +} + +impl core::ops::Add for Float { + type Output = FastFloat; + #[inline(always)] + fn add(self, o: FastFloat) -> Self::Output { + unsafe { FastFloat(fadd_fast(self, o.0)) } + } +} + +impl core::ops::Sub for FastFloat { + type Output = Self; + #[inline(always)] + fn sub(self, o: FastFloat) -> Self::Output { + unsafe { Self(fsub_fast(self.0, o.0)) } + } +} + +impl core::ops::Div for FastFloat { + type Output = Self; + #[inline(always)] + fn div(self, o: FastFloat) -> Self::Output { + unsafe { Self(fdiv_fast(self.0, o.0)) } + } +} + +impl core::ops::MulAssign for FastFloat { + #[inline(always)] + fn mul_assign(&mut self, o: FastFloat) { + unsafe { + self.0 = fmul_fast(self.0, o.0); + } + } +} + +impl core::ops::Mul for &FastFloat { + type Output = FastFloat; + #[inline(always)] + fn mul(self, o: Self) -> Self::Output { + unsafe { FastFloat(fmul_fast(self.0, o.0)) } + } +} + +impl From for FastFloat { + #[inline(always)] + fn from(f: Float) -> Self { + Self(f) + } +} +impl From for Float { + #[inline(always)] + fn from(f: FastFloat) -> Self { + f.0 + } +} + +mod numt { + use super::{FastFloat, Float}; + use num_traits::identities::{One, Zero}; + + impl One for FastFloat { + fn one() -> Self { + Self(Float::one()) + } + } + impl Zero for FastFloat { + fn zero() -> Self { + Self(Float::zero()) + } + fn is_zero(&self) -> bool { + self.0.is_zero() + } + } +} diff --git a/utils/float/Cargo.toml b/utils/float/Cargo.toml index a5fb980..c39f627 100644 --- a/utils/float/Cargo.toml +++ b/utils/float/Cargo.toml @@ -6,7 +6,5 @@ edition = "2018" [features] f32 = [] -fast-float = [] [dependencies] -num-traits = "0.2.14" diff --git a/utils/float/src/lib.rs b/utils/float/src/lib.rs index 526780a..7374315 100644 --- a/utils/float/src/lib.rs +++ b/utils/float/src/lib.rs @@ -1,6 +1,6 @@ //! The Float type used throughout the ecosystem #![no_std] -#![cfg_attr(feature = "fast-float", feature(core_intrinsics))] +#![forbid(unsafe_code)] /// Type used for floats, configure with the `f32` feature #[cfg(feature = "f32")] @@ -16,8 +16,3 @@ pub mod consts { #[cfg(not(feature = "f32"))] pub use core::f64::consts::*; } - -#[cfg(feature = "fast-float")] -mod fastfloat; -#[cfg(feature = "fast-float")] -pub use fastfloat::FastFloat;