From d5ac38ed9bb3454089f7fe4a4dc4a032aac39220 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 13 Aug 2023 21:51:12 +0200 Subject: [PATCH] util: split to_flat_coord() and from_flat_coord() out of coord_offset() --- src/util.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/util.rs b/src/util.rs index 43245ca..b4fdefe 100644 --- a/src/util.rs +++ b/src/util.rs @@ -27,6 +27,23 @@ impl ShiftMask for i32 { } } +#[inline] +pub fn to_flat_coord( + region: i8, + chunk: ChunkCoord, + block: BlockCoord, +) -> i32 { + (region as i32) << (BLOCK_BITS + CHUNK_BITS) | ((chunk.0 as i32) << BLOCK_BITS | block.0 as i32) +} + +#[inline] +pub fn from_flat_coord(coord: i32) -> (i8, ChunkCoord, BlockCoord) { + let (region_chunk, block) = coord.shift_mask(BLOCK_BITS); + let (region, chunk) = region_chunk.shift_mask(CHUNK_BITS); + debug_assert!(i8::try_from(region).is_ok()); + (region as i8, ChunkCoord::new(chunk), BlockCoord::new(block)) +} + /// Offsets a chunk and block coordinate pair by a number of blocks /// /// As the new coordinate may end up in a different region, a region offset @@ -37,11 +54,7 @@ pub fn coord_offset( block: BlockCoord, offset: i32, ) -> (i8, ChunkCoord, BlockCoord) { - let coord = ((chunk.0 as i32) << BLOCK_BITS | block.0 as i32) + offset; - let (region_chunk, block) = coord.shift_mask(BLOCK_BITS); - let (region, chunk) = region_chunk.shift_mask(CHUNK_BITS); - debug_assert!(i8::try_from(region).is_ok()); - (region as i8, ChunkCoord::new(chunk), BlockCoord::new(block)) + from_flat_coord(to_flat_coord(0, chunk, block) + offset) } #[cfg(test)]