Add no_std and forbid(unsafe) in utils

This commit is contained in:
Magnus Ulimoen 2021-03-23 19:12:09 +01:00
parent 00fcdf1031
commit df05c06270
3 changed files with 34 additions and 9 deletions

View File

@ -1,3 +1,4 @@
#![no_std]
#![feature(const_fn_floating_point_arithmetic)]
use float::Float;
@ -27,7 +28,7 @@ impl<T: Copy + Zero + PartialEq, const M: usize, const N: usize> Zero for Matrix
}
}
fn is_zero(&self) -> bool {
self == &Self::zero()
self.iter().all(|x| x.is_zero())
}
}
@ -113,10 +114,31 @@ impl<T, const N: usize> RowVector<T, N> {
}
}
impl<const M: usize, const P: usize> Matrix<Float, M, P> {
/// A specialised matmul for Float, using fma
pub fn matmul_float_into<const N: usize>(
&mut self,
lhs: &Matrix<Float, M, N>,
rhs: &Matrix<Float, N, P>,
) {
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<T, const M: usize, const P: usize> Matrix<T, M, P> {
pub fn matmul_into<const N: usize>(&mut self, lhs: &Matrix<T, M, N>, rhs: &Matrix<T, N, P>)
where
T: Default + Copy + core::ops::Mul<Output = T> + core::ops::Add<Output = T>,
T: 'static,
{
for i in 0..M {
for j in 0..P {
@ -135,6 +157,7 @@ macro_rules! impl_op_mul_mul {
impl<T, const N: usize, const M: usize, const P: usize> core::ops::Mul<$rhs> for $lhs
where
T: Copy + Default + core::ops::Add<Output = T> + core::ops::Mul<Output = T>,
T: 'static,
{
type Output = Matrix<T, M, P>;
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::<f64>());
assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::<f64>());
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::<f32>());
assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::<f32>());
let m = Matrix::new([[1], [2], [3], [4_i32]]);
assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::<i32>());
assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::<i32>());
let m = Matrix::new([[1], [2], [3], [4_u64]]);
assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::<u64>());
assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::<u64>());
let m = Matrix::new([[1], [2], [3], [4_u128]]);
assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::<u128>());
assert_eq!(core::mem::align_of_val(&m), core::mem::align_of::<u128>());
}
}

View File

@ -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")]

View File

@ -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;