Skip to content

Commit 6e506d8

Browse files
author
Pedro Gimeno
committed
Precision improvements (Part 5)
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.
1 parent d4daaa6 commit 6e506d8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

common/source/world/World.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ bool World::isReloadRequested = false;
3131

3232
Chunk *World::getChunkAtBlockPos(int x, int y, int z) const {
3333
return getChunk(
34-
std::floor((float)x / CHUNK_WIDTH),
35-
std::floor((float)y / CHUNK_DEPTH),
36-
std::floor((float)z / CHUNK_HEIGHT)
34+
(x & -CHUNK_WIDTH) / CHUNK_WIDTH,
35+
(y & -CHUNK_DEPTH) / CHUNK_DEPTH,
36+
(z & -CHUNK_HEIGHT) / CHUNK_HEIGHT
3737
);
3838
}
3939

0 commit comments

Comments
 (0)