rename fixed point converters

Q20.12 and Q16.16 come as .h files in the BIO C library,
so converters in Rust to shove data to and from the BIO in
those formats are provided.
This commit is contained in:
bunnie 2026-02-22 03:33:23 +08:00
parent 75ce3f886d
commit db6aa1255a
2 changed files with 47 additions and 3 deletions

View file

@ -5,7 +5,7 @@ use bao1x_hal::bio::{Bio, CoreCsr};
use math_test::*;
use utralib::utra::bio_bdma;
use super::Fp;
use crate::c::FpQ20_12;
pub struct MathTest {
bio_ss: Bio,
@ -68,7 +68,7 @@ impl MathTest {
pub fn test_cos(&mut self) {
for i in (0..360).step_by(4) {
// self.bio_ss.debug(self.resource_grant.cores[0]);
let arg = Fp::from_int(i);
let arg = FpQ20_12::from_int(i);
self.tx.csr.wo(bio_bdma::SFR_TXF0, arg.0 as u32);
// let mut dbg_count: usize = 0;
while self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {
@ -80,7 +80,7 @@ impl MathTest {
*/
}
let result = self.rx.csr.r(bio_bdma::SFR_RXF1) as i32; // result should be 0..20
let result_f32 = Fp::from_raw(result);
let result_f32 = FpQ20_12::from_raw(result);
let mut line = String::new();
for _ in 0..((result_f32.to_float() * 20.0f32) as usize) {
line.push(' ');

View file

@ -1,2 +1,46 @@
#[cfg(feature = "c-colorwheel")]
pub mod colorwheel;
#[cfg(feature = "c-math-test")]
pub mod math_test;
/// 19-bit signed integer + 12-bit fraction
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FpQ20_12(pub i32);
impl FpQ20_12 {
pub fn frac_bits() -> u32 { 12 }
pub fn frac_scale() -> i32 { 1 << Self::frac_bits() }
pub fn from_int(x: i32) -> Self { Self(x << Self::frac_bits()) }
pub fn from_float(x: f32) -> Self { Self((x * Self::frac_scale() as f32 + 0.5) as i32) }
pub fn to_int(self) -> i32 { self.0 >> Self::frac_bits() }
pub fn to_float(self) -> f32 { self.0 as f32 / Self::frac_scale() as f32 }
/// Wrap a raw i32 coming directly off the hardware register.
pub fn from_raw(raw: i32) -> Self { Self(raw) }
}
/// 15-bit signed integer + 16-bit fraction
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FpQ16_16(pub i32);
impl FpQ16_16 {
pub fn frac_bits() -> u32 { 16 }
pub fn frac_scale() -> i32 { 1 << Self::frac_bits() }
pub fn from_int(x: i32) -> Self { Self(x << Self::frac_bits()) }
pub fn from_float(x: f32) -> Self { Self((x * Self::frac_scale() as f32 + 0.5) as i32) }
pub fn to_int(self) -> i32 { self.0 >> Self::frac_bits() }
pub fn to_float(self) -> f32 { self.0 as f32 / Self::frac_scale() as f32 }
/// Wrap a raw i32 coming directly off the hardware register.
pub fn from_raw(raw: i32) -> Self { Self(raw) }
}