Skip to content

Commit

Permalink
Bizzare crash fix for 32-bit architectures
Browse files Browse the repository at this point in the history
- Removes undefined behaviour from VoxelCoord modulo calculations; the
  crash was caused by out of all places modulo, which was working as
  mathematically intended on 64-bit but was returning negative values on
  32-bit builds (because modulo seems to be implemented as a
  mathematical formula in the instruction set for some reason)
  • Loading branch information
untodesu committed Jan 1, 2025
1 parent 3b26d2b commit f00e0e7
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 97 deletions.
2 changes: 1 addition & 1 deletion source/common/packet_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ std::string PacketBuffer::read_string(PacketBuffer &buffer)
buffer.read_position += 1U;
}

return std::move(result);
return result;
}

void PacketBuffer::write_FP32(PacketBuffer &buffer, float value)
Expand Down
2 changes: 1 addition & 1 deletion source/game/client/entity/player_move.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static Vec3f accelerate(const Vec3f &wish_dir, const Vec3f &velocity, float wish
auto result = Vec3f(velocity);
result[0] += accel_speed * wish_dir[0];
result[2] += accel_speed * wish_dir[2];
return std::move(result);
return result;
}

static Vec3f air_move(const Vec3f &wish_dir, const Vec3f &velocity)
Expand Down
2 changes: 1 addition & 1 deletion source/game/client/world/chunk_quad.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ constexpr inline static ChunkQuad make_chunk_quad(const Vec3f &position, const V
result[1] |= (0x000007FFU & static_cast<std::uint32_t>(texture)) << 17U;
result[1] |= (0x0000001FU & static_cast<std::uint32_t>(frames)) << 12U;

return std::move(result);
return result;
}
12 changes: 6 additions & 6 deletions source/game/shared/world/chunk_coord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

VoxelCoord ChunkCoord::to_voxel(const ChunkCoord &cvec, std::size_t index)
{
return std::move(ChunkCoord::to_voxel(cvec, LocalCoord::from_index(index)));
return ChunkCoord::to_voxel(cvec, LocalCoord::from_index(index));
}

VoxelCoord ChunkCoord::to_voxel(const ChunkCoord &cvec, const LocalCoord &lvec)
Expand All @@ -18,15 +18,15 @@ VoxelCoord ChunkCoord::to_voxel(const ChunkCoord &cvec, const LocalCoord &lvec)
result[0] = lvec[0] + (cvec[0] << CHUNK_SIZE_LOG2);
result[1] = lvec[1] + (cvec[1] << CHUNK_SIZE_LOG2);
result[2] = lvec[2] + (cvec[2] << CHUNK_SIZE_LOG2);
return std::move(result);
return result;
}

WorldCoord ChunkCoord::to_world(const ChunkCoord &cvec, const Vec3f &lvec)
{
WorldCoord result = {};
result.chunk = cvec;
result.local = lvec;
return std::move(result);
return result;
}

WorldCoord ChunkCoord::to_world(const ChunkCoord &cvec, const LocalCoord &lvec)
Expand All @@ -36,14 +36,14 @@ WorldCoord ChunkCoord::to_world(const ChunkCoord &cvec, const LocalCoord &lvec)
result.local[0] = static_cast<float>(lvec[0]);
result.local[1] = static_cast<float>(lvec[1]);
result.local[2] = static_cast<float>(lvec[2]);
return std::move(result);
return result;
}

WorldCoord ChunkCoord::to_world(const ChunkCoord &cvec)
{
WorldCoord result = {};
result.chunk = cvec;
return std::move(result);
return result;
}

Vec3f ChunkCoord::to_vec3f(const ChunkCoord &cvec)
Expand All @@ -52,5 +52,5 @@ Vec3f ChunkCoord::to_vec3f(const ChunkCoord &cvec)
result[0] = static_cast<float>(cvec[0] << CHUNK_SIZE_LOG2);
result[1] = static_cast<float>(cvec[1] << CHUNK_SIZE_LOG2);
result[2] = static_cast<float>(cvec[2] << CHUNK_SIZE_LOG2);
return std::move(result);
return result;
}
4 changes: 2 additions & 2 deletions source/game/shared/world/local_coord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LocalCoord LocalCoord::from_index(std::size_t index)
result[0] = static_cast<std::int64_t>((index % CHUNK_SIZE));
result[1] = static_cast<std::int64_t>((index / CHUNK_SIZE) / CHUNK_SIZE);
result[2] = static_cast<std::int64_t>((index / CHUNK_SIZE) % CHUNK_SIZE);
return std::move(result);
return result;
}

std::size_t LocalCoord::to_index(const LocalCoord &lvec)
Expand All @@ -27,5 +27,5 @@ Vec3f LocalCoord::to_vec3f(const LocalCoord &lvec)
result[0] = static_cast<float>(lvec[0]);
result[1] = static_cast<float>(lvec[1]);
result[2] = static_cast<float>(lvec[2]);
return std::move(result);
return result;
}
20 changes: 10 additions & 10 deletions source/game/shared/world/voxel_coord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ ChunkCoord VoxelCoord::to_chunk(const VoxelCoord &vvec)
result[0] = vvec[0] >> CHUNK_SIZE_LOG2;
result[1] = vvec[1] >> CHUNK_SIZE_LOG2;
result[2] = vvec[2] >> CHUNK_SIZE_LOG2;
return std::move(result);
return result;
}

LocalCoord VoxelCoord::to_local(const VoxelCoord &vvec)
{
LocalCoord result = {};
result[0] = vvec[0] % CHUNK_SIZE;
result[1] = vvec[1] % CHUNK_SIZE;
result[2] = vvec[2] % CHUNK_SIZE;
return std::move(result);
result[0] = cxpr::mod_signed<VoxelCoord::value_type>(vvec[0], CHUNK_SIZE);
result[1] = cxpr::mod_signed<VoxelCoord::value_type>(vvec[1], CHUNK_SIZE);
result[2] = cxpr::mod_signed<VoxelCoord::value_type>(vvec[2], CHUNK_SIZE);
return result;
}

WorldCoord VoxelCoord::to_world(const VoxelCoord &vvec)
Expand All @@ -31,10 +31,10 @@ WorldCoord VoxelCoord::to_world(const VoxelCoord &vvec)
result.chunk[0] = vvec[0] >> CHUNK_SIZE_LOG2;
result.chunk[1] = vvec[1] >> CHUNK_SIZE_LOG2;
result.chunk[2] = vvec[2] >> CHUNK_SIZE_LOG2;
result.local[0] = static_cast<float>(vvec[0] % CHUNK_SIZE);
result.local[1] = static_cast<float>(vvec[1] % CHUNK_SIZE);
result.local[2] = static_cast<float>(vvec[2] % CHUNK_SIZE);
return std::move(result);
result.local[0] = static_cast<float>(cxpr::mod_signed<VoxelCoord::value_type>(vvec[0], CHUNK_SIZE));
result.local[1] = static_cast<float>(cxpr::mod_signed<VoxelCoord::value_type>(vvec[1], CHUNK_SIZE));
result.local[2] = static_cast<float>(cxpr::mod_signed<VoxelCoord::value_type>(vvec[2], CHUNK_SIZE));
return result;
}

Vec3f VoxelCoord::to_vec3f(const VoxelCoord &vvec)
Expand All @@ -43,5 +43,5 @@ Vec3f VoxelCoord::to_vec3f(const VoxelCoord &vvec)
result[0] = static_cast<float>(vvec[0]);
result[1] = static_cast<float>(vvec[1]);
result[2] = static_cast<float>(vvec[2]);
return std::move(result);
return result;
}
10 changes: 5 additions & 5 deletions source/game/shared/world/world_coord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LocalCoord WorldCoord::to_local(const WorldCoord &wvec)
result[0] = cxpr::floor<std::int64_t>(wvec.local[0]);
result[1] = cxpr::floor<std::int64_t>(wvec.local[1]);
result[2] = cxpr::floor<std::int64_t>(wvec.local[2]);
return std::move(result);
return result;
}

VoxelCoord WorldCoord::to_voxel(const WorldCoord &wvec)
Expand All @@ -18,7 +18,7 @@ VoxelCoord WorldCoord::to_voxel(const WorldCoord &wvec)
result[0] = cxpr::floor<std::int64_t>(wvec.local[0]) + (wvec.chunk[0] << CHUNK_SIZE_LOG2);
result[1] = cxpr::floor<std::int64_t>(wvec.local[1]) + (wvec.chunk[1] << CHUNK_SIZE_LOG2);
result[2] = cxpr::floor<std::int64_t>(wvec.local[2]) + (wvec.chunk[2] << CHUNK_SIZE_LOG2);
return std::move(result);
return result;
}

Vec3f WorldCoord::to_vec3f(const WorldCoord &wvec)
Expand All @@ -27,7 +27,7 @@ Vec3f WorldCoord::to_vec3f(const WorldCoord &wvec)
result[0] = static_cast<float>(wvec.chunk[0] << CHUNK_SIZE_LOG2) + wvec.local[0];
result[1] = static_cast<float>(wvec.chunk[1] << CHUNK_SIZE_LOG2) + wvec.local[1];
result[2] = static_cast<float>(wvec.chunk[2] << CHUNK_SIZE_LOG2) + wvec.local[2];
return std::move(result);
return result;
}

Vec3f WorldCoord::to_vec3f(const WorldCoord &pivot, const ChunkCoord &cvec)
Expand All @@ -36,7 +36,7 @@ Vec3f WorldCoord::to_vec3f(const WorldCoord &pivot, const ChunkCoord &cvec)
result[0] = static_cast<float>((cvec[0] - pivot.chunk[0]) << CHUNK_SIZE_LOG2) - pivot.local[0];
result[1] = static_cast<float>((cvec[1] - pivot.chunk[1]) << CHUNK_SIZE_LOG2) - pivot.local[1];
result[2] = static_cast<float>((cvec[2] - pivot.chunk[2]) << CHUNK_SIZE_LOG2) - pivot.local[2];
return std::move(result);
return result;
}

Vec3f WorldCoord::to_vec3f(const WorldCoord &pivot, const WorldCoord &wvec)
Expand All @@ -45,5 +45,5 @@ Vec3f WorldCoord::to_vec3f(const WorldCoord &pivot, const WorldCoord &wvec)
result[0] = static_cast<float>((wvec.chunk[0] - pivot.chunk[0]) << CHUNK_SIZE_LOG2) + (wvec.local[0] - pivot.local[0]);
result[1] = static_cast<float>((wvec.chunk[1] - pivot.chunk[1]) << CHUNK_SIZE_LOG2) + (wvec.local[1] - pivot.local[1]);
result[2] = static_cast<float>((wvec.chunk[2] - pivot.chunk[2]) << CHUNK_SIZE_LOG2) + (wvec.local[2] - pivot.local[2]);
return std::move(result);
return result;
}
10 changes: 5 additions & 5 deletions source/mathlib/box3base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ constexpr Box3base<T> Box3base<T>::from_bounds(const Vec3base<T> &min, const Vec
Box3base<T> result;
result.min = min;
result.max = max;
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -36,7 +36,7 @@ constexpr Box3base<T> Box3base<T>::from_offset(const Vec3base<T> &offset, const
Box3base<T> result;
result.min = offset;
result.max = offset + size;
return std::move(result);
return result;
}

template<typename T>
Expand Down Expand Up @@ -69,7 +69,7 @@ constexpr Box3base<T> Box3base<T>::combine(const Box3base<T> &abox, const Box3ba
Box3base<T> result;
result.min = abox.min;
result.max = bbox.max;
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -78,7 +78,7 @@ constexpr Box3base<T> Box3base<T>::multiply(const Box3base<T> &abox, const Box3b
Box3base<T> result;
result.min = bbox.min;
result.max = abox.max;
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -87,5 +87,5 @@ constexpr Box3base<T> Box3base<T>::push(const Box3base<T> &box, const Vec3base<T
Box3base<T> result;
result.min = box.min + vector;
result.max = box.max + vector;
return std::move(result);
return result;
}
13 changes: 13 additions & 0 deletions source/mathlib/constexpr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ constexpr static inline const T max(const T x, const T y);
template<typename T>
constexpr static inline const T min(const T x, const T y);
template<typename T>
constexpr static inline const T mod_signed(const T x, const T m);
template<typename T>
constexpr static inline const T pow2(const T x);
template<typename T>
constexpr static inline const T radians(const T x);
Expand Down Expand Up @@ -121,6 +123,17 @@ constexpr static inline const T cxpr::min(const T x, const T y)
return x;
}

template<typename T>
constexpr static inline const T cxpr::mod_signed(const T x, const T m)
{
static_assert(std::is_signed_v<T>);
static_assert(std::is_integral_v<T>);
const T result = static_cast<T>(x % m);
if(result < T(0))
return result + m;
return result;
}

template<typename T>
constexpr static inline const T cxpr::pow2(const T x)
{
Expand Down
10 changes: 5 additions & 5 deletions source/mathlib/mat4x4base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ constexpr inline Vec4base<T> Mat4x4base<T>::product(const Mat4x4base<T> &matrix,
result[1] = matrix[0][1] * vector[0] + matrix[1][1] * vector[1] + matrix[2][1] * vector[2] + matrix[3][1];
result[2] = matrix[0][2] * vector[0] + matrix[1][2] * vector[1] + matrix[2][2] * vector[2] + matrix[3][2];
result[3] = matrix[0][3] * vector[0] + matrix[1][3] * vector[1] + matrix[2][3] * vector[2] + matrix[3][3];
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -57,7 +57,7 @@ constexpr inline Vec4base<T> Mat4x4base<T>::product(const Mat4x4base<T> &matrix,
result[1] = matrix[0][1] * vector[0] + matrix[1][1] * vector[1] + matrix[2][1] * vector[2] + matrix[3][1] * vector[3];
result[2] = matrix[0][2] * vector[0] + matrix[1][2] * vector[1] + matrix[2][2] * vector[2] + matrix[3][2] * vector[3];
result[3] = matrix[0][3] * vector[0] + matrix[1][3] * vector[1] + matrix[2][3] * vector[2] + matrix[3][3] * vector[3];
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -68,7 +68,7 @@ constexpr inline Mat4x4base<T> Mat4x4base<T>::product(const Mat4x4base<T> &amat,
result[1] = amat[0] * bmat[1][0] + amat[1] * bmat[1][1] + amat[2] * bmat[1][2] + amat[3] * bmat[1][3];
result[2] = amat[0] * bmat[2][0] + amat[1] * bmat[2][1] + amat[2] * bmat[2][2] + amat[3] * bmat[2][3];
result[3] = amat[0] * bmat[3][0] + amat[1] * bmat[3][1] + amat[2] * bmat[3][2] + amat[3] * bmat[3][3];
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -79,7 +79,7 @@ constexpr inline Mat4x4base<T> Mat4x4base<T>::identity(void)
result[1] = Vec4base<T>(T(0), T(1), T(0), T(0));
result[2] = Vec4base<T>(T(0), T(0), T(1), T(0));
result[3] = Vec4base<T>(T(0), T(0), T(0), T(1));
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -90,5 +90,5 @@ constexpr inline Mat4x4base<T> Mat4x4base<T>::zero(void)
result[1] = Vec4base<T>(T(0), T(0), T(0), T(0));
result[2] = Vec4base<T>(T(0), T(0), T(0), T(0));
result[3] = Vec4base<T>(T(0), T(0), T(0), T(0));
return std::move(result);
return result;
}
8 changes: 4 additions & 4 deletions source/mathlib/mat4x4f.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Mat4x4f Mat4x4f::model_rotate(const Mat4x4f &model, const float angle, const Vec
result[1] = model[0] * rotmat[1][0] + model[1] * rotmat[1][1] + model[2] * rotmat[1][2];
result[2] = model[0] * rotmat[2][0] + model[1] * rotmat[2][1] + model[2] * rotmat[2][2];
result[3] = model[3];
return std::move(result);
return result;
}

Mat4x4f Mat4x4f::proj_ortho(float left, float right, float bottom, float top, float z_near, float z_far)
Expand All @@ -38,7 +38,7 @@ Mat4x4f Mat4x4f::proj_ortho(float left, float right, float bottom, float top, fl
result[3][0] = -1.0f * (right + left) / (right - left);
result[3][1] = -1.0f * (top + bottom) / (top - bottom);
result[3][2] = -1.0f * z_near / (z_far - z_near);
return std::move(result);
return result;
}

Mat4x4f Mat4x4f::proj_persp(float y_fov, float aspect, float z_near, float z_far)
Expand All @@ -52,7 +52,7 @@ Mat4x4f Mat4x4f::proj_persp(float y_fov, float aspect, float z_near, float z_far
result[2][2] = -1.0f * (z_far + z_near) * inv_z_delta;
result[2][3] = -1.0f;
result[3][2] = -2.0f * z_far * z_near * inv_z_delta;
return std::move(result);
return result;
}

Mat4x4f Mat4x4f::view_psrc(const Vec3f &position, const Vec3angles &angles)
Expand All @@ -73,5 +73,5 @@ Mat4x4f Mat4x4f::view_psrc(const Vec3f &position, const Vec3angles &angles)
result[1][2] = -forward[1];
result[2][2] = -forward[2];
result[3][2] = forward[0] * position[0] + forward[1] * position[1] + forward[2] * position[2];
return std::move(result);
return result;
}
14 changes: 7 additions & 7 deletions source/mathlib/vec2base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ constexpr inline Vec2base<T> Vec2base<T>::operator+(const Vec2base<T> &vector) c
Vec2base<T> result = {};
result[0] = this[0][0] + vector[0];
result[1] = this[0][1] + vector[1];
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -74,7 +74,7 @@ constexpr inline Vec2base<T> Vec2base<T>::operator-(const Vec2base<T> &vector) c
Vec2base<T> result = {};
result[0] = this[0][0] - vector[0];
result[1] = this[0][1] - vector[1];
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -83,7 +83,7 @@ constexpr inline Vec2base<T> Vec2base<T>::operator+(const T scalar) const
Vec2base<T> result = {};
result[0] = this[0][0] + scalar;
result[1] = this[0][1] + scalar;
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -92,7 +92,7 @@ constexpr inline Vec2base<T> Vec2base<T>::operator-(const T scalar) const
Vec2base<T> result = {};
result[0] = this[0][0] - scalar;
result[1] = this[0][1] - scalar;
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -101,7 +101,7 @@ constexpr inline Vec2base<T> Vec2base<T>::operator*(const T scalar) const
Vec2base<T> result = {};
result[0] = this[0][0] * scalar;
result[1] = this[0][1] * scalar;
return std::move(result);
return result;
}

template<typename T>
Expand All @@ -110,7 +110,7 @@ constexpr inline Vec2base<T> Vec2base<T>::operator/(const T scalar) const
Vec2base<T> result = {};
result[0] = this[0][0] / scalar;
result[1] = this[0][1] / scalar;
return std::move(result);
return result;
}

template<typename T>
Expand Down Expand Up @@ -164,5 +164,5 @@ constexpr inline Vec2base<T> &Vec2base<T>::operator/=(const T scalar)
template<typename T>
constexpr inline Vec2base<T> Vec2base<T>::zero(void)
{
return std::move(Vec2base<T>(T(0), T(0)));
return Vec2base<T>(T(0), T(0));
}
2 changes: 1 addition & 1 deletion source/mathlib/vec2f.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Vec2f Vec2f::normalized(const Vec2f &vector)
const float multip = 1.0f / Vec2f::length(vector);
result[0] = multip * vector[0];
result[1] = multip * vector[1];
return std::move(result);
return result;
}

float Vec2f::normalize(Vec2f &vector)
Expand Down
Loading

0 comments on commit f00e0e7

Please sign in to comment.