Fixed up nostd support, verified that sqrt and cbrt routines work to within 1ULP

This commit is contained in:
2022-04-25 22:36:05 +02:00
parent 8016a8c7f8
commit 1e1cac4f06
7 changed files with 75 additions and 39 deletions

View File

@@ -36,7 +36,7 @@ impl CommandQueue {
// Returns true if step signal should be high. This does the first, constant time part of stepping.
// Note that step_late needs to be called as well to advance the segment.
pub fn step_early(&mut self) -> bool {
let cur_segment = &mut self.segments[self.cur_segment as usize];
self.p.0 = (self.p + cur_segment.delta[0]).0.rem_euclid(self.step_size);

View File

@@ -1,4 +1,4 @@
// #![no_std]
#![no_std]
pub mod planner;
pub mod executor;

View File

@@ -41,12 +41,22 @@ fn sqrt_approx(value: f32) -> f32 {
pub fn cbrt_approx(value: f32) -> f32 {
let value = value.to_bits();
const EXP_BIAS: i32 = 0x3f80_0000;
//const EXP_BIAS: i32 = 0x3f80_0000;
f32::from_bits((value & 0x8000_0000) |
(((value as i32 & 0x7fff_ffff) - EXP_BIAS) / 3 + EXP_BIAS) as u32)
(((value & 0x7fff_ffff) / 3).wrapping_add(0x2a5119f2)))
}
fn frexp(value: f32) -> (i32, f32) {
let value = value.to_bits();
(((value >> 23) & 0xff) as i32 - 126, // we go to [0.5,1), whereas the mantissa represents [1,2)
f32::from_bits(value & 0x7f_ffff | (126 << 23)))
}
fn ldexp(exp: i32, mantissa: f32) -> f32 {
f32::from_bits(mantissa.to_bits().wrapping_add((exp as u32) << 23))
}
impl Roots for f32 {
fn sqrt(self) -> Self {
let mut estimate = sqrt_approx(self);
@@ -56,16 +66,18 @@ impl Roots for f32 {
estimate
}
//#[cfg(ignore)]
fn cbrt(self) -> Self {
let mut estimate = cbrt_approx(self); // TODO: improve this with bit twiddling
let mut estimate = cbrt_approx(self) as f64; // TODO: improve this with bit twiddling
let a = self as f64;
// Use halley's method to refine the approximation.
for _ in 0..3 {
for _ in 0..2 {
let e3 = estimate * estimate * estimate;
estimate = estimate * (e3 + self + self) / ( e3 + e3 + self);
estimate = estimate * (e3 + a + a) / ( e3 + e3 + a);
}
estimate
estimate as f32
}
}

View File

@@ -5,6 +5,7 @@
use core::num::Wrapping;
use core::option::Option;
use crate::math_ext::Roots as _;
pub struct Config {
/// Max jerk, in mm/s^3