Skip to content

Commit

Permalink
Precision improvements (Part 5)
Browse files Browse the repository at this point in the history
Get rid of floats when calculating chunk position from block position. This can be done in integers. Since CHUNK_WIDTH etc. are guaranteed to be powers of two, using & -N is equivalent to doing & ~(N-1), or put differently, making it divisible by N. That makes the division exact, and allows the compiler to transform it into a plain shift (tested with both gcc and clang).

We did the same in Part 3 of this series.
  • Loading branch information
Pedro Gimeno committed Feb 24, 2020
1 parent d4daaa6 commit 6e506d8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/source/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ bool World::isReloadRequested = false;

Chunk *World::getChunkAtBlockPos(int x, int y, int z) const {
return getChunk(
std::floor((float)x / CHUNK_WIDTH),
std::floor((float)y / CHUNK_DEPTH),
std::floor((float)z / CHUNK_HEIGHT)
(x & -CHUNK_WIDTH) / CHUNK_WIDTH,
(y & -CHUNK_DEPTH) / CHUNK_DEPTH,
(z & -CHUNK_HEIGHT) / CHUNK_HEIGHT
);
}

Expand Down

0 comments on commit 6e506d8

Please sign in to comment.