Commit 53812d12 authored by bbguimaraes's avatar bbguimaraes
Browse files

hcwocl: use portable C++ instead of Windows APIs

- `__debugbreak` -> `assert`
- `__declspec(align)` -> `alignas`
- `sprintf_s` -> `snprintf`
- `_aligned_malloc` -> `aligned_alloc`
- `QueryPerformanceCounter` -> `chrono`
- `LARGE_INTEGER` -> `int64_t`
- conditionally add `__stdcall`
parent 09edd865
......@@ -9,8 +9,8 @@
#endif
#ifdef _DEBUG
#include <assert.h>
#define CLASSERT(x) if(!(x)){__debugbreak(); }
#include <cassert>
#define CLASSERT(x) assert((x))
#else
#define CLASSERT(x) if(x){}
#endif
......
......@@ -175,7 +175,7 @@ void glDraw3DStrings(float value, const float4& pos)
glDisable(GL_LIGHTING);
char valueChar[128];
sprintf_s(valueChar, "%3.2f", value);
std::snprintf(valueChar, sizeof(valueChar), "%3.2f", value);
glDraw3DStrings(valueChar, pos);
glEnable(GL_LIGHTING);
......
/*
2011 Takahiro Harada
*/
#include <chrono>
#include <cstdint>
#include <cstring>
#include <stdio.h>
#include <fstream>
......@@ -339,10 +341,13 @@ KernelBuilderCL::KernelBuilder( const DeviceDataBase* deviceDataBase, char* file
char fileNameWithExtension[256];
if( addExtension )
sprintf_s( fileNameWithExtension, "%s.cl", fileName );
std::snprintf(
fileNameWithExtension, sizeof(fileNameWithExtension),
"%s.cl", fileName);
else
sprintf_s( fileNameWithExtension, "%s", fileName );
std::snprintf(
fileNameWithExtension, sizeof(fileNameWithExtension),
"%s", fileName);
CLASSERT( deviceDataBase->m_type == DeviceDataBase::TYPE_CL );
DeviceDataCL* deviceData = (DeviceDataCL*)deviceDataBase;
......@@ -601,7 +606,7 @@ struct StopwatchCL
CAPACITY = 64,
};
const DeviceDataBase* m_deviceData;
LARGE_INTEGER m_t[CAPACITY];
std::chrono::system_clock::time_point m_t[CAPACITY];
int m_idx;
};
......@@ -629,7 +634,7 @@ void StopwatchCL::start()
void StopwatchCL::split()
{
DeviceUtilsCL::waitForCompletion( m_deviceData );
QueryPerformanceCounter(&m_t[m_idx++]);
m_t[m_idx++] = std::chrono::system_clock::now();
}
void StopwatchCL::stop()
......@@ -639,20 +644,14 @@ void StopwatchCL::stop()
float StopwatchCL::getMs()
{
LARGE_INTEGER m_frequency;
QueryPerformanceFrequency( &m_frequency );
return (float)(1000*(m_t[1].QuadPart - m_t[0].QuadPart))/m_frequency.QuadPart;
using T = std::chrono::duration<float, std::milli>;
return std::chrono::duration_cast<T>(m_t[1] - m_t[0]).count();
}
void StopwatchCL::getMs( float* times, int capacity )
{
LARGE_INTEGER m_frequency;
QueryPerformanceFrequency( &m_frequency );
for(int i=0; i<capacity; i++) times[i] = 0.f;
for(int i=0; i<min2(capacity, m_idx); i++)
{
times[i] = (float)(1000*(m_t[i+1].QuadPart - m_t[i].QuadPart))/m_frequency.QuadPart;
}
using T = std::chrono::duration<float, std::milli>;
for(int i=0; i<min2(capacity, m_idx-1); i++)
times[i] = std::chrono::duration_cast<T>(m_t[i+1] - m_t[i]).count();
}
......@@ -7,8 +7,7 @@
#include <Common/Math/Math.h>
#include <Common/Math/Quaternion.h>
_MEM_CLASSALIGN16
struct Aabb
struct alignas(16) Aabb
{
public:
_MEM_ALIGNED_ALLOCATOR16;
......
......@@ -4,8 +4,10 @@
#ifndef ARRAY_H
#define ARRAY_H
#include <cstdlib>
#include <string.h>
#include <malloc.h>
#include <Common/Base/Error.h>
template <class T>
......@@ -64,7 +66,7 @@ Array<T>::Array()
m_size = 0;
m_capacity = DEFAULT_SIZE;
// m_data = new T[ m_capacity ];
m_data = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
m_data = static_cast<T*>(std::aligned_alloc(16, sizeof(T) * m_capacity));
for(int i=0; i<m_capacity; i++) new(&m_data[i])T;
}
......@@ -74,7 +76,7 @@ Array<T>::Array(int size)
m_size = size;
m_capacity = size;
// m_data = new T[ m_capacity ];
m_data = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
m_data = static_cast<T*>(std::aligned_alloc(16, sizeof(T) * m_capacity));
for(int i=0; i<m_capacity; i++) new(&m_data[i])T;
}
......@@ -84,7 +86,7 @@ Array<T>::~Array()
if( m_data )
{
// delete [] m_data;
_aligned_free( m_data );
std::free(m_data);
m_data = NULL;
}
}
......@@ -111,10 +113,10 @@ void Array<T>::pushBack(const T& elem)
int oldCap = m_capacity;
m_capacity += INCREASE_SIZE;
// T* s = new T[m_capacity];
T* s = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
T* s = static_cast<T*>(std::aligned_alloc(16, sizeof(T) * m_capacity));
memcpy( s, m_data, sizeof(T)*oldCap );
// delete [] m_data;
_aligned_free( m_data );
std::free(m_data);
m_data = s;
}
m_data[ m_size++ ] = elem;
......@@ -141,11 +143,11 @@ void Array<T>::setSize(int size)
int oldCap = m_capacity;
m_capacity = size;
// T* s = new T[m_capacity];
T* s = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
T* s = static_cast<T*>(std::aligned_alloc(16, sizeof(T) * m_capacity));
for(int i=0; i<m_capacity; i++) new(&s[i])T;
memcpy( s, m_data, sizeof(T)*oldCap );
// delete [] m_data;
_aligned_free( m_data );
std::free(m_data);
m_data = s;
}
m_size = size;
......
......@@ -4,7 +4,7 @@
#ifndef CL_MATH_H
#define CL_MATH_H
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <float.h>
#include <xmmintrin.h>
......@@ -17,16 +17,13 @@
#define PI 3.14159265358979323846f
#define NEXTMULTIPLEOF(num, alignment) (((num)/(alignment) + (((num)%(alignment)==0)?0:1))*(alignment))
#define _MEM_CLASSALIGN16 __declspec(align(16))
#define _MEM_ALIGNED_ALLOCATOR16 void* operator new(size_t size) { return _aligned_malloc( size, 16 ); } \
void operator delete(void *p) { _aligned_free( p ); } \
void* operator new[](size_t size) { return _aligned_malloc( size, 16 ); } \
void operator delete[](void *p) { _aligned_free( p ); } \
#define _MEM_ALIGNED_ALLOCATOR16 \
void* operator new(size_t size) { return std::aligned_alloc(16, size); } \
void operator delete(void *p) { std::free(p); } \
void* operator new[](size_t size) { return std::aligned_alloc(16, size); } \
void operator delete[](void *p) { std::free(p); } \
void* operator new(size_t size, void* p) { return p; } \
void operator delete(void *p, void* pp) {}
void operator delete(void *p, void* pp) {}
template<class T>
T nextPowerOf2(T n)
......@@ -37,9 +34,7 @@ T nextPowerOf2(T n)
return n+1;
}
_MEM_CLASSALIGN16
struct float4
struct alignas(16) float4
{
_MEM_ALIGNED_ALLOCATOR16;
union
......@@ -56,8 +51,7 @@ struct float4
};
};
_MEM_CLASSALIGN16
struct int4
struct alignas(16) int4
{
_MEM_ALIGNED_ALLOCATOR16;
union
......
......@@ -10,8 +10,7 @@
// Matrix3x3
///////////////////////////////////////
typedef
_MEM_CLASSALIGN16 struct
typedef struct alignas(16) Matrix3x3
{
_MEM_ALIGNED_ALLOCATOR16;
float4 m_row[3];
......
......@@ -4,14 +4,9 @@
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <time.h>
class Stopwatch
{
public:
__inline
Stopwatch();
__inline
void start();
__inline
......@@ -30,30 +25,22 @@ class Stopwatch
{
CAPACITY = 64,
};
LARGE_INTEGER m_frequency;
LARGE_INTEGER m_t[CAPACITY];
std::chrono::system_clock::time_point m_t[CAPACITY];
public:
int m_idx;
};
__inline
Stopwatch::Stopwatch()
{
QueryPerformanceFrequency( &m_frequency );
}
__inline
void Stopwatch::start()
{
m_idx = 0;
QueryPerformanceCounter(&m_t[m_idx++]);
split();
}
__inline
void Stopwatch::split()
{
QueryPerformanceCounter(&m_t[m_idx++]);
m_t[m_idx++] = std::chrono::system_clock::now();
}
__inline
......@@ -65,18 +52,17 @@ void Stopwatch::stop()
__inline
float Stopwatch::getMs()
{
return (float)(1000*(m_t[1].QuadPart - m_t[0].QuadPart))/m_frequency.QuadPart;
using T = std::chrono::duration<float, std::milli>;
return std::chrono::duration_cast<T>(m_t[1] - m_t[0]).count();
}
__inline
void Stopwatch::getMs(float* times, int capacity)
{
for(int i=0; i<capacity; i++) times[i] = 0.f;
using T = std::chrono::duration<float, std::milli>;
for(int i=0; i<min2(capacity, m_idx-1); i++)
{
times[i] = (float)(1000*(m_t[i+1].QuadPart - m_t[i].QuadPart))/m_frequency.QuadPart;
}
times[i] = std::chrono::duration_cast<T>(m_t[i+1] - m_t[i]).count();
}
......
......@@ -764,8 +764,8 @@ void Dem2Demo::step( float dt )
m_nTxtLines = 0;
{
sprintf_s(m_txtBuffer[m_nTxtLines++], LINE_CAPACITY, "S:%d, L:%d", m_numSParticles, m_numLParticles);
sprintf_s(m_txtBuffer[m_nTxtLines++], LINE_CAPACITY, "Total : %3.3f", tg);
std::snprintf(m_txtBuffer[m_nTxtLines++], LINE_CAPACITY, "S:%d, L:%d", m_numSParticles, m_numLParticles);
std::snprintf(m_txtBuffer[m_nTxtLines++], LINE_CAPACITY, "Total : %3.3f", tg);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment