Replace calls to rem_euclid with unknown denominator with simple &

This commit is contained in:
Gwilym Kuiper 2022-05-03 22:46:01 +01:00
parent fae71bfbca
commit 5513c8566f
2 changed files with 42 additions and 6 deletions

View file

@ -63,8 +63,8 @@ impl<'a> InfiniteScrolledMap<'a> {
let offset = self.current_pos - (x_start * 8, y_start * 8).into(); let offset = self.current_pos - (x_start * 8, y_start * 8).into();
let offset_scroll = ( let offset_scroll = (
offset.x.rem_euclid(self.map.size().width() as i32 * 8) as u16, self.map.size().rem_euclid_width(offset.x) as u16,
offset.y.rem_euclid(self.map.size().height() as i32 * 8) as u16, self.map.size().rem_euclid_height(offset.y) as u16,
) )
.into(); .into();
@ -179,8 +179,8 @@ impl<'a> InfiniteScrolledMap<'a> {
self.map.set_tile( self.map.set_tile(
vram, vram,
( (
(tile_x - self.offset.x).rem_euclid(size.width() as i32) as u16, size.rem_euclid_width(tile_x - self.offset.x) as u16,
(tile_y - self.offset.y).rem_euclid(size.height() as i32) as u16, size.rem_euclid_height(tile_y - self.offset.y) as u16,
) )
.into(), .into(),
tileset, tileset,
@ -190,8 +190,8 @@ impl<'a> InfiniteScrolledMap<'a> {
let current_scroll = self.map.scroll_pos(); let current_scroll = self.map.scroll_pos();
let new_scroll = ( let new_scroll = (
(current_scroll.x as i32 + difference.x).rem_euclid(size.width() as i32 * 8) as u16, size.rem_euclid_width(current_scroll.x as i32 + difference.x) as u16,
(current_scroll.y as i32 + difference.y).rem_euclid(size.height() as i32 * 8) as u16, size.rem_euclid_height(current_scroll.y as i32 + difference.y) as u16,
) )
.into(); .into();

View file

@ -47,6 +47,14 @@ impl RegularBackgroundSize {
pub(crate) fn num_tiles(&self) -> usize { pub(crate) fn num_tiles(&self) -> usize {
(self.width() * self.height()) as usize (self.width() * self.height()) as usize
} }
pub(crate) fn rem_euclid_width(&self, x: i32) -> u32 {
(x as u32) & (self.width() - 1)
}
pub(crate) fn rem_euclid_height(&self, y: i32) -> u32 {
(y as u32) & (self.height() - 1)
}
} }
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@ -88,3 +96,31 @@ impl TileSetting {
self.0 & !((1 << 10) - 1) self.0 & !((1 << 10) - 1)
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test_case]
fn rem_euclid_width_works(_gba: &mut crate::Gba) {
use RegularBackgroundSize::*;
let sizes = [
Background32x32,
Background32x64,
Background64x32,
Background64x64,
];
for size in sizes.iter() {
let width = size.width() as i32;
assert_eq!(size.rem_euclid_width(8), 8);
assert_eq!(size.rem_euclid_width(3 + width), 3);
assert_eq!(size.rem_euclid_width(7 + width * 9), 7);
assert_eq!(size.rem_euclid_width(-8), size.width() - 8);
assert_eq!(size.rem_euclid_width(-17 - width * 8), size.width() - 17);
}
}
}