Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no_std support #226

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace f32::fract with no_std compliant version
  • Loading branch information
vstroebel committed Feb 18, 2022
commit 861da025067a3af7ac7599dd47d95c9fb50af45c
25 changes: 23 additions & 2 deletions src/upsampler.rs
Original file line number Diff line number Diff line change
@@ -164,6 +164,13 @@ impl Upsample for UpsamplerH2V1 {
}
}

// no_std version to calculate fractional part of positive numbers
fn fract(v: f32) -> f32 {
debug_assert!(v >= 0.0 && v <= (1 << core::f32::MANTISSA_DIGITS - 1) as f32);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are doing this for positive numbers only, then why isn't it using f32::floor()? How does the conversion to usize work but not the fract() method? I'm just super confused. This needs better justification/eval of alternatives before affecting std code as well.

let int = v as usize as f32;
v - int
}

impl Upsample for UpsamplerH1V2 {
fn upsample_row(&self,
input: &[u8],
@@ -176,7 +183,7 @@ impl Upsample for UpsamplerH1V2 {
let row_near = row as f32 / 2.0;
// If row_near's fractional is 0.0 we want row_far to be the previous row and if it's 0.5 we
// want it to be the next row.
let row_far = (row_near + row_near.fract() * 3.0 - 0.25).min((input_height - 1) as f32);
let row_far = (row_near + fract(row_near) * 3.0 - 0.25).min((input_height - 1) as f32);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just use integer fixed-point math here?


let input_near = &input[row_near as usize * row_stride ..];
let input_far = &input[row_far as usize * row_stride ..];
@@ -202,7 +209,7 @@ impl Upsample for UpsamplerH2V2 {
let row_near = row as f32 / 2.0;
// If row_near's fractional is 0.0 we want row_far to be the previous row and if it's 0.5 we
// want it to be the next row.
let row_far = (row_near + row_near.fract() * 3.0 - 0.25).min((input_height - 1) as f32);
let row_far = (row_near + fract(row_near) * 3.0 - 0.25).min((input_height - 1) as f32);

let input_near = &input[row_near as usize * row_stride ..];
let input_far = &input[row_far as usize * row_stride ..];
@@ -250,3 +257,17 @@ impl Upsample for UpsamplerGeneric {
}
}
}

#[cfg(test)]
mod tests {
use crate::upsampler::fract;

#[test]
fn test_fract() {
let value = 3.6_f32;
let frac = fract(value);
let diff = frac - 0.6_f32;

assert!(diff >= -core::f32::EPSILON && diff <= core::f32::EPSILON);
}
}