Skip to content

Commit

Permalink
add inplace fn to avoid allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
s5suzuki committed Sep 9, 2024
1 parent 932936f commit 5d51301
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
10 changes: 7 additions & 3 deletions autd3-firmware-emulator/src/fpga/emulator/modulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ impl FPGAEmulator {
}

pub fn modulation_buffer(&self, segment: Segment) -> Vec<u8> {
(0..self.modulation_cycle(segment))
.map(|i| self.modulation_at(segment, i))
.collect()
let mut dst = vec![0; self.modulation_cycle(segment)];
self.modulation_buffer_inplace(segment, &mut dst);
dst
}

pub fn modulation_buffer_inplace(&self, segment: Segment, dst: &mut [u8]) {
(0..self.modulation_cycle(segment)).for_each(|i| dst[i] = self.modulation_at(segment, i));
}

pub fn modulation_transition_mode(&self) -> TransitionMode {
Expand Down
9 changes: 8 additions & 1 deletion autd3-firmware-emulator/src/fpga/emulator/pwe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ impl FPGAEmulator {
}

pub fn pulse_width_encoder_table(&self) -> Vec<u8> {
let mut dst = vec![0; 256];
self.pulse_width_encoder_table_inplace(&mut dst);
dst
}

pub fn pulse_width_encoder_table_inplace(&self, dst: &mut [u8]) {
self.mem
.duty_table_bram()
.iter()
.flat_map(|&d| vec![(d & 0xFF) as u8, (d >> 8) as u8])
.collect()
.enumerate()
.for_each(|(i, v)| dst[i] = v);
}

pub fn to_pulse_width(&self, a: EmitIntensity, b: u8) -> u8 {
Expand Down
10 changes: 5 additions & 5 deletions autd3-firmware-emulator/src/fpga/emulator/stm/foci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl FPGAEmulator {
}] as u8
}

pub(crate) fn foci_stm_drives(&self, segment: Segment, idx: usize) -> Vec<Drive> {
pub(crate) fn foci_stm_drives_inplace(&self, segment: Segment, idx: usize, dst: &mut [Drive]) {
let bram = match segment {
Segment::S0 => self.mem.stm_bram_0(),
Segment::S1 => self.mem.stm_bram_1(),
Expand All @@ -35,7 +35,8 @@ impl FPGAEmulator {
.tr_pos
.iter()
.take(self.mem.num_transducers)
.map(|&tr| {
.enumerate()
.for_each(|(i, &tr)| {
let tr_z = ((tr >> 32) & 0xFFFF) as i16 as i32;
let tr_x = ((tr >> 16) & 0xFFFF) as i16 as i32;
let tr_y = (tr & 0xFFFF) as i16 as i32;
Expand Down Expand Up @@ -68,9 +69,8 @@ impl FPGAEmulator {
let sin = ((sin / self.num_foci(segment) as u16) >> 1) as usize;
let cos = ((cos / self.num_foci(segment) as u16) >> 1) as usize;
let phase = self.mem.atan_table[(sin << 7) | cos];
Drive::new(Phase::new(phase), EmitIntensity::new(intensity))
})
.collect()
dst[i] = Drive::new(Phase::new(phase), EmitIntensity::new(intensity));
});
}

pub fn local_tr_pos(&self) -> &[u64] {
Expand Down
8 changes: 4 additions & 4 deletions autd3-firmware-emulator/src/fpga/emulator/stm/gain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use autd3_driver::{
use crate::FPGAEmulator;

impl FPGAEmulator {
pub(crate) fn gain_stm_drives(&self, segment: Segment, idx: usize) -> Vec<Drive> {
pub(crate) fn gain_stm_drives_inplace(&self, segment: Segment, idx: usize, dst: &mut [Drive]) {
match segment {
Segment::S0 => self.mem.stm_bram_0(),
Segment::S1 => self.mem.stm_bram_1(),
Expand All @@ -15,12 +15,12 @@ impl FPGAEmulator {
.iter()
.skip(256 * idx)
.take(self.mem.num_transducers)
.map(|&d| {
Drive::new(
.enumerate()
.for_each(|(i, &d)| {
dst[i] = Drive::new(
Phase::new((d & 0xFF) as u8),
EmitIntensity::new(((d >> 8) & 0xFF) as u8),
)
})
.collect()
}
}
10 changes: 8 additions & 2 deletions autd3-firmware-emulator/src/fpga/emulator/stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ impl FPGAEmulator {
}

pub fn drives_at(&self, segment: Segment, idx: usize) -> Vec<Drive> {
let mut dst = vec![Drive::null(); self.mem.num_transducers];
self.drives_at_inplace(segment, idx, &mut dst);
dst
}

pub fn drives_at_inplace(&self, segment: Segment, idx: usize, dst: &mut [Drive]) {
if self.is_stm_gain_mode(segment) {
self.gain_stm_drives(segment, idx)
self.gain_stm_drives_inplace(segment, idx, dst)
} else {
self.foci_stm_drives(segment, idx)
self.foci_stm_drives_inplace(segment, idx, dst)
}
}
}

0 comments on commit 5d51301

Please sign in to comment.