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
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2018-01-08 15:23:25 +01:00
parent cf17cdab19
commit a3d859fb4f
2 changed files with 55 additions and 0 deletions

View File

@ -18,4 +18,36 @@
*/ */
#include "util-math.h" #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);
}

View File

@ -21,6 +21,12 @@
#include <math.h> #include <math.h>
#include <inttypes.h> #include <inttypes.h>
// OBS
#include <graphics/vec2.h>
#include <graphics/vec3.h>
#include <graphics/vec4.h>
// Constants
#define PI 3.1415926535897932384626433832795 #define PI 3.1415926535897932384626433832795
#define PI2 6.283185307179586476925286766559 #define PI2 6.283185307179586476925286766559
#define PI2_SQROOT 2.506628274631000502415765284811 #define PI2_SQROOT 2.506628274631000502415765284811
@ -39,3 +45,20 @@ inline size_t GetNearestPowerOfTwoAbove(size_t v) {
inline size_t GetNearestPowerOfTwoBelow(size_t v) { inline size_t GetNearestPowerOfTwoBelow(size_t v) {
return 1ull << size_t(floor(log10(double(v)) / log10(2.0))); 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);
};
}