Skip to content

Commit

Permalink
demo: Fix strict aliasing issues with gcc-7.3
Browse files Browse the repository at this point in the history
hashRange reads unsigned int from the given byte range, which violates
strict aliasing as they refer to floats. Work around this by using the
union hack (which technically violates strict aliasing, but is blessed
by gcc).
  • Loading branch information
zeux committed Nov 13, 2018
1 parent e794cb4 commit b1d6082
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ struct Mesh
std::vector<unsigned int> indices;
};

struct Triangle
union Triangle
{
Vertex v[3];

bool operator<(const Triangle& other) const
{
return memcmp(v, other.v, sizeof(Triangle)) < 0;
}
char data[sizeof(Vertex)*3];
};

Mesh generatePlane(unsigned int N)
Expand Down Expand Up @@ -245,7 +241,7 @@ unsigned int hashMesh(const Mesh& mesh)
// skip degenerate triangles since some algorithms don't preserve them
if (rotateTriangle(t))
{
unsigned int hash = hashRange(reinterpret_cast<const char*>(&t), sizeof(t));
unsigned int hash = hashRange(t.data, sizeof(t.data));

h1 ^= hash;
h2 += hash;
Expand Down

0 comments on commit b1d6082

Please sign in to comment.