Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 6, 2023 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/ce0074827382226218ca64a02485bf8d to your computer and use it in GitHub Desktop.
Save rust-play/ce0074827382226218ca64a02485bf8d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// Ignore this function
fn fused_mul_add(a: [f32; 2], b: [f32; 2], c: [f32; 2]) -> [f32; 2] {
let mut ret = [0.0, 0.0];
(0..2).for_each(|i| {
ret[i] = a[i] * b[i] + c[i];
});
ret
}
// I think it should be possible to rewrite this function so that...
fn combined_fused_multiply_add<I>(mut iter: I) -> Option<[f32; 2]>
where
I: Iterator<Item = ([f32; 2], [f32; 2])>,
{
iter.next().map(|(a, b)| {
fused_mul_add(a, b, combined_fused_multiply_add(iter).unwrap_or_default())
})
}
fn main() {
let list: Vec<([f32; 2], [f32; 2])> = vec![
([0.1, 0.2], [1.1, 1.2]),
([0.3, 0.4], [1.3, 1.4]),
([0.5, 0.6], [1.3, 1.4]),
];
let res: [f32; 2] = combined_fused_multiply_add(list.into_iter()).unwrap(); // <-- current solution, this works
// let res: [f32; 2] = list.into_iter().fold(combined_fused_multiply_add.unwrap()); // ...something like this will work?
dbg!(&res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment