fixes
| -rw-r--r-- | Cargo.lock | 14 | ||||
| -rw-r--r-- | Cargo.toml | 13 | ||||
| -rw-r--r-- | src/algebraic.rs | 63 | ||||
| -rw-r--r-- | src/fast.rs | 88 | ||||
| -rw-r--r-- | src/fast.safety.md | 4 | ||||
| -rw-r--r-- | src/lib.rs | 38 |
6 files changed, 33 insertions, 187 deletions
@@ -4,16 +4,16 @@ version = 3 [[package]] name = "lower" -version = "0.1.3" +version = "0.1.4" dependencies = [ "lower-macros", ] [[package]] name = "lower-macros" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fff64bae8cfa89c8871e4b07d27629aa000978b341b98ba5fc1a0a55e2a79a53" +checksum = "383823c24b2a094a6a9de418eba3b60bf8186b688e74e4a20ba5321dfae04fba" dependencies = [ "proc-macro2", "quote", @@ -40,9 +40,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.96" +version = "2.0.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" dependencies = [ "proc-macro2", "quote", @@ -51,6 +51,6 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11cd88e12b17c6494200a9c1b683a04fcac9573ed74cd1b62aeb2727c5592243" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" @@ -1,18 +1,13 @@ [package] name = "lower" -version = "0.1.3" +version = "0.1.4" authors = ["bend-n <[email protected]>"] description = "desugar math where the compiler wont" edition = "2021" repository = "https://github.com/bend-n/lower-plus.git" license = "MIT" +keywrords = ["fast", "math", "float"] +categories = ["hardware-support", "mathematics"] [dependencies] -lower-macros = "0.1.0" - -[features] -default = ["modules"] -modules = [] - -[package.metadata.docs.rs] -all-features = true +lower-macros = "0.1.1" diff --git a/src/algebraic.rs b/src/algebraic.rs deleted file mode 100644 index a7d6082..0000000 --- a/src/algebraic.rs +++ /dev/null @@ -1,63 +0,0 @@ -//! Provides the algebraic math trait and macro. -/// Trait for Algebraic-ly math'd floats. Try to use [`algebraic::math`](`math`). -pub trait Float: Copy { - fn add(self, other: Self) -> Self; - fn sub(self, other: Self) -> Self; - fn mul(self, other: Self) -> Self; - fn div(self, other: Self) -> Self; - fn rem(self, other: Self) -> Self; - - fn eq(self, other: Self) -> bool; - fn lt(self, other: Self) -> bool; - fn le(self, other: Self) -> bool; - fn ne(self, other: Self) -> bool; - fn ge(self, other: Self) -> bool; - fn gt(self, other: Self) -> bool; - - fn deref(&self) -> Self { - *self - } - fn neg(self) -> Self; -} -use std::intrinsics::*; -macro_rules! imp { - ($this:ty) => { -#[rustfmt::skip] -impl Float for $this { - fn add(self, other: Self) -> Self { fadd_algebraic(self, other) } - fn sub(self, other: Self) -> Self { fsub_algebraic(self, other) } - fn mul(self, other: Self) -> Self { fmul_algebraic(self, other) } - fn div(self, other: Self) -> Self { fdiv_algebraic(self, other) } - fn rem(self, other: Self) -> Self { frem_algebraic(self, other) } - - fn eq(self, other: Self) -> bool { self == other } - fn lt(self, other: Self) -> bool { self < other } - fn le(self, other: Self) -> bool { self <= other } - fn ne(self, other: Self) -> bool { self != other } - fn ge(self, other: Self) -> bool { self >= other } - fn gt(self, other: Self) -> bool { self > other } - - fn deref(&self) -> Self { *self } - fn neg(self) -> Self { -self } -} - }; -} -imp!(f32); -imp!(f64); - -/// Changes all the math to algebraic float math. See [`fadd_algebraic`]. -/// -/// This allows automatic vectorization of float math easier for the compiler, among other things. -/// If you intend to create a function in this, first import [`lower::algebraic::Float`](crate::algebraic::Float), use [`lower::math`](crate::math). -/// -/// This would lower `a + b + c + d` to `alge_add(alge_add(alge_add(a, b), c), d)` (which could be organized by the compiler into `(a + b) + (c + d)` if it chooses) -#[macro_export] -#[doc(hidden)] -macro_rules! algebraic { - ($($x:tt)+) => {{ - use $crate::algebraic::Float as _; - lower_macros::math! { $($x)+ } - }} -} -#[doc(inline)] -pub use algebraic as math; diff --git a/src/fast.rs b/src/fast.rs deleted file mode 100644 index e375172..0000000 --- a/src/fast.rs +++ /dev/null @@ -1,88 +0,0 @@ -//! Provides the fast math trait and macro. See terms and conditions[^1]. -//! -//! [^1]: <https://simonbyrne.github.io/notes/fastmath> -#[allow(unused_imports)] -use std::f32::{INFINITY as INF, NAN}; - -macro_rules! s { - ($($x:tt)+) => { - #[doc = include_str!("fast.safety.md")] - $($x)+; - }; -} - -#[rustfmt::skip] -/// Trait for fast math floats. Try to use [`fast::math`](crate::fast::math). -/// These functions all assume that your float is NOT [`INF`] | [`NAN`]. -/// If so undefined behaviour is incurred. -pub unsafe trait Float: Copy { - s!(unsafe fn add(self, other: Self) -> Self); - s!(unsafe fn sub(self, other: Self) -> Self); - s!(unsafe fn mul(self, other: Self) -> Self); - s!(unsafe fn div(self, other: Self) -> Self); - s!(unsafe fn rem(self, other: Self) -> Self); - - s!(unsafe fn eq(self, other: Self) -> bool); - s!(unsafe fn lt(self, other: Self) -> bool); - s!(unsafe fn le(self, other: Self) -> bool); - s!(unsafe fn ne(self, other: Self) -> bool); - s!(unsafe fn ge(self, other: Self) -> bool); - s!(unsafe fn gt(self, other: Self) -> bool); - - fn deref(&self) -> Self { - *self - } - fn neg(self) -> Self; - - #[doc(hidden)] - unsafe fn finite(self, other: Self) -> Self; -} - -use std::intrinsics::*; -macro_rules! imp { - ($this:ty) => { -#[rustfmt::skip] -unsafe impl Float for $this { - #[inline(always)] - unsafe fn finite(self, other: Self) -> Self { - core::hint::assert_unchecked(self.is_finite()); - core::hint::assert_unchecked(other.is_finite()); - self - } - unsafe fn add(self, other: Self) -> Self { fadd_fast(self.finite(other), other) } - unsafe fn sub(self, other: Self) -> Self { fsub_fast(self.finite(other), other) } - unsafe fn mul(self, other: Self) -> Self { fmul_fast(self.finite(other), other) } - unsafe fn div(self, other: Self) -> Self { fdiv_fast(self.finite(other), other) } - unsafe fn rem(self, other: Self) -> Self { frem_fast(self.finite(other), other) } - - unsafe fn eq(self, other: Self) -> bool { (self.finite(other) + 0.0).to_bits() == (other + 0.0).to_bits() } - unsafe fn lt(self, other: Self) -> bool { self.finite(other) < other } - unsafe fn le(self, other: Self) -> bool { self.finite(other) <= other } - unsafe fn ne(self, other: Self) -> bool { self.finite(other) != other } - unsafe fn ge(self, other: Self) -> bool { self.finite(other) >= other } - unsafe fn gt(self, other: Self) -> bool { self.finite(other) > other } - - fn deref(&self) -> Self { *self } - fn neg(self) -> Self { -self } -} - }; -} -imp!(f32); -imp!(f64); - -/// Changes all the math to fast float math. See [`fadd_fast`]. -/// -/// This allows automatic vectorization of float math for the compiler, among other things. -/// If you intend to create a function in this, first import [`lower::fast::Float`](crate::fast::Float), use [`lower::math`](crate::math). -/// -/// This would lower `a * b + c` to `fadd(fmul(a, b), c)`. -#[macro_export] -#[doc(hidden)] -macro_rules! fast { - ($($x:tt)+) => {{ - use $crate::fast::Float as _; - lower_macros::math! { $($x)+ } - }} -} -#[doc(inline)] -pub use fast as math; diff --git a/src/fast.safety.md b/src/fast.safety.md deleted file mode 100644 index 115a9e2..0000000 --- a/src/fast.safety.md +++ /dev/null @@ -1,4 +0,0 @@ -# Safety - -MUSNT call this function with [`NAN`] | [`INF`]. -This function WILL assume the inputs are finite. @@ -2,16 +2,26 @@ //! //! provides a handy macro for converting `a + b` to `a.add(b)` for when you cant easily overload the `Add` trait, //! and some traits that provide [`f*_algebraic`](algebraic::math) and [`f*_fast`](fast::math) implementations. (requires nightly) -#![allow(internal_features)] -#![cfg_attr( - feature = "modules", - feature(doc_auto_cfg, core_intrinsics, hint_assert_unchecked) -)] - -#[cfg(feature = "modules")] -pub mod algebraic; -#[cfg(feature = "modules")] -pub mod fast; +/// Provides the algebraic math trait and macro. +pub mod algebraic { + /// Changes all the math to algebraic float math. See [`fadd_algebraic`](core::intrinsics::fadd_algebraic). + /// + /// This allows automatic vectorization of float math easier for the compiler, among other things. + /// + /// This would lower `a + b + c + d` to `alge_add(alge_add(alge_add(a, b), c), d)` (which could be organized by the compiler into `(a + b) + (c + d)` if it chooses) + pub use lower_macros::algebraic as math; +} +/// Provides the fast math trait and macro. See terms and conditions[^1]. +/// +/// [^1]: <https://simonbyrne.github.io/notes/fastmath> +pub mod fast { + /// Changes all the math to fast float math. See [`fadd_fast`](core::intrinsics::fadd_fast). + /// + /// This allows automatic vectorization of float math for the compiler, among other things. + /// + /// This would lower `a * b + c` to `fastadd(fastmul(a, b), c)`. + pub use lower_macros::fast as math; +} /// Lower math to method calls. Only useful if you define the functions. /// ``` @@ -21,9 +31,5 @@ pub mod fast; /// // expands to /// // a.mul((&b).deref()).add(c.neg()) /// ``` -#[macro_export] -macro_rules! math { - ($($x:tt)+) => { - lower_macros::math! { $($x)+ } - }; -} +/// <p style="display: none"> +pub use lower_macros::math; |