From a3d859fb4f3c55eccdd82b5969e8aabd48fa1ae6 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 8 Jan 2018 15:23:25 +0100 Subject: [PATCH] util-math: Implement aligned vec3 and vec4 versions A bug in Visual C++ 2013 32-bit & 2015 32-bit causes the C++ compiler to incorrectly align the vec3 and vec4 structs to 8-byte instead of 16-byte, resulting in a crash if the target PC supports SSE. Visual Studio 2017 and 64-bit builds are not affected. Related: #9 --- source/util-math.cpp | 32 ++++++++++++++++++++++++++++++++ source/util-math.h | 23 +++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/source/util-math.cpp b/source/util-math.cpp index 4e5d3c92..8a420274 100644 --- a/source/util-math.cpp +++ b/source/util-math.cpp @@ -18,4 +18,36 @@ */ #include "util-math.h" +#include "util-memory.h" +void* util::vec3a::operator new(size_t count){ + return malloc_aligned(16, count); +} + +void* util::vec3a::operator new[](size_t count) { + return malloc_aligned(16, count); +} + +void util::vec3a::operator delete(void* p) { + free_aligned(p); +} + +void util::vec3a::operator delete[](void* p) { + free_aligned(p); +} + +void* util::vec4a::operator new(size_t count) { + return malloc_aligned(16, count); +} + +void* util::vec4a::operator new[](size_t count) { + return malloc_aligned(16, count); +} + +void util::vec4a::operator delete(void* p) { + free_aligned(p); +} + +void util::vec4a::operator delete[](void* p) { + free_aligned(p); +} diff --git a/source/util-math.h b/source/util-math.h index 3fcfd50b..35bd3a43 100644 --- a/source/util-math.h +++ b/source/util-math.h @@ -21,6 +21,12 @@ #include #include +// OBS +#include +#include +#include + +// Constants #define PI 3.1415926535897932384626433832795 #define PI2 6.283185307179586476925286766559 #define PI2_SQROOT 2.506628274631000502415765284811 @@ -39,3 +45,20 @@ inline size_t GetNearestPowerOfTwoAbove(size_t v) { inline size_t GetNearestPowerOfTwoBelow(size_t v) { return 1ull << size_t(floor(log10(double(v)) / log10(2.0))); } + + +namespace util { + __declspec(align(16)) struct vec3a : public vec3 { + static void* vec3a::operator new(size_t count); + static void* vec3a::operator new[](size_t count); + static void vec3a::operator delete(void* p); + static void vec3a::operator delete[](void* p); + }; + + __declspec(align(16)) struct vec4a : public vec4 { + static void* vec4a::operator new(size_t count); + static void* vec4a::operator new[](size_t count); + static void vec4a::operator delete(void* p); + static void vec4a::operator delete[](void* p); + }; +} \ No newline at end of file