From b0e0b5144fec39f66c49d9b551d6be5b9d658f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 24 Nov 2020 09:27:47 +0100 Subject: [PATCH] processing: use faster unsafe truncate --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4c945e5..6a72552 100644 --- a/src/main.rs +++ b/src/main.rs @@ -779,7 +779,11 @@ const APP: () = { let x0 = f32::from(a as i16); let y0 = c.resources.iir_ch[1].update(&mut c.resources.iir_state[1], x0); - y0 as i16 as u16 ^ 0x8000 + // note(unsafe): The filter limits ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y0 = unsafe { y0.to_int_unchecked::() }; + // convert to DAC code + y0 as u16 ^ 0x8000 }; c.resources.dac1.send(output).unwrap(); @@ -792,7 +796,11 @@ const APP: () = { let x0 = f32::from(a as i16); let y0 = c.resources.iir_ch[0].update(&mut c.resources.iir_state[0], x0); - y0 as i16 as u16 ^ 0x8000 + // note(unsafe): The filter limits ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y0 = unsafe { y0.to_int_unchecked::() }; + // convert to DAC code + y0 as u16 ^ 0x8000 }; c.resources.dac0.send(output).unwrap();