From df05c06270f8dccd309352e775dc335c4e566fc2 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Tue, 23 Mar 2021 19:12:09 +0100 Subject: [PATCH] Add no_std and forbid(unsafe) in utils --- utils/constmatrix/src/lib.rs | 35 +++++++++++++++++++++++++++++------ utils/float/src/lib.rs | 6 +++--- utils/integrate/src/lib.rs | 2 ++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/utils/constmatrix/src/lib.rs b/utils/constmatrix/src/lib.rs index 7de31c3..d7d42f7 100644 --- a/utils/constmatrix/src/lib.rs +++ b/utils/constmatrix/src/lib.rs @@ -1,3 +1,4 @@ +#![no_std] #![feature(const_fn_floating_point_arithmetic)] use float::Float; @@ -27,7 +28,7 @@ impl Zero for Matrix } } fn is_zero(&self) -> bool { - self == &Self::zero() + self.iter().all(|x| x.is_zero()) } } @@ -113,10 +114,31 @@ impl RowVector { } } +impl Matrix { + /// A specialised matmul for Float, using fma + pub fn matmul_float_into( + &mut self, + lhs: &Matrix, + rhs: &Matrix, + ) { + for i in 0..M { + for j in 0..P { + let mut t = 0.0; + for k in 0..N { + t = Float::mul_add(lhs[(i, k)], rhs[(k, j)], t); + // t = t + lhs[(i, k)] * rhs[(k, j)]; + } + self[(i, j)] = t; + } + } + } +} + impl Matrix { pub fn matmul_into(&mut self, lhs: &Matrix, rhs: &Matrix) where T: Default + Copy + core::ops::Mul + core::ops::Add, + T: 'static, { for i in 0..M { for j in 0..P { @@ -135,6 +157,7 @@ macro_rules! impl_op_mul_mul { impl core::ops::Mul<$rhs> for $lhs where T: Copy + Default + core::ops::Add + core::ops::Mul, + T: 'static, { type Output = Matrix; fn mul(self, lhs: $rhs) -> Self::Output { @@ -351,14 +374,14 @@ mod flipping { #[test] fn assert_castability_of_alignment() { let m = Matrix::new([[1.0], [2.0], [3.0], [4.0_f64]]); - assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::()); let m = Matrix::new([[1.0], [2.0], [3.0], [4.0_f32]]); - assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::()); let m = Matrix::new([[1], [2], [3], [4_i32]]); - assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::()); let m = Matrix::new([[1], [2], [3], [4_u64]]); - assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::()); let m = Matrix::new([[1], [2], [3], [4_u128]]); - assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::()); } } diff --git a/utils/float/src/lib.rs b/utils/float/src/lib.rs index 92cb5c2..526780a 100644 --- a/utils/float/src/lib.rs +++ b/utils/float/src/lib.rs @@ -1,5 +1,5 @@ //! The Float type used throughout the ecosystem - +#![no_std] #![cfg_attr(feature = "fast-float", feature(core_intrinsics))] /// Type used for floats, configure with the `f32` feature @@ -12,9 +12,9 @@ pub type Float = f64; /// Associated constants for [`Float`] pub mod consts { #[cfg(feature = "f32")] - pub use std::f32::consts::*; + pub use core::f32::consts::*; #[cfg(not(feature = "f32"))] - pub use std::f64::consts::*; + pub use core::f64::consts::*; } #[cfg(feature = "fast-float")] diff --git a/utils/integrate/src/lib.rs b/utils/integrate/src/lib.rs index 1e49e72..085269f 100644 --- a/utils/integrate/src/lib.rs +++ b/utils/integrate/src/lib.rs @@ -7,6 +7,8 @@ //! The integration functions are memory efficient, and relies //! on the `k` parameter to hold the system state differences. //! This parameter is tied to the Butcher Tableau +#![forbid(unsafe_code)] +#![no_std] use float::Float;