Reformat test harness code (#940)

* Reformat common help text

Signed-off-by: Stuart Brady <stuart.brady@arm.com>

* Reformat test harness code

This goes part of the way to fixing issue #625.

Signed-off-by: Stuart Brady <stuart.brady@arm.com>
This commit is contained in:
Stuart Brady
2020-10-30 14:13:52 +00:00
committed by GitHub
parent 55976fad35
commit af7d914514
38 changed files with 7676 additions and 6692 deletions

View File

@@ -19,7 +19,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#if defined( __APPLE__ ) || defined( __linux__ ) || defined( _WIN32 ) // or any other POSIX system #if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
// or any other POSIX system
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
@@ -47,14 +48,16 @@ void ThreadPool_Init(void);
void ThreadPool_Exit(void); void ThreadPool_Exit(void);
#if defined(__MINGW32__) #if defined(__MINGW32__)
// Mutex for implementing super heavy atomic operations if you don't have GCC or MSVC // Mutex for implementing super heavy atomic operations if you don't have GCC or
// MSVC
CRITICAL_SECTION gAtomicLock; CRITICAL_SECTION gAtomicLock;
#elif defined(__GNUC__) || defined(_MSC_VER) #elif defined(__GNUC__) || defined(_MSC_VER)
#else #else
pthread_mutex_t gAtomicLock; pthread_mutex_t gAtomicLock;
#endif #endif
// Atomic add operator with mem barrier. Mem barrier needed to protect state modified by the worker functions. // Atomic add operator with mem barrier. Mem barrier needed to protect state
// modified by the worker functions.
cl_int ThreadPool_AtomicAdd(volatile cl_int *a, cl_int b) cl_int ThreadPool_AtomicAdd(volatile cl_int *a, cl_int b)
{ {
#if defined(__MINGW32__) #if defined(__MINGW32__)
@@ -65,19 +68,23 @@ cl_int ThreadPool_AtomicAdd( volatile cl_int *a, cl_int b )
LeaveCriticalSection(&gAtomicLock); LeaveCriticalSection(&gAtomicLock);
return old; return old;
#elif defined(__GNUC__) #elif defined(__GNUC__)
// GCC extension: http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html#Atomic-Builtins // GCC extension:
// http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html#Atomic-Builtins
return __sync_fetch_and_add(a, b); return __sync_fetch_and_add(a, b);
// do we need __sync_synchronize() here, too? GCC docs are unclear whether __sync_fetch_and_add does a synchronize // do we need __sync_synchronize() here, too? GCC docs are unclear whether
// __sync_fetch_and_add does a synchronize
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
return (cl_int)_InterlockedExchangeAdd((volatile LONG *)a, (LONG)b); return (cl_int)_InterlockedExchangeAdd((volatile LONG *)a, (LONG)b);
#else #else
#warning Please add a atomic add implementation here, with memory barrier. Fallback code is slow. #warning Please add a atomic add implementation here, with memory barrier. Fallback code is slow.
if (pthread_mutex_lock(&gAtomicLock)) if (pthread_mutex_lock(&gAtomicLock))
log_error( "Atomic operation failed. pthread_mutex_lock(&gAtomicLock) returned an error\n"); log_error("Atomic operation failed. pthread_mutex_lock(&gAtomicLock) "
"returned an error\n");
cl_int old = *a; cl_int old = *a;
*a = old + b; *a = old + b;
if (pthread_mutex_unlock(&gAtomicLock)) if (pthread_mutex_unlock(&gAtomicLock))
log_error( "Failed to release gAtomicLock. Further atomic operations may deadlock!\n"); log_error("Failed to release gAtomicLock. Further atomic operations "
"may deadlock!\n");
return old; return old;
#endif #endif
} }
@@ -100,16 +107,15 @@ typedef BOOL (CALLBACK *_PINIT_ONCE_FN)(_PINIT_ONCE, PVOID, PVOID *);
#define _INIT_ONCE_IN_PROGRESS 1 #define _INIT_ONCE_IN_PROGRESS 1
#define _INIT_ONCE_DONE 2 #define _INIT_ONCE_DONE 2
static BOOL _InitOnceExecuteOnce( static BOOL _InitOnceExecuteOnce(_PINIT_ONCE InitOnce, _PINIT_ONCE_FN InitFn,
_PINIT_ONCE InitOnce, PVOID Parameter, LPVOID *Context)
_PINIT_ONCE_FN InitFn,
PVOID Parameter,
LPVOID *Context
)
{ {
while (*InitOnce != _INIT_ONCE_DONE) while (*InitOnce != _INIT_ONCE_DONE)
{ {
if (*InitOnce != _INIT_ONCE_IN_PROGRESS && _InterlockedCompareExchange( InitOnce, _INIT_ONCE_IN_PROGRESS, _INIT_ONCE_UNINITIALIZED ) == _INIT_ONCE_UNINITIALIZED ) if (*InitOnce != _INIT_ONCE_IN_PROGRESS
&& _InterlockedCompareExchange(InitOnce, _INIT_ONCE_IN_PROGRESS,
_INIT_ONCE_UNINITIALIZED)
== _INIT_ONCE_UNINITIALIZED)
{ {
InitFn(InitOnce, Parameter, Context); InitFn(InitOnce, Parameter, Context);
*InitOnce = _INIT_ONCE_DONE; *InitOnce = _INIT_ONCE_DONE;
@@ -133,10 +139,12 @@ static BOOL _InitOnceExecuteOnce(
typedef struct typedef struct
{ {
HANDLE mEvent; // Used to park the thread. HANDLE mEvent; // Used to park the thread.
CRITICAL_SECTION mLock[1]; // Used to protect mWaiters, mGeneration and mReleaseCount. // Used to protect mWaiters, mGeneration and mReleaseCount:
CRITICAL_SECTION mLock[1];
volatile cl_int mWaiters; // Number of threads waiting on this cond var. volatile cl_int mWaiters; // Number of threads waiting on this cond var.
volatile cl_int mGeneration; // Wait generation count. volatile cl_int mGeneration; // Wait generation count.
volatile cl_int mReleaseCount; // Number of releases to execute before reseting the event. volatile cl_int mReleaseCount; // Number of releases to execute before
// reseting the event.
} _CONDITION_VARIABLE; } _CONDITION_VARIABLE;
typedef _CONDITION_VARIABLE *_PCONDITION_VARIABLE; typedef _CONDITION_VARIABLE *_PCONDITION_VARIABLE;
@@ -152,7 +160,9 @@ static void _InitializeConditionVariable( _PCONDITION_VARIABLE cond_var )
#endif // !NDEBUG #endif // !NDEBUG
} }
static void _SleepConditionVariableCS( _PCONDITION_VARIABLE cond_var, PCRITICAL_SECTION cond_lock, DWORD ignored) static void _SleepConditionVariableCS(_PCONDITION_VARIABLE cond_var,
PCRITICAL_SECTION cond_lock,
DWORD ignored)
{ {
EnterCriticalSection(cond_var->mLock); EnterCriticalSection(cond_var->mLock);
cl_int generation = cond_var->mGeneration; cl_int generation = cond_var->mGeneration;
@@ -164,7 +174,8 @@ static void _SleepConditionVariableCS( _PCONDITION_VARIABLE cond_var, PCRITICAL_
{ {
WaitForSingleObject(cond_var->mEvent, INFINITE); WaitForSingleObject(cond_var->mEvent, INFINITE);
EnterCriticalSection(cond_var->mLock); EnterCriticalSection(cond_var->mLock);
BOOL done = cond_var->mReleaseCount > 0 && cond_var->mGeneration != generation; BOOL done =
cond_var->mReleaseCount > 0 && cond_var->mGeneration != generation;
LeaveCriticalSection(cond_var->mLock); LeaveCriticalSection(cond_var->mLock);
if (done) if (done)
{ {
@@ -198,7 +209,8 @@ static void _WakeAllConditionVariable( _PCONDITION_VARIABLE cond_var )
#define MAX_COUNT (1 << 29) #define MAX_COUNT (1 << 29)
// Global state to coordinate whether the threads have been launched successfully or not // Global state to coordinate whether the threads have been launched
// successfully or not
#if defined(_MSC_VER) && (_WIN32_WINNT >= 0x600) #if defined(_MSC_VER) && (_WIN32_WINNT >= 0x600)
static _INIT_ONCE threadpool_init_control; static _INIT_ONCE threadpool_init_control;
#elif defined(_WIN32) // MingW of XP #elif defined(_WIN32) // MingW of XP
@@ -208,8 +220,9 @@ pthread_once_t threadpool_init_control = PTHREAD_ONCE_INIT;
#endif #endif
cl_int threadPoolInitErr = -1; // set to CL_SUCCESS on successful thread launch cl_int threadPoolInitErr = -1; // set to CL_SUCCESS on successful thread launch
// critical region lock around ThreadPool_Do. We can only run one ThreadPool_Do at a time, // critical region lock around ThreadPool_Do. We can only run one ThreadPool_Do
// because we are too lazy to set up a queue here, and don't expect to need one. // at a time, because we are too lazy to set up a queue here, and don't expect
// to need one.
#if defined(_WIN32) #if defined(_WIN32)
CRITICAL_SECTION gThreadPoolLock[1]; CRITICAL_SECTION gThreadPoolLock[1];
#else // !_WIN32 #else // !_WIN32
@@ -224,8 +237,11 @@ _CONDITION_VARIABLE cond_var[1];
pthread_mutex_t cond_lock; pthread_mutex_t cond_lock;
pthread_cond_t cond_var; pthread_cond_t cond_var;
#endif // !_WIN32 #endif // !_WIN32
volatile cl_int gRunCount = 0; // Condition variable state. How many iterations on the function left to run.
// set to CL_INT_MAX to cause worker threads to exit. Note: this value might go negative. // Condition variable state. How many iterations on the function left to run,
// set to CL_INT_MAX to cause worker threads to exit. Note: this value might
// go negative.
volatile cl_int gRunCount = 0;
// State that only changes when the threadpool is not working. // State that only changes when the threadpool is not working.
volatile TPFuncPtr gFunc_ptr = NULL; volatile TPFuncPtr gFunc_ptr = NULL;
@@ -242,7 +258,10 @@ HANDLE caller_event;
pthread_mutex_t caller_cond_lock; pthread_mutex_t caller_cond_lock;
pthread_cond_t caller_cond_var; pthread_cond_t caller_cond_var;
#endif // !_WIN32 #endif // !_WIN32
volatile cl_int gRunning = 0; // # of threads intended to be running. Running threads will decrement this as they discover they've run out of work to do.
// # of threads intended to be running. Running threads will decrement this
// as they discover they've run out of work to do.
volatile cl_int gRunning = 0;
// The total number of threads launched. // The total number of threads launched.
volatile cl_int gThreadCount = 0; volatile cl_int gThreadCount = 0;
@@ -271,13 +290,17 @@ void *ThreadPool_WorkerFunc( void *p )
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_mutex_lock(&cond_lock))) if ((err = pthread_mutex_lock(&cond_lock)))
{ {
log_error("Error %d from pthread_mutex_lock. Worker %d unable to block waiting for work. ThreadPool_WorkerFunc failed.\n", err, threadID ); log_error(
"Error %d from pthread_mutex_lock. Worker %d unable to "
"block waiting for work. ThreadPool_WorkerFunc failed.\n",
err, threadID);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
cl_int remaining = ThreadPool_AtomicAdd(&gRunning, -1); cl_int remaining = ThreadPool_AtomicAdd(&gRunning, -1);
// log_info( "ThreadPool_WorkerFunc: gRunning = %d\n", remaining - 1 ); // log_info("ThreadPool_WorkerFunc: gRunning = %d\n",
// remaining - 1);
if (1 == remaining) if (1 == remaining)
{ // last thread out signal the main thread to wake up { // last thread out signal the main thread to wake up
#if defined(_WIN32) #if defined(_WIN32)
@@ -285,23 +308,31 @@ void *ThreadPool_WorkerFunc( void *p )
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_mutex_lock(&caller_cond_lock))) if ((err = pthread_mutex_lock(&caller_cond_lock)))
{ {
log_error("Error %d from pthread_mutex_lock. Unable to wake caller.\n", err ); log_error("Error %d from pthread_mutex_lock. Unable to "
"wake caller.\n",
err);
goto exit; goto exit;
} }
if ((err = pthread_cond_broadcast(&caller_cond_var))) if ((err = pthread_cond_broadcast(&caller_cond_var)))
{ {
log_error("Error %d from pthread_cond_broadcast. Unable to wake up main thread. ThreadPool_WorkerFunc failed.\n", err ); log_error(
"Error %d from pthread_cond_broadcast. Unable to wake "
"up main thread. ThreadPool_WorkerFunc failed.\n",
err);
goto exit; goto exit;
} }
if ((err = pthread_mutex_unlock(&caller_cond_lock))) if ((err = pthread_mutex_unlock(&caller_cond_lock)))
{ {
log_error("Error %d from pthread_mutex_lock. Unable to wake caller.\n", err ); log_error("Error %d from pthread_mutex_lock. Unable to "
"wake caller.\n",
err);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
} }
// loop in case we are woken only to discover that some other thread already did all the work // loop in case we are woken only to discover that some other thread
// already did all the work
while (0 >= item) while (0 >= item)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@@ -309,7 +340,10 @@ void *ThreadPool_WorkerFunc( void *p )
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_cond_wait(&cond_var, &cond_lock))) if ((err = pthread_cond_wait(&cond_var, &cond_lock)))
{ {
log_error("Error %d from pthread_cond_wait. Unable to block for waiting for work. ThreadPool_WorkerFunc failed.\n", err ); log_error(
"Error %d from pthread_cond_wait. Unable to block for "
"waiting for work. ThreadPool_WorkerFunc failed.\n",
err);
pthread_mutex_unlock(&cond_lock); pthread_mutex_unlock(&cond_lock);
goto exit; goto exit;
} }
@@ -336,26 +370,33 @@ void *ThreadPool_WorkerFunc( void *p )
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_mutex_unlock(&cond_lock))) if ((err = pthread_mutex_unlock(&cond_lock)))
{ {
log_error("Error %d from pthread_mutex_unlock. Unable to block for waiting for work. ThreadPool_WorkerFunc failed.\n", err ); log_error(
"Error %d from pthread_mutex_unlock. Unable to block for "
"waiting for work. ThreadPool_WorkerFunc failed.\n",
err);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
} }
// we have a valid item, so do the work // we have a valid item, so do the work
if( CL_SUCCESS == jobError ) // but only if we haven't already encountered an error // but only if we haven't already encountered an error
if (CL_SUCCESS == jobError)
{ {
// log_info("Thread %d doing job %d\n", threadID, item - 1); // log_info("Thread %d doing job %d\n", threadID, item - 1);
#if defined(__APPLE__) && defined(__arm__) #if defined(__APPLE__) && defined(__arm__)
// On most platforms which support denorm, default is FTZ off. However, // On most platforms which support denorm, default is FTZ off.
// on some hardware where the reference is computed, default might be flush denorms to zero e.g. arm. // However, on some hardware where the reference is computed,
// This creates issues in result verification. Since spec allows the implementation to either flush or // default might be flush denorms to zero e.g. arm. This creates
// not flush denorms to zero, an implementation may choose not be flush i.e. return denorm result whereas // issues in result verification. Since spec allows the
// reference result may be zero (flushed denorm). Hence we need to disable denorm flushing on host side // implementation to either flush or not flush denorms to zero, an
// where reference is being computed to make sure we get non-flushed reference result. If implementation // implementation may choose not be flush i.e. return denorm result
// returns flushed result, we correctly take care of that in verification code. // whereas reference result may be zero (flushed denorm). Hence we
// need to disable denorm flushing on host side where reference is
// being computed to make sure we get non-flushed reference result.
// If implementation returns flushed result, we correctly take care
// of that in verification code.
FPU_mode_type oldMode; FPU_mode_type oldMode;
DisableFTZ(&oldMode); DisableFTZ(&oldMode);
#endif #endif
@@ -375,7 +416,8 @@ void *ThreadPool_WorkerFunc( void *p )
gRunCount = 0; gRunCount = 0;
LeaveCriticalSection(&gAtomicLock); LeaveCriticalSection(&gAtomicLock);
#elif defined(__GNUC__) #elif defined(__GNUC__)
// GCC extension: http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html#Atomic-Builtins // GCC extension:
// http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html#Atomic-Builtins
// set the new error if we are the first one there. // set the new error if we are the first one there.
__sync_val_compare_and_swap(&jobError, CL_SUCCESS, err); __sync_val_compare_and_swap(&jobError, CL_SUCCESS, err);
@@ -384,18 +426,22 @@ void *ThreadPool_WorkerFunc( void *p )
__sync_synchronize(); __sync_synchronize();
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
// set the new error if we are the first one there. // set the new error if we are the first one there.
_InterlockedCompareExchange( (volatile LONG*) &jobError, err, CL_SUCCESS ); _InterlockedCompareExchange((volatile LONG *)&jobError, err,
CL_SUCCESS);
// drop run count to 0 // drop run count to 0
gRunCount = 0; gRunCount = 0;
_mm_mfence(); _mm_mfence();
#else #else
if (pthread_mutex_lock(&gAtomicLock)) if (pthread_mutex_lock(&gAtomicLock))
log_error( "Atomic operation failed. pthread_mutex_lock(&gAtomicLock) returned an error\n"); log_error(
"Atomic operation failed. "
"pthread_mutex_lock(&gAtomicLock) returned an error\n");
if (jobError == CL_SUCCESS) jobError = err; if (jobError == CL_SUCCESS) jobError = err;
gRunCount = 0; gRunCount = 0;
if (pthread_mutex_unlock(&gAtomicLock)) if (pthread_mutex_unlock(&gAtomicLock))
log_error( "Failed to release gAtomicLock. Further atomic operations may deadlock\n"); log_error("Failed to release gAtomicLock. Further atomic "
"operations may deadlock\n");
#endif #endif
} }
} }
@@ -413,22 +459,24 @@ exit:
} }
// SetThreadCount() may be used to artifically set the number of worker threads // SetThreadCount() may be used to artifically set the number of worker threads
// If the value is 0 (the default) the number of threads will be determined based on // If the value is 0 (the default) the number of threads will be determined
// the number of CPU cores. If it is a unicore machine, then 2 will be used, so // based on the number of CPU cores. If it is a unicore machine, then 2 will be
// that we still get some testing for thread safety. // used, so that we still get some testing for thread safety.
// //
// If count < 2 or the CL_TEST_SINGLE_THREADED environment variable is set then the // If count < 2 or the CL_TEST_SINGLE_THREADED environment variable is set then
// code will run single threaded, but will report an error to indicate that the test // the code will run single threaded, but will report an error to indicate that
// is invalid. This option is intended for debugging purposes only. It is suggested // the test is invalid. This option is intended for debugging purposes only. It
// as a convention that test apps set the thread count to 1 in response to the -m flag. // is suggested as a convention that test apps set the thread count to 1 in
// response to the -m flag.
// //
// SetThreadCount() must be called before the first call to GetThreadCount() or ThreadPool_Do(), // SetThreadCount() must be called before the first call to GetThreadCount() or
// otherwise the behavior is indefined. // ThreadPool_Do(), otherwise the behavior is indefined.
void SetThreadCount(int count) void SetThreadCount(int count)
{ {
if (threadPoolInitErr == CL_SUCCESS) if (threadPoolInitErr == CL_SUCCESS)
{ {
log_error( "Error: It is illegal to set the thread count after the first call to ThreadPool_Do or GetThreadCount\n" ); log_error("Error: It is illegal to set the thread count after the "
"first call to ThreadPool_Do or GetThreadCount\n");
abort(); abort();
} }
@@ -441,15 +489,18 @@ void ThreadPool_Init(void)
int err; int err;
volatile cl_uint threadID = 0; volatile cl_uint threadID = 0;
// Check for manual override of multithreading code. We add this for better debuggability. // Check for manual override of multithreading code. We add this for better
// debuggability.
if (getenv("CL_TEST_SINGLE_THREADED")) if (getenv("CL_TEST_SINGLE_THREADED"))
{ {
log_error("ERROR: CL_TEST_SINGLE_THREADED is set in the environment. Running single threaded.\n*** TEST IS INVALID! ***\n"); log_error("ERROR: CL_TEST_SINGLE_THREADED is set in the environment. "
"Running single threaded.\n*** TEST IS INVALID! ***\n");
gThreadCount = 1; gThreadCount = 1;
return; return;
} }
// Figure out how many threads to run -- check first for non-zero to give the implementation the chance // Figure out how many threads to run -- check first for non-zero to give
// the implementation the chance
if (0 == gThreadCount) if (0 == gThreadCount)
{ {
#if defined(_MSC_VER) || defined(__MINGW64__) #if defined(_MSC_VER) || defined(__MINGW64__)
@@ -463,11 +514,15 @@ void ThreadPool_Init(void)
if (GetLogicalProcessorInformation(buffer, &length) == TRUE) if (GetLogicalProcessorInformation(buffer, &length) == TRUE)
{ {
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer; PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
while( ptr < &buffer[ length / sizeof( SYSTEM_LOGICAL_PROCESSOR_INFORMATION ) ] ) while (
ptr
< &buffer[length
/ sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)])
{ {
if (ptr->Relationship == RelationProcessorCore) if (ptr->Relationship == RelationProcessorCore)
{ {
// Count the number of bits in ProcessorMask (number of logical cores) // Count the number of bits in ProcessorMask (number of
// logical cores)
ULONG mask = ptr->ProcessorMask; ULONG mask = ptr->ProcessorMask;
while (mask) while (mask)
{ {
@@ -499,15 +554,17 @@ void ThreadPool_Init(void)
} }
else else
{ {
gThreadCount = (cl_int) sysconf(_SC_NPROCESSORS_CONF); // Hopefully your system returns logical cpus here, as does MacOS X // Hopefully your system returns logical cpus here, as does MacOS X
gThreadCount = (cl_int)sysconf(_SC_NPROCESSORS_CONF);
} }
#else // !_WIN32 #else /* !_WIN32 */
gThreadCount = (cl_int) sysconf(_SC_NPROCESSORS_CONF); // Hopefully your system returns logical cpus here, as does MacOS X // Hopefully your system returns logical cpus here, as does MacOS X
gThreadCount = (cl_int)sysconf(_SC_NPROCESSORS_CONF);
#endif // !_WIN32 #endif // !_WIN32
// Multithreaded tests are required to run multithreaded even on unicore systems so as to test thread safety // Multithreaded tests are required to run multithreaded even on unicore
if( 1 == gThreadCount ) // systems so as to test thread safety
gThreadCount = 2; if (1 == gThreadCount) gThreadCount = 2;
} }
// When working in 32 bit limit the thread number to 12 // When working in 32 bit limit the thread number to 12
@@ -517,15 +574,18 @@ void ThreadPool_Init(void)
// When running this test on dual socket machine in 32-bit, the // When running this test on dual socket machine in 32-bit, the
// process memory is not sufficient and the test fails // process memory is not sufficient and the test fails
#if defined(_WIN32) && !defined(_M_X64) #if defined(_WIN32) && !defined(_M_X64)
if (gThreadCount > 12) { if (gThreadCount > 12)
{
gThreadCount = 12; gThreadCount = 12;
} }
#endif #endif
//Allow the app to set thread count to <0 for debugging purposes. This will cause the test to run single threaded. // Allow the app to set thread count to <0 for debugging purposes.
// This will cause the test to run single threaded.
if (gThreadCount < 2) if (gThreadCount < 2)
{ {
log_error( "ERROR: Running single threaded because thread count < 2. \n*** TEST IS INVALID! ***\n"); log_error("ERROR: Running single threaded because thread count < 2. "
"\n*** TEST IS INVALID! ***\n");
gThreadCount = 1; gThreadCount = 1;
return; return;
} }
@@ -536,8 +596,8 @@ void ThreadPool_Init(void)
_InitializeConditionVariable(cond_var); _InitializeConditionVariable(cond_var);
caller_event = CreateEvent(NULL, FALSE, FALSE, NULL); caller_event = CreateEvent(NULL, FALSE, FALSE, NULL);
#elif defined(__GNUC__) #elif defined(__GNUC__)
// Dont rely on PTHREAD_MUTEX_INITIALIZER for intialization of a mutex since it might cause problem // Dont rely on PTHREAD_MUTEX_INITIALIZER for intialization of a mutex since
// with some flavors of gcc compilers. // it might cause problem with some flavors of gcc compilers.
pthread_cond_init(&cond_var, NULL); pthread_cond_init(&cond_var, NULL);
pthread_mutex_init(&cond_lock, NULL); pthread_mutex_init(&cond_lock, NULL);
pthread_cond_init(&caller_cond_var, NULL); pthread_cond_init(&caller_cond_var, NULL);
@@ -550,12 +610,15 @@ void ThreadPool_Init(void)
#elif defined(__MINGW32__) #elif defined(__MINGW32__)
InitializeCriticalSection(&gAtomicLock); InitializeCriticalSection(&gAtomicLock);
#endif #endif
// Make sure the last thread done in the work pool doesn't signal us to wake before we get to the point where we are supposed to wait // Make sure the last thread done in the work pool doesn't signal us to wake
// before we get to the point where we are supposed to wait
// That would cause a deadlock. // That would cause a deadlock.
#if !defined(_WIN32) #if !defined(_WIN32)
if ((err = pthread_mutex_lock(&caller_cond_lock))) if ((err = pthread_mutex_lock(&caller_cond_lock)))
{ {
log_error("Error %d from pthread_mutex_lock. Unable to block for work to finish. ThreadPool_Init failed.\n", err ); log_error("Error %d from pthread_mutex_lock. Unable to block for work "
"to finish. ThreadPool_Init failed.\n",
err);
gThreadCount = 1; gThreadCount = 1;
return; return;
} }
@@ -566,11 +629,13 @@ void ThreadPool_Init(void)
for (i = 0; i < gThreadCount; i++) for (i = 0; i < gThreadCount; i++)
{ {
#if defined(_WIN32) #if defined(_WIN32)
uintptr_t handle = _beginthread(ThreadPool_WorkerFunc, 0, (void*) &threadID); uintptr_t handle =
_beginthread(ThreadPool_WorkerFunc, 0, (void *)&threadID);
err = (handle == 0); err = (handle == 0);
#else // !_WIN32 #else // !_WIN32
pthread_t tid = 0; pthread_t tid = 0;
err = pthread_create( &tid, NULL, ThreadPool_WorkerFunc, (void*) &threadID ); err = pthread_create(&tid, NULL, ThreadPool_WorkerFunc,
(void *)&threadID);
#endif // !_WIN32 #endif // !_WIN32
if (err) if (err)
{ {
@@ -591,17 +656,20 @@ void ThreadPool_Init(void)
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_cond_wait(&caller_cond_var, &caller_cond_lock))) if ((err = pthread_cond_wait(&caller_cond_var, &caller_cond_lock)))
{ {
log_error("Error %d from pthread_cond_wait. Unable to block for work to finish. ThreadPool_Init failed.\n", err ); log_error("Error %d from pthread_cond_wait. Unable to block for "
"work to finish. ThreadPool_Init failed.\n",
err);
pthread_mutex_unlock(&caller_cond_lock); pthread_mutex_unlock(&caller_cond_lock);
return; return;
} }
#endif // !_WIN32 #endif // !_WIN32
} } while (gRunCount != -gThreadCount);
while( gRunCount != -gThreadCount );
#if !defined(_WIN32) #if !defined(_WIN32)
if ((err = pthread_mutex_unlock(&caller_cond_lock))) if ((err = pthread_mutex_unlock(&caller_cond_lock)))
{ {
log_error("Error %d from pthread_mutex_unlock. Unable to block for work to finish. ThreadPool_Init failed.\n", err ); log_error("Error %d from pthread_mutex_unlock. Unable to block for "
"work to finish. ThreadPool_Init failed.\n",
err);
return; return;
} }
#endif // !_WIN32 #endif // !_WIN32
@@ -610,7 +678,8 @@ void ThreadPool_Init(void)
} }
#if defined(_MSC_VER) #if defined(_MSC_VER)
static BOOL CALLBACK _ThreadPool_Init(_PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) static BOOL CALLBACK _ThreadPool_Init(_PINIT_ONCE InitOnce, PVOID Parameter,
PVOID *lpContex)
{ {
ThreadPool_Init(); ThreadPool_Init();
return TRUE; return TRUE;
@@ -623,7 +692,8 @@ void ThreadPool_Exit(void)
gRunCount = CL_INT_MAX; gRunCount = CL_INT_MAX;
#if defined(__GNUC__) #if defined(__GNUC__)
// GCC extension: http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html#Atomic-Builtins // GCC extension:
// http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html#Atomic-Builtins
__sync_synchronize(); __sync_synchronize();
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
_mm_mfence(); _mm_mfence();
@@ -640,7 +710,9 @@ void ThreadPool_Exit(void)
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_cond_broadcast(&cond_var))) if ((err = pthread_cond_broadcast(&cond_var)))
{ {
log_error("Error %d from pthread_cond_broadcast. Unable to wake up work threads. ThreadPool_Exit failed.\n", err ); log_error("Error %d from pthread_cond_broadcast. Unable to wake up "
"work threads. ThreadPool_Exit failed.\n",
err);
break; break;
} }
usleep(1000); usleep(1000);
@@ -648,7 +720,9 @@ void ThreadPool_Exit(void)
} }
if (gThreadCount) if (gThreadCount)
log_error( "Error: Thread pool timed out after 1 second with %d threads still active.\n", gThreadCount ); log_error("Error: Thread pool timed out after 1 second with %d threads "
"still active.\n",
gThreadCount);
else else
log_info("Thread pool exited in a orderly fashion.\n"); log_info("Thread pool exited in a orderly fashion.\n");
} }
@@ -662,17 +736,17 @@ void ThreadPool_Exit(void)
// can be running at a time. It is not intended for general purpose use. // can be running at a time. It is not intended for general purpose use.
// If clEnqueueNativeKernelFn, out of order queues and a CL_DEVICE_TYPE_CPU were // If clEnqueueNativeKernelFn, out of order queues and a CL_DEVICE_TYPE_CPU were
// all available then it would make more sense to use those features. // all available then it would make more sense to use those features.
cl_int ThreadPool_Do( TPFuncPtr func_ptr, cl_int ThreadPool_Do(TPFuncPtr func_ptr, cl_uint count, void *userInfo)
cl_uint count,
void *userInfo )
{ {
cl_int newErr; cl_int newErr;
cl_int err = 0; cl_int err = 0;
// Lazily set up our threads // Lazily set up our threads
#if defined(_MSC_VER) && (_WIN32_WINNT >= 0x600) #if defined(_MSC_VER) && (_WIN32_WINNT >= 0x600)
err = !_InitOnceExecuteOnce( &threadpool_init_control, _ThreadPool_Init, NULL, NULL ); err = !_InitOnceExecuteOnce(&threadpool_init_control, _ThreadPool_Init,
NULL, NULL);
#elif defined(_WIN32) #elif defined(_WIN32)
if (threadpool_init_control == 0) { if (threadpool_init_control == 0)
{
#warning This is buggy and race prone. Find a better way. #warning This is buggy and race prone. Find a better way.
ThreadPool_Init(); ThreadPool_Init();
threadpool_init_control = 1; threadpool_init_control = 1;
@@ -681,11 +755,14 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
err = pthread_once(&threadpool_init_control, ThreadPool_Init); err = pthread_once(&threadpool_init_control, ThreadPool_Init);
if (err) if (err)
{ {
log_error("Error %d from pthread_once. Unable to init threads. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_once. Unable to init threads. "
"ThreadPool_Do failed.\n",
err);
return err; return err;
} }
#endif #endif
// Single threaded code to handle case where threadpool wasn't allocated or was disabled by environment variable // Single threaded code to handle case where threadpool wasn't allocated or
// was disabled by environment variable
if (threadPoolInitErr) if (threadPoolInitErr)
{ {
cl_uint currentJob = 0; cl_uint currentJob = 0;
@@ -693,12 +770,15 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
#if defined(__APPLE__) && defined(__arm__) #if defined(__APPLE__) && defined(__arm__)
// On most platforms which support denorm, default is FTZ off. However, // On most platforms which support denorm, default is FTZ off. However,
// on some hardware where the reference is computed, default might be flush denorms to zero e.g. arm. // on some hardware where the reference is computed, default might be
// This creates issues in result verification. Since spec allows the implementation to either flush or // flush denorms to zero e.g. arm. This creates issues in result
// not flush denorms to zero, an implementation may choose not be flush i.e. return denorm result whereas // verification. Since spec allows the implementation to either flush or
// reference result may be zero (flushed denorm). Hence we need to disable denorm flushing on host side // not flush denorms to zero, an implementation may choose not be flush
// where reference is being computed to make sure we get non-flushed reference result. If implementation // i.e. return denorm result whereas reference result may be zero
// returns flushed result, we correctly take care of that in verification code. // (flushed denorm). Hence we need to disable denorm flushing on host
// side where reference is being computed to make sure we get
// non-flushed reference result. If implementation returns flushed
// result, we correctly take care of that in verification code.
FPU_mode_type oldMode; FPU_mode_type oldMode;
DisableFTZ(&oldMode); DisableFTZ(&oldMode);
#endif #endif
@@ -722,7 +802,9 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
if (count >= MAX_COUNT) if (count >= MAX_COUNT)
{ {
log_error("Error: ThreadPool_Do count %d >= max threadpool count of %d\n", count, MAX_COUNT ); log_error(
"Error: ThreadPool_Do count %d >= max threadpool count of %d\n",
count, MAX_COUNT);
return -1; return -1;
} }
@@ -735,13 +817,15 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
switch (err) switch (err)
{ {
case EDEADLK: case EDEADLK:
log_error("Error EDEADLK returned in ThreadPool_Do(). ThreadPool_Do is not designed to work recursively!\n" ); log_error(
"Error EDEADLK returned in ThreadPool_Do(). ThreadPool_Do "
"is not designed to work recursively!\n");
break; break;
case EINVAL: case EINVAL:
log_error("Error EINVAL returned in ThreadPool_Do(). How did we end up with an invalid gThreadPoolLock?\n" ); log_error("Error EINVAL returned in ThreadPool_Do(). How did "
break; "we end up with an invalid gThreadPoolLock?\n");
default:
break; break;
default: break;
} }
return err; return err;
} }
@@ -753,17 +837,22 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_mutex_lock(&cond_lock))) if ((err = pthread_mutex_lock(&cond_lock)))
{ {
log_error("Error %d from pthread_mutex_lock. Unable to wake up work threads. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_mutex_lock. Unable to wake up work "
"threads. ThreadPool_Do failed.\n",
err);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
// Make sure the last thread done in the work pool doesn't signal us to wake before we get to the point where we are supposed to wait // Make sure the last thread done in the work pool doesn't signal us to wake
// before we get to the point where we are supposed to wait
// That would cause a deadlock. // That would cause a deadlock.
#if !defined(_WIN32) #if !defined(_WIN32)
if ((err = pthread_mutex_lock(&caller_cond_lock))) if ((err = pthread_mutex_lock(&caller_cond_lock)))
{ {
log_error("Error %d from pthread_mutex_lock. Unable to block for work to finish. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_mutex_lock. Unable to block for work "
"to finish. ThreadPool_Do failed.\n",
err);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
@@ -781,17 +870,22 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_cond_broadcast(&cond_var))) if ((err = pthread_cond_broadcast(&cond_var)))
{ {
log_error("Error %d from pthread_cond_broadcast. Unable to wake up work threads. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_cond_broadcast. Unable to wake up "
"work threads. ThreadPool_Do failed.\n",
err);
goto exit; goto exit;
} }
if ((err = pthread_mutex_unlock(&cond_lock))) if ((err = pthread_mutex_unlock(&cond_lock)))
{ {
log_error("Error %d from pthread_mutex_unlock. Unable to wake up work threads. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_mutex_unlock. Unable to wake up work "
"threads. ThreadPool_Do failed.\n",
err);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
// block until they are done. It would be slightly more efficient to do some of the work here though. // block until they are done. It would be slightly more efficient to do
// some of the work here though.
do do
{ {
#if defined(_WIN32) #if defined(_WIN32)
@@ -799,17 +893,20 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
#else // !_WIN32 #else // !_WIN32
if ((err = pthread_cond_wait(&caller_cond_var, &caller_cond_lock))) if ((err = pthread_cond_wait(&caller_cond_var, &caller_cond_lock)))
{ {
log_error("Error %d from pthread_cond_wait. Unable to block for work to finish. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_cond_wait. Unable to block for "
"work to finish. ThreadPool_Do failed.\n",
err);
pthread_mutex_unlock(&caller_cond_lock); pthread_mutex_unlock(&caller_cond_lock);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
} } while (gRunning);
while( gRunning );
#if !defined(_WIN32) #if !defined(_WIN32)
if ((err = pthread_mutex_unlock(&caller_cond_lock))) if ((err = pthread_mutex_unlock(&caller_cond_lock)))
{ {
log_error("Error %d from pthread_mutex_unlock. Unable to block for work to finish. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_mutex_unlock. Unable to block for "
"work to finish. ThreadPool_Do failed.\n",
err);
goto exit; goto exit;
} }
#endif // !_WIN32 #endif // !_WIN32
@@ -824,7 +921,9 @@ exit:
newErr = pthread_mutex_unlock(&gThreadPoolLock); newErr = pthread_mutex_unlock(&gThreadPoolLock);
if (newErr) if (newErr)
{ {
log_error("Error %d from pthread_mutex_unlock. Unable to exit critical region. ThreadPool_Do failed.\n", newErr ); log_error("Error %d from pthread_mutex_unlock. Unable to exit critical "
"region. ThreadPool_Do failed.\n",
newErr);
return err; return err;
} }
#endif // !_WIN32 #endif // !_WIN32
@@ -836,9 +935,11 @@ cl_uint GetThreadCount( void )
{ {
// Lazily set up our threads // Lazily set up our threads
#if defined(_MSC_VER) && (_WIN32_WINNT >= 0x600) #if defined(_MSC_VER) && (_WIN32_WINNT >= 0x600)
cl_int err = !_InitOnceExecuteOnce( &threadpool_init_control, _ThreadPool_Init, NULL, NULL ); cl_int err = !_InitOnceExecuteOnce(&threadpool_init_control,
_ThreadPool_Init, NULL, NULL);
#elif defined(_WIN32) #elif defined(_WIN32)
if (threadpool_init_control == 0) { if (threadpool_init_control == 0)
{
#warning This is buggy and race prone. Find a better way. #warning This is buggy and race prone. Find a better way.
ThreadPool_Init(); ThreadPool_Init();
threadpool_init_control = 1; threadpool_init_control = 1;
@@ -847,13 +948,14 @@ cl_uint GetThreadCount( void )
cl_int err = pthread_once(&threadpool_init_control, ThreadPool_Init); cl_int err = pthread_once(&threadpool_init_control, ThreadPool_Init);
if (err) if (err)
{ {
log_error("Error %d from pthread_once. Unable to init threads. ThreadPool_Do failed.\n", err ); log_error("Error %d from pthread_once. Unable to init threads. "
"ThreadPool_Do failed.\n",
err);
return err; return err;
} }
#endif // !_WIN32 #endif // !_WIN32
if( gThreadCount < 1 ) if (gThreadCount < 1) return 1;
return 1;
return gThreadCount; return gThreadCount;
} }
@@ -864,21 +966,23 @@ cl_uint GetThreadCount( void )
#error ThreadPool implementation has not been multithreaded for this operating system. You must multithread this section. #error ThreadPool implementation has not been multithreaded for this operating system. You must multithread this section.
#endif #endif
// //
// We require multithreading in parts of the test as a means of simultaneously testing reentrancy requirements // We require multithreading in parts of the test as a means of simultaneously
// of OpenCL API, while also checking // testing reentrancy requirements of OpenCL API, while also checking
// //
// A sample single threaded implementation follows, for documentation / bootstrapping purposes. // A sample single threaded implementation follows, for documentation /
// It is not okay to use this for conformance testing!!! // bootstrapping purposes. It is not okay to use this for conformance testing!!!
// //
// Exception: If your operating system does not support multithreaded execution of any kind, then you may use this code. // Exception: If your operating system does not support multithreaded execution
// of any kind, then you may use this code.
// //
cl_int ThreadPool_AtomicAdd(volatile cl_int *a, cl_int b) cl_int ThreadPool_AtomicAdd(volatile cl_int *a, cl_int b)
{ {
cl_uint r = *a; cl_uint r = *a;
// since this fallback code path is not multithreaded, we just do a regular add here // since this fallback code path is not multithreaded, we just do a regular
// If your operating system supports memory-barrier-atomics, use those here // add here. If your operating system supports memory-barrier-atomics, use
// those here.
*a = r + b; *a = r + b;
return r; return r;
@@ -887,9 +991,7 @@ cl_int ThreadPool_AtomicAdd( volatile cl_int *a, cl_int b )
// Blocking API that farms out count jobs to a thread pool. // Blocking API that farms out count jobs to a thread pool.
// It may return with some work undone if func_ptr() returns a non-zero // It may return with some work undone if func_ptr() returns a non-zero
// result. // result.
cl_int ThreadPool_Do( TPFuncPtr func_ptr, cl_int ThreadPool_Do(TPFuncPtr func_ptr, cl_uint count, void *userInfo)
cl_uint count,
void *userInfo )
{ {
cl_uint currentJob = 0; cl_uint currentJob = 0;
cl_int result = CL_SUCCESS; cl_int result = CL_SUCCESS;
@@ -903,28 +1005,24 @@ cl_int ThreadPool_Do( TPFuncPtr func_ptr,
if (0 == spewCount) if (0 == spewCount)
{ {
log_info( "\nWARNING: The operating system is claimed not to support threads of any sort. Running single threaded.\n" ); log_info("\nWARNING: The operating system is claimed not to support "
"threads of any sort. Running single threaded.\n");
spewCount = 1; spewCount = 1;
} }
#endif #endif
// The multithreaded code should mimic this behavior: // The multithreaded code should mimic this behavior:
for (currentJob = 0; currentJob < count; currentJob++) for (currentJob = 0; currentJob < count; currentJob++)
if((result = func_ptr( currentJob, 0, userInfo ))) if ((result = func_ptr(currentJob, 0, userInfo))) return result;
return result;
return CL_SUCCESS; return CL_SUCCESS;
} }
cl_uint GetThreadCount( void ) cl_uint GetThreadCount(void) { return 1; }
{
return 1;
}
void SetThreadCount(int count) void SetThreadCount(int count)
{ {
if( count > 1 ) if (count > 1) log_info("WARNING: SetThreadCount(%d) ignored\n", count);
log_info( "WARNING: SetThreadCount(%d) ignored\n", count );
} }
#endif #endif

View File

@@ -28,40 +28,41 @@ cl_int ThreadPool_AtomicAdd( volatile cl_int *a, cl_int b ); // returns o
// Your function prototype // Your function prototype
// //
// A function pointer to the function you want to execute in a multithreaded context. No // A function pointer to the function you want to execute in a multithreaded
// synchronization primitives are provided, other than the atomic add above. You may not // context. No synchronization primitives are provided, other than the atomic
// call ThreadPool_Do from your function. ThreadPool_AtomicAdd() and GetThreadCount() should // add above. You may not call ThreadPool_Do from your function.
// work, however. // ThreadPool_AtomicAdd() and GetThreadCount() should work, however.
// //
// job ids and thread ids are 0 based. If number of jobs or threads was 8, they will numbered be 0 through 7. // job ids and thread ids are 0 based. If number of jobs or threads was 8, they
// Note that while every job will be run, it is not guaranteed that every thread will wake up before // will numbered be 0 through 7. Note that while every job will be run, it is
// the work is done. // not guaranteed that every thread will wake up before the work is done.
typedef cl_int (*TPFuncPtr)( cl_uint /*job_id*/, cl_uint /* thread_id */, void *userInfo ); typedef cl_int (*TPFuncPtr)(cl_uint /*job_id*/, cl_uint /* thread_id */,
void *userInfo);
// returns first non-zero result from func_ptr, or CL_SUCCESS if all are zero. // returns first non-zero result from func_ptr, or CL_SUCCESS if all are zero.
// Some workitems may not run if a non-zero result is returned from func_ptr(). // Some workitems may not run if a non-zero result is returned from func_ptr().
// This function may not be called from a TPFuncPtr. // This function may not be called from a TPFuncPtr.
cl_int ThreadPool_Do( TPFuncPtr func_ptr, cl_int ThreadPool_Do(TPFuncPtr func_ptr, cl_uint count, void *userInfo);
cl_uint count,
void *userInfo );
// Returns the number of worker threads that underlie the threadpool. The value passed // Returns the number of worker threads that underlie the threadpool. The value
// as the TPFuncPtrs thread_id will be between 0 and this value less one, inclusive. // passed as the TPFuncPtrs thread_id will be between 0 and this value less one,
// This is safe to call from a TPFuncPtr. // inclusive. This is safe to call from a TPFuncPtr.
cl_uint GetThreadCount(void); cl_uint GetThreadCount(void);
// SetThreadCount() may be used to artifically set the number of worker threads // SetThreadCount() may be used to artifically set the number of worker threads
// If the value is 0 (the default) the number of threads will be determined based on // If the value is 0 (the default) the number of threads will be determined
// the number of CPU cores. If it is a unicore machine, then 2 will be used, so // based on the number of CPU cores. If it is a unicore machine, then 2 will be
// that we still get some testing for thread safety. // used, so that we still get some testing for thread safety.
// //
// If count < 2 or the CL_TEST_SINGLE_THREADED environment variable is set then the // If count < 2 or the CL_TEST_SINGLE_THREADED environment variable is set then
// code will run single threaded, but will report an error to indicate that the test // the code will run single threaded, but will report an error to indicate that
// is invalid. This option is intended for debugging purposes only. It is suggested // the test is invalid. This option is intended for debugging purposes only. It
// as a convention that test apps set the thread count to 1 in response to the -m flag. // is suggested as a convention that test apps set the thread count to 1 in
// response to the -m flag.
// //
// SetThreadCount() must be called before the first call to GetThreadCount() or ThreadPool_Do(), // SetThreadCount() must be called before the first call to GetThreadCount() or
// otherwise the behavior is indefined. It may not be called from a TPFuncPtr. // ThreadPool_Do(), otherwise the behavior is indefined. It may not be called
// from a TPFuncPtr.
void SetThreadCount(int count); void SetThreadCount(int count);

View File

@@ -37,14 +37,13 @@ static void * align_malloc(size_t size, size_t alignment)
void* ptr = NULL; void* ptr = NULL;
#if defined(__ANDROID__) #if defined(__ANDROID__)
ptr = memalign(alignment, size); ptr = memalign(alignment, size);
if ( ptr ) if (ptr) return ptr;
return ptr;
#else #else
if (alignment < sizeof(void*)) { if (alignment < sizeof(void*))
{
alignment = sizeof(void*); alignment = sizeof(void*);
} }
if (0 == posix_memalign(&ptr, alignment, size)) if (0 == posix_memalign(&ptr, alignment, size)) return ptr;
return ptr;
#endif #endif
return NULL; return NULL;
#elif defined(__MINGW32__) #elif defined(__MINGW32__)
@@ -68,4 +67,3 @@ static void align_free(void * ptr)
} }
#endif // #ifndef HARNESS_ALLOC_H_ #endif // #ifndef HARNESS_ALLOC_H_

View File

@@ -29,13 +29,10 @@
// helper function to replace clCreateImage2D , to make the existing code use // helper function to replace clCreateImage2D , to make the existing code use
// the functions of version 1.2 and veriosn 1.1 respectively // the functions of version 1.2 and veriosn 1.1 respectively
static inline cl_mem create_image_2d (cl_context context, static inline cl_mem create_image_2d(cl_context context, cl_mem_flags flags,
cl_mem_flags flags,
const cl_image_format *image_format, const cl_image_format *image_format,
size_t image_width, size_t image_width, size_t image_height,
size_t image_height, size_t image_row_pitch, void *host_ptr,
size_t image_row_pitch,
void *host_ptr,
cl_int *errcode_ret) cl_int *errcode_ret)
{ {
cl_mem mImage = NULL; cl_mem mImage = NULL;
@@ -51,19 +48,26 @@
image_desc_dest.image_slice_pitch = 0; image_desc_dest.image_slice_pitch = 0;
image_desc_dest.num_mip_levels = 0; image_desc_dest.num_mip_levels = 0;
image_desc_dest.num_samples = 0; image_desc_dest.num_samples = 0;
image_desc_dest.mem_object = NULL;// no image type of CL_MEM_OBJECT_IMAGE1D_BUFFER in CL_VERSION_1_1, so always is NULL image_desc_dest.mem_object =
mImage = clCreateImage( context, flags, image_format, &image_desc_dest, host_ptr, errcode_ret ); NULL; // no image type of CL_MEM_OBJECT_IMAGE1D_BUFFER in
if (errcode_ret && (*errcode_ret)) { // CL_VERSION_1_1, so always is NULL
// Log an info message and rely on the calling function to produce an error mImage = clCreateImage(context, flags, image_format, &image_desc_dest,
// if necessary. host_ptr, errcode_ret);
if (errcode_ret && (*errcode_ret))
{
// Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage failed (%d)\n", *errcode_ret); log_info("clCreateImage failed (%d)\n", *errcode_ret);
} }
#else #else
mImage = clCreateImage2D( context, flags, image_format, image_width, image_height, image_row_pitch, host_ptr, errcode_ret ); mImage =
if (errcode_ret && (*errcode_ret)) { clCreateImage2D(context, flags, image_format, image_width, image_height,
// Log an info message and rely on the calling function to produce an error image_row_pitch, host_ptr, errcode_ret);
// if necessary. if (errcode_ret && (*errcode_ret))
{
// Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage2D failed (%d)\n", *errcode_ret); log_info("clCreateImage2D failed (%d)\n", *errcode_ret);
} }
#endif #endif
@@ -74,14 +78,11 @@
// helper function to replace clCreateImage2D , to make the existing code use // helper function to replace clCreateImage2D , to make the existing code use
// the functions of version 1.2 and veriosn 1.1 respectively // the functions of version 1.2 and veriosn 1.1 respectively
static inline cl_mem create_image_2d_buffer (cl_context context, static inline cl_mem
cl_mem_flags flags, create_image_2d_buffer(cl_context context, cl_mem_flags flags,
const cl_image_format *image_format, const cl_image_format *image_format, size_t image_width,
size_t image_width, size_t image_height, size_t image_row_pitch,
size_t image_height, cl_mem buffer, cl_int *errcode_ret)
size_t image_row_pitch,
cl_mem buffer,
cl_int *errcode_ret)
{ {
cl_mem mImage = NULL; cl_mem mImage = NULL;
@@ -96,10 +97,12 @@
image_desc_dest.num_mip_levels = 0; image_desc_dest.num_mip_levels = 0;
image_desc_dest.num_samples = 0; image_desc_dest.num_samples = 0;
image_desc_dest.mem_object = buffer; image_desc_dest.mem_object = buffer;
mImage = clCreateImage( context, flags, image_format, &image_desc_dest, NULL, errcode_ret ); mImage = clCreateImage(context, flags, image_format, &image_desc_dest, NULL,
if (errcode_ret && (*errcode_ret)) { errcode_ret);
// Log an info message and rely on the calling function to produce an error if (errcode_ret && (*errcode_ret))
// if necessary. {
// Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage failed (%d)\n", *errcode_ret); log_info("clCreateImage failed (%d)\n", *errcode_ret);
} }
@@ -107,16 +110,11 @@
} }
static inline cl_mem create_image_3d(cl_context context, cl_mem_flags flags,
static inline cl_mem create_image_3d (cl_context context,
cl_mem_flags flags,
const cl_image_format *image_format, const cl_image_format *image_format,
size_t image_width, size_t image_width, size_t image_height,
size_t image_height, size_t image_depth, size_t image_row_pitch,
size_t image_depth, size_t image_slice_pitch, void *host_ptr,
size_t image_row_pitch,
size_t image_slice_pitch,
void *host_ptr,
cl_int *errcode_ret) cl_int *errcode_ret)
{ {
cl_mem mImage; cl_mem mImage;
@@ -132,32 +130,26 @@
image_desc.image_slice_pitch = image_slice_pitch; image_desc.image_slice_pitch = image_slice_pitch;
image_desc.num_mip_levels = 0; image_desc.num_mip_levels = 0;
image_desc.num_samples = 0; image_desc.num_samples = 0;
image_desc.mem_object = NULL; // no image type of CL_MEM_OBJECT_IMAGE1D_BUFFER in CL_VERSION_1_1, so always is NULL image_desc.mem_object =
mImage = clCreateImage( context, NULL; // no image type of CL_MEM_OBJECT_IMAGE1D_BUFFER in
flags, // CL_VERSION_1_1, so always is NULL
image_format, mImage = clCreateImage(context, flags, image_format, &image_desc, host_ptr,
&image_desc,
host_ptr,
errcode_ret); errcode_ret);
if (errcode_ret && (*errcode_ret)) { if (errcode_ret && (*errcode_ret))
// Log an info message and rely on the calling function to produce an error {
// if necessary. // Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage failed (%d)\n", *errcode_ret); log_info("clCreateImage failed (%d)\n", *errcode_ret);
} }
#else #else
mImage = clCreateImage3D( context, mImage = clCreateImage3D(context, flags, image_format, image_width,
flags, image_format, image_height, image_depth, image_row_pitch,
image_width, image_slice_pitch, host_ptr, errcode_ret);
image_height, if (errcode_ret && (*errcode_ret))
image_depth, {
image_row_pitch, // Log an info message and rely on the calling function to produce an
image_slice_pitch, // error if necessary.
host_ptr,
errcode_ret );
if (errcode_ret && (*errcode_ret)) {
// Log an info message and rely on the calling function to produce an error
// if necessary.
log_info("clCreateImage3D failed (%d)\n", *errcode_ret); log_info("clCreateImage3D failed (%d)\n", *errcode_ret);
} }
#endif #endif
@@ -165,16 +157,12 @@
return mImage; return mImage;
} }
static inline cl_mem create_image_2d_array (cl_context context, static inline cl_mem
cl_mem_flags flags, create_image_2d_array(cl_context context, cl_mem_flags flags,
const cl_image_format *image_format, const cl_image_format *image_format, size_t image_width,
size_t image_width, size_t image_height, size_t image_array_size,
size_t image_height, size_t image_row_pitch, size_t image_slice_pitch,
size_t image_array_size, void *host_ptr, cl_int *errcode_ret)
size_t image_row_pitch,
size_t image_slice_pitch,
void *host_ptr,
cl_int *errcode_ret)
{ {
cl_mem mImage; cl_mem mImage;
@@ -189,30 +177,22 @@
image_desc.num_mip_levels = 0; image_desc.num_mip_levels = 0;
image_desc.num_samples = 0; image_desc.num_samples = 0;
image_desc.mem_object = NULL; image_desc.mem_object = NULL;
mImage = clCreateImage( context, mImage = clCreateImage(context, flags, image_format, &image_desc, host_ptr,
flags,
image_format,
&image_desc,
host_ptr,
errcode_ret); errcode_ret);
if (errcode_ret && (*errcode_ret)) { if (errcode_ret && (*errcode_ret))
// Log an info message and rely on the calling function to produce an error {
// if necessary. // Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage failed (%d)\n", *errcode_ret); log_info("clCreateImage failed (%d)\n", *errcode_ret);
} }
return mImage; return mImage;
} }
static inline cl_mem create_image_1d_array (cl_context context, static inline cl_mem create_image_1d_array(
cl_mem_flags flags, cl_context context, cl_mem_flags flags, const cl_image_format *image_format,
const cl_image_format *image_format, size_t image_width, size_t image_array_size, size_t image_row_pitch,
size_t image_width, size_t image_slice_pitch, void *host_ptr, cl_int *errcode_ret)
size_t image_array_size,
size_t image_row_pitch,
size_t image_slice_pitch,
void *host_ptr,
cl_int *errcode_ret)
{ {
cl_mem mImage; cl_mem mImage;
@@ -227,34 +207,29 @@
image_desc.num_mip_levels = 0; image_desc.num_mip_levels = 0;
image_desc.num_samples = 0; image_desc.num_samples = 0;
image_desc.mem_object = NULL; image_desc.mem_object = NULL;
mImage = clCreateImage( context, mImage = clCreateImage(context, flags, image_format, &image_desc, host_ptr,
flags,
image_format,
&image_desc,
host_ptr,
errcode_ret); errcode_ret);
if (errcode_ret && (*errcode_ret)) { if (errcode_ret && (*errcode_ret))
// Log an info message and rely on the calling function to produce an error {
// if necessary. // Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage failed (%d)\n", *errcode_ret); log_info("clCreateImage failed (%d)\n", *errcode_ret);
} }
return mImage; return mImage;
} }
static inline cl_mem create_image_1d (cl_context context, static inline cl_mem create_image_1d(cl_context context, cl_mem_flags flags,
cl_mem_flags flags,
const cl_image_format *image_format, const cl_image_format *image_format,
size_t image_width, size_t image_width, size_t image_row_pitch,
size_t image_row_pitch, void *host_ptr, cl_mem buffer,
void *host_ptr,
cl_mem buffer,
cl_int *errcode_ret) cl_int *errcode_ret)
{ {
cl_mem mImage; cl_mem mImage;
cl_image_desc image_desc; cl_image_desc image_desc;
image_desc.image_type = buffer ? CL_MEM_OBJECT_IMAGE1D_BUFFER: CL_MEM_OBJECT_IMAGE1D; image_desc.image_type =
buffer ? CL_MEM_OBJECT_IMAGE1D_BUFFER : CL_MEM_OBJECT_IMAGE1D;
image_desc.image_width = image_width; image_desc.image_width = image_width;
image_desc.image_height = 1; image_desc.image_height = 1;
image_desc.image_depth = 1; image_desc.image_depth = 1;
@@ -263,15 +238,12 @@
image_desc.num_mip_levels = 0; image_desc.num_mip_levels = 0;
image_desc.num_samples = 0; image_desc.num_samples = 0;
image_desc.mem_object = buffer; image_desc.mem_object = buffer;
mImage = clCreateImage( context, mImage = clCreateImage(context, flags, image_format, &image_desc, host_ptr,
flags,
image_format,
&image_desc,
host_ptr,
errcode_ret); errcode_ret);
if (errcode_ret && (*errcode_ret)) { if (errcode_ret && (*errcode_ret))
// Log an info message and rely on the calling function to produce an error {
// if necessary. // Log an info message and rely on the calling function to produce an
// error if necessary.
log_info("clCreateImage failed (%d)\n", *errcode_ret); log_info("clCreateImage failed (%d)\n", *errcode_ret);
} }

View File

@@ -52,8 +52,7 @@ typedef char bool;
#endif #endif
#else #else
#include <stdbool.h> #include <stdbool.h>
#endif #endif // defined(_MSC_VER) && MSC_VER <= 1700
// //
@@ -61,7 +60,9 @@ typedef char bool;
// //
// stdint.h appeared in MS C v16 (VS 10/2010) and Intel C v12. // stdint.h appeared in MS C v16 (VS 10/2010) and Intel C v12.
#if defined( _MSC_VER ) && ( ! defined( __INTEL_COMPILER ) && _MSC_VER <= 1500 || defined( __INTEL_COMPILER ) && __INTEL_COMPILER < 1200 ) #if defined(_MSC_VER) \
&& (!defined(__INTEL_COMPILER) && _MSC_VER <= 1500 \
|| defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1200)
typedef unsigned char uint8_t; typedef unsigned char uint8_t;
typedef char int8_t; typedef char int8_t;
typedef unsigned short uint16_t; typedef unsigned short uint16_t;
@@ -78,7 +79,6 @@ typedef long long int64_t;
#endif #endif
// //
// float.h // float.h
// //
@@ -86,7 +86,6 @@ typedef long long int64_t;
#include <float.h> #include <float.h>
// //
// fenv.h // fenv.h
// //
@@ -237,14 +236,13 @@ typedef long long int64_t;
} }
#endif #endif
#endif #endif // defined(_MSC_VER)
#if defined(__ANDROID__) #if defined(__ANDROID__)
#define log2(X) (log(X) / log(2)) #define log2(X) (log(X) / log(2))
#endif #endif
// //
// stdio.h // stdio.h
// //
@@ -254,8 +252,7 @@ typedef long long int64_t;
#if _MSC_VER < 1900 #if _MSC_VER < 1900
#define snprintf sprintf_s #define snprintf sprintf_s
#endif #endif
#endif #endif // defined(_MSC_VER)
// //
@@ -267,7 +264,6 @@ typedef long long int64_t;
#endif #endif
// //
// unistd.h // unistd.h
// //
@@ -278,7 +274,6 @@ typedef long long int64_t;
#endif #endif
// //
// syscall.h // syscall.h
// //
@@ -289,7 +284,6 @@ typedef long long int64_t;
#endif #endif
// Some tests use _malloca which defined in malloc.h. // Some tests use _malloca which defined in malloc.h.
#if !defined(__APPLE__) #if !defined(__APPLE__)
#include <malloc.h> #include <malloc.h>
@@ -323,9 +317,9 @@ typedef long long int64_t;
#endif #endif
/* /*-----------------------------------------------------------------------------
------------------------------------------------------------------------------------------------ WARNING: DO NOT USE THESE MACROS:
WARNING: DO NOT USE THESE MACROS: MAKE_HEX_FLOAT, MAKE_HEX_DOUBLE, MAKE_HEX_LONG. MAKE_HEX_FLOAT, MAKE_HEX_DOUBLE, MAKE_HEX_LONG.
This is a typical usage of the macros: This is a typical usage of the macros:
@@ -334,16 +328,18 @@ typedef long long int64_t;
(taken from math_brute_force/reference_math.c). There are two problems: (taken from math_brute_force/reference_math.c). There are two problems:
1. There is an error here. On Windows in will produce incorrect result 1. There is an error here. On Windows in will produce incorrect result
`0x1.5555555555555p+50'. To have a correct result it should be written as `0x1.5555555555555p+50'.
`MAKE_HEX_DOUBLE(0x1.5555555555555p-2,0x15555555555555LL,-54)'. A proper value of the To have a correct result it should be written as:
third argument is not obvious -- sometimes it should be the same as exponent of the MAKE_HEX_DOUBLE(0x1.5555555555555p-2, 0x15555555555555LL, -54)
first argument, but sometimes not. A proper value of the third argument is not obvious -- sometimes it
should be the same as exponent of the first argument, but sometimes
not.
2. Information is duplicated. It is easy to make a mistake. 2. Information is duplicated. It is easy to make a mistake.
Use HEX_FLT, HEX_DBL, HEX_LDBL macros instead (see them in the bottom of the file). Use HEX_FLT, HEX_DBL, HEX_LDBL macros instead
------------------------------------------------------------------------------------------------ (see them in the bottom of the file).
*/ -----------------------------------------------------------------------------*/
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#define MAKE_HEX_FLOAT(x, y, z) ((float)ldexp((float)(y), z)) #define MAKE_HEX_FLOAT(x, y, z) ((float)ldexp((float)(y), z))
@@ -360,36 +356,45 @@ typedef long long int64_t;
#endif #endif
/* /*-----------------------------------------------------------------------------
------------------------------------------------------------------------------------------------ HEX_FLT, HEXT_DBL, HEX_LDBL -- Create hex floating point literal of type
HEX_FLT, HEXT_DBL, HEX_LDBL -- Create hex floating point literal of type float, double, long float, double, long double respectively. Arguments:
double respectively. Arguments:
sm -- sign of number, sm -- sign of number,
int -- integer part of mantissa (without `0x' prefix), int -- integer part of mantissa (without `0x' prefix),
fract -- fractional part of mantissa (without decimal point and `L' or `LL' suffixes), fract -- fractional part of mantissa (without decimal point and `L' or
`LL' suffixes),
se -- sign of exponent, se -- sign of exponent,
exp -- absolute value of (binary) exponent. exp -- absolute value of (binary) exponent.
Example: Example:
double yhi = HEX_DBL( +, 1, 5555555555555, -, 2 ); // == 0x1.5555555555555p-2 double yhi = HEX_DBL(+, 1, 5555555555555, -, 2); // 0x1.5555555555555p-2
Note: Note:
We have to pass signs as separate arguments because gcc pass negative integer values We have to pass signs as separate arguments because gcc pass negative
(e. g. `-2') into a macro as two separate tokens, so `HEX_FLT( 1, 0, -2 )' produces result integer values (e. g. `-2') into a macro as two separate tokens, so
`0x1.0p- 2' (note a space between minus and two) which is not a correct floating point `HEX_FLT(1, 0, -2)' produces result `0x1.0p- 2' (note a space between minus
literal. and two) which is not a correct floating point literal.
------------------------------------------------------------------------------------------------ -----------------------------------------------------------------------------*/
*/
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
// If compiler does not support hex floating point literals: // If compiler does not support hex floating point literals:
#define HEX_FLT( sm, int, fract, se, exp ) sm ldexpf( (float)( 0x ## int ## fract ## UL ), se exp + ilogbf( (float) 0x ## int ) - ilogbf( ( float )( 0x ## int ## fract ## UL ) ) ) #define HEX_FLT(sm, int, fract, se, exp) \
#define HEX_DBL( sm, int, fract, se, exp ) sm ldexp( (double)( 0x ## int ## fract ## ULL ), se exp + ilogb( (double) 0x ## int ) - ilogb( ( double )( 0x ## int ## fract ## ULL ) ) ) sm ldexpf((float)(0x##int##fract##UL), \
#define HEX_LDBL( sm, int, fract, se, exp ) sm ldexpl( (long double)( 0x ## int ## fract ## ULL ), se exp + ilogbl( (long double) 0x ## int ) - ilogbl( ( long double )( 0x ## int ## fract ## ULL ) ) ) se exp + ilogbf((float)0x##int) \
- ilogbf((float)(0x##int##fract##UL)))
#define HEX_DBL(sm, int, fract, se, exp) \
sm ldexp((double)(0x##int##fract##ULL), \
se exp + ilogb((double)0x##int) \
- ilogb((double)(0x##int##fract##ULL)))
#define HEX_LDBL(sm, int, fract, se, exp) \
sm ldexpl((long double)(0x##int##fract##ULL), \
se exp + ilogbl((long double)0x##int) \
- ilogbl((long double)(0x##int##fract##ULL)))
#else #else
// If compiler supports hex floating point literals: just concatenate all the parts into a literal. // If compiler supports hex floating point literals: just concatenate all the
// parts into a literal.
#define HEX_FLT(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##F #define HEX_FLT(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##F
#define HEX_DBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp #define HEX_DBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp
#define HEX_LDBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##L #define HEX_LDBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##L

View File

@@ -27,77 +27,51 @@
#include <emmintrin.h> #include <emmintrin.h>
#endif #endif
void print_type_to_string(ExplicitType type, void *data, char* string) { void print_type_to_string(ExplicitType type, void *data, char *string)
switch (type) { {
switch (type)
{
case kBool: case kBool:
if (*(char *)data) if (*(char *)data)
sprintf(string, "true"); sprintf(string, "true");
else else
sprintf(string, "false"); sprintf(string, "false");
return; return;
case kChar: case kChar: sprintf(string, "%d", (int)*((cl_char *)data)); return;
sprintf(string, "%d", (int)*((cl_char*)data));
return;
case kUChar: case kUChar:
case kUnsignedChar: case kUnsignedChar:
sprintf(string, "%u", (int)*((cl_uchar *)data)); sprintf(string, "%u", (int)*((cl_uchar *)data));
return; return;
case kShort: case kShort: sprintf(string, "%d", (int)*((cl_short *)data)); return;
sprintf(string, "%d", (int)*((cl_short*)data));
return;
case kUShort: case kUShort:
case kUnsignedShort: case kUnsignedShort:
sprintf(string, "%u", (int)*((cl_ushort *)data)); sprintf(string, "%u", (int)*((cl_ushort *)data));
return; return;
case kInt: case kInt: sprintf(string, "%d", *((cl_int *)data)); return;
sprintf(string, "%d", *((cl_int*)data));
return;
case kUInt: case kUInt:
case kUnsignedInt: case kUnsignedInt: sprintf(string, "%u", *((cl_uint *)data)); return;
sprintf(string, "%u", *((cl_uint*)data)); case kLong: sprintf(string, "%lld", *((cl_long *)data)); return;
return;
case kLong:
sprintf(string, "%lld", *((cl_long*)data));
return;
case kULong: case kULong:
case kUnsignedLong: case kUnsignedLong:
sprintf(string, "%llu", *((cl_ulong *)data)); sprintf(string, "%llu", *((cl_ulong *)data));
return; return;
case kFloat: case kFloat: sprintf(string, "%f", *((cl_float *)data)); return;
sprintf(string, "%f", *((cl_float*)data)); case kHalf: sprintf(string, "half"); return;
return; case kDouble: sprintf(string, "%g", *((cl_double *)data)); return;
case kHalf: default: sprintf(string, "INVALID"); return;
sprintf(string, "half");
return;
case kDouble:
sprintf(string, "%g", *((cl_double*)data));
return;
default:
sprintf(string, "INVALID");
return;
} }
} }
size_t get_explicit_type_size(ExplicitType type) size_t get_explicit_type_size(ExplicitType type)
{ {
/* Quick method to avoid branching: make sure the following array matches the Enum order */ /* Quick method to avoid branching: make sure the following array matches
* the Enum order */
static size_t sExplicitTypeSizes[] = { static size_t sExplicitTypeSizes[] = {
sizeof( cl_bool ), sizeof(cl_bool), sizeof(cl_char), sizeof(cl_uchar),
sizeof( cl_char ), sizeof(cl_uchar), sizeof(cl_short), sizeof(cl_ushort),
sizeof( cl_uchar ), sizeof(cl_ushort), sizeof(cl_int), sizeof(cl_uint),
sizeof( cl_uchar ), sizeof(cl_uint), sizeof(cl_long), sizeof(cl_ulong),
sizeof( cl_short ), sizeof(cl_ulong), sizeof(cl_float), sizeof(cl_half),
sizeof( cl_ushort ),
sizeof( cl_ushort ),
sizeof( cl_int ),
sizeof( cl_uint ),
sizeof( cl_uint ),
sizeof( cl_long ),
sizeof( cl_ulong ),
sizeof( cl_ulong ),
sizeof( cl_float ),
sizeof( cl_half ),
sizeof(cl_double) sizeof(cl_double)
}; };
@@ -106,9 +80,13 @@ size_t get_explicit_type_size( ExplicitType type )
const char *get_explicit_type_name(ExplicitType type) const char *get_explicit_type_name(ExplicitType type)
{ {
/* Quick method to avoid branching: make sure the following array matches the Enum order */ /* Quick method to avoid branching: make sure the following array matches
static const char *sExplicitTypeNames[] = { "bool", "char", "uchar", "unsigned char", "short", "ushort", "unsigned short", "int", * the Enum order */
"uint", "unsigned int", "long", "ulong", "unsigned long", "float", "half", "double" }; static const char *sExplicitTypeNames[] = {
"bool", "char", "uchar", "unsigned char", "short", "ushort",
"unsigned short", "int", "uint", "unsigned int", "long", "ulong",
"unsigned long", "float", "half", "double"
};
return sExplicitTypeNames[type]; return sExplicitTypeNames[type];
} }
@@ -116,13 +94,12 @@ const char * get_explicit_type_name( ExplicitType type )
static long lrintf_clamped(float f); static long lrintf_clamped(float f);
static long lrintf_clamped(float f) static long lrintf_clamped(float f)
{ {
static const float magic[2] = { MAKE_HEX_FLOAT( 0x1.0p23f, 0x1, 23), - MAKE_HEX_FLOAT( 0x1.0p23f, 0x1, 23) }; static const float magic[2] = { MAKE_HEX_FLOAT(0x1.0p23f, 0x1, 23),
-MAKE_HEX_FLOAT(0x1.0p23f, 0x1, 23) };
if( f >= -(float) LONG_MIN ) if (f >= -(float)LONG_MIN) return LONG_MAX;
return LONG_MAX;
if( f <= (float) LONG_MIN ) if (f <= (float)LONG_MIN) return LONG_MIN;
return LONG_MIN;
// Round fractional values to integer in round towards nearest mode // Round fractional values to integer in round towards nearest mode
if (fabsf(f) < MAKE_HEX_FLOAT(0x1.0p23f, 0x1, 23)) if (fabsf(f) < MAKE_HEX_FLOAT(0x1.0p23f, 0x1, 23))
@@ -131,7 +108,8 @@ static long lrintf_clamped( float f )
float magicVal = magic[f < 0]; float magicVal = magic[f < 0];
#if defined(__SSE__) || defined(_WIN32) #if defined(__SSE__) || defined(_WIN32)
// Defeat x87 based arithmetic, which cant do FTZ, and will round this incorrectly // Defeat x87 based arithmetic, which cant do FTZ, and will round this
// incorrectly
__m128 v = _mm_set_ss(x); __m128 v = _mm_set_ss(x);
__m128 m = _mm_set_ss(magicVal); __m128 m = _mm_set_ss(magicVal);
v = _mm_add_ss(v, m); v = _mm_add_ss(v, m);
@@ -150,21 +128,19 @@ static long lrintf_clamped( float f )
static long lrint_clamped(double f); static long lrint_clamped(double f);
static long lrint_clamped(double f) static long lrint_clamped(double f)
{ {
static const double magic[2] = { MAKE_HEX_DOUBLE(0x1.0p52, 0x1LL, 52), MAKE_HEX_DOUBLE(-0x1.0p52, -0x1LL, 52) }; static const double magic[2] = { MAKE_HEX_DOUBLE(0x1.0p52, 0x1LL, 52),
MAKE_HEX_DOUBLE(-0x1.0p52, -0x1LL, 52) };
if (sizeof(long) > 4) if (sizeof(long) > 4)
{ {
if( f >= -(double) LONG_MIN ) if (f >= -(double)LONG_MIN) return LONG_MAX;
return LONG_MAX;
} }
else else
{ {
if( f >= LONG_MAX ) if (f >= LONG_MAX) return LONG_MAX;
return LONG_MAX;
} }
if( f <= (double) LONG_MIN ) if (f <= (double)LONG_MIN) return LONG_MIN;
return LONG_MIN;
// Round fractional values to integer in round towards nearest mode // Round fractional values to integer in round towards nearest mode
if (fabs(f) < MAKE_HEX_DOUBLE(0x1.0p52, 0x1LL, 52)) if (fabs(f) < MAKE_HEX_DOUBLE(0x1.0p52, 0x1LL, 52))
@@ -172,7 +148,8 @@ static long lrint_clamped( double f )
volatile double x = f; volatile double x = f;
double magicVal = magic[f < 0]; double magicVal = magic[f < 0];
#if defined(__SSE2__) || (defined(_MSC_VER)) #if defined(__SSE2__) || (defined(_MSC_VER))
// Defeat x87 based arithmetic, which cant do FTZ, and will round this incorrectly // Defeat x87 based arithmetic, which cant do FTZ, and will round this
// incorrectly
__m128d v = _mm_set_sd(x); __m128d v = _mm_set_sd(x);
__m128d m = _mm_set_sd(magicVal); __m128d m = _mm_set_sd(magicVal);
v = _mm_add_sd(v, m); v = _mm_add_sd(v, m);
@@ -192,23 +169,41 @@ static long lrint_clamped( double f )
typedef cl_long Long; typedef cl_long Long;
typedef cl_ulong ULong; typedef cl_ulong ULong;
static ULong sUpperLimits[ kNumExplicitTypes ] = static ULong sUpperLimits[kNumExplicitTypes] = {
{
0, 0,
127, 255, 255, 127,
32767, 65535, 65535, 255,
0x7fffffffLL, 0xffffffffLL, 0xffffffffLL, 255,
0x7fffffffffffffffLL, 0xffffffffffffffffLL, 0xffffffffffffffffLL, 32767,
0, 0 }; // Last two values aren't stored here 65535,
65535,
0x7fffffffLL,
0xffffffffLL,
0xffffffffLL,
0x7fffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0,
0
}; // Last two values aren't stored here
static Long sLowerLimits[ kNumExplicitTypes ] = static Long sLowerLimits[kNumExplicitTypes] = {
{
-1, -1,
-128, 0, 0, -128,
-32768, 0, 0, 0,
(Long)0xffffffff80000000LL, 0, 0, 0,
(Long)0x8000000000000000LL, 0, 0, -32768,
0, 0 }; // Last two values aren't stored here 0,
0,
(Long)0xffffffff80000000LL,
0,
0,
(Long)0x8000000000000000LL,
0,
0,
0,
0
}; // Last two values aren't stored here
#define BOOL_CASE(inType) \ #define BOOL_CASE(inType) \
case kBool: \ case kBool: \
@@ -222,20 +217,28 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
*outType##Ptr = (outType)(*inType##Ptr); \ *outType##Ptr = (outType)(*inType##Ptr); \
break; break;
// Sadly, the ULong downcasting cases need a separate #define to get rid of signed/unsigned comparison warnings // Sadly, the ULong downcasting cases need a separate #define to get rid of
// signed/unsigned comparison warnings
#define DOWN_CAST_CASE(inType, outEnum, outType, sat) \ #define DOWN_CAST_CASE(inType, outEnum, outType, sat) \
case outEnum: \ case outEnum: \
outType##Ptr = (outType *)outRaw; \ outType##Ptr = (outType *)outRaw; \
if (sat) \ if (sat) \
{ \ { \
if( ( sLowerLimits[outEnum] < 0 && *inType##Ptr > (Long)sUpperLimits[outEnum] ) || ( sLowerLimits[outEnum] == 0 && (ULong)*inType##Ptr > sUpperLimits[outEnum] ) )\ if ((sLowerLimits[outEnum] < 0 \
&& *inType##Ptr > (Long)sUpperLimits[outEnum]) \
|| (sLowerLimits[outEnum] == 0 \
&& (ULong)*inType##Ptr > sUpperLimits[outEnum])) \
*outType##Ptr = (outType)sUpperLimits[outEnum]; \ *outType##Ptr = (outType)sUpperLimits[outEnum]; \
else if (*inType##Ptr < sLowerLimits[outEnum]) \ else if (*inType##Ptr < sLowerLimits[outEnum]) \
*outType##Ptr = (outType)sLowerLimits[outEnum]; \ *outType##Ptr = (outType)sLowerLimits[outEnum]; \
else \ else \
*outType##Ptr = (outType)*inType##Ptr; \ *outType##Ptr = (outType)*inType##Ptr; \
} else { \ } \
*outType##Ptr = (outType)( *inType##Ptr & ( 0xffffffffffffffffLL >> ( 64 - ( sizeof( outType ) * 8 ) ) ) ); \ else \
{ \
*outType##Ptr = (outType)( \
*inType##Ptr \
& (0xffffffffffffffffLL >> (64 - (sizeof(outType) * 8)))); \
} \ } \
break; break;
@@ -248,8 +251,12 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
*outType##Ptr = (outType)sUpperLimits[outEnum]; \ *outType##Ptr = (outType)sUpperLimits[outEnum]; \
else \ else \
*outType##Ptr = (outType)*inType##Ptr; \ *outType##Ptr = (outType)*inType##Ptr; \
} else { \ } \
*outType##Ptr = (outType)( *inType##Ptr & ( 0xffffffffffffffffLL >> ( 64 - ( sizeof( outType ) * 8 ) ) ) ); \ else \
{ \
*outType##Ptr = (outType)( \
*inType##Ptr \
& (0xffffffffffffffffLL >> (64 - (sizeof(outType) * 8)))); \
} \ } \
break; break;
@@ -265,7 +272,8 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
break; break;
/* Note: we use lrintf here to force the rounding instead of whatever the processor's current rounding mode is */ /* Note: we use lrintf here to force the rounding instead of whatever the
* processor's current rounding mode is */
#define FLOAT_ROUND_TO_NEAREST_CASE(outEnum, outType) \ #define FLOAT_ROUND_TO_NEAREST_CASE(outEnum, outType) \
case outEnum: \ case outEnum: \
outType##Ptr = (outType *)outRaw; \ outType##Ptr = (outType *)outRaw; \
@@ -273,8 +281,7 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
break; break;
#define FLOAT_ROUND_CASE(outEnum, outType, rounding, sat) \ #define FLOAT_ROUND_CASE(outEnum, outType, rounding, sat) \
case outEnum: \ case outEnum: { \
{ \
outType##Ptr = (outType *)outRaw; \ outType##Ptr = (outType *)outRaw; \
/* Get the tens digit */ \ /* Get the tens digit */ \
Long wholeValue = (Long)*floatPtr; \ Long wholeValue = (Long)*floatPtr; \
@@ -292,14 +299,12 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
else if (rounding == kRoundToPosInf) \ else if (rounding == kRoundToPosInf) \
{ \ { \
/* Only positive numbers are wrong */ \ /* Only positive numbers are wrong */ \
if( largeRemainder != 0.f && wholeValue >= 0 ) \ if (largeRemainder != 0.f && wholeValue >= 0) wholeValue++; \
wholeValue++; \
} \ } \
else if (rounding == kRoundToNegInf) \ else if (rounding == kRoundToNegInf) \
{ \ { \
/* Only negative numbers are off */ \ /* Only negative numbers are off */ \
if( largeRemainder != 0.f && wholeValue < 0 ) \ if (largeRemainder != 0.f && wholeValue < 0) wholeValue--; \
wholeValue--; \
} \ } \
else \ else \
{ /* Default is round-to-nearest */ \ { /* Default is round-to-nearest */ \
@@ -308,21 +313,27 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
/* Now apply saturation rules */ \ /* Now apply saturation rules */ \
if (sat) \ if (sat) \
{ \ { \
if( ( sLowerLimits[outEnum] < 0 && wholeValue > (Long)sUpperLimits[outEnum] ) || ( sLowerLimits[outEnum] == 0 && (ULong)wholeValue > sUpperLimits[outEnum] ) )\ if ((sLowerLimits[outEnum] < 0 \
&& wholeValue > (Long)sUpperLimits[outEnum]) \
|| (sLowerLimits[outEnum] == 0 \
&& (ULong)wholeValue > sUpperLimits[outEnum])) \
*outType##Ptr = (outType)sUpperLimits[outEnum]; \ *outType##Ptr = (outType)sUpperLimits[outEnum]; \
else if (wholeValue < sLowerLimits[outEnum]) \ else if (wholeValue < sLowerLimits[outEnum]) \
*outType##Ptr = (outType)sLowerLimits[outEnum]; \ *outType##Ptr = (outType)sLowerLimits[outEnum]; \
else \ else \
*outType##Ptr = (outType)wholeValue; \ *outType##Ptr = (outType)wholeValue; \
} else { \ } \
*outType##Ptr = (outType)( wholeValue & ( 0xffffffffffffffffLL >> ( 64 - ( sizeof( outType ) * 8 ) ) ) ); \ else \
{ \
*outType##Ptr = (outType)( \
wholeValue \
& (0xffffffffffffffffLL >> (64 - (sizeof(outType) * 8)))); \
} \ } \
} \ } \
break; break;
#define DOUBLE_ROUND_CASE(outEnum, outType, rounding, sat) \ #define DOUBLE_ROUND_CASE(outEnum, outType, rounding, sat) \
case outEnum: \ case outEnum: { \
{ \
outType##Ptr = (outType *)outRaw; \ outType##Ptr = (outType *)outRaw; \
/* Get the tens digit */ \ /* Get the tens digit */ \
Long wholeValue = (Long)*doublePtr; \ Long wholeValue = (Long)*doublePtr; \
@@ -340,14 +351,12 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
else if (rounding == kRoundToPosInf) \ else if (rounding == kRoundToPosInf) \
{ \ { \
/* Only positive numbers are wrong */ \ /* Only positive numbers are wrong */ \
if( largeRemainder != 0.0 && wholeValue >= 0 ) \ if (largeRemainder != 0.0 && wholeValue >= 0) wholeValue++; \
wholeValue++; \
} \ } \
else if (rounding == kRoundToNegInf) \ else if (rounding == kRoundToNegInf) \
{ \ { \
/* Only negative numbers are off */ \ /* Only negative numbers are off */ \
if( largeRemainder != 0.0 && wholeValue < 0 ) \ if (largeRemainder != 0.0 && wholeValue < 0) wholeValue--; \
wholeValue--; \
} \ } \
else \ else \
{ /* Default is round-to-nearest */ \ { /* Default is round-to-nearest */ \
@@ -356,14 +365,21 @@ static Long sLowerLimits[ kNumExplicitTypes ] =
/* Now apply saturation rules */ \ /* Now apply saturation rules */ \
if (sat) \ if (sat) \
{ \ { \
if( ( sLowerLimits[outEnum] < 0 && wholeValue > (Long)sUpperLimits[outEnum] ) || ( sLowerLimits[outEnum] == 0 && (ULong)wholeValue > sUpperLimits[outEnum] ) )\ if ((sLowerLimits[outEnum] < 0 \
&& wholeValue > (Long)sUpperLimits[outEnum]) \
|| (sLowerLimits[outEnum] == 0 \
&& (ULong)wholeValue > sUpperLimits[outEnum])) \
*outType##Ptr = (outType)sUpperLimits[outEnum]; \ *outType##Ptr = (outType)sUpperLimits[outEnum]; \
else if (wholeValue < sLowerLimits[outEnum]) \ else if (wholeValue < sLowerLimits[outEnum]) \
*outType##Ptr = (outType)sLowerLimits[outEnum]; \ *outType##Ptr = (outType)sLowerLimits[outEnum]; \
else \ else \
*outType##Ptr = (outType)wholeValue; \ *outType##Ptr = (outType)wholeValue; \
} else { \ } \
*outType##Ptr = (outType)( wholeValue & ( 0xffffffffffffffffLL >> ( 64 - ( sizeof( outType ) * 8 ) ) ) ); \ else \
{ \
*outType##Ptr = (outType)( \
wholeValue \
& (0xffffffffffffffffLL >> (64 - (sizeof(outType) * 8)))); \
} \ } \
} \ } \
break; break;
@@ -373,7 +389,9 @@ typedef unsigned short ushort;
typedef unsigned int uint; typedef unsigned int uint;
typedef unsigned long ulong; typedef unsigned long ulong;
void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, bool saturate, RoundingType roundType, ExplicitType outType ) void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
bool saturate, RoundingType roundType,
ExplicitType outType)
{ {
bool *boolPtr; bool *boolPtr;
char *charPtr; char *charPtr;
@@ -410,7 +428,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
case kLong: case kLong:
case kULong: case kULong:
case kUnsignedLong: case kUnsignedLong:
memset( outRaw, *boolPtr ? 0xff : 0, get_explicit_type_size( outType ) ); memset(outRaw, *boolPtr ? 0xff : 0,
get_explicit_type_size(outType));
break; break;
case kFloat: case kFloat:
@@ -422,7 +441,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
*doublePtr = (*boolPtr) ? -1.0 : 0.0; *doublePtr = (*boolPtr) ? -1.0 : 0.0;
break; break;
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -453,7 +473,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(char) TO_DOUBLE_CASE(char)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -484,7 +505,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(uchar) TO_DOUBLE_CASE(uchar)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -515,7 +537,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(uchar) TO_DOUBLE_CASE(uchar)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -546,7 +569,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(short) TO_DOUBLE_CASE(short)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -577,7 +601,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(ushort) TO_DOUBLE_CASE(ushort)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -608,7 +633,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(ushort) TO_DOUBLE_CASE(ushort)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -639,7 +665,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(int) TO_DOUBLE_CASE(int)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -670,7 +697,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(uint) TO_DOUBLE_CASE(uint)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -701,7 +729,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(uint) TO_DOUBLE_CASE(uint)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -732,7 +761,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(Long) TO_DOUBLE_CASE(Long)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -763,7 +793,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(ULong) TO_DOUBLE_CASE(ULong)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -794,7 +825,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(ULong) TO_DOUBLE_CASE(ULong)
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -825,7 +857,8 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
TO_DOUBLE_CASE(float); TO_DOUBLE_CASE(float);
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
@@ -856,18 +889,21 @@ void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, boo
break; break;
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break; break;
} }
break; break;
default: default:
log_error( "ERROR: Invalid type given to convert_explicit_value!!\n" ); log_error(
"ERROR: Invalid type given to convert_explicit_value!!\n");
break; break;
} }
} }
void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outData ) void generate_random_data(ExplicitType type, size_t count, MTdata d,
void *outData)
{ {
bool *boolPtr; bool *boolPtr;
cl_char *charPtr; cl_char *charPtr;
@@ -897,7 +933,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
bitsLeft = 32; bitsLeft = 32;
} }
boolPtr[i] = (bits & 1) ? true : false; boolPtr[i] = (bits & 1) ? true : false;
bits >>= 1; bitsLeft -= 1; bits >>= 1;
bitsLeft -= 1;
} }
break; break;
@@ -911,7 +948,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
bitsLeft = 32; bitsLeft = 32;
} }
charPtr[i] = (cl_char)((cl_int)(bits & 255) - 127); charPtr[i] = (cl_char)((cl_int)(bits & 255) - 127);
bits >>= 8; bitsLeft -= 8; bits >>= 8;
bitsLeft -= 8;
} }
break; break;
@@ -926,7 +964,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
bitsLeft = 32; bitsLeft = 32;
} }
ucharPtr[i] = (cl_uchar)(bits & 255); ucharPtr[i] = (cl_uchar)(bits & 255);
bits >>= 8; bitsLeft -= 8; bits >>= 8;
bitsLeft -= 8;
} }
break; break;
@@ -940,7 +979,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
bitsLeft = 32; bitsLeft = 32;
} }
shortPtr[i] = (cl_short)((cl_int)(bits & 65535) - 32767); shortPtr[i] = (cl_short)((cl_int)(bits & 65535) - 32767);
bits >>= 16; bitsLeft -= 16; bits >>= 16;
bitsLeft -= 16;
} }
break; break;
@@ -955,7 +995,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
bitsLeft = 32; bitsLeft = 32;
} }
ushortPtr[i] = (cl_ushort)((cl_int)(bits & 65535)); ushortPtr[i] = (cl_ushort)((cl_int)(bits & 65535));
bits >>= 16; bitsLeft -= 16; bits >>= 16;
bitsLeft -= 16;
} }
break; break;
@@ -980,7 +1021,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
longPtr = (cl_long *)outData; longPtr = (cl_long *)outData;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
longPtr[i] = (cl_long)genrand_int32(d) | ( (cl_long)genrand_int32(d) << 32 ); longPtr[i] = (cl_long)genrand_int32(d)
| ((cl_long)genrand_int32(d) << 32);
} }
break; break;
@@ -989,7 +1031,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
ulongPtr = (cl_ulong *)outData; ulongPtr = (cl_ulong *)outData;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
ulongPtr[i] = (cl_ulong)genrand_int32(d) | ( (cl_ulong)genrand_int32(d) << 32 ); ulongPtr[i] = (cl_ulong)genrand_int32(d)
| ((cl_ulong)genrand_int32(d) << 32);
} }
break; break;
@@ -999,7 +1042,8 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
{ {
// [ -(double) 0x7fffffff, (double) 0x7fffffff ] // [ -(double) 0x7fffffff, (double) 0x7fffffff ]
double t = genrand_real1(d); double t = genrand_real1(d);
floatPtr[i] = (float) ((1.0 - t) * -(double) 0x7fffffff + t * (double) 0x7fffffff); floatPtr[i] = (float)((1.0 - t) * -(double)0x7fffffff
+ t * (double)0x7fffffff);
} }
break; break;
@@ -1007,9 +1051,11 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
doublePtr = (cl_double *)outData; doublePtr = (cl_double *)outData;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
cl_long u = (cl_long)genrand_int32(d) | ( (cl_long)genrand_int32(d) << 32 ); cl_long u = (cl_long)genrand_int32(d)
| ((cl_long)genrand_int32(d) << 32);
double t = (double)u; double t = (double)u;
t *= MAKE_HEX_DOUBLE( 0x1.0p-32, 0x1, -32 ); // scale [-2**63, 2**63] to [-2**31, 2**31] // scale [-2**63, 2**63] to [-2**31, 2**31]
t *= MAKE_HEX_DOUBLE(0x1.0p-32, 0x1, -32);
doublePtr[i] = t; doublePtr[i] = t;
} }
break; break;
@@ -1023,13 +1069,16 @@ void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outD
bits = genrand_int32(d); bits = genrand_int32(d);
bitsLeft = 32; bitsLeft = 32;
} }
halfPtr[i] = bits & 65535; /* Kindly generates random bits for us */ halfPtr[i] =
bits >>= 16; bitsLeft -= 16; bits & 65535; /* Kindly generates random bits for us */
bits >>= 16;
bitsLeft -= 16;
} }
break; break;
default: default:
log_error( "ERROR: Invalid type passed in to generate_random_data!\n" ); log_error(
"ERROR: Invalid type passed in to generate_random_data!\n");
break; break;
} }
} }
@@ -1045,28 +1094,19 @@ cl_long read_upscale_signed( void *inRaw, ExplicitType inType )
{ {
switch (inType) switch (inType)
{ {
case kChar: case kChar: return (cl_long)(*((cl_char *)inRaw));
return (cl_long)( *( (cl_char *)inRaw ) );
case kUChar: case kUChar:
case kUnsignedChar: case kUnsignedChar: return (cl_long)(*((cl_uchar *)inRaw));
return (cl_long)( *( (cl_uchar *)inRaw ) ); case kShort: return (cl_long)(*((cl_short *)inRaw));
case kShort:
return (cl_long)( *( (cl_short *)inRaw ) );
case kUShort: case kUShort:
case kUnsignedShort: case kUnsignedShort: return (cl_long)(*((cl_ushort *)inRaw));
return (cl_long)( *( (cl_ushort *)inRaw ) ); case kInt: return (cl_long)(*((cl_int *)inRaw));
case kInt:
return (cl_long)( *( (cl_int *)inRaw ) );
case kUInt: case kUInt:
case kUnsignedInt: case kUnsignedInt: return (cl_long)(*((cl_uint *)inRaw));
return (cl_long)( *( (cl_uint *)inRaw ) ); case kLong: return (cl_long)(*((cl_long *)inRaw));
case kLong:
return (cl_long)( *( (cl_long *)inRaw ) );
case kULong: case kULong:
case kUnsignedLong: case kUnsignedLong: return (cl_long)(*((cl_ulong *)inRaw));
return (cl_long)( *( (cl_ulong *)inRaw ) ); default: return 0;
default:
return 0;
} }
} }
@@ -1074,28 +1114,19 @@ cl_ulong read_upscale_unsigned( void *inRaw, ExplicitType inType )
{ {
switch (inType) switch (inType)
{ {
case kChar: case kChar: return (cl_ulong)(*((cl_char *)inRaw));
return (cl_ulong)( *( (cl_char *)inRaw ) );
case kUChar: case kUChar:
case kUnsignedChar: case kUnsignedChar: return (cl_ulong)(*((cl_uchar *)inRaw));
return (cl_ulong)( *( (cl_uchar *)inRaw ) ); case kShort: return (cl_ulong)(*((cl_short *)inRaw));
case kShort:
return (cl_ulong)( *( (cl_short *)inRaw ) );
case kUShort: case kUShort:
case kUnsignedShort: case kUnsignedShort: return (cl_ulong)(*((cl_ushort *)inRaw));
return (cl_ulong)( *( (cl_ushort *)inRaw ) ); case kInt: return (cl_ulong)(*((cl_int *)inRaw));
case kInt:
return (cl_ulong)( *( (cl_int *)inRaw ) );
case kUInt: case kUInt:
case kUnsignedInt: case kUnsignedInt: return (cl_ulong)(*((cl_uint *)inRaw));
return (cl_ulong)( *( (cl_uint *)inRaw ) ); case kLong: return (cl_ulong)(*((cl_long *)inRaw));
case kLong:
return (cl_ulong)( *( (cl_long *)inRaw ) );
case kULong: case kULong:
case kUnsignedLong: case kUnsignedLong: return (cl_ulong)(*((cl_ulong *)inRaw));
return (cl_ulong)( *( (cl_ulong *)inRaw ) ); default: return 0;
default:
return 0;
} }
} }
@@ -1103,32 +1134,21 @@ float read_as_float( void *inRaw, ExplicitType inType )
{ {
switch (inType) switch (inType)
{ {
case kChar: case kChar: return (float)(*((cl_char *)inRaw));
return (float)( *( (cl_char *)inRaw ) );
case kUChar: case kUChar:
case kUnsignedChar: case kUnsignedChar: return (float)(*((cl_char *)inRaw));
return (float)( *( (cl_char *)inRaw ) ); case kShort: return (float)(*((cl_short *)inRaw));
case kShort:
return (float)( *( (cl_short *)inRaw ) );
case kUShort: case kUShort:
case kUnsignedShort: case kUnsignedShort: return (float)(*((cl_ushort *)inRaw));
return (float)( *( (cl_ushort *)inRaw ) ); case kInt: return (float)(*((cl_int *)inRaw));
case kInt:
return (float)( *( (cl_int *)inRaw ) );
case kUInt: case kUInt:
case kUnsignedInt: case kUnsignedInt: return (float)(*((cl_uint *)inRaw));
return (float)( *( (cl_uint *)inRaw ) ); case kLong: return (float)(*((cl_long *)inRaw));
case kLong:
return (float)( *( (cl_long *)inRaw ) );
case kULong: case kULong:
case kUnsignedLong: case kUnsignedLong: return (float)(*((cl_ulong *)inRaw));
return (float)( *( (cl_ulong *)inRaw ) ); case kFloat: return *((float *)inRaw);
case kFloat: case kDouble: return (float)*((double *)inRaw);
return *( (float *)inRaw ); default: return 0;
case kDouble:
return (float) *( (double*)inRaw );
default:
return 0;
} }
} }
@@ -1140,15 +1160,15 @@ float get_random_float(float low, float high, MTdata d)
double get_random_double(double low, double high, MTdata d) double get_random_double(double low, double high, MTdata d)
{ {
cl_ulong u = (cl_ulong) genrand_int32(d) | ((cl_ulong) genrand_int32(d) << 32 ); cl_ulong u =
(cl_ulong)genrand_int32(d) | ((cl_ulong)genrand_int32(d) << 32);
double t = (double)u * MAKE_HEX_DOUBLE(0x1.0p-64, 0x1, -64); double t = (double)u * MAKE_HEX_DOUBLE(0x1.0p-64, 0x1, -64);
return (1.0f - t) * low + t * high; return (1.0f - t) * low + t * high;
} }
float any_float(MTdata d) float any_float(MTdata d)
{ {
union union {
{
float f; float f;
cl_uint u; cl_uint u;
} u; } u;
@@ -1160,8 +1180,7 @@ float any_float( MTdata d )
double any_double(MTdata d) double any_double(MTdata d)
{ {
union union {
{
double f; double f;
cl_ulong u; cl_ulong u;
} u; } u;
@@ -1178,14 +1197,18 @@ int random_in_range( int minV, int maxV, MTdata d )
size_t get_random_size_t(size_t low, size_t high, MTdata d) size_t get_random_size_t(size_t low, size_t high, MTdata d)
{ {
enum { N = sizeof(size_t)/sizeof(int) }; enum
{
N = sizeof(size_t) / sizeof(int)
};
union { union {
int word[N]; int word[N];
size_t size; size_t size;
} u; } u;
for (unsigned i=0; i != N; ++i) { for (unsigned i = 0; i != N; ++i)
{
u.word[i] = genrand_int32(d); u.word[i] = genrand_int32(d);
} }
@@ -1194,5 +1217,3 @@ size_t get_random_size_t(size_t low, size_t high, MTdata d)
return (range) ? low + ((u.size - low) % range) : low; return (range) ? low + ((u.size - low) % range) : low;
} }

View File

@@ -68,9 +68,13 @@ typedef enum RoundingTypes RoundingType;
extern void print_type_to_string(ExplicitType type, void *data, char *string); extern void print_type_to_string(ExplicitType type, void *data, char *string);
extern size_t get_explicit_type_size(ExplicitType type); extern size_t get_explicit_type_size(ExplicitType type);
extern const char *get_explicit_type_name(ExplicitType type); extern const char *get_explicit_type_name(ExplicitType type);
extern void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, bool saturate, RoundingType roundType, ExplicitType outType ); extern void convert_explicit_value(void *inRaw, void *outRaw,
ExplicitType inType, bool saturate,
RoundingType roundType,
ExplicitType outType);
extern void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outData ); extern void generate_random_data(ExplicitType type, size_t count, MTdata d,
void *outData);
extern void *create_random_data(ExplicitType type, MTdata d, size_t count); extern void *create_random_data(ExplicitType type, MTdata d, size_t count);
extern cl_long read_upscale_signed(void *inRaw, ExplicitType inType); extern cl_long read_upscale_signed(void *inRaw, ExplicitType inType);
@@ -91,11 +95,15 @@ static inline int IsFloatSubnormal( float x )
{ {
#if 2 == FLT_RADIX #if 2 == FLT_RADIX
// Do this in integer to avoid problems with FTZ behavior // Do this in integer to avoid problems with FTZ behavior
union{ float d; uint32_t u;}u; union {
float d;
uint32_t u;
} u;
u.d = fabsf(x); u.d = fabsf(x);
return (u.u - 1) < 0x007fffffU; return (u.u - 1) < 0x007fffffU;
#else #else
// rely on floating point hardware for non-radix2 non-IEEE-754 hardware -- will fail if you flush subnormals to zero // rely on floating point hardware for non-radix2 non-IEEE-754 hardware --
// will fail if you flush subnormals to zero
return fabs(x) < (double)FLT_MIN && x != 0.0; return fabs(x) < (double)FLT_MIN && x != 0.0;
#endif #endif
} }
@@ -104,11 +112,15 @@ static inline int IsDoubleSubnormal( double x )
{ {
#if 2 == FLT_RADIX #if 2 == FLT_RADIX
// Do this in integer to avoid problems with FTZ behavior // Do this in integer to avoid problems with FTZ behavior
union{ double d; uint64_t u;}u; union {
double d;
uint64_t u;
} u;
u.d = fabs(x); u.d = fabs(x);
return (u.u - 1) < 0x000fffffffffffffULL; return (u.u - 1) < 0x000fffffffffffffULL;
#else #else
// rely on floating point hardware for non-radix2 non-IEEE-754 hardware -- will fail if you flush subnormals to zero // rely on floating point hardware for non-radix2 non-IEEE-754 hardware --
// will fail if you flush subnormals to zero
return fabs(x) < (double)DBL_MIN && x != 0.0; return fabs(x) < (double)DBL_MIN && x != 0.0;
#endif #endif
} }
@@ -120,5 +132,3 @@ static inline int IsHalfSubnormal( cl_half x )
} }
#endif // _conversions_h #endif // _conversions_h

View File

@@ -32,22 +32,28 @@ const char *IGetErrorString( int clErrorCode )
case CL_DEVICE_NOT_FOUND: return "CL_DEVICE_NOT_FOUND"; case CL_DEVICE_NOT_FOUND: return "CL_DEVICE_NOT_FOUND";
case CL_DEVICE_NOT_AVAILABLE: return "CL_DEVICE_NOT_AVAILABLE"; case CL_DEVICE_NOT_AVAILABLE: return "CL_DEVICE_NOT_AVAILABLE";
case CL_COMPILER_NOT_AVAILABLE: return "CL_COMPILER_NOT_AVAILABLE"; case CL_COMPILER_NOT_AVAILABLE: return "CL_COMPILER_NOT_AVAILABLE";
case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "CL_MEM_OBJECT_ALLOCATION_FAILURE"; case CL_MEM_OBJECT_ALLOCATION_FAILURE:
return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
case CL_OUT_OF_RESOURCES: return "CL_OUT_OF_RESOURCES"; case CL_OUT_OF_RESOURCES: return "CL_OUT_OF_RESOURCES";
case CL_OUT_OF_HOST_MEMORY: return "CL_OUT_OF_HOST_MEMORY"; case CL_OUT_OF_HOST_MEMORY: return "CL_OUT_OF_HOST_MEMORY";
case CL_PROFILING_INFO_NOT_AVAILABLE: return "CL_PROFILING_INFO_NOT_AVAILABLE"; case CL_PROFILING_INFO_NOT_AVAILABLE:
return "CL_PROFILING_INFO_NOT_AVAILABLE";
case CL_MEM_COPY_OVERLAP: return "CL_MEM_COPY_OVERLAP"; case CL_MEM_COPY_OVERLAP: return "CL_MEM_COPY_OVERLAP";
case CL_IMAGE_FORMAT_MISMATCH: return "CL_IMAGE_FORMAT_MISMATCH"; case CL_IMAGE_FORMAT_MISMATCH: return "CL_IMAGE_FORMAT_MISMATCH";
case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "CL_IMAGE_FORMAT_NOT_SUPPORTED"; case CL_IMAGE_FORMAT_NOT_SUPPORTED:
return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
case CL_BUILD_PROGRAM_FAILURE: return "CL_BUILD_PROGRAM_FAILURE"; case CL_BUILD_PROGRAM_FAILURE: return "CL_BUILD_PROGRAM_FAILURE";
case CL_MAP_FAILURE: return "CL_MAP_FAILURE"; case CL_MAP_FAILURE: return "CL_MAP_FAILURE";
case CL_MISALIGNED_SUB_BUFFER_OFFSET: return "CL_MISALIGNED_SUB_BUFFER_OFFSET"; case CL_MISALIGNED_SUB_BUFFER_OFFSET:
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST: return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST"; return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
case CL_COMPILE_PROGRAM_FAILURE: return "CL_COMPILE_PROGRAM_FAILURE"; case CL_COMPILE_PROGRAM_FAILURE: return "CL_COMPILE_PROGRAM_FAILURE";
case CL_LINKER_NOT_AVAILABLE: return "CL_LINKER_NOT_AVAILABLE"; case CL_LINKER_NOT_AVAILABLE: return "CL_LINKER_NOT_AVAILABLE";
case CL_LINK_PROGRAM_FAILURE: return "CL_LINK_PROGRAM_FAILURE"; case CL_LINK_PROGRAM_FAILURE: return "CL_LINK_PROGRAM_FAILURE";
case CL_DEVICE_PARTITION_FAILED: return "CL_DEVICE_PARTITION_FAILED"; case CL_DEVICE_PARTITION_FAILED: return "CL_DEVICE_PARTITION_FAILED";
case CL_KERNEL_ARG_INFO_NOT_AVAILABLE: return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE"; case CL_KERNEL_ARG_INFO_NOT_AVAILABLE:
return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
case CL_INVALID_VALUE: return "CL_INVALID_VALUE"; case CL_INVALID_VALUE: return "CL_INVALID_VALUE";
case CL_INVALID_DEVICE_TYPE: return "CL_INVALID_DEVICE_TYPE"; case CL_INVALID_DEVICE_TYPE: return "CL_INVALID_DEVICE_TYPE";
case CL_INVALID_DEVICE: return "CL_INVALID_DEVICE"; case CL_INVALID_DEVICE: return "CL_INVALID_DEVICE";
@@ -56,15 +62,18 @@ const char *IGetErrorString( int clErrorCode )
case CL_INVALID_COMMAND_QUEUE: return "CL_INVALID_COMMAND_QUEUE"; case CL_INVALID_COMMAND_QUEUE: return "CL_INVALID_COMMAND_QUEUE";
case CL_INVALID_HOST_PTR: return "CL_INVALID_HOST_PTR"; case CL_INVALID_HOST_PTR: return "CL_INVALID_HOST_PTR";
case CL_INVALID_MEM_OBJECT: return "CL_INVALID_MEM_OBJECT"; case CL_INVALID_MEM_OBJECT: return "CL_INVALID_MEM_OBJECT";
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR"; case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
case CL_INVALID_IMAGE_SIZE: return "CL_INVALID_IMAGE_SIZE"; case CL_INVALID_IMAGE_SIZE: return "CL_INVALID_IMAGE_SIZE";
case CL_INVALID_SAMPLER: return "CL_INVALID_SAMPLER"; case CL_INVALID_SAMPLER: return "CL_INVALID_SAMPLER";
case CL_INVALID_BINARY: return "CL_INVALID_BINARY"; case CL_INVALID_BINARY: return "CL_INVALID_BINARY";
case CL_INVALID_BUILD_OPTIONS: return "CL_INVALID_BUILD_OPTIONS"; case CL_INVALID_BUILD_OPTIONS: return "CL_INVALID_BUILD_OPTIONS";
case CL_INVALID_PROGRAM: return "CL_INVALID_PROGRAM"; case CL_INVALID_PROGRAM: return "CL_INVALID_PROGRAM";
case CL_INVALID_PROGRAM_EXECUTABLE: return "CL_INVALID_PROGRAM_EXECUTABLE"; case CL_INVALID_PROGRAM_EXECUTABLE:
return "CL_INVALID_PROGRAM_EXECUTABLE";
case CL_INVALID_KERNEL_NAME: return "CL_INVALID_KERNEL_NAME"; case CL_INVALID_KERNEL_NAME: return "CL_INVALID_KERNEL_NAME";
case CL_INVALID_KERNEL_DEFINITION: return "CL_INVALID_KERNEL_DEFINITION"; case CL_INVALID_KERNEL_DEFINITION:
return "CL_INVALID_KERNEL_DEFINITION";
case CL_INVALID_KERNEL: return "CL_INVALID_KERNEL"; case CL_INVALID_KERNEL: return "CL_INVALID_KERNEL";
case CL_INVALID_ARG_INDEX: return "CL_INVALID_ARG_INDEX"; case CL_INVALID_ARG_INDEX: return "CL_INVALID_ARG_INDEX";
case CL_INVALID_ARG_VALUE: return "CL_INVALID_ARG_VALUE"; case CL_INVALID_ARG_VALUE: return "CL_INVALID_ARG_VALUE";
@@ -155,18 +164,14 @@ int IsChannelOrderSupported( cl_channel_order order )
case CL_sRGBx: case CL_sRGBx:
case CL_sBGRA: case CL_sBGRA:
case CL_sRGBA: case CL_sRGBA:
case CL_DEPTH: case CL_DEPTH: return 1;
return 1;
#if defined CL_1RGB_APPLE #if defined CL_1RGB_APPLE
case CL_1RGB_APPLE: case CL_1RGB_APPLE: return 1;
return 1;
#endif #endif
#if defined CL_BGR1_APPLE #if defined CL_BGR1_APPLE
case CL_BGR1_APPLE: case CL_BGR1_APPLE: return 1;
return 1;
#endif #endif
default: default: return 0;
return 0;
} }
} }
@@ -216,14 +221,11 @@ int IsChannelTypeSupported( cl_channel_type type )
case CL_UNSIGNED_INT16: case CL_UNSIGNED_INT16:
case CL_UNSIGNED_INT32: case CL_UNSIGNED_INT32:
case CL_HALF_FLOAT: case CL_HALF_FLOAT:
case CL_FLOAT: case CL_FLOAT: return 1;
return 1;
#ifdef CL_SFIXED14_APPLE #ifdef CL_SFIXED14_APPLE
case CL_SFIXED14_APPLE: case CL_SFIXED14_APPLE: return 1;
return 1;
#endif #endif
default: default: return 0;
return 0;
} }
} }
@@ -252,13 +254,13 @@ const char *GetDeviceTypeName( cl_device_type type )
} }
} }
const char *GetDataVectorString( void *dataBuffer, size_t typeSize, size_t vecSize, char *buffer ) const char *GetDataVectorString(void *dataBuffer, size_t typeSize,
size_t vecSize, char *buffer)
{ {
static char scratch[1024]; static char scratch[1024];
size_t i, j; size_t i, j;
if( buffer == NULL ) if (buffer == NULL) buffer = scratch;
buffer = scratch;
unsigned char *p = (unsigned char *)dataBuffer; unsigned char *p = (unsigned char *)dataBuffer;
char *bPtr; char *bPtr;
@@ -312,17 +314,21 @@ const char *GetQueuePropertyName(cl_command_queue_properties property)
#define HALF_MANT_DIG 11 #define HALF_MANT_DIG 11
static float Ulp_Error_Half_Float(float test, double reference) static float Ulp_Error_Half_Float(float test, double reference)
{ {
union{ double d; uint64_t u; }u; u.d = reference; union {
double d;
uint64_t u;
} u;
u.d = reference;
// Note: This function presumes that someone has already tested whether the result is correctly, // Note: This function presumes that someone has already tested whether the
// rounded before calling this function. That test: // result is correctly, rounded before calling this function. That test:
// //
// if( (float) reference == test ) // if( (float) reference == test )
// return 0.0f; // return 0.0f;
// //
// would ensure that cases like fabs(reference) > FLT_MAX are weeded out before we get here. // would ensure that cases like fabs(reference) > FLT_MAX are weeded out
// Otherwise, we'll return inf ulp error here, for what are otherwise correctly rounded // before we get here. Otherwise, we'll return inf ulp error here, for what
// results. // are otherwise correctly rounded results.
double testVal = test; double testVal = test;
@@ -348,14 +354,16 @@ static float Ulp_Error_Half_Float( float test, double reference )
return 0.0f; // if we are expecting a NaN, any NaN is fine return 0.0f; // if we are expecting a NaN, any NaN is fine
// The unbiased exponent of the ulp unit place // The unbiased exponent of the ulp unit place
int ulp_exp = HALF_MANT_DIG - 1 - MAX( ilogb( reference), HALF_MIN_EXP-1 ); int ulp_exp =
HALF_MANT_DIG - 1 - MAX(ilogb(reference), HALF_MIN_EXP - 1);
// Scale the exponent of the error // Scale the exponent of the error
return (float)scalbn(testVal - reference, ulp_exp); return (float)scalbn(testVal - reference, ulp_exp);
} }
// reference is a normal power of two or a zero // reference is a normal power of two or a zero
int ulp_exp = HALF_MANT_DIG - 1 - MAX( ilogb( reference) - 1, HALF_MIN_EXP-1 ); int ulp_exp =
HALF_MANT_DIG - 1 - MAX(ilogb(reference) - 1, HALF_MIN_EXP - 1);
// Scale the exponent of the error // Scale the exponent of the error
return (float)scalbn(testVal - reference, ulp_exp); return (float)scalbn(testVal - reference, ulp_exp);
@@ -369,49 +377,56 @@ float Ulp_Error_Half(cl_half test, float reference)
float Ulp_Error(float test, double reference) float Ulp_Error(float test, double reference)
{ {
union{ double d; uint64_t u; }u; u.d = reference; union {
double d;
uint64_t u;
} u;
u.d = reference;
double testVal = test; double testVal = test;
// Note: This function presumes that someone has already tested whether the result is correctly, // Note: This function presumes that someone has already tested whether the
// rounded before calling this function. That test: // result is correctly, rounded before calling this function. That test:
// //
// if( (float) reference == test ) // if( (float) reference == test )
// return 0.0f; // return 0.0f;
// //
// would ensure that cases like fabs(reference) > FLT_MAX are weeded out before we get here. // would ensure that cases like fabs(reference) > FLT_MAX are weeded out
// Otherwise, we'll return inf ulp error here, for what are otherwise correctly rounded // before we get here. Otherwise, we'll return inf ulp error here, for what
// results. // are otherwise correctly rounded results.
if (isinf(reference)) if (isinf(reference))
{ {
if( testVal == reference ) if (testVal == reference) return 0.0f;
return 0.0f;
return (float)(testVal - reference); return (float)(testVal - reference);
} }
if (isinf(testVal)) if (isinf(testVal))
{ // infinite test value, but finite (but possibly overflowing in float) reference. { // infinite test value, but finite (but possibly overflowing in float)
// reference.
// //
// The function probably overflowed prematurely here. Formally, the spec says this is // The function probably overflowed prematurely here. Formally, the spec
// an infinite ulp error and should not be tolerated. Unfortunately, this would mean // says this is an infinite ulp error and should not be tolerated.
// that the internal precision of some half_pow implementations would have to be 29+ bits // Unfortunately, this would mean that the internal precision of some
// at half_powr( 0x1.fffffep+31, 4) to correctly determine that 4*log2( 0x1.fffffep+31 ) // half_pow implementations would have to be 29+ bits at half_powr(
// is not exactly 128.0. You might represent this for example as 4*(32 - ~2**-24), which // 0x1.fffffep+31, 4) to correctly determine that 4*log2( 0x1.fffffep+31 )
// after rounding to single is 4*32 = 128, which will ultimately result in premature // is not exactly 128.0. You might represent this for example as 4*(32 -
// overflow, even though a good faith representation would be correct to within 2**-29 // ~2**-24), which after rounding to single is 4*32 = 128, which will
// interally. // ultimately result in premature overflow, even though a good faith
// representation would be correct to within 2**-29 interally.
// In the interest of not requiring the implementation go to extraordinary lengths to // In the interest of not requiring the implementation go to
// deliver a half precision function, we allow premature overflow within the limit // extraordinary lengths to deliver a half precision function, we allow
// of the allowed ulp error. Towards, that end, we "pretend" the test value is actually // premature overflow within the limit of the allowed ulp error.
// 2**128, the next value that would appear in the number line if float had sufficient range. // Towards, that end, we "pretend" the test value is actually 2**128,
// the next value that would appear in the number line if float had
// sufficient range.
testVal = copysign(MAKE_HEX_DOUBLE(0x1.0p128, 0x1LL, 128), testVal); testVal = copysign(MAKE_HEX_DOUBLE(0x1.0p128, 0x1LL, 128), testVal);
// Note that the same hack may not work in long double, which is not guaranteed to have // Note that the same hack may not work in long double, which is not
// more range than double. It is not clear that premature overflow should be tolerated for // guaranteed to have more range than double. It is not clear that
// double. // premature overflow should be tolerated for double.
} }
if (u.u & 0x000fffffffffffffULL) if (u.u & 0x000fffffffffffffULL)
@@ -437,37 +452,47 @@ float Ulp_Error( float test, double reference )
float Ulp_Error_Double(double test, long double reference) float Ulp_Error_Double(double test, long double reference)
{ {
// Deal with long double = double // Deal with long double = double
// On most systems long double is a higher precision type than double. They provide either // On most systems long double is a higher precision type than double. They
// a 80-bit or greater floating point type, or they provide a head-tail double double format. // provide either a 80-bit or greater floating point type, or they provide a
// That is sufficient to represent the accuracy of a floating point result to many more bits // head-tail double double format. That is sufficient to represent the
// than double and we can calculate sub-ulp errors. This is the standard system for which this // accuracy of a floating point result to many more bits than double and we
// can calculate sub-ulp errors. This is the standard system for which this
// test suite is designed. // test suite is designed.
// //
// On some systems double and long double are the same thing. Then we run into a problem, // On some systems double and long double are the same thing. Then we run
// because our representation of the infinitely precise result (passed in as reference above) // into a problem, because our representation of the infinitely precise
// can be off by as much as a half double precision ulp itself. In this case, we inflate the // result (passed in as reference above) can be off by as much as a half
// reported error by half an ulp to take this into account. A more correct and permanent fix // double precision ulp itself. In this case, we inflate the reported error
// would be to undertake refactoring the reference code to return results in this format: // by half an ulp to take this into account. A more correct and permanent
// fix would be to undertake refactoring the reference code to return
// results in this format:
// //
// typedef struct DoubleReference // typedef struct DoubleReference
// { // true value = correctlyRoundedResult + ulps * ulp(correctlyRoundedResult) (infinitely precise) // {
// double correctlyRoundedResult; // as best we can // // true value = correctlyRoundedResult + ulps *
// double ulps; // plus a fractional amount to account for the difference // // ulp(correctlyRoundedResult) (infinitely precise)
// }DoubleReference; // between infinitely precise result and correctlyRoundedResult, in units of ulps. // // as best we can:
// double correctlyRoundedResult;
// // plus a fractional amount to account for the difference
// // between infinitely precise result and correctlyRoundedResult,
// // in units of ulps:
// double ulps;
// } DoubleReference;
// //
// This would provide a useful higher-than-double precision format for everyone that we can use, // This would provide a useful higher-than-double precision format for
// and would solve a few problems with representing absolute errors below DBL_MIN and over DBL_MAX for systems // everyone that we can use, and would solve a few problems with
// representing absolute errors below DBL_MIN and over DBL_MAX for systems
// that use a head to tail double double for long double. // that use a head to tail double double for long double.
// Note: This function presumes that someone has already tested whether the result is correctly, // Note: This function presumes that someone has already tested whether the
// rounded before calling this function. That test: // result is correctly, rounded before calling this function. That test:
// //
// if( (float) reference == test ) // if( (float) reference == test )
// return 0.0f; // return 0.0f;
// //
// would ensure that cases like fabs(reference) > FLT_MAX are weeded out before we get here. // would ensure that cases like fabs(reference) > FLT_MAX are weeded out
// Otherwise, we'll return inf ulp error here, for what are otherwise correctly rounded // before we get here. Otherwise, we'll return inf ulp error here, for what
// results. // are otherwise correctly rounded results.
int x; int x;
@@ -476,8 +501,7 @@ float Ulp_Error_Double( double test, long double reference )
{ // Non-power of two and NaN { // Non-power of two and NaN
if (isinf(reference)) if (isinf(reference))
{ {
if( testVal == reference ) if (testVal == reference) return 0.0f;
return 0.0f;
return (float)(testVal - reference); return (float)(testVal - reference);
} }
@@ -486,212 +510,174 @@ float Ulp_Error_Double( double test, long double reference )
return 0.0f; // if we are expecting a NaN, any NaN is fine return 0.0f; // if we are expecting a NaN, any NaN is fine
// The unbiased exponent of the ulp unit place // The unbiased exponent of the ulp unit place
int ulp_exp = DBL_MANT_DIG - 1 - MAX( ilogbl( reference), DBL_MIN_EXP-1 ); int ulp_exp =
DBL_MANT_DIG - 1 - MAX(ilogbl(reference), DBL_MIN_EXP - 1);
// Scale the exponent of the error // Scale the exponent of the error
float result = (float)scalbnl(testVal - reference, ulp_exp); float result = (float)scalbnl(testVal - reference, ulp_exp);
// account for rounding error in reference result on systems that do not have a higher precision floating point type (see above) // account for rounding error in reference result on systems that do not
// have a higher precision floating point type (see above)
if (sizeof(long double) == sizeof(double)) if (sizeof(long double) == sizeof(double))
result += copysignf(0.5f, result); result += copysignf(0.5f, result);
return result; return result;
} }
// reference is a normal power of two or a zero // reference is a normal power of two or a zero
// The unbiased exponent of the ulp unit place // The unbiased exponent of the ulp unit place
int ulp_exp = DBL_MANT_DIG - 1 - MAX( ilogbl( reference) - 1, DBL_MIN_EXP-1 ); int ulp_exp =
DBL_MANT_DIG - 1 - MAX(ilogbl(reference) - 1, DBL_MIN_EXP - 1);
// Scale the exponent of the error // Scale the exponent of the error
float result = (float)scalbnl(testVal - reference, ulp_exp); float result = (float)scalbnl(testVal - reference, ulp_exp);
// account for rounding error in reference result on systems that do not have a higher precision floating point type (see above) // account for rounding error in reference result on systems that do not
// have a higher precision floating point type (see above)
if (sizeof(long double) == sizeof(double)) if (sizeof(long double) == sizeof(double))
result += copysignf(0.5f, result); result += copysignf(0.5f, result);
return result; return result;
} }
cl_int OutputBuildLogs(cl_program program, cl_uint num_devices, cl_device_id *device_list) cl_int OutputBuildLogs(cl_program program, cl_uint num_devices,
cl_device_id *device_list)
{ {
int error; int error;
size_t size_ret; size_t size_ret;
// Does the program object exist? // Does the program object exist?
if (program != NULL) { if (program != NULL)
{
// Was the number of devices given // Was the number of devices given
if (num_devices == 0) { if (num_devices == 0)
{
// If zero devices were specified then allocate and query the device list from the context // If zero devices were specified then allocate and query the device
// list from the context
cl_context context; cl_context context;
error = clGetProgramInfo(program, CL_PROGRAM_CONTEXT, sizeof(context), &context, NULL); error = clGetProgramInfo(program, CL_PROGRAM_CONTEXT,
sizeof(context), &context, NULL);
test_error(error, "Unable to query program's context"); test_error(error, "Unable to query program's context");
error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &size_ret); error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL,
&size_ret);
test_error(error, "Unable to query context's device size"); test_error(error, "Unable to query context's device size");
num_devices = size_ret / sizeof(cl_device_id); num_devices = size_ret / sizeof(cl_device_id);
device_list = (cl_device_id *)malloc(size_ret); device_list = (cl_device_id *)malloc(size_ret);
if (device_list == NULL) { if (device_list == NULL)
{
print_error(error, "malloc failed"); print_error(error, "malloc failed");
return CL_OUT_OF_HOST_MEMORY; return CL_OUT_OF_HOST_MEMORY;
} }
error = clGetContextInfo(context, CL_CONTEXT_DEVICES, size_ret, device_list, NULL); error = clGetContextInfo(context, CL_CONTEXT_DEVICES, size_ret,
device_list, NULL);
test_error(error, "Unable to query context's devices"); test_error(error, "Unable to query context's devices");
} }
// For each device in the device_list // For each device in the device_list
unsigned int i; unsigned int i;
for (i = 0; i < num_devices; i++) { for (i = 0; i < num_devices; i++)
{
// Get the build status // Get the build status
cl_build_status build_status; cl_build_status build_status;
error = clGetProgramBuildInfo(program, error = clGetProgramBuildInfo(
device_list[i], program, device_list[i], CL_PROGRAM_BUILD_STATUS,
CL_PROGRAM_BUILD_STATUS, sizeof(build_status), &build_status, &size_ret);
sizeof(build_status),
&build_status,
&size_ret);
test_error(error, "Unable to query build status"); test_error(error, "Unable to query build status");
// If the build failed then log the status, and allocate the build log, log it and free it // If the build failed then log the status, and allocate the build
if (build_status != CL_BUILD_SUCCESS) { // log, log it and free it
if (build_status != CL_BUILD_SUCCESS)
{
log_error("ERROR: CL_PROGRAM_BUILD_STATUS=%d\n", (int) build_status); log_error("ERROR: CL_PROGRAM_BUILD_STATUS=%d\n",
error = clGetProgramBuildInfo(program, device_list[i], CL_PROGRAM_BUILD_LOG, 0, NULL, &size_ret); (int)build_status);
error = clGetProgramBuildInfo(program, device_list[i],
CL_PROGRAM_BUILD_LOG, 0, NULL,
&size_ret);
test_error(error, "Unable to query build log size"); test_error(error, "Unable to query build log size");
char *build_log = (char *)malloc(size_ret); char *build_log = (char *)malloc(size_ret);
error = clGetProgramBuildInfo(program, device_list[i], CL_PROGRAM_BUILD_LOG, size_ret, build_log, &size_ret); error = clGetProgramBuildInfo(program, device_list[i],
CL_PROGRAM_BUILD_LOG, size_ret,
build_log, &size_ret);
test_error(error, "Unable to query build log"); test_error(error, "Unable to query build log");
log_error("ERROR: CL_PROGRAM_BUILD_LOG:\n%s\n", build_log); log_error("ERROR: CL_PROGRAM_BUILD_LOG:\n%s\n", build_log);
free(build_log); free(build_log);
} }
} }
// Was the number of devices given // Was the number of devices given
if (num_devices == 0) { if (num_devices == 0)
{
// If zero devices were specified then free the device list // If zero devices were specified then free the device list
free(device_list); free(device_list);
} }
} }
return CL_SUCCESS; return CL_SUCCESS;
} }
const char *subtests_requiring_opencl_1_2[] = { const char *subtests_requiring_opencl_1_2[] = {
"device_partition_equally", "device_partition_equally", "device_partition_by_counts",
"device_partition_by_counts",
"device_partition_by_affinity_domain_numa", "device_partition_by_affinity_domain_numa",
"device_partition_by_affinity_domain_l4_cache", "device_partition_by_affinity_domain_l4_cache",
"device_partition_by_affinity_domain_l3_cache", "device_partition_by_affinity_domain_l3_cache",
"device_partition_by_affinity_domain_l2_cache", "device_partition_by_affinity_domain_l2_cache",
"device_partition_by_affinity_domain_l1_cache", "device_partition_by_affinity_domain_l1_cache",
"device_partition_by_affinity_domain_next_partitionable", "device_partition_by_affinity_domain_next_partitionable",
"device_partition_all", "device_partition_all", "buffer_fill_int", "buffer_fill_uint",
"buffer_fill_int", "buffer_fill_short", "buffer_fill_ushort", "buffer_fill_char",
"buffer_fill_uint", "buffer_fill_uchar", "buffer_fill_long", "buffer_fill_ulong",
"buffer_fill_short", "buffer_fill_float", "buffer_fill_struct",
"buffer_fill_ushort", "test_mem_host_write_only_buffer", "test_mem_host_write_only_subbuffer",
"buffer_fill_char", "test_mem_host_no_access_buffer", "test_mem_host_no_access_subbuffer",
"buffer_fill_uchar", "test_mem_host_read_only_image", "test_mem_host_write_only_image",
"buffer_fill_long",
"buffer_fill_ulong",
"buffer_fill_float",
"buffer_fill_struct",
"test_mem_host_write_only_buffer",
"test_mem_host_write_only_subbuffer",
"test_mem_host_no_access_buffer",
"test_mem_host_no_access_subbuffer",
"test_mem_host_read_only_image",
"test_mem_host_write_only_image",
"test_mem_host_no_access_image", "test_mem_host_no_access_image",
// CL_MEM_HOST_{READ|WRITE}_ONLY api/ // CL_MEM_HOST_{READ|WRITE}_ONLY api/
"get_buffer_info", "get_buffer_info", "get_image1d_info", "get_image1d_array_info",
"get_image1d_info",
"get_image1d_array_info",
"get_image2d_array_info", "get_image2d_array_info",
// gl/ // gl/
"images_read_1D", "images_read_1D", "images_write_1D", "images_1D_getinfo",
"images_write_1D", "images_read_1Darray", "images_write_1Darray", "images_1Darray_getinfo",
"images_1D_getinfo", "images_read_2Darray", "images_write_2Darray", "images_2Darray_getinfo",
"images_read_1Darray", "buffer_migrate", "image_migrate",
"images_write_1Darray",
"images_1Darray_getinfo",
"images_read_2Darray",
"images_write_2Darray",
"images_2Darray_getinfo",
"buffer_migrate",
"image_migrate",
// compiler/ // compiler/
"load_program_source", "load_program_source", "load_multistring_source", "load_two_kernel_source",
"load_multistring_source", "load_null_terminated_source", "load_null_terminated_multi_line_source",
"load_two_kernel_source",
"load_null_terminated_source",
"load_null_terminated_multi_line_source",
"load_null_terminated_partial_multi_line_source", "load_null_terminated_partial_multi_line_source",
"load_discreet_length_source", "load_discreet_length_source", "get_program_source",
"get_program_source", "get_program_build_info", "get_program_info", "large_compile",
"get_program_build_info", "async_build", "options_build_optimizations", "options_build_macro",
"get_program_info", "options_build_macro_existence", "options_include_directory",
"large_compile", "options_denorm_cache", "preprocessor_define_udef", "preprocessor_include",
"async_build", "preprocessor_line_error", "preprocessor_pragma",
"options_build_optimizations", "compiler_defines_for_extensions", "image_macro", "simple_compile_only",
"options_build_macro", "simple_static_compile_only", "simple_extern_compile_only",
"options_build_macro_existence", "simple_compile_with_callback", "simple_embedded_header_compile",
"options_include_directory", "simple_link_only", "two_file_regular_variable_access",
"options_denorm_cache", "two_file_regular_struct_access", "two_file_regular_function_access",
"preprocessor_define_udef", "simple_link_with_callback", "simple_embedded_header_link",
"preprocessor_include",
"preprocessor_line_error",
"preprocessor_pragma",
"compiler_defines_for_extensions",
"image_macro",
"simple_compile_only",
"simple_static_compile_only",
"simple_extern_compile_only",
"simple_compile_with_callback",
"simple_embedded_header_compile",
"simple_link_only",
"two_file_regular_variable_access",
"two_file_regular_struct_access",
"two_file_regular_function_access",
"simple_link_with_callback",
"simple_embedded_header_link",
"execute_after_simple_compile_and_link", "execute_after_simple_compile_and_link",
"execute_after_simple_compile_and_link_no_device_info", "execute_after_simple_compile_and_link_no_device_info",
"execute_after_simple_compile_and_link_with_defines", "execute_after_simple_compile_and_link_with_defines",
"execute_after_simple_compile_and_link_with_callbacks", "execute_after_simple_compile_and_link_with_callbacks",
"execute_after_simple_library_with_link", "execute_after_simple_library_with_link", "execute_after_two_file_link",
"execute_after_two_file_link", "execute_after_two_file_link", "execute_after_embedded_header_link",
"execute_after_two_file_link",
"execute_after_embedded_header_link",
"execute_after_included_header_link", "execute_after_included_header_link",
"execute_after_serialize_reload_object", "execute_after_serialize_reload_object",
"execute_after_serialize_reload_library", "execute_after_serialize_reload_library", "simple_library_only",
"simple_library_only", "simple_library_with_callback", "simple_library_with_link", "two_file_link",
"simple_library_with_callback", "multi_file_libraries", "multiple_files", "multiple_libraries",
"simple_library_with_link", "multiple_files_multiple_libraries", "multiple_embedded_headers",
"two_file_link", "program_binary_type", "compile_and_link_status_options_log",
"multi_file_libraries",
"multiple_files",
"multiple_libraries",
"multiple_files_multiple_libraries",
"multiple_embedded_headers",
"program_binary_type",
"compile_and_link_status_options_log",
// CL_PROGRAM_NUM_KERNELS, in api/ // CL_PROGRAM_NUM_KERNELS, in api/
"get_kernel_arg_info", "get_kernel_arg_info", "create_kernels_in_program",
"create_kernels_in_program",
// clEnqueue..WithWaitList, in events/ // clEnqueue..WithWaitList, in events/
"event_enqueue_marker_with_event_list", "event_enqueue_marker_with_event_list",
"event_enqueue_barrier_with_event_list", "event_enqueue_barrier_with_event_list", "popcount"
"popcount"
}; };
const char *subtests_to_skip_with_offline_compiler[] = { const char *subtests_to_skip_with_offline_compiler[] = {
@@ -754,14 +740,18 @@ const char *subtests_to_skip_with_offline_compiler[] = {
"async_build", "async_build",
}; };
int check_functions_for_offline_compiler(const char *subtestname, cl_device_id device) int check_functions_for_offline_compiler(const char *subtestname,
cl_device_id device)
{ {
if (gCompilationMode != kOnline) if (gCompilationMode != kOnline)
{ {
int nNotRequiredWithOfflineCompiler = sizeof(subtests_to_skip_with_offline_compiler)/sizeof(char *); int nNotRequiredWithOfflineCompiler =
sizeof(subtests_to_skip_with_offline_compiler) / sizeof(char *);
size_t i; size_t i;
for(i=0; i < nNotRequiredWithOfflineCompiler; ++i) { for (i = 0; i < nNotRequiredWithOfflineCompiler; ++i)
if(!strcmp(subtestname, subtests_to_skip_with_offline_compiler[i])) { {
if (!strcmp(subtestname, subtests_to_skip_with_offline_compiler[i]))
{
return 1; return 1;
} }
} }

View File

@@ -32,10 +32,14 @@
#define log_info printf #define log_info printf
#define log_error printf #define log_error printf
#define log_missing_feature printf #define log_missing_feature printf
#define log_perf(_number, _higherBetter, _numType, _format, ...) printf("Performance Number " _format " (in %s, %s): %g\n",##__VA_ARGS__, _numType, \ #define log_perf(_number, _higherBetter, _numType, _format, ...) \
_higherBetter?"higher is better":"lower is better", _number ) printf("Performance Number " _format " (in %s, %s): %g\n", ##__VA_ARGS__, \
#define vlog_perf(_number, _higherBetter, _numType, _format, ...) printf("Performance Number " _format " (in %s, %s): %g\n",##__VA_ARGS__, _numType, \ _numType, _higherBetter ? "higher is better" : "lower is better", \
_higherBetter?"higher is better":"lower is better" , _number) _number)
#define vlog_perf(_number, _higherBetter, _numType, _format, ...) \
printf("Performance Number " _format " (in %s, %s): %g\n", ##__VA_ARGS__, \
_numType, _higherBetter ? "higher is better" : "lower is better", \
_number)
#ifdef _WIN32 #ifdef _WIN32
#ifdef __MINGW32__ #ifdef __MINGW32__
// Use __mingw_printf since it supports "%a" format specifier // Use __mingw_printf since it supports "%a" format specifier
@@ -54,7 +58,8 @@ static int vlog_win32(const char *format, ...);
#define ct_assert(b) ct_assert_i(b, __LINE__) #define ct_assert(b) ct_assert_i(b, __LINE__)
#define ct_assert_i(b, line) ct_assert_ii(b, line) #define ct_assert_i(b, line) ct_assert_ii(b, line)
#define ct_assert_ii(b, line) int _compile_time_assertion_on_line_##line[b ? 1 : -1]; #define ct_assert_ii(b, line) \
int _compile_time_assertion_on_line_##line[b ? 1 : -1];
#define test_fail(msg, ...) \ #define test_fail(msg, ...) \
{ \ { \
@@ -71,26 +76,73 @@ static int vlog_win32(const char *format, ...);
return retValue; \ return retValue; \
} \ } \
} }
#define print_error(errCode,msg) log_error( "ERROR: %s! (%s from %s:%d)\n", msg, IGetErrorString( errCode ), __FILE__, __LINE__ ); #define print_error(errCode, msg) \
log_error("ERROR: %s! (%s from %s:%d)\n", msg, IGetErrorString(errCode), \
__FILE__, __LINE__);
#define test_missing_feature(errCode, msg) test_missing_feature_ret(errCode, msg, errCode) #define test_missing_feature(errCode, msg) \
test_missing_feature_ret(errCode, msg, errCode)
// this macro should always return CL_SUCCESS, but print the missing feature // this macro should always return CL_SUCCESS, but print the missing feature
// message // message
#define test_missing_feature_ret(errCode,msg,retValue) { if( errCode != CL_SUCCESS ) { print_missing_feature( errCode, msg ); return CL_SUCCESS ; } } #define test_missing_feature_ret(errCode, msg, retValue) \
#define print_missing_feature(errCode, msg) log_missing_feature("ERROR: Subtest %s tests a feature not supported by the device version! (from %s:%d)\n", msg, __FILE__, __LINE__ ); { \
if (errCode != CL_SUCCESS) \
{ \
print_missing_feature(errCode, msg); \
return CL_SUCCESS; \
} \
}
#define print_missing_feature(errCode, msg) \
log_missing_feature("ERROR: Subtest %s tests a feature not supported by " \
"the device version! (from %s:%d)\n", \
msg, __FILE__, __LINE__);
#define test_missing_support_offline_cmpiler(errCode, msg) test_missing_support_offline_cmpiler_ret(errCode, msg, errCode) #define test_missing_support_offline_cmpiler(errCode, msg) \
test_missing_support_offline_cmpiler_ret(errCode, msg, errCode)
// this macro should always return CL_SUCCESS, but print the skip message on // this macro should always return CL_SUCCESS, but print the skip message on
// test not supported with offline compiler // test not supported with offline compiler
#define test_missing_support_offline_cmpiler_ret(errCode,msg,retValue) { if( errCode != CL_SUCCESS ) { log_info( "INFO: Subtest %s tests is not supported in offline compiler execution path! (from %s:%d)\n", msg, __FILE__, __LINE__ ); return TEST_SKIP ; } } #define test_missing_support_offline_cmpiler_ret(errCode, msg, retValue) \
{ \
if (errCode != CL_SUCCESS) \
{ \
log_info("INFO: Subtest %s tests is not supported in offline " \
"compiler execution path! (from %s:%d)\n", \
msg, __FILE__, __LINE__); \
return TEST_SKIP; \
} \
}
// expected error code vs. what we got // expected error code vs. what we got
#define test_failure_error(errCode, expectedErrCode, msg) test_failure_error_ret(errCode, expectedErrCode, msg, errCode != expectedErrCode) #define test_failure_error(errCode, expectedErrCode, msg) \
#define test_failure_error_ret(errCode, expectedErrCode, msg, retValue) { if( errCode != expectedErrCode ) { print_failure_error( errCode, expectedErrCode, msg ); return retValue ; } } test_failure_error_ret(errCode, expectedErrCode, msg, \
#define print_failure_error(errCode, expectedErrCode, msg) log_error( "ERROR: %s! (Got %s, expected %s from %s:%d)\n", msg, IGetErrorString( errCode ), IGetErrorString( expectedErrCode ), __FILE__, __LINE__ ); errCode != expectedErrCode)
#define test_failure_warning(errCode, expectedErrCode, msg) test_failure_warning_ret(errCode, expectedErrCode, msg, errCode != expectedErrCode) #define test_failure_error_ret(errCode, expectedErrCode, msg, retValue) \
#define test_failure_warning_ret(errCode, expectedErrCode, msg, retValue) { if( errCode != expectedErrCode ) { print_failure_warning( errCode, expectedErrCode, msg ); warnings++ ; } } { \
#define print_failure_warning(errCode, expectedErrCode, msg) log_error( "WARNING: %s! (Got %s, expected %s from %s:%d)\n", msg, IGetErrorString( errCode ), IGetErrorString( expectedErrCode ), __FILE__, __LINE__ ); if (errCode != expectedErrCode) \
{ \
print_failure_error(errCode, expectedErrCode, msg); \
return retValue; \
} \
}
#define print_failure_error(errCode, expectedErrCode, msg) \
log_error("ERROR: %s! (Got %s, expected %s from %s:%d)\n", msg, \
IGetErrorString(errCode), IGetErrorString(expectedErrCode), \
__FILE__, __LINE__);
#define test_failure_warning(errCode, expectedErrCode, msg) \
test_failure_warning_ret(errCode, expectedErrCode, msg, \
errCode != expectedErrCode)
#define test_failure_warning_ret(errCode, expectedErrCode, msg, retValue) \
{ \
if (errCode != expectedErrCode) \
{ \
print_failure_warning(errCode, expectedErrCode, msg); \
warnings++; \
} \
}
#define print_failure_warning(errCode, expectedErrCode, msg) \
log_error("WARNING: %s! (Got %s, expected %s from %s:%d)\n", msg, \
IGetErrorString(errCode), IGetErrorString(expectedErrCode), \
__FILE__, __LINE__);
// generate an error when an assertion is false (not error code related) // generate an error when an assertion is false (not error code related)
#define test_assert_error(condition, msg) \ #define test_assert_error(condition, msg) \
@@ -134,11 +186,15 @@ extern const char *GetAddressModeName( cl_addressing_mode mode );
extern const char *GetQueuePropertyName(cl_command_queue_properties properties); extern const char *GetQueuePropertyName(cl_command_queue_properties properties);
extern const char *GetDeviceTypeName(cl_device_type type); extern const char *GetDeviceTypeName(cl_device_type type);
int check_functions_for_offline_compiler(const char *subtestname, cl_device_id device); int check_functions_for_offline_compiler(const char *subtestname,
cl_device_id device);
cl_int OutputBuildLogs(cl_program program, cl_uint num_devices, cl_int OutputBuildLogs(cl_program program, cl_uint num_devices,
cl_device_id *device_list); cl_device_id *device_list);
// NON-REENTRANT UNLESS YOU PROVIDE A BUFFER PTR (pass null to use static storage, but it's not reentrant then!)
extern const char *GetDataVectorString( void *dataBuffer, size_t typeSize, size_t vecSize, char *buffer ); // NON-REENTRANT UNLESS YOU PROVIDE A BUFFER PTR (pass null to use static
// storage, but it's not reentrant then!)
extern const char *GetDataVectorString(void *dataBuffer, size_t typeSize,
size_t vecSize, char *buffer);
#if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && !defined(__MINGW32__)
#include <stdarg.h> #include <stdarg.h>
@@ -148,16 +204,20 @@ static int vlog_win32(const char *format, ...)
{ {
const char *new_format = format; const char *new_format = format;
if (strstr(format, "%a")) { if (strstr(format, "%a"))
{
char *temp; char *temp;
if ((temp = strdup(format)) == NULL) { if ((temp = strdup(format)) == NULL)
{
printf("vlog_win32: Failed to allocate memory for strdup\n"); printf("vlog_win32: Failed to allocate memory for strdup\n");
return -1; return -1;
} }
new_format = temp; new_format = temp;
while (*temp) { while (*temp)
{
// replace %a with %f // replace %a with %f
if ((*temp == '%') && (*(temp+1) == 'a')) { if ((*temp == '%') && (*(temp + 1) == 'a'))
{
*(temp + 1) = 'f'; *(temp + 1) = 'f';
} }
temp++; temp++;
@@ -169,7 +229,8 @@ static int vlog_win32(const char *format, ...)
vprintf(new_format, args); vprintf(new_format, args);
va_end(args); va_end(args);
if (new_format != format) { if (new_format != format)
{
free((void *)new_format); free((void *)new_format);
} }
@@ -179,5 +240,3 @@ static int vlog_win32(const char *format, ...)
#endif // _errorHelpers_h #endif // _errorHelpers_h

View File

@@ -16,16 +16,23 @@
#ifndef _fpcontrol_h #ifndef _fpcontrol_h
#define _fpcontrol_h #define _fpcontrol_h
// In order to get tests for correctly rounded operations (e.g. multiply) to work properly we need to be able to set the reference hardware // In order to get tests for correctly rounded operations (e.g. multiply) to
// to FTZ mode if the device hardware is running in that mode. We have explored all other options short of writing correctly rounded operations // work properly we need to be able to set the reference hardware to FTZ mode if
// in integer code, and have found this is the only way to correctly verify operation. // the device hardware is running in that mode. We have explored all other
// options short of writing correctly rounded operations in integer code, and
// have found this is the only way to correctly verify operation.
// //
// Non-Apple implementations will need to provide their own implentation for these features. If the reference hardware and device are both // Non-Apple implementations will need to provide their own implentation for
// running in the same state (either FTZ or IEEE compliant modes) then these functions may be empty. If the device is running in non-default // these features. If the reference hardware and device are both running in the
// rounding mode (e.g. round toward zero), then these functions should also set the reference device into that rounding mode. // same state (either FTZ or IEEE compliant modes) then these functions may be
#if defined( __APPLE__ ) || defined( _MSC_VER ) || defined( __linux__ ) || defined (__MINGW32__) // empty. If the device is running in non-default rounding mode (e.g. round
// toward zero), then these functions should also set the reference device into
// that rounding mode.
#if defined(__APPLE__) || defined(_MSC_VER) || defined(__linux__) \
|| defined(__MINGW32__)
typedef int FPU_mode_type; typedef int FPU_mode_type;
#if defined( __i386__ ) || defined( __x86_64__ ) || defined( _MSC_VER ) || defined( __MINGW32__ ) #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER) \
|| defined(__MINGW32__)
#include <xmmintrin.h> #include <xmmintrin.h>
#elif defined(__PPC__) #elif defined(__PPC__)
#include <fpu_control.h> #include <fpu_control.h>
@@ -34,7 +41,8 @@
// Set the reference hardware floating point unit to FTZ mode // Set the reference hardware floating point unit to FTZ mode
static inline void ForceFTZ(FPU_mode_type *mode) static inline void ForceFTZ(FPU_mode_type *mode)
{ {
#if defined( __i386__ ) || defined( __x86_64__ ) || defined( _MSC_VER ) || defined (__MINGW32__) #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER) \
|| defined(__MINGW32__)
*mode = _mm_getcsr(); *mode = _mm_getcsr();
_mm_setcsr(*mode | 0x8040); _mm_setcsr(*mode | 0x8040);
#elif defined(__PPC__) #elif defined(__PPC__)
@@ -59,7 +67,8 @@
// Disable the denorm flush to zero // Disable the denorm flush to zero
static inline void DisableFTZ(FPU_mode_type *mode) static inline void DisableFTZ(FPU_mode_type *mode)
{ {
#if defined( __i386__ ) || defined( __x86_64__ ) || defined( _MSC_VER ) || defined (__MINGW32__) #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER) \
|| defined(__MINGW32__)
*mode = _mm_getcsr(); *mode = _mm_getcsr();
_mm_setcsr(*mode & ~0x8040); _mm_setcsr(*mode & ~0x8040);
#elif defined(__PPC__) #elif defined(__PPC__)
@@ -84,7 +93,8 @@
// Restore the reference hardware to floating point state indicated by *mode // Restore the reference hardware to floating point state indicated by *mode
static inline void RestoreFPState(FPU_mode_type *mode) static inline void RestoreFPState(FPU_mode_type *mode)
{ {
#if defined( __i386__ ) || defined( __x86_64__ ) || defined( _MSC_VER ) || defined (__MINGW32__) #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER) \
|| defined(__MINGW32__)
_mm_setcsr(*mode); _mm_setcsr(*mode);
#elif defined(__PPC__) #elif defined(__PPC__)
fpu_control = *mode; fpu_control = *mode;

View File

@@ -30,10 +30,12 @@ void * genericThread::IStaticReflector( void * data )
bool genericThread::Start(void) bool genericThread::Start(void)
{ {
#if defined(_WIN32) #if defined(_WIN32)
mHandle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) IStaticReflector, this, 0, NULL ); mHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)IStaticReflector,
this, 0, NULL);
return (mHandle != NULL); return (mHandle != NULL);
#else // !_WIN32 #else // !_WIN32
int error = pthread_create( (pthread_t*)&mHandle, NULL, IStaticReflector, (void *)this ); int error = pthread_create((pthread_t *)&mHandle, NULL, IStaticReflector,
(void *)this);
return (error == 0); return (error == 0);
#endif // !_WIN32 #endif // !_WIN32
} }
@@ -46,8 +48,7 @@ void * genericThread::Join( void )
#else // !_WIN32 #else // !_WIN32
void *retVal; void *retVal;
int error = pthread_join((pthread_t)mHandle, &retVal); int error = pthread_join((pthread_t)mHandle, &retVal);
if( error != 0 ) if (error != 0) retVal = NULL;
retVal = NULL;
return retVal; return retVal;
#endif // !_WIN32 #endif // !_WIN32
} }

View File

@@ -18,25 +18,20 @@
#include <stdio.h> #include <stdio.h>
class genericThread class genericThread {
{
public: public:
virtual ~genericThread() {} virtual ~genericThread() {}
bool Start(void); bool Start(void);
void* Join(void); void* Join(void);
protected: protected:
virtual void* IRun(void) = 0; virtual void* IRun(void) = 0;
private: private:
void* mHandle; void* mHandle;
static void* IStaticReflector(void* data); static void* IStaticReflector(void* data);
}; };
#endif // _genericThread_h #endif // _genericThread_h

File diff suppressed because it is too large Load Diff

View File

@@ -46,7 +46,8 @@
extern cl_device_type gDeviceType; extern cl_device_type gDeviceType;
extern bool gTestRounding; extern bool gTestRounding;
// Number of iterations per image format to test if not testing max images, rounding, or small images // Number of iterations per image format to test if not testing max images,
// rounding, or small images
#define NUM_IMAGE_ITERATIONS 3 #define NUM_IMAGE_ITERATIONS 3
@@ -55,7 +56,8 @@ extern bool gTestRounding;
#define MAX_lRGB_TO_sRGB_CONVERSION_ERROR 0.6 #define MAX_lRGB_TO_sRGB_CONVERSION_ERROR 0.6
// Definition for our own sampler type, to mirror the cl_sampler internals // Definition for our own sampler type, to mirror the cl_sampler internals
typedef struct { typedef struct
{
cl_addressing_mode addressing_mode; cl_addressing_mode addressing_mode;
cl_filter_mode filter_mode; cl_filter_mode filter_mode;
bool normalized_coords; bool normalized_coords;
@@ -65,21 +67,27 @@ int round_to_even( float v );
#define NORMALIZE(v, max) (v < 0 ? 0 : (v > 1.f ? max : round_to_even(v * max))) #define NORMALIZE(v, max) (v < 0 ? 0 : (v > 1.f ? max : round_to_even(v * max)))
#define NORMALIZE_UNROUNDED(v, max) (v < 0 ? 0 : (v > 1.f ? max : v * max)) #define NORMALIZE_UNROUNDED(v, max) (v < 0 ? 0 : (v > 1.f ? max : v * max))
#define NORMALIZE_SIGNED( v, min, max ) ( v < -1.0f ? min : ( v > 1.f ? max : round_to_even( v * max ) ) ) #define NORMALIZE_SIGNED(v, min, max) \
#define NORMALIZE_SIGNED_UNROUNDED( v, min, max ) ( v < -1.0f ? min : ( v > 1.f ? max : v * max ) ) (v < -1.0f ? min : (v > 1.f ? max : round_to_even(v * max)))
#define CONVERT_INT( v, min, max, max_val) ( v < min ? min : ( v > max ? max_val : round_to_even( v ) ) ) #define NORMALIZE_SIGNED_UNROUNDED(v, min, max) \
#define CONVERT_UINT( v, max, max_val) ( v < 0 ? 0 : ( v > max ? max_val : round_to_even( v ) ) ) (v < -1.0f ? min : (v > 1.f ? max : v * max))
#define CONVERT_INT(v, min, max, max_val) \
(v < min ? min : (v > max ? max_val : round_to_even(v)))
#define CONVERT_UINT(v, max, max_val) \
(v < 0 ? 0 : (v > max ? max_val : round_to_even(v)))
extern void print_read_header( cl_image_format *format, image_sampler_data *sampler, bool err = false, int t = 0 ); extern void print_read_header(cl_image_format *format,
image_sampler_data *sampler, bool err = false,
int t = 0);
extern void print_write_header(cl_image_format *format, bool err); extern void print_write_header(cl_image_format *format, bool err);
extern void print_header(cl_image_format *format, bool err); extern void print_header(cl_image_format *format, bool err);
extern bool find_format( cl_image_format *formatList, unsigned int numFormats, cl_image_format *formatToFind ); extern bool find_format(cl_image_format *formatList, unsigned int numFormats,
extern bool is_image_format_required(cl_image_format format, cl_image_format *formatToFind);
cl_mem_flags flags, extern bool is_image_format_required(cl_image_format format, cl_mem_flags flags,
cl_mem_object_type image_type, cl_mem_object_type image_type,
cl_device_id device); cl_device_id device);
extern void build_required_image_formats(cl_mem_flags flags, extern void
cl_mem_object_type image_type, build_required_image_formats(cl_mem_flags flags, cl_mem_object_type image_type,
cl_device_id device, cl_device_id device,
std::vector<cl_image_format> &formatsToSupport); std::vector<cl_image_format> &formatsToSupport);
@@ -93,10 +101,16 @@ extern int is_format_signed( const cl_image_format *format );
extern uint32_t get_pixel_size(cl_image_format *format); extern uint32_t get_pixel_size(cl_image_format *format);
/* Helper to get any ol image format as long as it is 8-bits-per-channel */ /* Helper to get any ol image format as long as it is 8-bits-per-channel */
extern int get_8_bit_image_format( cl_context context, cl_mem_object_type objType, cl_mem_flags flags, size_t channelCount, cl_image_format *outFormat ); extern int get_8_bit_image_format(cl_context context,
cl_mem_object_type objType,
cl_mem_flags flags, size_t channelCount,
cl_image_format *outFormat);
/* Helper to get any ol image format as long as it is 32-bits-per-channel */ /* Helper to get any ol image format as long as it is 32-bits-per-channel */
extern int get_32_bit_image_format( cl_context context, cl_mem_object_type objType, cl_mem_flags flags, size_t channelCount, cl_image_format *outFormat ); extern int get_32_bit_image_format(cl_context context,
cl_mem_object_type objType,
cl_mem_flags flags, size_t channelCount,
cl_image_format *outFormat);
int random_in_range(int minV, int maxV, MTdata d); int random_in_range(int minV, int maxV, MTdata d);
int random_log_in_range(int minV, int maxV, MTdata d); int random_log_in_range(int minV, int maxV, MTdata d);
@@ -121,27 +135,43 @@ typedef struct
} FloatPixel; } FloatPixel;
void get_max_sizes(size_t *numberOfSizes, const int maxNumberOfSizes, void get_max_sizes(size_t *numberOfSizes, const int maxNumberOfSizes,
size_t sizes[][3], size_t maxWidth, size_t maxHeight, size_t maxDepth, size_t maxArraySize, size_t sizes[][3], size_t maxWidth, size_t maxHeight,
const cl_ulong maxIndividualAllocSize, const cl_ulong maxTotalAllocSize, cl_mem_object_type image_type, cl_image_format *format, int usingMaxPixelSize=0); size_t maxDepth, size_t maxArraySize,
const cl_ulong maxIndividualAllocSize,
const cl_ulong maxTotalAllocSize,
cl_mem_object_type image_type, cl_image_format *format,
int usingMaxPixelSize = 0);
extern size_t get_format_max_int(cl_image_format *format); extern size_t get_format_max_int(cl_image_format *format);
extern cl_ulong get_image_size(image_descriptor const *imageInfo); extern cl_ulong get_image_size(image_descriptor const *imageInfo);
extern cl_ulong get_image_size_mb(image_descriptor const *imageInfo); extern cl_ulong get_image_size_mb(image_descriptor const *imageInfo);
extern char * generate_random_image_data( image_descriptor *imageInfo, BufferOwningPtr<char> &Owner, MTdata d ); extern char *generate_random_image_data(image_descriptor *imageInfo,
BufferOwningPtr<char> &Owner, MTdata d);
extern int debug_find_vector_in_image( void *imagePtr, image_descriptor *imageInfo, extern int debug_find_vector_in_image(void *imagePtr,
void *vectorToFind, size_t vectorSize, int *outX, int *outY, int *outZ, size_t lod = 0 ); image_descriptor *imageInfo,
void *vectorToFind, size_t vectorSize,
int *outX, int *outY, int *outZ,
size_t lod = 0);
extern int debug_find_pixel_in_image( void *imagePtr, image_descriptor *imageInfo, extern int debug_find_pixel_in_image(void *imagePtr,
unsigned int *valuesToFind, int *outX, int *outY, int *outZ, int lod = 0 ); image_descriptor *imageInfo,
extern int debug_find_pixel_in_image( void *imagePtr, image_descriptor *imageInfo, unsigned int *valuesToFind, int *outX,
int *valuesToFind, int *outX, int *outY, int *outZ, int lod = 0 ); int *outY, int *outZ, int lod = 0);
extern int debug_find_pixel_in_image( void *imagePtr, image_descriptor *imageInfo, extern int debug_find_pixel_in_image(void *imagePtr,
float *valuesToFind, int *outX, int *outY, int *outZ, int lod = 0 ); image_descriptor *imageInfo,
int *valuesToFind, int *outX, int *outY,
int *outZ, int lod = 0);
extern int debug_find_pixel_in_image(void *imagePtr,
image_descriptor *imageInfo,
float *valuesToFind, int *outX, int *outY,
int *outZ, int lod = 0);
extern void copy_image_data( image_descriptor *srcImageInfo, image_descriptor *dstImageInfo, void *imageValues, void *destImageValues, extern void copy_image_data(image_descriptor *srcImageInfo,
const size_t sourcePos[], const size_t destPos[], const size_t regionSize[] ); image_descriptor *dstImageInfo, void *imageValues,
void *destImageValues, const size_t sourcePos[],
const size_t destPos[], const size_t regionSize[]);
int has_alpha(cl_image_format *format); int has_alpha(cl_image_format *format);
@@ -153,46 +183,59 @@ cl_uint compute_max_mip_levels( size_t width, size_t height, size_t depth);
cl_ulong compute_mipmapped_image_size(image_descriptor imageInfo); cl_ulong compute_mipmapped_image_size(image_descriptor imageInfo);
size_t compute_mip_level_offset(image_descriptor *imageInfo, size_t lod); size_t compute_mip_level_offset(image_descriptor *imageInfo, size_t lod);
template <class T> void read_image_pixel( void *imageData, image_descriptor *imageInfo, template <class T>
int x, int y, int z, T *outData, int lod ) void read_image_pixel(void *imageData, image_descriptor *imageInfo, int x,
int y, int z, T *outData, int lod)
{ {
size_t width_lod = imageInfo->width, height_lod = imageInfo->height, depth_lod = imageInfo->depth, slice_pitch_lod = 0/*imageInfo->slicePitch*/ , row_pitch_lod = 0/*imageInfo->rowPitch*/; size_t width_lod = imageInfo->width, height_lod = imageInfo->height,
depth_lod = imageInfo->depth,
slice_pitch_lod = 0 /*imageInfo->slicePitch*/,
row_pitch_lod = 0 /*imageInfo->rowPitch*/;
width_lod = (imageInfo->width >> lod) ? (imageInfo->width >> lod) : 1; width_lod = (imageInfo->width >> lod) ? (imageInfo->width >> lod) : 1;
if ( imageInfo->type != CL_MEM_OBJECT_IMAGE1D_ARRAY && imageInfo->type != CL_MEM_OBJECT_IMAGE1D) if (imageInfo->type != CL_MEM_OBJECT_IMAGE1D_ARRAY
height_lod = ( imageInfo->height >> lod) ?( imageInfo->height >> lod):1; && imageInfo->type != CL_MEM_OBJECT_IMAGE1D)
height_lod =
(imageInfo->height >> lod) ? (imageInfo->height >> lod) : 1;
if (imageInfo->type == CL_MEM_OBJECT_IMAGE3D) if (imageInfo->type == CL_MEM_OBJECT_IMAGE3D)
depth_lod = (imageInfo->depth >> lod) ? (imageInfo->depth >> lod) : 1; depth_lod = (imageInfo->depth >> lod) ? (imageInfo->depth >> lod) : 1;
row_pitch_lod = (imageInfo->num_mip_levels > 0)? (width_lod * get_pixel_size( imageInfo->format )): imageInfo->rowPitch; row_pitch_lod = (imageInfo->num_mip_levels > 0)
slice_pitch_lod = (imageInfo->num_mip_levels > 0)? (row_pitch_lod * height_lod): imageInfo->slicePitch; ? (width_lod * get_pixel_size(imageInfo->format))
: imageInfo->rowPitch;
slice_pitch_lod = (imageInfo->num_mip_levels > 0)
? (row_pitch_lod * height_lod)
: imageInfo->slicePitch;
// correct depth_lod and height_lod for array image types in order to avoid // correct depth_lod and height_lod for array image types in order to avoid
// return // return
if (imageInfo->type == CL_MEM_OBJECT_IMAGE1D_ARRAY && height_lod == 1 && depth_lod == 1) { if (imageInfo->type == CL_MEM_OBJECT_IMAGE1D_ARRAY && height_lod == 1
&& depth_lod == 1)
{
depth_lod = 0; depth_lod = 0;
height_lod = 0; height_lod = 0;
} }
if (imageInfo->type == CL_MEM_OBJECT_IMAGE2D_ARRAY && depth_lod == 1) { if (imageInfo->type == CL_MEM_OBJECT_IMAGE2D_ARRAY && depth_lod == 1)
{
depth_lod = 0; depth_lod = 0;
} }
if (x < 0 || x >= (int)width_lod if (x < 0 || x >= (int)width_lod
|| (height_lod != 0 && (y < 0 || y >= (int)height_lod)) || (height_lod != 0 && (y < 0 || y >= (int)height_lod))
|| (depth_lod != 0 && (z < 0 || z >= (int)depth_lod)) || (depth_lod != 0 && (z < 0 || z >= (int)depth_lod))
|| ( imageInfo->arraySize != 0 && ( z < 0 || z >= (int)imageInfo->arraySize ) ) ) || (imageInfo->arraySize != 0
&& (z < 0 || z >= (int)imageInfo->arraySize)))
{ {
// Border color // Border color
if (imageInfo->format->image_channel_order == CL_DEPTH) if (imageInfo->format->image_channel_order == CL_DEPTH)
{ {
outData[0] = 1; outData[0] = 1;
} }
else { else
{
outData[0] = outData[1] = outData[2] = outData[3] = 0; outData[0] = outData[1] = outData[2] = outData[3] = 0;
if (!has_alpha(imageInfo->format)) if (!has_alpha(imageInfo->format)) outData[3] = 1;
outData[3] = 1;
} }
return; return;
} }
@@ -211,64 +254,56 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
// OpenCL only supports reading floats from certain formats // OpenCL only supports reading floats from certain formats
switch (format->image_channel_data_type) switch (format->image_channel_data_type)
{ {
case CL_SNORM_INT8: case CL_SNORM_INT8: {
{
cl_char *dPtr = (cl_char *)ptr; cl_char *dPtr = (cl_char *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_UNORM_INT8: case CL_UNORM_INT8: {
{
cl_uchar *dPtr = (cl_uchar *)ptr; cl_uchar *dPtr = (cl_uchar *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_SIGNED_INT8: case CL_SIGNED_INT8: {
{
cl_char *dPtr = (cl_char *)ptr; cl_char *dPtr = (cl_char *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_UNSIGNED_INT8: case CL_UNSIGNED_INT8: {
{
cl_uchar *dPtr = (cl_uchar *)ptr; cl_uchar *dPtr = (cl_uchar *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_SNORM_INT16: case CL_SNORM_INT16: {
{
cl_short *dPtr = (cl_short *)ptr; cl_short *dPtr = (cl_short *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_UNORM_INT16: case CL_UNORM_INT16: {
{
cl_ushort *dPtr = (cl_ushort *)ptr; cl_ushort *dPtr = (cl_ushort *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_SIGNED_INT16: case CL_SIGNED_INT16: {
{
cl_short *dPtr = (cl_short *)ptr; cl_short *dPtr = (cl_short *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_UNSIGNED_INT16: case CL_UNSIGNED_INT16: {
{
cl_ushort *dPtr = (cl_ushort *)ptr; cl_ushort *dPtr = (cl_ushort *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
@@ -282,24 +317,21 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
case CL_SIGNED_INT32: case CL_SIGNED_INT32: {
{
cl_int *dPtr = (cl_int *)ptr; cl_int *dPtr = (cl_int *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_UNSIGNED_INT32: case CL_UNSIGNED_INT32: {
{
cl_uint *dPtr = (cl_uint *)ptr; cl_uint *dPtr = (cl_uint *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
case CL_UNORM_SHORT_565: case CL_UNORM_SHORT_565: {
{
cl_ushort *dPtr = (cl_ushort *)ptr; cl_ushort *dPtr = (cl_ushort *)ptr;
tempData[0] = (T)(dPtr[0] >> 11); tempData[0] = (T)(dPtr[0] >> 11);
tempData[1] = (T)((dPtr[0] >> 5) & 63); tempData[1] = (T)((dPtr[0] >> 5) & 63);
@@ -308,8 +340,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
} }
#ifdef OBSOLETE_FORMAT #ifdef OBSOLETE_FORMAT
case CL_UNORM_SHORT_565_REV: case CL_UNORM_SHORT_565_REV: {
{
unsigned short *dPtr = (unsigned short *)ptr; unsigned short *dPtr = (unsigned short *)ptr;
tempData[2] = (T)(dPtr[0] >> 11); tempData[2] = (T)(dPtr[0] >> 11);
tempData[1] = (T)((dPtr[0] >> 5) & 63); tempData[1] = (T)((dPtr[0] >> 5) & 63);
@@ -317,8 +348,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
case CL_UNORM_SHORT_555_REV: case CL_UNORM_SHORT_555_REV: {
{
unsigned short *dPtr = (unsigned short *)ptr; unsigned short *dPtr = (unsigned short *)ptr;
tempData[2] = (T)((dPtr[0] >> 10) & 31); tempData[2] = (T)((dPtr[0] >> 10) & 31);
tempData[1] = (T)((dPtr[0] >> 5) & 31); tempData[1] = (T)((dPtr[0] >> 5) & 31);
@@ -326,8 +356,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
case CL_UNORM_INT_8888: case CL_UNORM_INT_8888: {
{
unsigned int *dPtr = (unsigned int *)ptr; unsigned int *dPtr = (unsigned int *)ptr;
tempData[3] = (T)(dPtr[0] >> 24); tempData[3] = (T)(dPtr[0] >> 24);
tempData[2] = (T)((dPtr[0] >> 16) & 0xff); tempData[2] = (T)((dPtr[0] >> 16) & 0xff);
@@ -335,8 +364,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
tempData[0] = (T)(dPtr[0] & 0xff); tempData[0] = (T)(dPtr[0] & 0xff);
break; break;
} }
case CL_UNORM_INT_8888_REV: case CL_UNORM_INT_8888_REV: {
{
unsigned int *dPtr = (unsigned int *)ptr; unsigned int *dPtr = (unsigned int *)ptr;
tempData[0] = (T)(dPtr[0] >> 24); tempData[0] = (T)(dPtr[0] >> 24);
tempData[1] = (T)((dPtr[0] >> 16) & 0xff); tempData[1] = (T)((dPtr[0] >> 16) & 0xff);
@@ -345,8 +373,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
case CL_UNORM_INT_101010_REV: case CL_UNORM_INT_101010_REV: {
{
unsigned int *dPtr = (unsigned int *)ptr; unsigned int *dPtr = (unsigned int *)ptr;
tempData[2] = (T)((dPtr[0] >> 20) & 0x3ff); tempData[2] = (T)((dPtr[0] >> 20) & 0x3ff);
tempData[1] = (T)((dPtr[0] >> 10) & 0x3ff); tempData[1] = (T)((dPtr[0] >> 10) & 0x3ff);
@@ -354,8 +381,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
#endif #endif
case CL_UNORM_SHORT_555: case CL_UNORM_SHORT_555: {
{
cl_ushort *dPtr = (cl_ushort *)ptr; cl_ushort *dPtr = (cl_ushort *)ptr;
tempData[0] = (T)((dPtr[0] >> 10) & 31); tempData[0] = (T)((dPtr[0] >> 10) & 31);
tempData[1] = (T)((dPtr[0] >> 5) & 31); tempData[1] = (T)((dPtr[0] >> 5) & 31);
@@ -363,8 +389,7 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
case CL_UNORM_INT_101010: case CL_UNORM_INT_101010: {
{
cl_uint *dPtr = (cl_uint *)ptr; cl_uint *dPtr = (cl_uint *)ptr;
tempData[0] = (T)((dPtr[0] >> 20) & 0x3ff); tempData[0] = (T)((dPtr[0] >> 20) & 0x3ff);
tempData[1] = (T)((dPtr[0] >> 10) & 0x3ff); tempData[1] = (T)((dPtr[0] >> 10) & 0x3ff);
@@ -372,16 +397,14 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
break; break;
} }
case CL_FLOAT: case CL_FLOAT: {
{
cl_float *dPtr = (cl_float *)ptr; cl_float *dPtr = (cl_float *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i]; tempData[i] = (T)dPtr[i];
break; break;
} }
#ifdef CL_SFIXED14_APPLE #ifdef CL_SFIXED14_APPLE
case CL_SFIXED14_APPLE: case CL_SFIXED14_APPLE: {
{
cl_float *dPtr = (cl_float *)ptr; cl_float *dPtr = (cl_float *)ptr;
for (i = 0; i < get_format_channel_count(format); i++) for (i = 0; i < get_format_channel_count(format); i++)
tempData[i] = (T)dPtr[i] + 0x4000; tempData[i] = (T)dPtr[i] + 0x4000;
@@ -421,20 +444,23 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
outData[0] = tempData[0]; outData[0] = tempData[0];
outData[1] = tempData[1]; outData[1] = tempData[1];
} }
else if(( format->image_channel_order == CL_RGB ) || ( format->image_channel_order == CL_sRGB )) else if ((format->image_channel_order == CL_RGB)
|| (format->image_channel_order == CL_sRGB))
{ {
outData[0] = tempData[0]; outData[0] = tempData[0];
outData[1] = tempData[1]; outData[1] = tempData[1];
outData[2] = tempData[2]; outData[2] = tempData[2];
} }
else if(( format->image_channel_order == CL_RGBx ) || ( format->image_channel_order == CL_sRGBx )) else if ((format->image_channel_order == CL_RGBx)
|| (format->image_channel_order == CL_sRGBx))
{ {
outData[0] = tempData[0]; outData[0] = tempData[0];
outData[1] = tempData[1]; outData[1] = tempData[1];
outData[2] = tempData[2]; outData[2] = tempData[2];
outData[3] = 0; outData[3] = 0;
} }
else if(( format->image_channel_order == CL_RGBA ) || ( format->image_channel_order == CL_sRGBA )) else if ((format->image_channel_order == CL_RGBA)
|| (format->image_channel_order == CL_sRGBA))
{ {
outData[0] = tempData[0]; outData[0] = tempData[0];
outData[1] = tempData[1]; outData[1] = tempData[1];
@@ -448,7 +474,8 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
outData[2] = tempData[3]; outData[2] = tempData[3];
outData[3] = tempData[0]; outData[3] = tempData[0];
} }
else if(( format->image_channel_order == CL_BGRA ) || ( format->image_channel_order == CL_sBGRA )) else if ((format->image_channel_order == CL_BGRA)
|| (format->image_channel_order == CL_sBGRA))
{ {
outData[0] = tempData[2]; outData[0] = tempData[2];
outData[1] = tempData[1]; outData[1] = tempData[1];
@@ -495,27 +522,32 @@ template <class T> void read_image_pixel( void *imageData, image_descriptor *ima
} }
} }
template <class T> void read_image_pixel( void *imageData, image_descriptor *imageInfo, template <class T>
int x, int y, int z, T *outData ) void read_image_pixel(void *imageData, image_descriptor *imageInfo, int x,
int y, int z, T *outData)
{ {
read_image_pixel<T>(imageData, imageInfo, x, y, z, outData, 0); read_image_pixel<T>(imageData, imageInfo, x, y, z, outData, 0);
} }
// Stupid template rules // Stupid template rules
bool get_integer_coords( float x, float y, float z, bool get_integer_coords(float x, float y, float z, size_t width, size_t height,
size_t depth, image_sampler_data *imageSampler,
image_descriptor *imageInfo, int &outX, int &outY,
int &outZ);
bool get_integer_coords_offset(float x, float y, float z, float xAddressOffset,
float yAddressOffset, float zAddressOffset,
size_t width, size_t height, size_t depth, size_t width, size_t height, size_t depth,
image_sampler_data *imageSampler, image_descriptor *imageInfo, image_sampler_data *imageSampler,
int &outX, int &outY, int &outZ ); image_descriptor *imageInfo, int &outX,
bool get_integer_coords_offset( float x, float y, float z, int &outY, int &outZ);
float xAddressOffset, float yAddressOffset, float zAddressOffset,
size_t width, size_t height, size_t depth,
image_sampler_data *imageSampler, image_descriptor *imageInfo,
int &outX, int &outY, int &outZ );
template <class T> void sample_image_pixel_offset( void *imageData, image_descriptor *imageInfo, template <class T>
float x, float y, float z, float xAddressOffset, float yAddressOffset, float zAddressOffset, void sample_image_pixel_offset(void *imageData, image_descriptor *imageInfo,
image_sampler_data *imageSampler, T *outData, int lod ) float x, float y, float z, float xAddressOffset,
float yAddressOffset, float zAddressOffset,
image_sampler_data *imageSampler, T *outData,
int lod)
{ {
int iX = 0, iY = 0, iZ = 0; int iX = 0, iY = 0, iZ = 0;
@@ -523,7 +555,8 @@ template <class T> void sample_image_pixel_offset( void *imageData, image_descri
float max_h; float max_h;
float max_d; float max_d;
switch (imageInfo->type) { switch (imageInfo->type)
{
case CL_MEM_OBJECT_IMAGE1D_ARRAY: case CL_MEM_OBJECT_IMAGE1D_ARRAY:
max_h = imageInfo->arraySize; max_h = imageInfo->arraySize;
max_d = 0; max_d = 0;
@@ -540,65 +573,100 @@ template <class T> void sample_image_pixel_offset( void *imageData, image_descri
if (/*gTestMipmaps*/ imageInfo->num_mip_levels > 1) if (/*gTestMipmaps*/ imageInfo->num_mip_levels > 1)
{ {
switch (imageInfo->type) { switch (imageInfo->type)
{
case CL_MEM_OBJECT_IMAGE3D: case CL_MEM_OBJECT_IMAGE3D:
max_d = (float)((imageInfo->depth >> lod) ? (imageInfo->depth >> lod) : 1); max_d = (float)((imageInfo->depth >> lod)
? (imageInfo->depth >> lod)
: 1);
case CL_MEM_OBJECT_IMAGE2D: case CL_MEM_OBJECT_IMAGE2D:
case CL_MEM_OBJECT_IMAGE2D_ARRAY: case CL_MEM_OBJECT_IMAGE2D_ARRAY:
max_h = (float)((imageInfo->height >> lod) ? (imageInfo->height >> lod) : 1); max_h = (float)((imageInfo->height >> lod)
? (imageInfo->height >> lod)
: 1);
break; break;
default: default:;
;
} }
max_w = (float)((imageInfo->width >> lod) ? (imageInfo->width >> lod) : 1); max_w =
(float)((imageInfo->width >> lod) ? (imageInfo->width >> lod) : 1);
} }
get_integer_coords_offset( x, y, z, xAddressOffset, yAddressOffset, zAddressOffset, max_w, max_h, max_d, imageSampler, imageInfo, iX, iY, iZ ); get_integer_coords_offset(x, y, z, xAddressOffset, yAddressOffset,
zAddressOffset, max_w, max_h, max_d, imageSampler,
imageInfo, iX, iY, iZ);
read_image_pixel<T>(imageData, imageInfo, iX, iY, iZ, outData, lod); read_image_pixel<T>(imageData, imageInfo, iX, iY, iZ, outData, lod);
} }
template <class T> void sample_image_pixel_offset( void *imageData, image_descriptor *imageInfo, template <class T>
float x, float y, float z, float xAddressOffset, float yAddressOffset, float zAddressOffset, void sample_image_pixel_offset(void *imageData, image_descriptor *imageInfo,
float x, float y, float z, float xAddressOffset,
float yAddressOffset, float zAddressOffset,
image_sampler_data *imageSampler, T *outData) image_sampler_data *imageSampler, T *outData)
{ {
sample_image_pixel_offset<T>( imageData, imageInfo, x, y, z, xAddressOffset, yAddressOffset, zAddressOffset, sample_image_pixel_offset<T>(imageData, imageInfo, x, y, z, xAddressOffset,
imageSampler, outData, 0); yAddressOffset, zAddressOffset, imageSampler,
outData, 0);
} }
template <class T> void sample_image_pixel( void *imageData, image_descriptor *imageInfo, template <class T>
float x, float y, float z, image_sampler_data *imageSampler, T *outData ) void sample_image_pixel(void *imageData, image_descriptor *imageInfo, float x,
float y, float z, image_sampler_data *imageSampler,
T *outData)
{ {
return sample_image_pixel_offset<T>(imageData, imageInfo, x, y, z, 0.0f, 0.0f, 0.0f, imageSampler, outData); return sample_image_pixel_offset<T>(imageData, imageInfo, x, y, z, 0.0f,
0.0f, 0.0f, imageSampler, outData);
} }
FloatPixel sample_image_pixel_float( void *imageData, image_descriptor *imageInfo, FloatPixel
float x, float y, float z, image_sampler_data *imageSampler, float *outData, int verbose, int *containsDenorms ); sample_image_pixel_float(void *imageData, image_descriptor *imageInfo, float x,
float y, float z, image_sampler_data *imageSampler,
float *outData, int verbose, int *containsDenorms);
FloatPixel sample_image_pixel_float( void *imageData, image_descriptor *imageInfo, FloatPixel sample_image_pixel_float(void *imageData,
float x, float y, float z, image_sampler_data *imageSampler, float *outData, int verbose, int *containsDenorms, int lod ); image_descriptor *imageInfo, float x,
float y, float z,
image_sampler_data *imageSampler,
float *outData, int verbose,
int *containsDenorms, int lod);
FloatPixel sample_image_pixel_float_offset( void *imageData, image_descriptor *imageInfo, FloatPixel sample_image_pixel_float_offset(
float x, float y, float z, float xAddressOffset, float yAddressOffset, float zAddressOffset, void *imageData, image_descriptor *imageInfo, float x, float y, float z,
image_sampler_data *imageSampler, float *outData, int verbose, int *containsDenorms ); float xAddressOffset, float yAddressOffset, float zAddressOffset,
FloatPixel sample_image_pixel_float_offset( void *imageData, image_descriptor *imageInfo, image_sampler_data *imageSampler, float *outData, int verbose,
float x, float y, float z, float xAddressOffset, float yAddressOffset, float zAddressOffset, int *containsDenorms);
image_sampler_data *imageSampler, float *outData, int verbose, int *containsDenorms, int lod ); FloatPixel sample_image_pixel_float_offset(
void *imageData, image_descriptor *imageInfo, float x, float y, float z,
float xAddressOffset, float yAddressOffset, float zAddressOffset,
image_sampler_data *imageSampler, float *outData, int verbose,
int *containsDenorms, int lod);
extern void pack_image_pixel( unsigned int *srcVector, const cl_image_format *imageFormat, void *outData ); extern void pack_image_pixel(unsigned int *srcVector,
extern void pack_image_pixel( int *srcVector, const cl_image_format *imageFormat, void *outData ); const cl_image_format *imageFormat, void *outData);
extern void pack_image_pixel( float *srcVector, const cl_image_format *imageFormat, void *outData ); extern void pack_image_pixel(int *srcVector, const cl_image_format *imageFormat,
extern void pack_image_pixel_error( const float *srcVector, const cl_image_format *imageFormat, const void *results, float *errors ); void *outData);
extern void pack_image_pixel(float *srcVector,
const cl_image_format *imageFormat, void *outData);
extern void pack_image_pixel_error(const float *srcVector,
const cl_image_format *imageFormat,
const void *results, float *errors);
extern char *create_random_image_data( ExplicitType dataType, image_descriptor *imageInfo, BufferOwningPtr<char> &P, MTdata d, bool image2DFromBuffer = false ); extern char *create_random_image_data(ExplicitType dataType,
image_descriptor *imageInfo,
BufferOwningPtr<char> &P, MTdata d,
bool image2DFromBuffer = false);
// deprecated // deprecated
//extern bool clamp_image_coord( image_sampler_data *imageSampler, float value, size_t max, int &outValue ); // extern bool clamp_image_coord( image_sampler_data *imageSampler, float value,
// size_t max, int &outValue );
extern void get_sampler_kernel_code( image_sampler_data *imageSampler, char *outLine ); extern void get_sampler_kernel_code(image_sampler_data *imageSampler,
extern float get_max_absolute_error( cl_image_format *format, image_sampler_data *sampler); char *outLine);
extern float get_max_relative_error( cl_image_format *format, image_sampler_data *sampler, int is3D, int isLinearFilter ); extern float get_max_absolute_error(cl_image_format *format,
image_sampler_data *sampler);
extern float get_max_relative_error(cl_image_format *format,
image_sampler_data *sampler, int is3D,
int isLinearFilter);
#define errMax(_x, _y) ((_x) != (_x) ? (_x) : (_x) > (_y) ? (_x) : (_y)) #define errMax(_x, _y) ((_x) != (_x) ? (_x) : (_x) > (_y) ? (_x) : (_y))
@@ -616,16 +684,14 @@ static inline cl_uint abs_diff_int( cl_int x, cl_int y )
static inline cl_float relative_error(float test, float expected) static inline cl_float relative_error(float test, float expected)
{ {
// 0-0/0 is 0 in this case, not NaN // 0-0/0 is 0 in this case, not NaN
if( test == 0.0f && expected == 0.0f ) if (test == 0.0f && expected == 0.0f) return 0.0f;
return 0.0f;
return (test - expected) / expected; return (test - expected) / expected;
} }
extern float random_float(float low, float high); extern float random_float(float low, float high);
class CoordWalker class CoordWalker {
{
public: public:
CoordWalker(void *coords, bool useFloats, size_t vecSize); CoordWalker(void *coords, bool useFloats, size_t vecSize);
~CoordWalker(); ~CoordWalker();

File diff suppressed because it is too large Load Diff

View File

@@ -43,7 +43,8 @@
#include <functional> #include <functional>
/* /*
* The below code is intended to be used at the top of kernels that appear inline in files to set line and file info for the kernel: * The below code is intended to be used at the top of kernels that appear
* inline in files to set line and file info for the kernel:
* *
* const char *source = { * const char *source = {
* INIT_OPENCL_DEBUG_INFO * INIT_OPENCL_DEBUG_INFO
@@ -54,7 +55,8 @@
* }; * };
*/ */
#define INIT_OPENCL_DEBUG_INFO SET_OPENCL_LINE_INFO(__LINE__, __FILE__) #define INIT_OPENCL_DEBUG_INFO SET_OPENCL_LINE_INFO(__LINE__, __FILE__)
#define SET_OPENCL_LINE_INFO(_line, _file) "#line " STRINGIFY(_line) " " STRINGIFY(_file) "\n" #define SET_OPENCL_LINE_INFO(_line, _file) \
"#line " STRINGIFY(_line) " " STRINGIFY(_file) "\n"
#ifndef STRINGIFY_VALUE #ifndef STRINGIFY_VALUE
#define STRINGIFY_VALUE(_x) STRINGIFY(_x) #define STRINGIFY_VALUE(_x) STRINGIFY(_x)
#endif #endif
@@ -64,73 +66,78 @@
const int MAX_LEN_FOR_KERNEL_LIST = 20; const int MAX_LEN_FOR_KERNEL_LIST = 20;
/* Helper that creates a single program and kernel from a single-kernel program source */ /* Helper that creates a single program and kernel from a single-kernel program
extern int create_single_kernel_helper(cl_context context, * source */
cl_program *outProgram, extern int
cl_kernel *outKernel, create_single_kernel_helper(cl_context context, cl_program *outProgram,
unsigned int numKernelLines, cl_kernel *outKernel, unsigned int numKernelLines,
const char **kernelProgram, const char **kernelProgram, const char *kernelName,
const char *kernelName,
const char *buildOptions = NULL, const char *buildOptions = NULL,
const bool openclCXX = false); const bool openclCXX = false);
extern int create_single_kernel_helper_with_build_options(cl_context context, extern int create_single_kernel_helper_with_build_options(
cl_program *outProgram, cl_context context, cl_program *outProgram, cl_kernel *outKernel,
cl_kernel *outKernel, unsigned int numKernelLines, const char **kernelProgram,
unsigned int numKernelLines, const char *kernelName, const char *buildOptions,
const char **kernelProgram,
const char *kernelName,
const char *buildOptions,
const bool openclCXX = false); const bool openclCXX = false);
extern int create_single_kernel_helper_create_program(cl_context context, extern int create_single_kernel_helper_create_program(
cl_program *outProgram, cl_context context, cl_program *outProgram, unsigned int numKernelLines,
unsigned int numKernelLines, const char **kernelProgram, const char *buildOptions = NULL,
const char **kernelProgram,
const char *buildOptions = NULL,
const bool openclCXX = false); const bool openclCXX = false);
extern int create_single_kernel_helper_create_program_for_device(cl_context context, extern int create_single_kernel_helper_create_program_for_device(
cl_device_id device, cl_context context, cl_device_id device, cl_program *outProgram,
cl_program *outProgram, unsigned int numKernelLines, const char **kernelProgram,
unsigned int numKernelLines, const char *buildOptions = NULL, const bool openclCXX = false);
const char **kernelProgram,
const char *buildOptions = NULL,
const bool openclCXX = false);
/* Creates OpenCL C++ program. This one must be used for creating OpenCL C++ program. */ /* Creates OpenCL C++ program. This one must be used for creating OpenCL C++
extern int create_openclcpp_program(cl_context context, * program. */
cl_program *outProgram, extern int create_openclcpp_program(cl_context context, cl_program *outProgram,
unsigned int numKernelLines, unsigned int numKernelLines,
const char **kernelProgram, const char **kernelProgram,
const char *buildOptions = NULL); const char *buildOptions = NULL);
/* Builds program (outProgram) and creates one kernel */ /* Builds program (outProgram) and creates one kernel */
int build_program_create_kernel_helper(cl_context context, int build_program_create_kernel_helper(
cl_program *outProgram, cl_context context, cl_program *outProgram, cl_kernel *outKernel,
cl_kernel *outKernel, unsigned int numKernelLines, const char **kernelProgram,
unsigned int numKernelLines, const char *kernelName, const char *buildOptions = NULL);
const char **kernelProgram,
const char *kernelName,
const char *buildOptions = NULL);
/* Helper to obtain the biggest fit work group size for all the devices in a given group and for the given global thread size */ /* Helper to obtain the biggest fit work group size for all the devices in a
extern int get_max_common_work_group_size( cl_context context, cl_kernel kernel, size_t globalThreadSize, size_t *outSize ); * given group and for the given global thread size */
extern int get_max_common_work_group_size(cl_context context, cl_kernel kernel,
size_t globalThreadSize,
size_t *outSize);
/* Helper to obtain the biggest fit work group size for all the devices in a given group and for the given global thread size */ /* Helper to obtain the biggest fit work group size for all the devices in a
extern int get_max_common_2D_work_group_size( cl_context context, cl_kernel kernel, size_t *globalThreadSize, size_t *outSizes ); * given group and for the given global thread size */
extern int get_max_common_2D_work_group_size(cl_context context,
cl_kernel kernel,
size_t *globalThreadSize,
size_t *outSizes);
/* Helper to obtain the biggest fit work group size for all the devices in a given group and for the given global thread size */ /* Helper to obtain the biggest fit work group size for all the devices in a
extern int get_max_common_3D_work_group_size( cl_context context, cl_kernel kernel, size_t *globalThreadSize, size_t *outSizes ); * given group and for the given global thread size */
extern int get_max_common_3D_work_group_size(cl_context context,
cl_kernel kernel,
size_t *globalThreadSize,
size_t *outSizes);
/* Helper to obtain the biggest allowed work group size for all the devices in a given group */ /* Helper to obtain the biggest allowed work group size for all the devices in a
extern int get_max_allowed_work_group_size( cl_context context, cl_kernel kernel, size_t *outSize, size_t *outLimits ); * given group */
extern int get_max_allowed_work_group_size(cl_context context, cl_kernel kernel,
size_t *outSize, size_t *outLimits);
/* Helper to obtain the biggest allowed 1D work group size on a given device */ /* Helper to obtain the biggest allowed 1D work group size on a given device */
extern int get_max_allowed_1d_work_group_size_on_device( cl_device_id device, cl_kernel kernel, size_t *outSize ); extern int get_max_allowed_1d_work_group_size_on_device(cl_device_id device,
cl_kernel kernel,
size_t *outSize);
/* Helper to determine if a device supports an image format */ /* Helper to determine if a device supports an image format */
extern int is_image_format_supported( cl_context context, cl_mem_flags flags, cl_mem_object_type image_type, const cl_image_format *fmt ); extern int is_image_format_supported(cl_context context, cl_mem_flags flags,
cl_mem_object_type image_type,
const cl_image_format *fmt);
/* Helper to get pixel size for a pixel format */ /* Helper to get pixel size for a pixel format */
size_t get_pixel_bytes(const cl_image_format *fmt); size_t get_pixel_bytes(const cl_image_format *fmt);
@@ -138,18 +145,23 @@ size_t get_pixel_bytes( const cl_image_format *fmt );
/* Verify the given device supports images. */ /* Verify the given device supports images. */
extern test_status verifyImageSupport(cl_device_id device); extern test_status verifyImageSupport(cl_device_id device);
/* Checks that the given device supports images. Same as verify, but doesn't print an error */ /* Checks that the given device supports images. Same as verify, but doesn't
* print an error */
extern int checkForImageSupport(cl_device_id device); extern int checkForImageSupport(cl_device_id device);
extern int checkFor3DImageSupport(cl_device_id device); extern int checkFor3DImageSupport(cl_device_id device);
extern int checkForReadWriteImageSupport(cl_device_id device); extern int checkForReadWriteImageSupport(cl_device_id device);
/* Checks that a given queue property is supported on the specified device. Returns 1 if supported, 0 if not or an error. */ /* Checks that a given queue property is supported on the specified device.
extern int checkDeviceForQueueSupport( cl_device_id device, cl_command_queue_properties prop ); * Returns 1 if supported, 0 if not or an error. */
extern int checkDeviceForQueueSupport(cl_device_id device,
cl_command_queue_properties prop);
/* Helper to obtain the min alignment for a given context, i.e the max of all min alignments for devices attached to the context*/ /* Helper to obtain the min alignment for a given context, i.e the max of all
* min alignments for devices attached to the context*/
size_t get_min_alignment(cl_context context); size_t get_min_alignment(cl_context context);
/* Helper to obtain the default rounding mode for single precision computation. (Double is always CL_FP_ROUND_TO_NEAREST.) Returns 0 on error. */ /* Helper to obtain the default rounding mode for single precision computation.
* (Double is always CL_FP_ROUND_TO_NEAREST.) Returns 0 on error. */
cl_device_fp_config get_default_rounding_mode(cl_device_id device); cl_device_fp_config get_default_rounding_mode(cl_device_id device);
#define PASSIVE_REQUIRE_IMAGE_SUPPORT(device) \ #define PASSIVE_REQUIRE_IMAGE_SUPPORT(device) \
@@ -176,7 +188,8 @@ cl_device_fp_config get_default_rounding_mode( cl_device_id device );
return TEST_SKIPPED_ITSELF; \ return TEST_SKIPPED_ITSELF; \
} }
/* Prints out the standard device header for all tests given the device to print for */ /* Prints out the standard device header for all tests given the device to print
* for */
extern int printDeviceHeader(cl_device_id device); extern int printDeviceHeader(cl_device_id device);
// Execute the CL_DEVICE_OPENCL_C_VERSION query and return the OpenCL C version // Execute the CL_DEVICE_OPENCL_C_VERSION query and return the OpenCL C version

View File

@@ -22,30 +22,33 @@
// This function is unavailable on various mingw compilers, // This function is unavailable on various mingw compilers,
// especially 64 bit so implementing it here // especially 64 bit so implementing it here
const char *basename_dot = "."; const char *basename_dot = ".";
char* char *basename(char *path)
basename(char *path)
{ {
char *p = path, *b = NULL; char *p = path, *b = NULL;
int len = strlen(path); int len = strlen(path);
if (path == NULL) { if (path == NULL)
{
return (char *)basename_dot; return (char *)basename_dot;
} }
// Not absolute path on windows // Not absolute path on windows
if (path[1] != ':') { if (path[1] != ':')
{
return path; return path;
} }
// Trim trailing path seperators // Trim trailing path seperators
if (path[len - 1] == '\\' || if (path[len - 1] == '\\' || path[len - 1] == '/')
path[len - 1] == '/' ) { {
len--; len--;
path[len] = '\0'; path[len] = '\0';
} }
while (len) { while (len)
while((*p != '\\' || *p != '/') && len) { {
while ((*p != '\\' || *p != '/') && len)
{
p++; p++;
len--; len--;
} }

View File

@@ -34,7 +34,10 @@
float copysignf(float x, float y) float copysignf(float x, float y)
{ {
union{ cl_uint u; float f; }ux, uy; union {
cl_uint u;
float f;
} ux, uy;
ux.f = x; ux.f = x;
uy.f = y; uy.f = y;
@@ -46,7 +49,10 @@ float copysignf( float x, float y )
double copysign(double x, double y) double copysign(double x, double y)
{ {
union{ cl_ulong u; double f; }ux, uy; union {
cl_ulong u;
double f;
} ux, uy;
ux.f = x; ux.f = x;
uy.f = y; uy.f = y;
@@ -58,10 +64,13 @@ double copysign( double x, double y )
long double copysignl(long double x, long double y) long double copysignl(long double x, long double y)
{ {
union union {
{
long double f; long double f;
struct{ cl_ulong m; cl_ushort sexp; }u; struct
{
cl_ulong m;
cl_ushort sexp;
} u;
} ux, uy; } ux, uy;
ux.f = x; ux.f = x;
@@ -108,7 +117,8 @@ long double rintl(long double x)
if (absx < 9223372036854775808.0L /* 0x1.0p64f */) if (absx < 9223372036854775808.0L /* 0x1.0p64f */)
{ {
long double magic = copysignl( 9223372036854775808.0L /* 0x1.0p63L */, x ); long double magic =
copysignl(9223372036854775808.0L /* 0x1.0p63L */, x);
long double rounded = x + magic; long double rounded = x + magic;
rounded -= magic; rounded -= magic;
x = copysignl(rounded, x); x = copysignl(rounded, x);
@@ -134,21 +144,22 @@ long double rintl(long double x)
int ilogb(double x) int ilogb(double x)
{ {
union{ double f; cl_ulong u;} u; union {
double f;
cl_ulong u;
} u;
u.f = x; u.f = x;
cl_ulong absx = u.u & CL_LONG_MAX; cl_ulong absx = u.u & CL_LONG_MAX;
if( absx - 0x0001000000000000ULL >= 0x7ff0000000000000ULL - 0x0001000000000000ULL) if (absx - 0x0001000000000000ULL
>= 0x7ff0000000000000ULL - 0x0001000000000000ULL)
{ {
switch (absx) switch (absx)
{ {
case 0: case 0: return FP_ILOGB0;
return FP_ILOGB0; case 0x7ff0000000000000ULL: return INT_MAX;
case 0x7ff0000000000000ULL:
return INT_MAX;
default: default:
if( absx > 0x7ff0000000000000ULL ) if (absx > 0x7ff0000000000000ULL) return FP_ILOGBNAN;
return FP_ILOGBNAN;
// subnormal // subnormal
u.u = absx | 0x3ff0000000000000ULL; u.u = absx | 0x3ff0000000000000ULL;
@@ -163,7 +174,10 @@ int ilogb (double x)
int ilogbf(float x) int ilogbf(float x)
{ {
union{ float f; cl_uint u;} u; union {
float f;
cl_uint u;
} u;
u.f = x; u.f = x;
cl_uint absx = u.u & 0x7fffffff; cl_uint absx = u.u & 0x7fffffff;
@@ -171,13 +185,10 @@ int ilogbf (float x)
{ {
switch (absx) switch (absx)
{ {
case 0: case 0: return FP_ILOGB0;
return FP_ILOGB0; case 0x7f800000U: return INT_MAX;
case 0x7f800000U:
return INT_MAX;
default: default:
if( absx > 0x7f800000 ) if (absx > 0x7f800000) return FP_ILOGBNAN;
return FP_ILOGBNAN;
// subnormal // subnormal
u.u = absx | 0x3f800000U; u.u = absx | 0x3f800000U;
@@ -191,18 +202,20 @@ int ilogbf (float x)
int ilogbl(long double x) int ilogbl(long double x)
{ {
union union {
{
long double f; long double f;
struct{ cl_ulong m; cl_ushort sexp; }u; struct
{
cl_ulong m;
cl_ushort sexp;
} u;
} u; } u;
u.f = x; u.f = x;
int exp = u.u.sexp & 0x7fff; int exp = u.u.sexp & 0x7fff;
if (0 == exp) if (0 == exp)
{ {
if( 0 == u.u.m ) if (0 == u.u.m) return FP_ILOGB0;
return FP_ILOGB0;
// subnormal // subnormal
u.u.sexp = 0x3fff; u.u.sexp = 0x3fff;
@@ -213,8 +226,7 @@ int ilogbl (long double x)
} }
else if (0x7fff == exp) else if (0x7fff == exp)
{ {
if( u.u.m & CL_LONG_MAX ) if (u.u.m & CL_LONG_MAX) return FP_ILOGBNAN;
return FP_ILOGBNAN;
return INT_MAX; return INT_MAX;
} }
@@ -232,7 +244,10 @@ int ilogbl (long double x)
static void GET_BITS_SP32(float fx, unsigned int* ux) static void GET_BITS_SP32(float fx, unsigned int* ux)
{ {
volatile union {float f; unsigned int u;} _bitsy; volatile union {
float f;
unsigned int u;
} _bitsy;
_bitsy.f = (fx); _bitsy.f = (fx);
*ux = _bitsy.u; *ux = _bitsy.u;
} }
@@ -244,7 +259,10 @@ static void GET_BITS_SP32(float fx, unsigned int* ux)
/* } */ /* } */
static void PUT_BITS_SP32(unsigned int ux, float* fx) static void PUT_BITS_SP32(unsigned int ux, float* fx)
{ {
volatile union {float f; unsigned int u;} _bitsy; volatile union {
float f;
unsigned int u;
} _bitsy;
_bitsy.u = (ux); _bitsy.u = (ux);
*fx = _bitsy.f; *fx = _bitsy.f;
} }
@@ -256,13 +274,19 @@ static void PUT_BITS_SP32(unsigned int ux, float* fx)
/* } */ /* } */
static void GET_BITS_DP64(double dx, unsigned __int64* lx) static void GET_BITS_DP64(double dx, unsigned __int64* lx)
{ {
volatile union {double d; unsigned __int64 l;} _bitsy; volatile union {
double d;
unsigned __int64 l;
} _bitsy;
_bitsy.d = (dx); _bitsy.d = (dx);
*lx = _bitsy.l; *lx = _bitsy.l;
} }
static void PUT_BITS_DP64(unsigned __int64 lx, double* dx) static void PUT_BITS_DP64(unsigned __int64 lx, double* dx)
{ {
volatile union {double d; unsigned __int64 l;} _bitsy; volatile union {
double d;
unsigned __int64 l;
} _bitsy;
_bitsy.l = (lx); _bitsy.l = (lx);
*dx = _bitsy.d; *dx = _bitsy.d;
} }
@@ -287,8 +311,7 @@ int SIGNBIT_DP64(double x )
that x is NaN; gcc does. */ that x is NaN; gcc does. */
double fmax(double x, double y) double fmax(double x, double y)
{ {
if( isnan(y) ) if (isnan(y)) return x;
return x;
return x >= y ? x : y; return x >= y ? x : y;
} }
@@ -301,8 +324,7 @@ double fmax(double x, double y)
double fmin(double x, double y) double fmin(double x, double y)
{ {
if( isnan(y) ) if (isnan(y)) return x;
return x;
return x <= y ? x : y; return x <= y ? x : y;
} }
@@ -310,8 +332,7 @@ double fmin(double x, double y)
float fmaxf(float x, float y) float fmaxf(float x, float y)
{ {
if( isnan(y) ) if (isnan(y)) return x;
return x;
return x >= y ? x : y; return x >= y ? x : y;
} }
@@ -323,26 +344,26 @@ float fmaxf( float x, float y )
float fminf(float x, float y) float fminf(float x, float y)
{ {
if( isnan(y) ) if (isnan(y)) return x;
return x;
return x <= y ? x : y; return x <= y ? x : y;
} }
long double scalblnl(long double x, long n) long double scalblnl(long double x, long n)
{ {
union union {
{
long double d; long double d;
struct{ cl_ulong m; cl_ushort sexp;}u; struct
{
cl_ulong m;
cl_ushort sexp;
} u;
} u; } u;
u.u.m = CL_LONG_MIN; u.u.m = CL_LONG_MIN;
if( x == 0.0L || n < -2200) if (x == 0.0L || n < -2200) return copysignl(0.0L, x);
return copysignl( 0.0L, x );
if( n > 2200 ) if (n > 2200) return INFINITY;
return INFINITY;
if (n < 0) if (n < 0)
{ {
@@ -383,10 +404,7 @@ const static cl_double log_10_base2 = 3.3219280948873623478;
// double log10(double x); // double log10(double x);
double log2(double x) double log2(double x) { return 1.44269504088896340735992468100189214 * log(x); }
{
return 1.44269504088896340735992468100189214 * log(x);
}
long double log2l(long double x) long double log2l(long double x)
{ {
@@ -423,8 +441,7 @@ long lround(double x)
{ {
double absx = fabs(x); double absx = fabs(x);
if( absx < 0.5 ) if (absx < 0.5) return 0;
return 0;
if (absx < 4503599627370496.0 /* 0x1.0p52 */) if (absx < 4503599627370496.0 /* 0x1.0p52 */)
{ {
@@ -434,8 +451,7 @@ long lround(double x)
x = copysign(absx, x); x = copysign(absx, x);
} }
if( x >= (double) LONG_MAX ) if (x >= (double)LONG_MAX) return LONG_MAX;
return LONG_MAX;
return (long)x; return (long)x;
} }
@@ -444,8 +460,7 @@ long lroundf(float x)
{ {
float absx = fabsf(x); float absx = fabsf(x);
if( absx < 0.5f ) if (absx < 0.5f) return 0;
return 0;
if (absx < 8388608.0f) if (absx < 8388608.0f)
{ {
@@ -455,8 +470,7 @@ long lroundf(float x)
x = copysignf(absx, x); x = copysignf(absx, x);
} }
if( x >= (float) LONG_MAX ) if (x >= (float)LONG_MAX) return LONG_MAX;
return LONG_MAX;
return (long)x; return (long)x;
} }
@@ -465,8 +479,7 @@ double round(double x)
{ {
double absx = fabs(x); double absx = fabs(x);
if( absx < 0.5 ) if (absx < 0.5) return copysign(0.0, x);
return copysign( 0.0, x);
if (absx < 4503599627370496.0 /* 0x1.0p52 */) if (absx < 4503599627370496.0 /* 0x1.0p52 */)
{ {
@@ -483,8 +496,7 @@ float roundf(float x)
{ {
float absx = fabsf(x); float absx = fabsf(x);
if( absx < 0.5f ) if (absx < 0.5f) return copysignf(0.0f, x);
return copysignf( 0.0f, x);
if (absx < 8388608.0f) if (absx < 8388608.0f)
{ {
@@ -501,8 +513,7 @@ long double roundl(long double x)
{ {
long double absx = fabsl(x); long double absx = fabsl(x);
if( absx < 0.5L ) if (absx < 0.5L) return copysignl(0.0L, x);
return copysignl( 0.0L, x);
if (absx < 9223372036854775808.0L /*0x1.0p63L*/) if (absx < 9223372036854775808.0L /*0x1.0p63L*/)
{ {
@@ -521,17 +532,13 @@ float cbrtf( float x )
return copysignf(z, x); return copysignf(z, x);
} }
double cbrt( double x ) double cbrt(double x) { return copysign(pow(fabs(x), 1.0 / 3.0), x); }
{
return copysign( pow( fabs( x ), 1.0 / 3.0 ), x );
}
long int lrint(double x) long int lrint(double x)
{ {
double absx = fabs(x); double absx = fabs(x);
if( x >= (double) LONG_MAX ) if (x >= (double)LONG_MAX) return LONG_MAX;
return LONG_MAX;
if (absx < 4503599627370496.0 /* 0x1.0p52 */) if (absx < 4503599627370496.0 /* 0x1.0p52 */)
{ {
@@ -548,8 +555,7 @@ long int lrintf (float x)
{ {
float absx = fabsf(x); float absx = fabsf(x);
if( x >= (float) LONG_MAX ) if (x >= (float)LONG_MAX) return LONG_MAX;
return LONG_MAX;
if (absx < 8388608.0f /* 0x1.0p23f */) if (absx < 8388608.0f /* 0x1.0p23f */)
{ {
@@ -574,13 +580,12 @@ long int lrintf (float x)
int fetestexcept(int excepts) int fetestexcept(int excepts)
{ {
unsigned int status = _statusfp(); unsigned int status = _statusfp();
return excepts & ( return excepts
((status & _SW_INEXACT) ? FE_INEXACT : 0) | & (((status & _SW_INEXACT) ? FE_INEXACT : 0)
((status & _SW_UNDERFLOW) ? FE_UNDERFLOW : 0) | | ((status & _SW_UNDERFLOW) ? FE_UNDERFLOW : 0)
((status & _SW_OVERFLOW) ? FE_OVERFLOW : 0) | | ((status & _SW_OVERFLOW) ? FE_OVERFLOW : 0)
((status & _SW_ZERODIVIDE) ? FE_DIVBYZERO : 0) | | ((status & _SW_ZERODIVIDE) ? FE_DIVBYZERO : 0)
((status & _SW_INVALID) ? FE_INVALID : 0) | ((status & _SW_INVALID) ? FE_INVALID : 0));
);
} }
int feclearexcept(int excepts) int feclearexcept(int excepts)
@@ -612,10 +617,13 @@ double nan( const char* str)
// double check this implementatation // double check this implementatation
long double nanl(const char* str) long double nanl(const char* str)
{ {
union union {
{
long double f; long double f;
struct { cl_ulong m; cl_ushort sexp; }u; struct
{
cl_ulong m;
cl_ushort sexp;
} u;
} u; } u;
u.u.sexp = 0x7fff; u.u.sexp = 0x7fff;
u.u.m = 0x8000000000000000ULL | atoi(str); u.u.m = 0x8000000000000000ULL | atoi(str);
@@ -632,11 +640,13 @@ long double nanl( const char* str)
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
/* /*
// This function is commented out because the Windows implementation should never call munmap. // This function is commented out because the Windows implementation should
never call munmap.
// If it is calling it, we have a bug. Please file a bugzilla. // If it is calling it, we have a bug. Please file a bugzilla.
int munmap(void *addr, size_t len) int munmap(void *addr, size_t len)
{ {
// FIXME: this is not correct. munmap is like free() http://www.opengroup.org/onlinepubs/7990989775/xsh/munmap.html // FIXME: this is not correct. munmap is like free()
// http://www.opengroup.org/onlinepubs/7990989775/xsh/munmap.html
return (int)VirtualAlloc( (LPVOID)addr, len, return (int)VirtualAlloc( (LPVOID)addr, len,
MEM_COMMIT|MEM_RESERVE, PAGE_NOACCESS ); MEM_COMMIT|MEM_RESERVE, PAGE_NOACCESS );
@@ -654,7 +664,8 @@ double SubtractTime( uint64_t endTime, uint64_t startTime )
{ {
static double PerformanceFrequency = 0.0; static double PerformanceFrequency = 0.0;
if (PerformanceFrequency == 0.0) { if (PerformanceFrequency == 0.0)
{
LARGE_INTEGER frequency; LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency); QueryPerformanceFrequency(&frequency);
PerformanceFrequency = (double)frequency.QuadPart; PerformanceFrequency = (double)frequency.QuadPart;
@@ -665,8 +676,7 @@ double SubtractTime( uint64_t endTime, uint64_t startTime )
int cf_signbit(double x) int cf_signbit(double x)
{ {
union union {
{
double f; double f;
cl_ulong u; cl_ulong u;
} u; } u;
@@ -676,8 +686,7 @@ int cf_signbit(double x)
int cf_signbitf(float x) int cf_signbitf(float x)
{ {
union union {
{
float f; float f;
cl_uint u; cl_uint u;
} u; } u;
@@ -723,9 +732,12 @@ int __builtin_clz(unsigned int pattern)
#endif #endif
unsigned long index; unsigned long index;
unsigned char res = _BitScanReverse(&index, pattern); unsigned char res = _BitScanReverse(&index, pattern);
if (res) { if (res)
{
return 8 * sizeof(int) - 1 - index; return 8 * sizeof(int) - 1 - index;
} else { }
else
{
return 8 * sizeof(int); return 8 * sizeof(int);
} }
} }
@@ -733,15 +745,35 @@ int __builtin_clz(unsigned int pattern)
int __builtin_clz(unsigned int pattern) int __builtin_clz(unsigned int pattern)
{ {
int count; int count;
if (pattern == 0u) { if (pattern == 0u)
{
return 32; return 32;
} }
count = 31; count = 31;
if (pattern >= 1u<<16) { pattern >>= 16; count -= 16; } if (pattern >= 1u << 16)
if (pattern >= 1u<<8) { pattern >>= 8; count -= 8; } {
if (pattern >= 1u<<4) { pattern >>= 4; count -= 4; } pattern >>= 16;
if (pattern >= 1u<<2) { pattern >>= 2; count -= 2; } count -= 16;
if (pattern >= 1u<<1) { count -= 1; } }
if (pattern >= 1u << 8)
{
pattern >>= 8;
count -= 8;
}
if (pattern >= 1u << 4)
{
pattern >>= 4;
count -= 4;
}
if (pattern >= 1u << 2)
{
pattern >>= 2;
count -= 2;
}
if (pattern >= 1u << 1)
{
count -= 1;
}
return count; return count;
} }

View File

@@ -26,8 +26,8 @@
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
@@ -79,9 +79,10 @@ MTdata init_genrand(cl_uint s)
cl_uint *mt = r->mt; cl_uint *mt = r->mt;
int mti = 0; int mti = 0;
mt[0] = s; // & 0xffffffffUL; mt[0] = s; // & 0xffffffffUL;
for (mti=1; mti<N; mti++) { for (mti = 1; mti < N; mti++)
mt[mti] = (cl_uint) {
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); mt[mti] = (cl_uint)(
1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */ /* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */ /* only MSBs of the array mt[]. */
@@ -97,8 +98,7 @@ MTdata init_genrand(cl_uint s)
void free_mtdata(MTdata d) void free_mtdata(MTdata d)
{ {
if(d) if (d) align_free(d);
align_free(d);
} }
/* generates a random number on [0,0xffffffff]-interval */ /* generates a random number on [0,0xffffffff]-interval */
@@ -108,7 +108,10 @@ cl_uint genrand_int32( MTdata d)
static const cl_uint mag01[2] = { 0x0UL, MATRIX_A }; static const cl_uint mag01[2] = { 0x0UL, MATRIX_A };
#ifdef __SSE2__ #ifdef __SSE2__
static volatile int init = 0; static volatile int init = 0;
static union{ __m128i v; cl_uint s[4]; } upper_mask, lower_mask, one, matrix_a, c0, c1; static union {
__m128i v;
cl_uint s[4];
} upper_mask, lower_mask, one, matrix_a, c0, c1;
#endif #endif
@@ -122,10 +125,13 @@ cl_uint genrand_int32( MTdata d)
#ifdef __SSE2__ #ifdef __SSE2__
if (0 == init) if (0 == init)
{ {
upper_mask.s[0] = upper_mask.s[1] = upper_mask.s[2] = upper_mask.s[3] = UPPER_MASK; upper_mask.s[0] = upper_mask.s[1] = upper_mask.s[2] =
lower_mask.s[0] = lower_mask.s[1] = lower_mask.s[2] = lower_mask.s[3] = LOWER_MASK; upper_mask.s[3] = UPPER_MASK;
lower_mask.s[0] = lower_mask.s[1] = lower_mask.s[2] =
lower_mask.s[3] = LOWER_MASK;
one.s[0] = one.s[1] = one.s[2] = one.s[3] = 1; one.s[0] = one.s[1] = one.s[2] = one.s[3] = 1;
matrix_a.s[0] = matrix_a.s[1] = matrix_a.s[2] = matrix_a.s[3] = MATRIX_A; matrix_a.s[0] = matrix_a.s[1] = matrix_a.s[2] = matrix_a.s[3] =
MATRIX_A;
c0.s[0] = c0.s[1] = c0.s[2] = c0.s[3] = (cl_uint)0x9d2c5680UL; c0.s[0] = c0.s[1] = c0.s[2] = c0.s[3] = (cl_uint)0x9d2c5680UL;
c1.s[0] = c1.s[1] = c1.s[2] = c1.s[3] = (cl_uint)0xefc60000UL; c1.s[0] = c1.s[1] = c1.s[2] = c1.s[3] = (cl_uint)0xefc60000UL;
init = 1; init = 1;
@@ -137,24 +143,36 @@ cl_uint genrand_int32( MTdata d)
// vector loop // vector loop
for (; kk + 4 <= N - M; kk += 4) for (; kk + 4 <= N - M; kk += 4)
{ {
__m128i vy = _mm_or_si128( _mm_and_si128( _mm_load_si128( (__m128i*)(mt + kk) ), upper_mask.v ), // ((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK))
_mm_and_si128( _mm_loadu_si128( (__m128i*)(mt + kk + 1) ), lower_mask.v )); // ((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)) __m128i vy = _mm_or_si128(
_mm_and_si128(_mm_load_si128((__m128i *)(mt + kk)),
upper_mask.v),
_mm_and_si128(_mm_loadu_si128((__m128i *)(mt + kk + 1)),
lower_mask.v));
__m128i mask = _mm_cmpeq_epi32( _mm_and_si128( vy, one.v), one.v ); // y & 1 ? -1 : 0 // y & 1 ? -1 : 0
__m128i vmag01 = _mm_and_si128( mask, matrix_a.v ); // y & 1 ? MATRIX_A, 0 = mag01[y & (cl_uint) 0x1UL] __m128i mask = _mm_cmpeq_epi32(_mm_and_si128(vy, one.v), one.v);
__m128i vr = _mm_xor_si128( _mm_loadu_si128( (__m128i*)(mt + kk + M)), (__m128i) _mm_srli_epi32( vy, 1 ) ); // mt[kk+M] ^ (y >> 1) // y & 1 ? MATRIX_A, 0 = mag01[y & (cl_uint) 0x1UL]
vr = _mm_xor_si128( vr, vmag01 ); // mt[kk+M] ^ (y >> 1) ^ mag01[y & (cl_uint) 0x1UL] __m128i vmag01 = _mm_and_si128(mask, matrix_a.v);
// mt[kk+M] ^ (y >> 1)
__m128i vr =
_mm_xor_si128(_mm_loadu_si128((__m128i *)(mt + kk + M)),
(__m128i)_mm_srli_epi32(vy, 1));
// mt[kk+M] ^ (y >> 1) ^ mag01[y & (cl_uint) 0x1UL]
vr = _mm_xor_si128(vr, vmag01);
_mm_store_si128((__m128i *)(mt + kk), vr); _mm_store_si128((__m128i *)(mt + kk), vr);
} }
#endif #endif
for ( ;kk<N-M;kk++) { for (; kk < N - M; kk++)
{
y = (cl_uint)((mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)); y = (cl_uint)((mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK));
mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & (cl_uint)0x1UL]; mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & (cl_uint)0x1UL];
} }
#ifdef __SSE2__ #ifdef __SSE2__
// advance to next aligned location // advance to next aligned location
for (;kk<N-1 && (kk & 3);kk++) { for (; kk < N - 1 && (kk & 3); kk++)
{
y = (cl_uint)((mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)); y = (cl_uint)((mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK));
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & (cl_uint)0x1UL]; mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & (cl_uint)0x1UL];
} }
@@ -162,18 +180,29 @@ cl_uint genrand_int32( MTdata d)
// vector loop // vector loop
for (; kk + 4 <= N - 1; kk += 4) for (; kk + 4 <= N - 1; kk += 4)
{ {
__m128i vy = _mm_or_si128( _mm_and_si128( _mm_load_si128( (__m128i*)(mt + kk) ), upper_mask.v ), __m128i vy = _mm_or_si128(
_mm_and_si128( _mm_loadu_si128( (__m128i*)(mt + kk + 1) ), lower_mask.v )); // ((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)) _mm_and_si128(_mm_load_si128((__m128i *)(mt + kk)),
upper_mask.v),
// ((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK))
_mm_and_si128(_mm_loadu_si128((__m128i *)(mt + kk + 1)),
lower_mask.v));
__m128i mask = _mm_cmpeq_epi32( _mm_and_si128( vy, one.v), one.v ); // y & 1 ? -1 : 0 // y & 1 ? -1 : 0
__m128i vmag01 = _mm_and_si128( mask, matrix_a.v ); // y & 1 ? MATRIX_A, 0 = mag01[y & (cl_uint) 0x1UL] __m128i mask = _mm_cmpeq_epi32(_mm_and_si128(vy, one.v), one.v);
__m128i vr = _mm_xor_si128( _mm_loadu_si128( (__m128i*)(mt + kk + M - N)), _mm_srli_epi32( vy, 1 ) ); // mt[kk+M-N] ^ (y >> 1) // y & 1 ? MATRIX_A, 0 = mag01[y & (cl_uint) 0x1UL]
vr = _mm_xor_si128( vr, vmag01 ); // mt[kk+M] ^ (y >> 1) ^ mag01[y & (cl_uint) 0x1UL] __m128i vmag01 = _mm_and_si128(mask, matrix_a.v);
// mt[kk+M-N] ^ (y >> 1)
__m128i vr =
_mm_xor_si128(_mm_loadu_si128((__m128i *)(mt + kk + M - N)),
_mm_srli_epi32(vy, 1));
// mt[kk+M] ^ (y >> 1) ^ mag01[y & (cl_uint) 0x1UL]
vr = _mm_xor_si128(vr, vmag01);
_mm_store_si128((__m128i *)(mt + kk), vr); _mm_store_si128((__m128i *)(mt + kk), vr);
} }
#endif #endif
for (;kk<N-1;kk++) { for (; kk < N - 1; kk++)
{
y = (cl_uint)((mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)); y = (cl_uint)((mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK));
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & (cl_uint)0x1UL]; mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & (cl_uint)0x1UL];
} }
@@ -184,11 +213,16 @@ cl_uint genrand_int32( MTdata d)
// Do the tempering ahead of time in vector code // Do the tempering ahead of time in vector code
for (kk = 0; kk + 4 <= N; kk += 4) for (kk = 0; kk + 4 <= N; kk += 4)
{ {
__m128i vy = _mm_load_si128( (__m128i*)(mt + kk ) ); // y = mt[k]; // y = mt[k];
vy = _mm_xor_si128( vy, _mm_srli_epi32( vy, 11 ) ); // y ^= (y >> 11); __m128i vy = _mm_load_si128((__m128i *)(mt + kk));
vy = _mm_xor_si128( vy, _mm_and_si128( _mm_slli_epi32( vy, 7 ), c0.v) ); // y ^= (y << 7) & (cl_uint) 0x9d2c5680UL; // y ^= (y >> 11);
vy = _mm_xor_si128( vy, _mm_and_si128( _mm_slli_epi32( vy, 15 ), c1.v) ); // y ^= (y << 15) & (cl_uint) 0xefc60000UL; vy = _mm_xor_si128(vy, _mm_srli_epi32(vy, 11));
vy = _mm_xor_si128( vy, _mm_srli_epi32( vy, 18 ) ); // y ^= (y >> 18); // y ^= (y << 7) & (cl_uint) 0x9d2c5680UL;
vy = _mm_xor_si128(vy, _mm_and_si128(_mm_slli_epi32(vy, 7), c0.v));
// y ^= (y << 15) & (cl_uint) 0xefc60000UL;
vy = _mm_xor_si128(vy, _mm_and_si128(_mm_slli_epi32(vy, 15), c1.v));
// y ^= (y >> 18);
vy = _mm_xor_si128(vy, _mm_srli_epi32(vy, 18));
_mm_store_si128((__m128i *)(d->cache + kk), vy); _mm_store_si128((__m128i *)(d->cache + kk), vy);
} }
#endif #endif

View File

@@ -31,8 +31,8 @@
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
@@ -92,21 +92,19 @@ double genrand_res53( MTdata /*data*/ );
#include <cassert> #include <cassert>
struct MTdataHolder { struct MTdataHolder
MTdataHolder(cl_uint seed) { {
MTdataHolder(cl_uint seed)
{
m_mtdata = init_genrand(seed); m_mtdata = init_genrand(seed);
assert(m_mtdata != nullptr); assert(m_mtdata != nullptr);
} }
MTdataHolder(MTdata mtdata): m_mtdata(mtdata) {} MTdataHolder(MTdata mtdata): m_mtdata(mtdata) {}
~MTdataHolder() { ~MTdataHolder() { free_mtdata(m_mtdata); }
free_mtdata(m_mtdata);
}
operator MTdata () const { operator MTdata() const { return m_mtdata; }
return m_mtdata;
}
private: private:
MTdata m_mtdata; MTdata m_mtdata;

View File

@@ -33,7 +33,8 @@
#endif #endif
#define CHECK_PTR(ptr) \ #define CHECK_PTR(ptr) \
if ( (ptr) == NULL ) { \ if ((ptr) == NULL) \
{ \
abort(); \ abort(); \
} }
@@ -57,12 +58,11 @@ int const _count = 8; // How many times we will try to double buff
#include <libgen.h> // dirname #include <libgen.h> // dirname
static static std::string
std::string _err_msg(int err, // Error number (e. g. errno).
_err_msg(
int err, // Error number (e. g. errno).
int level // Nesting level, for avoiding infinite recursion. int level // Nesting level, for avoiding infinite recursion.
) { )
{
/* /*
There are 3 incompatible versions of strerror_r: There are 3 incompatible versions of strerror_r:
@@ -71,43 +71,53 @@ int const _count = 8; // How many times we will try to double buff
int strerror_r( int, char *, size_t ); // BSD version int strerror_r( int, char *, size_t ); // BSD version
int strerror_r( int, char *, size_t ); // XSI version int strerror_r( int, char *, size_t ); // XSI version
BSD version returns error code, while XSI version returns 0 or -1 and sets errno. BSD version returns error code, while XSI version returns 0 or -1 and
sets errno.
*/ */
// BSD version of strerror_r. // BSD version of strerror_r.
buffer_t buffer(100); buffer_t buffer(100);
int count = _count; int count = _count;
for ( ; ; ) { for (;;)
{
int rc = strerror_r(err, &buffer.front(), buffer.size()); int rc = strerror_r(err, &buffer.front(), buffer.size());
if ( rc == EINVAL ) { if (rc == EINVAL)
{
// Error code is not recognized, but anyway we got the message. // Error code is not recognized, but anyway we got the message.
return &buffer.front(); return &buffer.front();
} else if ( rc == ERANGE ) { }
else if (rc == ERANGE)
{
// Buffer is not enough. // Buffer is not enough.
if ( count > 0 ) { if (count > 0)
{
// Enlarge the buffer. // Enlarge the buffer.
--count; --count;
buffer.resize(buffer.size() * 2); buffer.resize(buffer.size() * 2);
} else { }
else
{
std::stringstream ostr; std::stringstream ostr;
ostr ostr << "Error " << err << " "
<< "Error " << err << " "
<< "(Getting error message failed: " << "(Getting error message failed: "
<< "Buffer of " << buffer.size() << " bytes is still too small" << "Buffer of " << buffer.size()
<< " bytes is still too small"
<< ")"; << ")";
return ostr.str(); return ostr.str();
}; // if }; // if
} else if ( rc == 0 ) { }
else if (rc == 0)
{
// We got the message. // We got the message.
return &buffer.front(); return &buffer.front();
} else { }
else
{
std::stringstream ostr; std::stringstream ostr;
ostr ostr << "Error " << err << " "
<< "Error " << err << " "
<< "(Getting error message failed: " << "(Getting error message failed: "
<< ( level < 2 ? _err_msg( rc, level + 1 ) : "Oops" ) << (level < 2 ? _err_msg(rc, level + 1) : "Oops") << ")";
<< ")";
return ostr.str(); return ostr.str();
}; // if }; // if
}; // forever }; // forever
@@ -115,33 +125,32 @@ int const _count = 8; // How many times we will try to double buff
} // _err_msg } // _err_msg
std::string std::string dir_sep() { return "/"; } // dir_sep
dir_sep(
) {
return "/";
} // dir_sep
std::string std::string exe_path()
exe_path( {
) {
buffer_t path(_size); buffer_t path(_size);
int count = _count; int count = _count;
for ( ; ; ) { for (;;)
{
uint32_t size = path.size(); uint32_t size = path.size();
int rc = _NSGetExecutablePath(&path.front(), &size); int rc = _NSGetExecutablePath(&path.front(), &size);
if ( rc == 0 ) { if (rc == 0)
{
break; break;
}; // if }; // if
if ( count > 0 ) { if (count > 0)
{
--count; --count;
path.resize(size); path.resize(size);
} else { }
log_error( else
"ERROR: Getting executable path failed: " {
"_NSGetExecutablePath failed: Buffer of %lu bytes is still too small\n", log_error("ERROR: Getting executable path failed: "
(unsigned long) path.size() "_NSGetExecutablePath failed: Buffer of %lu bytes is "
); "still too small\n",
(unsigned long)path.size());
exit(2); exit(2);
}; // if }; // if
}; // forever }; // forever
@@ -149,12 +158,13 @@ int const _count = 8; // How many times we will try to double buff
} // exe_path } // exe_path
std::string std::string exe_dir()
exe_dir( {
) {
std::string path = exe_path(); std::string path = exe_path();
// We cannot pass path.c_str() to `dirname' bacause `dirname' modifies its argument. // We cannot pass path.c_str() to `dirname' bacause `dirname' modifies its
buffer_t buffer( path.c_str(), path.c_str() + path.size() + 1 ); // Copy with trailing zero. // argument.
buffer_t buffer(path.c_str(),
path.c_str() + path.size() + 1); // Copy with trailing zero.
return dirname(&buffer.front()); return dirname(&buffer.front());
} // exe_dir } // exe_dir
@@ -173,12 +183,8 @@ int const _count = 8; // How many times we will try to double buff
#include <unistd.h> // readlink #include <unistd.h> // readlink
static static std::string _err_msg(int err, int level)
std::string {
_err_msg(
int err,
int level
) {
/* /*
There are 3 incompatible versions of strerror_r: There are 3 incompatible versions of strerror_r:
@@ -187,44 +193,54 @@ int const _count = 8; // How many times we will try to double buff
int strerror_r( int, char *, size_t ); // BSD version int strerror_r( int, char *, size_t ); // BSD version
int strerror_r( int, char *, size_t ); // XSI version int strerror_r( int, char *, size_t ); // XSI version
BSD version returns error code, while XSI version returns 0 or -1 and sets errno. BSD version returns error code, while XSI version returns 0 or -1 and
sets errno.
*/ */
#if (defined(__ANDROID__) && __ANDROID_API__ < 23) || ( ( _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 ) && ! _GNU_SOURCE ) #if (defined(__ANDROID__) && __ANDROID_API__ < 23) \
|| ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
// XSI version of strerror_r. // XSI version of strerror_r.
#warning Not tested! #warning Not tested!
buffer_t buffer(200); buffer_t buffer(200);
int count = _count; int count = _count;
for ( ; ; ) { for (;;)
{
int rc = strerror_r(err, &buffer.front(), buffer.size()); int rc = strerror_r(err, &buffer.front(), buffer.size());
if ( rc == -1 ) { if (rc == -1)
{
int _err = errno; int _err = errno;
if ( _err == ERANGE ) { if (_err == ERANGE)
if ( count > 0 ) { {
if (count > 0)
{
// Enlarge the buffer. // Enlarge the buffer.
--count; --count;
buffer.resize(buffer.size() * 2); buffer.resize(buffer.size() * 2);
} else { }
else
{
std::stringstream ostr; std::stringstream ostr;
ostr ostr << "Error " << err << " "
<< "Error " << err << " "
<< "(Getting error message failed: " << "(Getting error message failed: "
<< "Buffer of " << buffer.size() << " bytes is still too small" << "Buffer of " << buffer.size()
<< " bytes is still too small"
<< ")"; << ")";
return ostr.str(); return ostr.str();
}; // if }; // if
} else { }
else
{
std::stringstream ostr; std::stringstream ostr;
ostr ostr << "Error " << err << " "
<< "Error " << err << " "
<< "(Getting error message failed: " << "(Getting error message failed: "
<< ( level < 2 ? _err_msg( _err, level + 1 ) : "Oops" ) << (level < 2 ? _err_msg(_err, level + 1) : "Oops") << ")";
<< ")";
return ostr.str(); return ostr.str();
}; // if }; // if
} else { }
else
{
// We got the message. // We got the message.
return &buffer.front(); return &buffer.front();
}; // if }; // if
@@ -241,55 +257,52 @@ int const _count = 8; // How many times we will try to double buff
} // _err_msg } // _err_msg
std::string std::string dir_sep() { return "/"; } // dir_sep
dir_sep(
) {
return "/";
} // dir_sep
std::string std::string exe_path()
exe_path( {
) {
static std::string const exe = "/proc/self/exe"; static std::string const exe = "/proc/self/exe";
buffer_t path(_size); buffer_t path(_size);
int count = _count; // Max number of iterations. int count = _count; // Max number of iterations.
for ( ; ; ) { for (;;)
{
ssize_t len = readlink(exe.c_str(), &path.front(), path.size()); ssize_t len = readlink(exe.c_str(), &path.front(), path.size());
if ( len < 0 ) { if (len < 0)
{
// Oops. // Oops.
int err = errno; int err = errno;
log_error( log_error("ERROR: Getting executable path failed: "
"ERROR: Getting executable path failed: "
"Reading symlink `%s' failed: %s\n", "Reading symlink `%s' failed: %s\n",
exe.c_str(), err_msg( err ).c_str() exe.c_str(), err_msg(err).c_str());
);
exit(2); exit(2);
}; // if }; // if
if ( len < path.size() ) { if (len < path.size())
{
// We got the path. // We got the path.
path.resize(len); path.resize(len);
break; break;
}; // if }; // if
// Oops, buffer is too small. // Oops, buffer is too small.
if ( count > 0 ) { if (count > 0)
{
--count; --count;
// Enlarge the buffer. // Enlarge the buffer.
path.resize(path.size() * 2); path.resize(path.size() * 2);
} else { }
log_error( else
"ERROR: Getting executable path failed: " {
"Reading symlink `%s' failed: Buffer of %lu bytes is still too small\n", log_error("ERROR: Getting executable path failed: "
exe.c_str(), "Reading symlink `%s' failed: Buffer of %lu bytes is "
(unsigned long) path.size() "still too small\n",
); exe.c_str(), (unsigned long)path.size());
exit(2); exit(2);
}; // if }; // if
@@ -300,12 +313,13 @@ int const _count = 8; // How many times we will try to double buff
} // exe_path } // exe_path
std::string std::string exe_dir()
exe_dir( {
) {
std::string path = exe_path(); std::string path = exe_path();
// We cannot pass path.c_str() to `dirname' bacause `dirname' modifies its argument. // We cannot pass path.c_str() to `dirname' bacause `dirname' modifies its
buffer_t buffer( path.c_str(), path.c_str() + path.size() + 1 ); // Copy with trailing zero. // argument.
buffer_t buffer(path.c_str(),
path.c_str() + path.size() + 1); // Copy with trailing zero.
return dirname(&buffer.front()); return dirname(&buffer.front());
} // exe_dir } // exe_dir
@@ -327,48 +341,40 @@ int const _count = 8; // How many times we will try to double buff
#include <algorithm> #include <algorithm>
static static std::string _err_msg(int err, int level)
std::string {
_err_msg(
int err,
int level
) {
std::string msg; std::string msg;
LPSTR buffer = NULL; LPSTR buffer = NULL;
DWORD flags = DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_ALLOCATE_BUFFER | | FORMAT_MESSAGE_IGNORE_INSERTS;
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
DWORD len = DWORD len = FormatMessageA(flags, NULL, err, LANG_USER_DEFAULT,
FormatMessageA( reinterpret_cast<LPSTR>(&buffer), 0, NULL);
flags,
NULL,
err,
LANG_USER_DEFAULT,
reinterpret_cast< LPSTR >( & buffer ),
0,
NULL
);
if ( buffer == NULL || len == 0 ) { if (buffer == NULL || len == 0)
{
int _err = GetLastError(); int _err = GetLastError();
char str[1024] = { 0 }; char str[1024] = { 0 };
snprintf(str, sizeof(str), "Error 0x%08x (Getting error message failed: %s )", err, ( level < 2 ? _err_msg( _err, level + 1 ).c_str() : "Oops" )); snprintf(str, sizeof(str),
"Error 0x%08x (Getting error message failed: %s )", err,
(level < 2 ? _err_msg(_err, level + 1).c_str() : "Oops"));
msg = std::string(str); msg = std::string(str);
}
} else { else
{
// Trim trailing whitespace (including `\r' and `\n'). // Trim trailing whitespace (including `\r' and `\n').
while ( len > 0 && isspace( buffer[ len - 1 ] ) ) { while (len > 0 && isspace(buffer[len - 1]))
{
--len; --len;
}; // while }; // while
// Drop trailing full stop. // Drop trailing full stop.
if ( len > 0 && buffer[ len - 1 ] == '.' ) { if (len > 0 && buffer[len - 1] == '.')
{
--len; --len;
}; // if }; // if
@@ -376,7 +382,8 @@ int const _count = 8; // How many times we will try to double buff
}; // if }; // if
if ( buffer != NULL ) { if (buffer != NULL)
{
LocalFree(buffer); LocalFree(buffer);
}; // if }; // if
@@ -385,45 +392,45 @@ int const _count = 8; // How many times we will try to double buff
} // _get_err_msg } // _get_err_msg
std::string std::string dir_sep() { return "\\"; } // dir_sep
dir_sep(
) {
return "\\";
} // dir_sep
std::string std::string exe_path()
exe_path( {
) {
buffer_t path(_size); buffer_t path(_size);
int count = _count; int count = _count;
for ( ; ; ) { for (;;)
{
DWORD len = GetModuleFileNameA(NULL, &path.front(), path.size()); DWORD len = GetModuleFileNameA(NULL, &path.front(), path.size());
if ( len == 0 ) { if (len == 0)
{
int err = GetLastError(); int err = GetLastError();
log_error( "ERROR: Getting executable path failed: %s\n", err_msg( err ).c_str() ); log_error("ERROR: Getting executable path failed: %s\n",
err_msg(err).c_str());
exit(2); exit(2);
}; // if }; // if
if ( len < path.size() ) { if (len < path.size())
{
path.resize(len); path.resize(len);
break; break;
}; // if }; // if
// Buffer too small. // Buffer too small.
if ( count > 0 ) { if (count > 0)
{
--count; --count;
path.resize(path.size() * 2); path.resize(path.size() * 2);
} else { }
log_error( else
"ERROR: Getting executable path failed: " {
log_error("ERROR: Getting executable path failed: "
"Buffer of %lu bytes is still too small\n", "Buffer of %lu bytes is still too small\n",
(unsigned long) path.size() (unsigned long)path.size());
);
exit(2); exit(2);
}; // if }; // if
@@ -434,9 +441,8 @@ int const _count = 8; // How many times we will try to double buff
} // exe_path } // exe_path
std::string std::string exe_dir()
exe_dir( {
) {
std::string exe = exe_path(); std::string exe = exe_path();
int count = 0; int count = 0;
@@ -446,62 +452,61 @@ int const _count = 8; // How many times we will try to double buff
buffer_t dir(_MAX_DIR); buffer_t dir(_MAX_DIR);
count = _count; count = _count;
#if defined(_MSC_VER) #if defined(_MSC_VER)
for ( ; ; ) { for (;;)
{
int rc = int rc =
_splitpath_s( _splitpath_s(exe.c_str(), &drv.front(), drv.size(), &dir.front(),
exe.c_str(), dir.size(), NULL, 0, // We need neither name
& drv.front(), drv.size(),
& dir.front(), dir.size(),
NULL, 0, // We need neither name
NULL, 0 // nor extension NULL, 0 // nor extension
); );
if ( rc == 0 ) { if (rc == 0)
{
break; break;
} else if ( rc == ERANGE ) { }
if ( count > 0 ) { else if (rc == ERANGE)
{
if (count > 0)
{
--count; --count;
// Buffer is too small, but it is not clear which one. // Buffer is too small, but it is not clear which one.
// So we have to enlarge all. // So we have to enlarge all.
drv.resize(drv.size() * 2); drv.resize(drv.size() * 2);
dir.resize(dir.size() * 2); dir.resize(dir.size() * 2);
} else { }
log_error( else
"ERROR: Getting executable path failed: " {
log_error("ERROR: Getting executable path failed: "
"Splitting path `%s' to components failed: " "Splitting path `%s' to components failed: "
"Buffers of %lu and %lu bytes are still too small\n", "Buffers of %lu and %lu bytes are still too small\n",
exe.c_str(), exe.c_str(), (unsigned long)drv.size(),
(unsigned long) drv.size(), (unsigned long)dir.size());
(unsigned long) dir.size()
);
exit(2); exit(2);
}; // if }; // if
} else { }
log_error( else
"ERROR: Getting executable path failed: " {
log_error("ERROR: Getting executable path failed: "
"Splitting path `%s' to components failed: %s\n", "Splitting path `%s' to components failed: %s\n",
exe.c_str(), exe.c_str(), err_msg(rc).c_str());
err_msg( rc ).c_str()
);
exit(2); exit(2);
}; // if }; // if
}; // forever }; // forever
#else // __MINGW32__ #else // __MINGW32__
// MinGW does not have the "secure" _splitpath_s, use the insecure version instead. // MinGW does not have the "secure" _splitpath_s, use the insecure version
_splitpath( // instead.
exe.c_str(), _splitpath(exe.c_str(), &drv.front(), &dir.front(),
& drv.front(),
& dir.front(),
NULL, // We need neither name NULL, // We need neither name
NULL // nor extension NULL // nor extension
); );
#endif // __MINGW32__ #endif // __MINGW32__
// Combining components back to path. // Combining components back to path.
// I failed with "secure" `_makepath_s'. If buffer is too small, instead of returning // I failed with "secure" `_makepath_s'. If buffer is too small, instead of
// ERANGE, `_makepath_s' pops up dialog box and offers to debug the program. D'oh! // returning ERANGE, `_makepath_s' pops up dialog box and offers to debug
// So let us try to guess the size of result and go with insecure `_makepath'. // the program. D'oh! So let us try to guess the size of result and go with
// insecure `_makepath'.
buffer_t path(std::max(drv.size() + dir.size(), size_t(_MAX_PATH)) + 10); buffer_t path(std::max(drv.size() + dir.size(), size_t(_MAX_PATH)) + 10);
_makepath(&path.front(), &drv.front(), &dir.front(), NULL, NULL); _makepath(&path.front(), &drv.front(), &dir.front(), NULL, NULL);
@@ -513,14 +518,7 @@ int const _count = 8; // How many times we will try to double buff
#endif // _WIN32 #endif // _WIN32
std::string std::string err_msg(int err) { return _err_msg(err, 0); } // err_msg
err_msg(
int err
) {
return _err_msg( err, 0 );
} // err_msg
// ================================================================================================= // =================================================================================================
@@ -528,37 +526,32 @@ err_msg(
// ================================================================================================= // =================================================================================================
char * char* get_err_msg(int err)
get_err_msg( {
int err
) {
char* msg = strdup(err_msg(err).c_str()); char* msg = strdup(err_msg(err).c_str());
CHECK_PTR(msg); CHECK_PTR(msg);
return msg; return msg;
} // get_err_msg } // get_err_msg
char * char* get_dir_sep()
get_dir_sep( {
) {
char* sep = strdup(dir_sep().c_str()); char* sep = strdup(dir_sep().c_str());
CHECK_PTR(sep); CHECK_PTR(sep);
return sep; return sep;
} // get_dir_sep } // get_dir_sep
char * char* get_exe_path()
get_exe_path( {
) {
char* path = strdup(exe_path().c_str()); char* path = strdup(exe_path().c_str());
CHECK_PTR(path); CHECK_PTR(path);
return path; return path;
} // get_exe_path } // get_exe_path
char * char* get_exe_dir()
get_exe_dir( {
) {
char* dir = strdup(exe_dir().c_str()); char* dir = strdup(exe_dir().c_str());
CHECK_PTR(dir); CHECK_PTR(dir);
return dir; return dir;

View File

@@ -36,24 +36,33 @@ std::string gCompilationProgram = DEFAULT_COMPILATION_PROGRAM;
void helpInfo() void helpInfo()
{ {
log_info("Common options:\n" log_info(
" -h, --help This help\n" R"(Common options:
" --compilation-mode <mode> Specify a compilation mode. Mode can be:\n" -h, --help
" online Use online compilation (default)\n" This help
" binary Use binary offline compilation\n" --compilation-mode <mode>
" spir-v Use SPIR-V offline compilation\n" Specify a compilation mode. Mode can be:
"\n" online Use online compilation (default)
" For offline compilation (binary and spir-v modes) only:\n" binary Use binary offline compilation
" --compilation-cache-mode <cache-mode> Specify a compilation caching mode:\n" spir-v Use SPIR-V offline compilation
" compile-if-absent Read from cache if already populated, or\n"
" else perform offline compilation (default)\n" For offline compilation (binary and spir-v modes) only:
" force-read Force reading from the cache\n" --compilation-cache-mode <cache-mode>
" overwrite Disable reading from the cache\n" Specify a compilation caching mode:
" dump-cl-files Dumps the .cl and build .options files used by the test suite\n" compile-if-absent
" --compilation-cache-path <path> Path for offline compiler output and CL source\n" Read from cache if already populated, or else perform
" --compilation-program <prog> Program to use for offline compilation,\n" offline compilation (default)
" defaults to " DEFAULT_COMPILATION_PROGRAM "\n" force-read
"\n"); Force reading from the cache
overwrite
Disable reading from the cache
dump-cl-files
Dumps the .cl and build .options files used by the test suite
--compilation-cache-path <path>
Path for offline compiler output and CL source
--compilation-program <prog>
Program to use for offline compilation, defaults to:
)" DEFAULT_COMPILATION_PROGRAM "\n\n");
} }
int parseCustomParam(int argc, const char *argv[], const char *ignore) int parseCustomParam(int argc, const char *argv[], const char *ignore)
@@ -64,12 +73,14 @@ int parseCustomParam (int argc, const char *argv[], const char *ignore)
{ {
if (ignore != 0) if (ignore != 0)
{ {
// skip parameters that require special/different treatment in application // skip parameters that require special/different treatment in
// (generic interpretation and parameter removal will not be performed) // application (generic interpretation and parameter removal will
// not be performed)
const char *ptr = strstr(ignore, argv[i]); const char *ptr = strstr(ignore, argv[i]);
if(ptr != 0 && if (ptr != 0 && (ptr == ignore || ptr[-1] == ' ')
(ptr == ignore || ptr[-1] == ' ') && //first on list or ' ' before && // first on list or ' ' before
(ptr[strlen(argv[i])] == 0 || ptr[strlen(argv[i])] == ' ')) // last on list or ' ' after (ptr[strlen(argv[i])] == 0
|| ptr[strlen(argv[i])] == ' ')) // last on list or ' ' after
continue; continue;
} }
@@ -142,15 +153,18 @@ int parseCustomParam (int argc, const char *argv[], const char *ignore)
} }
else else
{ {
log_error("Compilation cache mode not recognized: %s\n", mode); log_error("Compilation cache mode not recognized: %s\n",
mode);
return -1; return -1;
} }
log_info("Compilation cache mode specified: %s\n", mode); log_info("Compilation cache mode specified: %s\n", mode);
} }
else else
{ {
log_error("Compilation cache mode parameters are incorrect. Usage:\n" log_error(
" --compilation-cache-mode <compile-if-absent|force-read|overwrite>\n"); "Compilation cache mode parameters are incorrect. Usage:\n"
" --compilation-cache-mode "
"<compile-if-absent|force-read|overwrite>\n");
return -1; return -1;
} }
} }
@@ -164,7 +178,8 @@ int parseCustomParam (int argc, const char *argv[], const char *ignore)
} }
else else
{ {
log_error("Path argument for --compilation-cache-path was not specified.\n"); log_error("Path argument for --compilation-cache-path was not "
"specified.\n");
return -1; return -1;
} }
} }
@@ -178,34 +193,34 @@ int parseCustomParam (int argc, const char *argv[], const char *ignore)
} }
else else
{ {
log_error("Program argument for --compilation-program was not specified.\n"); log_error("Program argument for --compilation-program was not "
"specified.\n");
return -1; return -1;
} }
} }
// cleaning parameters from argv tab // cleaning parameters from argv tab
for (int j = i; j < argc - delArg; j++) for (int j = i; j < argc - delArg; j++) argv[j] = argv[j + delArg];
argv[j] = argv[j + delArg];
argc -= delArg; argc -= delArg;
i -= delArg; i -= delArg;
} }
if ((gCompilationCacheMode == kCacheModeForceRead || gCompilationCacheMode == kCacheModeOverwrite) if ((gCompilationCacheMode == kCacheModeForceRead
|| gCompilationCacheMode == kCacheModeOverwrite)
&& gCompilationMode == kOnline) && gCompilationMode == kOnline)
{ {
log_error("Compilation cache mode can only be specified when using an offline compilation mode.\n"); log_error("Compilation cache mode can only be specified when using an "
"offline compilation mode.\n");
return -1; return -1;
} }
return argc; return argc;
} }
bool is_power_of_two(int number) bool is_power_of_two(int number) { return number && !(number & (number - 1)); }
{
return number && !(number & (number - 1));
}
extern void parseWimpyReductionFactor(const char *&arg, int &wimpyReductionFactor) extern void parseWimpyReductionFactor(const char *&arg,
int &wimpyReductionFactor)
{ {
const char *arg_temp = strchr(&arg[1], ']'); const char *arg_temp = strchr(&arg[1], ']');
if (arg_temp != 0) if (arg_temp != 0)
@@ -214,12 +229,15 @@ extern void parseWimpyReductionFactor(const char *&arg, int &wimpyReductionFacto
arg = arg_temp; // Advance until ']' arg = arg_temp; // Advance until ']'
if (is_power_of_two(new_factor)) if (is_power_of_two(new_factor))
{ {
log_info("\n Wimpy reduction factor changed from %d to %d \n", wimpyReductionFactor, new_factor); log_info("\n Wimpy reduction factor changed from %d to %d \n",
wimpyReductionFactor, new_factor);
wimpyReductionFactor = new_factor; wimpyReductionFactor = new_factor;
} }
else else
{ {
log_info("\n WARNING: Incorrect wimpy reduction factor %d, must be power of 2. The default value will be used.\n", new_factor); log_info("\n WARNING: Incorrect wimpy reduction factor %d, must be "
"power of 2. The default value will be used.\n",
new_factor);
} }
} }
} }

View File

@@ -39,8 +39,10 @@ extern CompilationCacheMode gCompilationCacheMode;
extern std::string gCompilationCachePath; extern std::string gCompilationCachePath;
extern std::string gCompilationProgram; extern std::string gCompilationProgram;
extern int parseCustomParam (int argc, const char *argv[], const char *ignore = 0 ); extern int parseCustomParam(int argc, const char *argv[],
const char *ignore = 0);
extern void parseWimpyReductionFactor(const char *&arg, int &wimpyReductionFactor); extern void parseWimpyReductionFactor(const char *&arg,
int &wimpyReductionFactor);
#endif // _parseParameters_h #endif // _parseParameters_h

View File

@@ -18,16 +18,21 @@
#define MARK_REF_COUNT_BASE(c, type, bigType) \ #define MARK_REF_COUNT_BASE(c, type, bigType) \
cl_uint c##_refCount; \ cl_uint c##_refCount; \
error = clGet##type##Info( c, CL_##bigType##_REFERENCE_COUNT, sizeof( c##_refCount ), &c##_refCount, NULL ); \ error = clGet##type##Info(c, CL_##bigType##_REFERENCE_COUNT, \
sizeof(c##_refCount), &c##_refCount, NULL); \
test_error(error, "Unable to check reference count for " #type); test_error(error, "Unable to check reference count for " #type);
#define TEST_REF_COUNT_BASE(c, type, bigType) \ #define TEST_REF_COUNT_BASE(c, type, bigType) \
cl_uint c##_refCount_new; \ cl_uint c##_refCount_new; \
error = clGet##type##Info( c, CL_##bigType##_REFERENCE_COUNT, sizeof( c##_refCount_new ), &c##_refCount_new, NULL ); \ error = \
clGet##type##Info(c, CL_##bigType##_REFERENCE_COUNT, \
sizeof(c##_refCount_new), &c##_refCount_new, NULL); \
test_error(error, "Unable to check reference count for " #type); \ test_error(error, "Unable to check reference count for " #type); \
if (c##_refCount != c##_refCount_new) \ if (c##_refCount != c##_refCount_new) \
{ \ { \
log_error( "ERROR: Reference count for " #type " changed! (was %d, now %d)\n", c##_refCount, c##_refCount_new ); \ log_error("ERROR: Reference count for " #type \
" changed! (was %d, now %d)\n", \
c##_refCount, c##_refCount_new); \
return -1; \ return -1; \
} }

View File

@@ -37,13 +37,16 @@
#define _ARM_FE_TOWARDZERO 0xc00000 #define _ARM_FE_TOWARDZERO 0xc00000
RoundingMode set_round(RoundingMode r, Type outType) RoundingMode set_round(RoundingMode r, Type outType)
{ {
static const int flt_rounds[ kRoundingModeCount ] = { _ARM_FE_TONEAREST, static const int flt_rounds[kRoundingModeCount] = {
_ARM_FE_TONEAREST, _ARM_FE_UPWARD, _ARM_FE_DOWNWARD, _ARM_FE_TOWARDZERO }; _ARM_FE_TONEAREST, _ARM_FE_TONEAREST, _ARM_FE_UPWARD, _ARM_FE_DOWNWARD,
static const int int_rounds[ kRoundingModeCount ] = { _ARM_FE_TOWARDZERO, _ARM_FE_TOWARDZERO
_ARM_FE_TONEAREST, _ARM_FE_UPWARD, _ARM_FE_DOWNWARD, _ARM_FE_TOWARDZERO }; };
static const int int_rounds[kRoundingModeCount] = {
_ARM_FE_TOWARDZERO, _ARM_FE_TONEAREST, _ARM_FE_UPWARD, _ARM_FE_DOWNWARD,
_ARM_FE_TOWARDZERO
};
const int *p = int_rounds; const int *p = int_rounds;
if( outType == kfloat || outType == kdouble ) if (outType == kfloat || outType == kdouble) p = flt_rounds;
p = flt_rounds;
int fpscr = 0; int fpscr = 0;
RoundingMode oldRound = get_round(); RoundingMode oldRound = get_round();
@@ -64,14 +67,10 @@ RoundingMode get_round( void )
switch (oldRound) switch (oldRound)
{ {
case _ARM_FE_TONEAREST: case _ARM_FE_TONEAREST: return kRoundToNearestEven;
return kRoundToNearestEven; case _ARM_FE_UPWARD: return kRoundUp;
case _ARM_FE_UPWARD: case _ARM_FE_DOWNWARD: return kRoundDown;
return kRoundUp; case _ARM_FE_TOWARDZERO: return kRoundTowardZero;
case _ARM_FE_DOWNWARD:
return kRoundDown;
case _ARM_FE_TOWARDZERO:
return kRoundTowardZero;
} }
return kDefaultRoundingMode; return kDefaultRoundingMode;
@@ -80,26 +79,24 @@ RoundingMode get_round( void )
#elif !(defined(_WIN32) && defined(_MSC_VER)) #elif !(defined(_WIN32) && defined(_MSC_VER))
RoundingMode set_round(RoundingMode r, Type outType) RoundingMode set_round(RoundingMode r, Type outType)
{ {
static const int flt_rounds[ kRoundingModeCount ] = { FE_TONEAREST, FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO }; static const int flt_rounds[kRoundingModeCount] = {
static const int int_rounds[ kRoundingModeCount ] = { FE_TOWARDZERO, FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO }; FE_TONEAREST, FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
};
static const int int_rounds[kRoundingModeCount] = {
FE_TOWARDZERO, FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
};
const int *p = int_rounds; const int *p = int_rounds;
if( outType == kfloat || outType == kdouble ) if (outType == kfloat || outType == kdouble) p = flt_rounds;
p = flt_rounds;
int oldRound = fegetround(); int oldRound = fegetround();
fesetround(p[r]); fesetround(p[r]);
switch (oldRound) switch (oldRound)
{ {
case FE_TONEAREST: case FE_TONEAREST: return kRoundToNearestEven;
return kRoundToNearestEven; case FE_UPWARD: return kRoundUp;
case FE_UPWARD: case FE_DOWNWARD: return kRoundDown;
return kRoundUp; case FE_TOWARDZERO: return kRoundTowardZero;
case FE_DOWNWARD: default: abort(); // ??!
return kRoundDown;
case FE_TOWARDZERO:
return kRoundTowardZero;
default:
abort(); // ??!
} }
return kDefaultRoundingMode; // never happens return kDefaultRoundingMode; // never happens
} }
@@ -110,14 +107,10 @@ RoundingMode get_round( void )
switch (oldRound) switch (oldRound)
{ {
case FE_TONEAREST: case FE_TONEAREST: return kRoundToNearestEven;
return kRoundToNearestEven; case FE_UPWARD: return kRoundUp;
case FE_UPWARD: case FE_DOWNWARD: return kRoundDown;
return kRoundUp; case FE_TOWARDZERO: return kRoundTowardZero;
case FE_DOWNWARD:
return kRoundDown;
case FE_TOWARDZERO:
return kRoundTowardZero;
} }
return kDefaultRoundingMode; return kDefaultRoundingMode;
@@ -126,25 +119,33 @@ RoundingMode get_round( void )
#else #else
RoundingMode set_round(RoundingMode r, Type outType) RoundingMode set_round(RoundingMode r, Type outType)
{ {
static const int flt_rounds[ kRoundingModeCount ] = { _RC_NEAR, _RC_NEAR, _RC_UP, _RC_DOWN, _RC_CHOP }; static const int flt_rounds[kRoundingModeCount] = { _RC_NEAR, _RC_NEAR,
static const int int_rounds[ kRoundingModeCount ] = { _RC_CHOP, _RC_NEAR, _RC_UP, _RC_DOWN, _RC_CHOP }; _RC_UP, _RC_DOWN,
const int *p = ( outType == kfloat || outType == kdouble )? flt_rounds : int_rounds; _RC_CHOP };
static const int int_rounds[kRoundingModeCount] = { _RC_CHOP, _RC_NEAR,
_RC_UP, _RC_DOWN,
_RC_CHOP };
const int *p =
(outType == kfloat || outType == kdouble) ? flt_rounds : int_rounds;
unsigned int oldRound; unsigned int oldRound;
int err = _controlfp_s(&oldRound, 0, 0); // get rounding mode into oldRound int err = _controlfp_s(&oldRound, 0, 0); // get rounding mode into oldRound
if (err) { if (err)
vlog_error("\t\tERROR: -- cannot get rounding mode in %s:%d\n", __FILE__, __LINE__); {
vlog_error("\t\tERROR: -- cannot get rounding mode in %s:%d\n",
__FILE__, __LINE__);
return kDefaultRoundingMode; // what else never happens return kDefaultRoundingMode; // what else never happens
} }
oldRound &= _MCW_RC; oldRound &= _MCW_RC;
RoundingMode old = RoundingMode old = (oldRound == _RC_NEAR)
(oldRound == _RC_NEAR)? kRoundToNearestEven : ? kRoundToNearestEven
(oldRound == _RC_UP)? kRoundUp : : (oldRound == _RC_UP) ? kRoundUp
(oldRound == _RC_DOWN)? kRoundDown : : (oldRound == _RC_DOWN)
(oldRound == _RC_CHOP)? kRoundTowardZero: ? kRoundDown
kDefaultRoundingMode; : (oldRound == _RC_CHOP) ? kRoundTowardZero
: kDefaultRoundingMode;
_controlfp_s(&oldRound, p[r], _MCW_RC); // setting new rounding mode _controlfp_s(&oldRound, p[r], _MCW_RC); // setting new rounding mode
return old; // returning old rounding mode return old; // returning old rounding mode
@@ -156,34 +157,41 @@ RoundingMode get_round( void )
int err = _controlfp_s(&oldRound, 0, 0); // get rounding mode into oldRound int err = _controlfp_s(&oldRound, 0, 0); // get rounding mode into oldRound
oldRound &= _MCW_RC; oldRound &= _MCW_RC;
return return (oldRound == _RC_NEAR)
(oldRound == _RC_NEAR)? kRoundToNearestEven : ? kRoundToNearestEven
(oldRound == _RC_UP)? kRoundUp : : (oldRound == _RC_UP) ? kRoundUp
(oldRound == _RC_DOWN)? kRoundDown : : (oldRound == _RC_DOWN)
(oldRound == _RC_CHOP)? kRoundTowardZero: ? kRoundDown
kDefaultRoundingMode; : (oldRound == _RC_CHOP) ? kRoundTowardZero
: kDefaultRoundingMode;
} }
#endif #endif
// //
// FlushToZero() sets the host processor into ftz mode. It is intended to have a remote effect on the behavior of the code in // FlushToZero() sets the host processor into ftz mode. It is intended to have
// basic_test_conversions.c. Some host processors may not support this mode, which case you'll need to do some clamping in // a remote effect on the behavior of the code in basic_test_conversions.c. Some
// software by testing against FLT_MIN or DBL_MIN in that file. // host processors may not support this mode, which case you'll need to do some
// clamping in software by testing against FLT_MIN or DBL_MIN in that file.
// //
// Note: IEEE-754 says conversions are basic operations. As such they do *NOT* have the behavior in section 7.5.3 of // Note: IEEE-754 says conversions are basic operations. As such they do *NOT*
// the OpenCL spec. They *ALWAYS* flush to zero for subnormal inputs or outputs when FTZ mode is on like other basic // have the behavior in section 7.5.3 of the OpenCL spec. They *ALWAYS* flush to
// zero for subnormal inputs or outputs when FTZ mode is on like other basic
// operators do (e.g. add, subtract, multiply, divide, etc.) // operators do (e.g. add, subtract, multiply, divide, etc.)
// //
// Configuring hardware to FTZ mode varies by platform. // Configuring hardware to FTZ mode varies by platform.
// CAUTION: Some C implementations may also fail to behave properly in this mode. // CAUTION: Some C implementations may also fail to behave properly in this
// mode.
// //
// On PowerPC, it is done by setting the FPSCR into non-IEEE mode. // On PowerPC, it is done by setting the FPSCR into non-IEEE mode.
// On Intel, you can do this by turning on the FZ and DAZ bits in the MXCSR -- provided that SSE/SSE2 // On Intel, you can do this by turning on the FZ and DAZ bits in the MXCSR --
// is used for floating point computation! If your OS uses x87, you'll need to figure out how // provided that SSE/SSE2
// to turn that off for the conversions code in basic_test_conversions.c so that they flush to // is used for floating point computation! If your OS uses x87, you'll
// zero properly. Otherwise, you'll need to add appropriate software clamping to basic_test_conversions.c // need to figure out how to turn that off for the conversions code in
// in which case, these function are at liberty to do nothing. // basic_test_conversions.c so that they flush to zero properly.
// Otherwise, you'll need to add appropriate software clamping to
// basic_test_conversions.c in which case, these function are at
// liberty to do nothing.
// //
#if defined(__i386__) || defined(__x86_64__) || defined(_WIN32) #if defined(__i386__) || defined(__x86_64__) || defined(_WIN32)
#include <xmmintrin.h> #include <xmmintrin.h>
@@ -194,7 +202,10 @@ void *FlushToZero( void )
{ {
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32) #if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
#if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER) #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER)
union{ int i; void *p; }u = { _mm_getcsr() }; union {
int i;
void *p;
} u = { _mm_getcsr() };
_mm_setcsr(u.i | 0x8040); _mm_setcsr(u.i | 0x8040);
return u.p; return u.p;
#elif defined(__arm__) || defined(__aarch64__) #elif defined(__arm__) || defined(__aarch64__)
@@ -216,12 +227,16 @@ void *FlushToZero( void )
#endif #endif
} }
// Undo the effects of FlushToZero above, restoring the host to default behavior, using the information passed in p. // Undo the effects of FlushToZero above, restoring the host to default
// behavior, using the information passed in p.
void UnFlushToZero(void *p) void UnFlushToZero(void *p)
{ {
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32) #if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
#if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER) #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER)
union{ void *p; int i; }u = { p }; union {
void *p;
int i;
} u = { p };
_mm_setcsr(u.i); _mm_setcsr(u.i);
#elif defined(__arm__) || defined(__aarch64__) #elif defined(__arm__) || defined(__aarch64__)
int fpscr; int fpscr;

View File

@@ -59,5 +59,4 @@ extern void *FlushToZero( void );
extern void UnFlushToZero(void *p); extern void UnFlushToZero(void *p);
#endif /* __ROUNDING_MODE_H__ */ #endif /* __ROUNDING_MODE_H__ */

View File

@@ -60,20 +60,25 @@ bool gCoreILProgram = true;
#define DEFAULT_NUM_ELEMENTS 0x4000 #define DEFAULT_NUM_ELEMENTS 0x4000
int runTestHarness( int argc, const char *argv[], int testNum, test_definition testList[], int runTestHarness(int argc, const char *argv[], int testNum,
int imageSupportRequired, int forceNoContextCreation, cl_command_queue_properties queueProps ) test_definition testList[], int imageSupportRequired,
int forceNoContextCreation,
cl_command_queue_properties queueProps)
{ {
return runTestHarnessWithCheck( argc, argv, testNum, testList, forceNoContextCreation, queueProps, return runTestHarnessWithCheck(
argc, argv, testNum, testList, forceNoContextCreation, queueProps,
(imageSupportRequired) ? verifyImageSupport : NULL); (imageSupportRequired) ? verifyImageSupport : NULL);
} }
int skip_init_info(int count) { int skip_init_info(int count)
{
log_info("Test skipped while initialization\n"); log_info("Test skipped while initialization\n");
log_info("SKIPPED %d of %d tests.\n", count, count); log_info("SKIPPED %d of %d tests.\n", count, count);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int fail_init_info(int count) { int fail_init_info(int count)
{
log_info("Test failed while initialization\n"); log_info("Test failed while initialization\n");
log_info("FAILED %d of %d tests.\n", count, count); log_info("FAILED %d of %d tests.\n", count, count);
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -86,8 +91,10 @@ void version_expected_info(const char *test_name, const char *api_name,
"reports %s version %s)\n", "reports %s version %s)\n",
test_name, api_name, expected_version, api_name, device_version); test_name, api_name, expected_version, api_name, device_version);
} }
int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_definition testList[], int runTestHarnessWithCheck(int argc, const char *argv[], int testNum,
int forceNoContextCreation, cl_command_queue_properties queueProps, test_definition testList[],
int forceNoContextCreation,
cl_command_queue_properties queueProps,
DeviceCheckFn deviceCheckFn) DeviceCheckFn deviceCheckFn)
{ {
test_start(); test_start();
@@ -112,17 +119,23 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
if (env_mode != NULL) if (env_mode != NULL)
{ {
based_on_env_var = 1; based_on_env_var = 1;
if( strcmp( env_mode, "gpu" ) == 0 || strcmp( env_mode, "CL_DEVICE_TYPE_GPU" ) == 0 ) if (strcmp(env_mode, "gpu") == 0
|| strcmp(env_mode, "CL_DEVICE_TYPE_GPU") == 0)
device_type = CL_DEVICE_TYPE_GPU; device_type = CL_DEVICE_TYPE_GPU;
else if( strcmp( env_mode, "cpu" ) == 0 || strcmp( env_mode, "CL_DEVICE_TYPE_CPU" ) == 0 ) else if (strcmp(env_mode, "cpu") == 0
|| strcmp(env_mode, "CL_DEVICE_TYPE_CPU") == 0)
device_type = CL_DEVICE_TYPE_CPU; device_type = CL_DEVICE_TYPE_CPU;
else if( strcmp( env_mode, "accelerator" ) == 0 || strcmp( env_mode, "CL_DEVICE_TYPE_ACCELERATOR" ) == 0 ) else if (strcmp(env_mode, "accelerator") == 0
|| strcmp(env_mode, "CL_DEVICE_TYPE_ACCELERATOR") == 0)
device_type = CL_DEVICE_TYPE_ACCELERATOR; device_type = CL_DEVICE_TYPE_ACCELERATOR;
else if( strcmp( env_mode, "default" ) == 0 || strcmp( env_mode, "CL_DEVICE_TYPE_DEFAULT" ) == 0 ) else if (strcmp(env_mode, "default") == 0
|| strcmp(env_mode, "CL_DEVICE_TYPE_DEFAULT") == 0)
device_type = CL_DEVICE_TYPE_DEFAULT; device_type = CL_DEVICE_TYPE_DEFAULT;
else else
{ {
log_error( "Unknown CL_DEVICE_TYPE env variable setting: %s.\nAborting...\n", env_mode ); log_error("Unknown CL_DEVICE_TYPE env variable setting: "
"%s.\nAborting...\n",
env_mode);
abort(); abort();
} }
} }
@@ -162,17 +175,26 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
} }
/* Special case: just list the tests */ /* Special case: just list the tests */
if( ( argc > 1 ) && (!strcmp( argv[ 1 ], "-list" ) || !strcmp( argv[ 1 ], "-h" ) || !strcmp( argv[ 1 ], "--help" ))) if ((argc > 1)
&& (!strcmp(argv[1], "-list") || !strcmp(argv[1], "-h")
|| !strcmp(argv[1], "--help")))
{ {
char *fileName = getenv("CL_CONFORMANCE_RESULTS_FILENAME"); char *fileName = getenv("CL_CONFORMANCE_RESULTS_FILENAME");
log_info( "Usage: %s [<test name>*] [pid<num>] [id<num>] [<device type>]\n", argv[0] ); log_info(
log_info( "\t<test name>\tOne or more of: (wildcard character '*') (default *)\n"); "Usage: %s [<test name>*] [pid<num>] [id<num>] [<device type>]\n",
log_info( "\tpid<num>\tIndicates platform at index <num> should be used (default 0).\n" ); argv[0]);
log_info( "\tid<num>\t\tIndicates device at index <num> should be used (default 0).\n" ); log_info("\t<test name>\tOne or more of: (wildcard character '*') "
log_info( "\t<device_type>\tcpu|gpu|accelerator|<CL_DEVICE_TYPE_*> (default CL_DEVICE_TYPE_DEFAULT)\n" ); "(default *)\n");
log_info("\tpid<num>\tIndicates platform at index <num> should be used "
"(default 0).\n");
log_info("\tid<num>\t\tIndicates device at index <num> should be used "
"(default 0).\n");
log_info("\t<device_type>\tcpu|gpu|accelerator|<CL_DEVICE_TYPE_*> "
"(default CL_DEVICE_TYPE_DEFAULT)\n");
log_info("\n"); log_info("\n");
log_info( "\tNOTE: You may pass environment variable CL_CONFORMANCE_RESULTS_FILENAME (currently '%s')\n", log_info("\tNOTE: You may pass environment variable "
"CL_CONFORMANCE_RESULTS_FILENAME (currently '%s')\n",
fileName != NULL ? fileName : "<undefined>"); fileName != NULL ? fileName : "<undefined>");
log_info("\t to save results to JSON file.\n"); log_info("\t to save results to JSON file.\n");
@@ -198,13 +220,15 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
log_info(" Initializing random seed to 0.\n"); log_info(" Initializing random seed to 0.\n");
} }
/* Do we have an integer to specify the number of elements to pass to tests? */ /* Do we have an integer to specify the number of elements to pass to tests?
*/
if (argc > 1) if (argc > 1)
{ {
ret = (int)strtol(argv[argc - 1], &endPtr, 10); ret = (int)strtol(argv[argc - 1], &endPtr, 10);
if (endPtr != argv[argc - 1] && *endPtr == 0) if (endPtr != argv[argc - 1] && *endPtr == 0)
{ {
/* By spec, this means the entire string was a valid integer, so we treat it as a num_elements spec */ /* By spec, this means the entire string was a valid integer, so we
* treat it as a num_elements spec */
/* (hence why we stored the result in ret first) */ /* (hence why we stored the result in ret first) */
num_elements = ret; num_elements = ret;
log_info("Testing with num_elements of %d\n", num_elements); log_info("Testing with num_elements of %d\n", num_elements);
@@ -215,17 +239,20 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
/* Do we have a CPU/GPU specification? */ /* Do we have a CPU/GPU specification? */
if (argc > 1) if (argc > 1)
{ {
if( strcmp( argv[ argc - 1 ], "gpu" ) == 0 || strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_GPU" ) == 0 ) if (strcmp(argv[argc - 1], "gpu") == 0
|| strcmp(argv[argc - 1], "CL_DEVICE_TYPE_GPU") == 0)
{ {
device_type = CL_DEVICE_TYPE_GPU; device_type = CL_DEVICE_TYPE_GPU;
argc--; argc--;
} }
else if( strcmp( argv[ argc - 1 ], "cpu" ) == 0 || strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_CPU" ) == 0 ) else if (strcmp(argv[argc - 1], "cpu") == 0
|| strcmp(argv[argc - 1], "CL_DEVICE_TYPE_CPU") == 0)
{ {
device_type = CL_DEVICE_TYPE_CPU; device_type = CL_DEVICE_TYPE_CPU;
argc--; argc--;
} }
else if( strcmp( argv[ argc - 1 ], "accelerator" ) == 0 || strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_ACCELERATOR" ) == 0 ) else if (strcmp(argv[argc - 1], "accelerator") == 0
|| strcmp(argv[argc - 1], "CL_DEVICE_TYPE_ACCELERATOR") == 0)
{ {
device_type = CL_DEVICE_TYPE_ACCELERATOR; device_type = CL_DEVICE_TYPE_ACCELERATOR;
argc--; argc--;
@@ -240,7 +267,8 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
/* Did we choose a specific device index? */ /* Did we choose a specific device index? */
if (argc > 1) if (argc > 1)
{ {
if( strlen( argv[ argc - 1 ] ) >= 3 && argv[ argc - 1 ][0] == 'i' && argv[ argc - 1 ][1] == 'd' ) if (strlen(argv[argc - 1]) >= 3 && argv[argc - 1][0] == 'i'
&& argv[argc - 1][1] == 'd')
{ {
choosen_device_index = atoi(&(argv[argc - 1][2])); choosen_device_index = atoi(&(argv[argc - 1][2]));
argc--; argc--;
@@ -250,7 +278,8 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
/* Did we choose a specific platform index? */ /* Did we choose a specific platform index? */
if (argc > 1) if (argc > 1)
{ {
if( strlen( argv[ argc - 1 ] ) >= 3 && argv[ argc - 1 ][0] == 'p' && argv[ argc - 1 ][1] == 'i' && argv[ argc - 1 ][2] == 'd') if (strlen(argv[argc - 1]) >= 3 && argv[argc - 1][0] == 'p'
&& argv[argc - 1][1] == 'i' && argv[argc - 1][2] == 'd')
{ {
choosen_platform_index = atoi(&(argv[argc - 1][3])); choosen_platform_index = atoi(&(argv[argc - 1][3]));
argc--; argc--;
@@ -258,17 +287,22 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
} }
switch (device_type) switch (device_type)
{ {
case CL_DEVICE_TYPE_GPU: log_info("Requesting GPU device "); break; case CL_DEVICE_TYPE_GPU: log_info("Requesting GPU device "); break;
case CL_DEVICE_TYPE_CPU: log_info("Requesting CPU device "); break; case CL_DEVICE_TYPE_CPU: log_info("Requesting CPU device "); break;
case CL_DEVICE_TYPE_ACCELERATOR: log_info("Requesting Accelerator device "); break; case CL_DEVICE_TYPE_ACCELERATOR:
case CL_DEVICE_TYPE_DEFAULT: log_info("Requesting Default device "); break; log_info("Requesting Accelerator device ");
break;
case CL_DEVICE_TYPE_DEFAULT:
log_info("Requesting Default device ");
break;
default: log_error("Requesting unknown device "); return EXIT_FAILURE; default: log_error("Requesting unknown device "); return EXIT_FAILURE;
} }
log_info(based_on_env_var ? "based on environment variable " : "based on command line "); log_info(based_on_env_var ? "based on environment variable "
log_info("for platform index %d and device index %d\n", choosen_platform_index, choosen_device_index); : "based on command line ");
log_info("for platform index %d and device index %d\n",
choosen_platform_index, choosen_device_index);
#if defined(__APPLE__) #if defined(__APPLE__)
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
@@ -290,7 +324,8 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
else if (0 == strcasecmp(env, "SSE3")) else if (0 == strcasecmp(env, "SSE3"))
mask = kHasSSE4_2 | kHasSSE4_1 | kHasSupplementalSSE3; mask = kHasSSE4_2 | kHasSSE4_1 | kHasSupplementalSSE3;
else if (0 == strcasecmp(env, "SSE2")) else if (0 == strcasecmp(env, "SSE2"))
mask = kHasSSE4_2 | kHasSSE4_1 | kHasSupplementalSSE3 | kHasSSE3; mask =
kHasSSE4_2 | kHasSSE4_1 | kHasSupplementalSSE3 | kHasSSE3;
else else
{ {
log_error("Error: Unknown CL_MAX_SSE setting: %s\n", env); log_error("Error: Unknown CL_MAX_SSE setting: %s\n", env);
@@ -306,49 +341,63 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
/* Get the platform */ /* Get the platform */
err = clGetPlatformIDs(0, NULL, &num_platforms); err = clGetPlatformIDs(0, NULL, &num_platforms);
if (err) { if (err)
{
print_error(err, "clGetPlatformIDs failed"); print_error(err, "clGetPlatformIDs failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
platforms = (cl_platform_id *) malloc( num_platforms * sizeof( cl_platform_id ) ); platforms =
if (!platforms || choosen_platform_index >= num_platforms) { (cl_platform_id *)malloc(num_platforms * sizeof(cl_platform_id));
log_error( "platform index out of range -- choosen_platform_index (%d) >= num_platforms (%d)\n", choosen_platform_index, num_platforms ); if (!platforms || choosen_platform_index >= num_platforms)
{
log_error("platform index out of range -- choosen_platform_index (%d) "
">= num_platforms (%d)\n",
choosen_platform_index, num_platforms);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
BufferOwningPtr<cl_platform_id> platformsBuf(platforms); BufferOwningPtr<cl_platform_id> platformsBuf(platforms);
err = clGetPlatformIDs(num_platforms, platforms, NULL); err = clGetPlatformIDs(num_platforms, platforms, NULL);
if (err) { if (err)
{
print_error(err, "clGetPlatformIDs failed"); print_error(err, "clGetPlatformIDs failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* Get the number of requested devices */ /* Get the number of requested devices */
err = clGetDeviceIDs(platforms[choosen_platform_index], device_type, 0, NULL, &num_devices ); err = clGetDeviceIDs(platforms[choosen_platform_index], device_type, 0,
if (err) { NULL, &num_devices);
if (err)
{
print_error(err, "clGetDeviceIDs failed"); print_error(err, "clGetDeviceIDs failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
devices = (cl_device_id *)malloc(num_devices * sizeof(cl_device_id)); devices = (cl_device_id *)malloc(num_devices * sizeof(cl_device_id));
if (!devices || choosen_device_index >= num_devices) { if (!devices || choosen_device_index >= num_devices)
log_error( "device index out of range -- choosen_device_index (%d) >= num_devices (%d)\n", choosen_device_index, num_devices ); {
log_error("device index out of range -- choosen_device_index (%d) >= "
"num_devices (%d)\n",
choosen_device_index, num_devices);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
BufferOwningPtr<cl_device_id> devicesBuf(devices); BufferOwningPtr<cl_device_id> devicesBuf(devices);
/* Get the requested device */ /* Get the requested device */
err = clGetDeviceIDs(platforms[choosen_platform_index], device_type, num_devices, devices, NULL ); err = clGetDeviceIDs(platforms[choosen_platform_index], device_type,
if (err) { num_devices, devices, NULL);
if (err)
{
print_error(err, "clGetDeviceIDs failed"); print_error(err, "clGetDeviceIDs failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
device = devices[choosen_device_index]; device = devices[choosen_device_index];
err = clGetDeviceInfo( device, CL_DEVICE_TYPE, sizeof(gDeviceType), &gDeviceType, NULL ); err = clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(gDeviceType),
&gDeviceType, NULL);
if (err) if (err)
{ {
print_error(err, "Unable to get device type"); print_error(err, "Unable to get device type");
@@ -361,19 +410,24 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
} }
cl_device_fp_config fpconfig = 0; cl_device_fp_config fpconfig = 0;
err = clGetDeviceInfo( device, CL_DEVICE_SINGLE_FP_CONFIG, sizeof( fpconfig ), &fpconfig, NULL ); err = clGetDeviceInfo(device, CL_DEVICE_SINGLE_FP_CONFIG, sizeof(fpconfig),
if (err) { &fpconfig, NULL);
print_error(err, "clGetDeviceInfo for CL_DEVICE_SINGLE_FP_CONFIG failed"); if (err)
{
print_error(err,
"clGetDeviceInfo for CL_DEVICE_SINGLE_FP_CONFIG failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
gFlushDenormsToZero = (0 == (fpconfig & CL_FP_DENORM)); gFlushDenormsToZero = (0 == (fpconfig & CL_FP_DENORM));
log_info( "Supports single precision denormals: %s\n", gFlushDenormsToZero ? "NO" : "YES" ); log_info("Supports single precision denormals: %s\n",
gFlushDenormsToZero ? "NO" : "YES");
log_info("sizeof( void*) = %d (host)\n", (int)sizeof(void *)); log_info("sizeof( void*) = %d (host)\n", (int)sizeof(void *));
// detect whether profile of the device is embedded // detect whether profile of the device is embedded
char profile[1024] = ""; char profile[1024] = "";
err = clGetDeviceInfo(device, CL_DEVICE_PROFILE, sizeof(profile), profile, NULL); err = clGetDeviceInfo(device, CL_DEVICE_PROFILE, sizeof(profile), profile,
NULL);
if (err) if (err)
{ {
print_error(err, "clGetDeviceInfo for CL_DEVICE_PROFILE failed\n"); print_error(err, "clGetDeviceInfo for CL_DEVICE_PROFILE failed\n");
@@ -383,27 +437,30 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
// detect the floating point capabilities // detect the floating point capabilities
cl_device_fp_config floatCapabilities = 0; cl_device_fp_config floatCapabilities = 0;
err = clGetDeviceInfo(device, CL_DEVICE_SINGLE_FP_CONFIG, sizeof(floatCapabilities), &floatCapabilities, NULL); err = clGetDeviceInfo(device, CL_DEVICE_SINGLE_FP_CONFIG,
sizeof(floatCapabilities), &floatCapabilities, NULL);
if (err) if (err)
{ {
print_error(err, "clGetDeviceInfo for CL_DEVICE_SINGLE_FP_CONFIG failed\n"); print_error(err,
"clGetDeviceInfo for CL_DEVICE_SINGLE_FP_CONFIG failed\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Check for problems that only embedded will have // Check for problems that only embedded will have
if (gIsEmbedded) if (gIsEmbedded)
{ {
//If the device is embedded, we need to detect if the device supports Infinity and NaN // If the device is embedded, we need to detect if the device supports
if ((floatCapabilities & CL_FP_INF_NAN) == 0) // Infinity and NaN
gInfNanSupport = 0; if ((floatCapabilities & CL_FP_INF_NAN) == 0) gInfNanSupport = 0;
// check the extensions list to see if ulong and long are supported // check the extensions list to see if ulong and long are supported
if( !is_extension_available(device, "cles_khr_int64" )) if (!is_extension_available(device, "cles_khr_int64")) gHasLong = 0;
gHasLong = 0;
} }
cl_uint device_address_bits = 0; cl_uint device_address_bits = 0;
if( (err = clGetDeviceInfo( device, CL_DEVICE_ADDRESS_BITS, sizeof( device_address_bits ), &device_address_bits, NULL ) )) if ((err = clGetDeviceInfo(device, CL_DEVICE_ADDRESS_BITS,
sizeof(device_address_bits),
&device_address_bits, NULL)))
{ {
print_error(err, "Unable to obtain device address bits"); print_error(err, "Unable to obtain device address bits");
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -435,31 +492,32 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
test_status status = deviceCheckFn(device); test_status status = deviceCheckFn(device);
switch (status) switch (status)
{ {
case TEST_PASS: case TEST_PASS: break;
break; case TEST_FAIL: return fail_init_info(testNum);
case TEST_FAIL: case TEST_SKIP: return skip_init_info(testNum);
return fail_init_info(testNum);
case TEST_SKIP:
return skip_init_info(testNum);
} }
} }
if (num_elements <= 0) if (num_elements <= 0) num_elements = DEFAULT_NUM_ELEMENTS;
num_elements = DEFAULT_NUM_ELEMENTS;
// On most platforms which support denorm, default is FTZ off. However, // On most platforms which support denorm, default is FTZ off. However,
// on some hardware where the reference is computed, default might be flush denorms to zero e.g. arm. // on some hardware where the reference is computed, default might be
// This creates issues in result verification. Since spec allows the implementation to either flush or // flush denorms to zero e.g. arm. This creates issues in result
// not flush denorms to zero, an implementation may choose not be flush i.e. return denorm result whereas // verification. Since spec allows the implementation to either flush or
// reference result may be zero (flushed denorm). Hence we need to disable denorm flushing on host side // not flush denorms to zero, an implementation may choose not be flush
// where reference is being computed to make sure we get non-flushed reference result. If implementation // i.e. return denorm result whereas reference result may be zero
// returns flushed result, we correctly take care of that in verification code. // (flushed denorm). Hence we need to disable denorm flushing on host
// side where reference is being computed to make sure we get
// non-flushed reference result. If implementation returns flushed
// result, we correctly take care of that in verification code.
#if defined(__APPLE__) && defined(__arm__) #if defined(__APPLE__) && defined(__arm__)
FPU_mode_type oldMode; FPU_mode_type oldMode;
DisableFTZ(&oldMode); DisableFTZ(&oldMode);
#endif #endif
int error = parseAndCallCommandLineTests( argc, argv, device, testNum, testList, forceNoContextCreation, queueProps, num_elements ); int error = parseAndCallCommandLineTests(argc, argv, device, testNum,
testList, forceNoContextCreation,
queueProps, num_elements);
#if defined(__APPLE__) && defined(__arm__) #if defined(__APPLE__) && defined(__arm__)
// Restore the old FP mode before leaving. // Restore the old FP mode before leaving.
@@ -469,7 +527,8 @@ int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_def
return (error == 0) ? EXIT_SUCCESS : EXIT_FAILURE; return (error == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
} }
static int find_matching_tests( test_definition testList[], unsigned char selectedTestList[], int testNum, static int find_matching_tests(test_definition testList[],
unsigned char selectedTestList[], int testNum,
const char *argument, bool isWildcard) const char *argument, bool isWildcard)
{ {
int found_tests = 0; int found_tests = 0;
@@ -477,17 +536,20 @@ static int find_matching_tests( test_definition testList[], unsigned char select
for (int i = 0; i < testNum; i++) for (int i = 0; i < testNum; i++)
{ {
if( ( !isWildcard && strcmp( testList[i].name, argument ) == 0 ) || if ((!isWildcard && strcmp(testList[i].name, argument) == 0)
( isWildcard && strncmp( testList[i].name, argument, wildcard_length ) == 0 ) ) || (isWildcard
&& strncmp(testList[i].name, argument, wildcard_length) == 0))
{ {
if (selectedTestList[i]) if (selectedTestList[i])
{ {
log_error( "ERROR: Test '%s' has already been selected.\n", testList[i].name ); log_error("ERROR: Test '%s' has already been selected.\n",
testList[i].name);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
else if (testList[i].func == NULL) else if (testList[i].func == NULL)
{ {
log_error( "ERROR: Test '%s' is missing implementation.\n", testList[i].name ); log_error("ERROR: Test '%s' is missing implementation.\n",
testList[i].name);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
else else
@@ -504,20 +566,24 @@ static int find_matching_tests( test_definition testList[], unsigned char select
if (!found_tests) if (!found_tests)
{ {
log_error( "ERROR: The argument '%s' did not match any test names.\n", argument ); log_error("ERROR: The argument '%s' did not match any test names.\n",
argument);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
static int saveResultsToJson( const char *fileName, const char *suiteName, test_definition testList[], static int saveResultsToJson(const char *fileName, const char *suiteName,
unsigned char selectedTestList[], test_status resultTestList[], int testNum ) test_definition testList[],
unsigned char selectedTestList[],
test_status resultTestList[], int testNum)
{ {
FILE *file = fopen(fileName, "w"); FILE *file = fopen(fileName, "w");
if (NULL == file) if (NULL == file)
{ {
log_error( "ERROR: Failed to open '%s' for writing results.\n", fileName ); log_error("ERROR: Failed to open '%s' for writing results.\n",
fileName);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@@ -534,7 +600,8 @@ static int saveResultsToJson( const char *fileName, const char *suiteName, test_
{ {
if (selectedTestList[i]) if (selectedTestList[i])
{ {
fprintf( file, "%s\t\t\"%s\": \"%s\"", linebreak[add_linebreak], testList[i].name, result_map[(int)resultTestList[i]] ); fprintf(file, "%s\t\t\"%s\": \"%s\"", linebreak[add_linebreak],
testList[i].name, result_map[(int)resultTestList[i]]);
add_linebreak = 1; add_linebreak = 1;
} }
} }
@@ -581,9 +648,12 @@ static void print_results( int failed, int count, const char* name )
} }
} }
int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id device, int testNum, int parseAndCallCommandLineTests(int argc, const char *argv[],
test_definition testList[], int forceNoContextCreation, cl_device_id device, int testNum,
cl_command_queue_properties queueProps, int num_elements ) test_definition testList[],
int forceNoContextCreation,
cl_command_queue_properties queueProps,
int num_elements)
{ {
int ret = EXIT_SUCCESS; int ret = EXIT_SUCCESS;
@@ -601,7 +671,8 @@ int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id dev
{ {
if (strchr(argv[i], '*') != NULL) if (strchr(argv[i], '*') != NULL)
{ {
ret = find_matching_tests( testList, selectedTestList, testNum, argv[i], true ); ret = find_matching_tests(testList, selectedTestList, testNum,
argv[i], true);
} }
else else
{ {
@@ -612,7 +683,8 @@ int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id dev
} }
else else
{ {
ret = find_matching_tests( testList, selectedTestList, testNum, argv[i], false ); ret = find_matching_tests(testList, selectedTestList,
testNum, argv[i], false);
} }
} }
@@ -625,10 +697,12 @@ int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id dev
if (ret == EXIT_SUCCESS) if (ret == EXIT_SUCCESS)
{ {
resultTestList = ( test_status* ) calloc( testNum, sizeof(*resultTestList) ); resultTestList =
(test_status *)calloc(testNum, sizeof(*resultTestList));
callTestFunctions( testList, selectedTestList, resultTestList, testNum, device, callTestFunctions(testList, selectedTestList, resultTestList, testNum,
forceNoContextCreation, num_elements, queueProps ); device, forceNoContextCreation, num_elements,
queueProps);
print_results(gFailCount, gTestCount, "sub-test"); print_results(gFailCount, gTestCount, "sub-test");
print_results(gTestsFailed, gTestsFailed + gTestsPassed, "test"); print_results(gTestsFailed, gTestsFailed + gTestsPassed, "test");
@@ -636,7 +710,8 @@ int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id dev
char *filename = getenv("CL_CONFORMANCE_RESULTS_FILENAME"); char *filename = getenv("CL_CONFORMANCE_RESULTS_FILENAME");
if (filename != NULL) if (filename != NULL)
{ {
ret = saveResultsToJson( filename, argv[0], testList, selectedTestList, resultTestList, testNum ); ret = saveResultsToJson(filename, argv[0], testList,
selectedTestList, resultTestList, testNum);
} }
} }
@@ -659,28 +734,36 @@ int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id dev
return ret; return ret;
} }
void callTestFunctions( test_definition testList[], unsigned char selectedTestList[], test_status resultTestList[], void callTestFunctions(test_definition testList[],
int testNum, cl_device_id deviceToUse, int forceNoContextCreation, int numElementsToUse, unsigned char selectedTestList[],
test_status resultTestList[], int testNum,
cl_device_id deviceToUse, int forceNoContextCreation,
int numElementsToUse,
cl_command_queue_properties queueProps) cl_command_queue_properties queueProps)
{ {
for (int i = 0; i < testNum; ++i) for (int i = 0; i < testNum; ++i)
{ {
if (selectedTestList[i]) if (selectedTestList[i])
{ {
resultTestList[i] = callSingleTestFunction( testList[i], deviceToUse, forceNoContextCreation, resultTestList[i] = callSingleTestFunction(
testList[i], deviceToUse, forceNoContextCreation,
numElementsToUse, queueProps); numElementsToUse, queueProps);
} }
} }
} }
void CL_CALLBACK notify_callback(const char *errinfo, const void *private_info, size_t cb, void *user_data) void CL_CALLBACK notify_callback(const char *errinfo, const void *private_info,
size_t cb, void *user_data)
{ {
log_info("%s\n", errinfo); log_info("%s\n", errinfo);
} }
// Actual function execution // Actual function execution
test_status callSingleTestFunction( test_definition test, cl_device_id deviceToUse, int forceNoContextCreation, test_status callSingleTestFunction(test_definition test,
int numElementsToUse, const cl_queue_properties queueProps ) cl_device_id deviceToUse,
int forceNoContextCreation,
int numElementsToUse,
const cl_queue_properties queueProps)
{ {
test_status status; test_status status;
cl_int error; cl_int error;
@@ -702,19 +785,27 @@ test_status callSingleTestFunction( test_definition test, cl_device_id deviceToU
/* Create a context to work with, unless we're told not to */ /* Create a context to work with, unless we're told not to */
if (!forceNoContextCreation) if (!forceNoContextCreation)
{ {
context = clCreateContext(NULL, 1, &deviceToUse, notify_callback, NULL, &error ); context = clCreateContext(NULL, 1, &deviceToUse, notify_callback, NULL,
&error);
if (!context) if (!context)
{ {
print_error(error, "Unable to create testing context"); print_error(error, "Unable to create testing context");
return TEST_FAIL; return TEST_FAIL;
} }
if (device_version < Version(2, 0)) { if (device_version < Version(2, 0))
queue = clCreateCommandQueue(context, deviceToUse, queueProps, &error); {
} else { queue =
const cl_command_queue_properties cmd_queueProps = (queueProps)?CL_QUEUE_PROPERTIES:0; clCreateCommandQueue(context, deviceToUse, queueProps, &error);
cl_command_queue_properties queueCreateProps[] = {cmd_queueProps, queueProps, 0}; }
queue = clCreateCommandQueueWithProperties( context, deviceToUse, &queueCreateProps[0], &error ); else
{
const cl_command_queue_properties cmd_queueProps =
(queueProps) ? CL_QUEUE_PROPERTIES : 0;
cl_command_queue_properties queueCreateProps[] = { cmd_queueProps,
queueProps, 0 };
queue = clCreateCommandQueueWithProperties(
context, deviceToUse, &queueCreateProps[0], &error);
} }
if (queue == NULL) if (queue == NULL)
@@ -730,13 +821,17 @@ test_status callSingleTestFunction( test_definition test, cl_device_id deviceToU
if (test.func == NULL) if (test.func == NULL)
{ {
// Skip unimplemented test, can happen when all of the tests are selected // Skip unimplemented test, can happen when all of the tests are
// selected
log_info("%s test currently not implemented\n", test.name); log_info("%s test currently not implemented\n", test.name);
status = TEST_SKIP; status = TEST_SKIP;
} }
else else
{ {
int ret = test.func(deviceToUse, context, queue, numElementsToUse); //test_threaded_function( ptr_basefn_list[i], group, context, num_elements); int ret = test.func(
deviceToUse, context, queue,
numElementsToUse); // test_threaded_function( ptr_basefn_list[i],
// group, context, num_elements);
if (ret == TEST_NOT_IMPLEMENTED) if (ret == TEST_NOT_IMPLEMENTED)
{ {
/* Tests can also let us know they're not implemented yet */ /* Tests can also let us know they're not implemented yet */
@@ -745,14 +840,16 @@ test_status callSingleTestFunction( test_definition test, cl_device_id deviceToU
} }
else if (ret == TEST_SKIPPED_ITSELF) else if (ret == TEST_SKIPPED_ITSELF)
{ {
/* Tests can also let us know they're not supported by the implementation */ /* Tests can also let us know they're not supported by the
* implementation */
log_info("%s test not supported\n", test.name); log_info("%s test not supported\n", test.name);
status = TEST_SKIP; status = TEST_SKIP;
} }
else else
{ {
/* Print result */ /* Print result */
if( ret == 0 ) { if (ret == 0)
{
log_info("%s passed\n", test.name); log_info("%s passed\n", test.name);
gTestsPassed++; gTestsPassed++;
status = TEST_PASS; status = TEST_PASS;
@@ -770,7 +867,8 @@ test_status callSingleTestFunction( test_definition test, cl_device_id deviceToU
if (!forceNoContextCreation) if (!forceNoContextCreation)
{ {
int error = clFinish(queue); int error = clFinish(queue);
if (error) { if (error)
{
log_error("clFinish failed: %d", error); log_error("clFinish failed: %d", error);
status = TEST_FAIL; status = TEST_FAIL;
} }
@@ -789,21 +887,20 @@ void memset_pattern4(void *dest, const void *src_pattern, size_t bytes )
size_t i; size_t i;
uint32_t *d = (uint32_t *)dest; uint32_t *d = (uint32_t *)dest;
for( i = 0; i < count; i++ ) for (i = 0; i < count; i++) d[i] = pat;
d[i] = pat;
d += i; d += i;
bytes &= 3; bytes &= 3;
if( bytes ) if (bytes) memcpy(d, src_pattern, bytes);
memcpy( d, src_pattern, bytes );
} }
#endif #endif
cl_device_type GetDeviceType(cl_device_id d) cl_device_type GetDeviceType(cl_device_id d)
{ {
cl_device_type result = -1; cl_device_type result = -1;
cl_int err = clGetDeviceInfo( d, CL_DEVICE_TYPE, sizeof( result ), &result, NULL ); cl_int err =
clGetDeviceInfo(d, CL_DEVICE_TYPE, sizeof(result), &result, NULL);
if (CL_SUCCESS != err) if (CL_SUCCESS != err)
log_error("ERROR: Unable to get device type for device %p\n", d); log_error("ERROR: Unable to get device type for device %p\n", d);
return result; return result;
@@ -818,7 +915,8 @@ cl_device_id GetOpposingDevice( cl_device_id device )
cl_platform_id plat; cl_platform_id plat;
// Get the platform of the device to use for getting a list of devices // Get the platform of the device to use for getting a list of devices
error = clGetDeviceInfo( device, CL_DEVICE_PLATFORM, sizeof( plat ), &plat, NULL ); error =
clGetDeviceInfo(device, CL_DEVICE_PLATFORM, sizeof(plat), &plat, NULL);
if (error != CL_SUCCESS) if (error != CL_SUCCESS)
{ {
print_error(error, "Unable to get device's platform"); print_error(error, "Unable to get device's platform");
@@ -833,13 +931,15 @@ cl_device_id GetOpposingDevice( cl_device_id device )
return NULL; return NULL;
} }
otherDevices = (cl_device_id *)malloc(actualCount * sizeof(cl_device_id)); otherDevices = (cl_device_id *)malloc(actualCount * sizeof(cl_device_id));
if (NULL == otherDevices) { if (NULL == otherDevices)
{
print_error(error, "Unable to allocate list of other devices."); print_error(error, "Unable to allocate list of other devices.");
return NULL; return NULL;
} }
BufferOwningPtr<cl_device_id> otherDevicesBuf(otherDevices); BufferOwningPtr<cl_device_id> otherDevicesBuf(otherDevices);
error = clGetDeviceIDs( plat, CL_DEVICE_TYPE_ALL, actualCount, otherDevices, NULL ); error = clGetDeviceIDs(plat, CL_DEVICE_TYPE_ALL, actualCount, otherDevices,
NULL);
if (error != CL_SUCCESS) if (error != CL_SUCCESS)
{ {
print_error(error, "Unable to get list of devices"); print_error(error, "Unable to get list of devices");
@@ -848,7 +948,8 @@ cl_device_id GetOpposingDevice( cl_device_id device )
if (actualCount == 1) if (actualCount == 1)
{ {
return device; // NULL means error, returning self means we couldn't find another one return device; // NULL means error, returning self means we couldn't
// find another one
} }
// Loop and just find one that isn't the one we were given // Loop and just find one that isn't the one we were given
@@ -858,10 +959,12 @@ cl_device_id GetOpposingDevice( cl_device_id device )
if (otherDevices[i] != device) if (otherDevices[i] != device)
{ {
cl_device_type newType; cl_device_type newType;
error = clGetDeviceInfo( otherDevices[ i ], CL_DEVICE_TYPE, sizeof( newType ), &newType, NULL ); error = clGetDeviceInfo(otherDevices[i], CL_DEVICE_TYPE,
sizeof(newType), &newType, NULL);
if (error != CL_SUCCESS) if (error != CL_SUCCESS)
{ {
print_error( error, "Unable to get device type for other device" ); print_error(error,
"Unable to get device type for other device");
return NULL; return NULL;
} }
cl_device_id result = otherDevices[i]; cl_device_id result = otherDevices[i];
@@ -880,7 +983,8 @@ Version get_device_cl_version(cl_device_id device)
ASSERT_SUCCESS(err, "clGetDeviceInfo"); ASSERT_SUCCESS(err, "clGetDeviceInfo");
std::vector<char> str(str_size); std::vector<char> str(str_size);
err = clGetDeviceInfo(device, CL_DEVICE_VERSION, str_size, str.data(), NULL); err =
clGetDeviceInfo(device, CL_DEVICE_VERSION, str_size, str.data(), NULL);
ASSERT_SUCCESS(err, "clGetDeviceInfo"); ASSERT_SUCCESS(err, "clGetDeviceInfo");
if (strstr(str.data(), "OpenCL 1.0") != NULL) if (strstr(str.data(), "OpenCL 1.0") != NULL)
@@ -898,7 +1002,8 @@ Version get_device_cl_version(cl_device_id device)
else if (strstr(str.data(), "OpenCL 3.0") != NULL) else if (strstr(str.data(), "OpenCL 3.0") != NULL)
return Version(3, 0); return Version(3, 0);
throw std::runtime_error(std::string("Unknown OpenCL version: ") + str.data()); throw std::runtime_error(std::string("Unknown OpenCL version: ")
+ str.data());
} }
bool check_device_spirv_version_reported(cl_device_id device) bool check_device_spirv_version_reported(cl_device_id device)
@@ -1100,10 +1205,12 @@ void PrintArch( void )
#elif defined(__linux__) #elif defined(__linux__)
struct utsname buffer; struct utsname buffer;
if (uname(&buffer) != 0) { if (uname(&buffer) != 0)
{
vlog("uname error"); vlog("uname error");
} }
else { else
{
vlog("system name = %s\n", buffer.sysname); vlog("system name = %s\n", buffer.sysname);
vlog("node name = %s\n", buffer.nodename); vlog("node name = %s\n", buffer.nodename);
vlog("release = %s\n", buffer.release); vlog("release = %s\n", buffer.release);
@@ -1112,4 +1219,3 @@ void PrintArch( void )
} }
#endif #endif
} }

View File

@@ -23,16 +23,24 @@
#include <string> #include <string>
class Version class Version {
{
public: public:
Version(): m_major(0), m_minor(0) {} Version(): m_major(0), m_minor(0) {}
Version(int major, int minor): m_major(major), m_minor(minor) {} Version(int major, int minor): m_major(major), m_minor(minor) {}
bool operator>(const Version &rhs) const { return to_int() > rhs.to_int(); } bool operator>(const Version &rhs) const { return to_int() > rhs.to_int(); }
bool operator<(const Version &rhs) const { return to_int() < rhs.to_int(); } bool operator<(const Version &rhs) const { return to_int() < rhs.to_int(); }
bool operator<=(const Version& rhs) const { return to_int() <= rhs.to_int(); } bool operator<=(const Version &rhs) const
bool operator>=(const Version& rhs) const { return to_int() >= rhs.to_int(); } {
bool operator==(const Version& rhs) const { return to_int() == rhs.to_int(); } return to_int() <= rhs.to_int();
}
bool operator>=(const Version &rhs) const
{
return to_int() >= rhs.to_int();
}
bool operator==(const Version &rhs) const
{
return to_int() == rhs.to_int();
}
int to_int() const { return m_major * 10 + m_minor; } int to_int() const { return m_major * 10 + m_minor; }
std::string to_string() const std::string to_string() const
{ {
@@ -83,56 +91,77 @@ extern int gTestCount;
extern cl_uint gReSeed; extern cl_uint gReSeed;
extern cl_uint gRandomSeed; extern cl_uint gRandomSeed;
// Supply a list of functions to test here. This will allocate a CL device, create a context, all that // Supply a list of functions to test here. This will allocate a CL device,
// setup work, and then call each function in turn as dictatated by the passed arguments. // create a context, all that setup work, and then call each function in turn as
// Returns EXIT_SUCCESS iff all tests succeeded or the tests were listed, // dictatated by the passed arguments. Returns EXIT_SUCCESS iff all tests
// otherwise return EXIT_FAILURE. // succeeded or the tests were listed, otherwise return EXIT_FAILURE.
extern int runTestHarness( int argc, const char *argv[], int testNum, test_definition testList[], extern int runTestHarness(int argc, const char *argv[], int testNum,
int imageSupportRequired, int forceNoContextCreation, cl_command_queue_properties queueProps ); test_definition testList[], int imageSupportRequired,
int forceNoContextCreation,
// Device checking function. See runTestHarnessWithCheck. If this function returns anything other than TEST_PASS, the harness exits.
typedef test_status (*DeviceCheckFn)( cl_device_id device );
// Same as runTestHarness, but also supplies a function that checks the created device for required functionality.
// Returns EXIT_SUCCESS iff all tests succeeded or the tests were listed,
// otherwise return EXIT_FAILURE.
extern int runTestHarnessWithCheck( int argc, const char *argv[], int testNum, test_definition testList[],
int forceNoContextCreation, cl_command_queue_properties queueProps,
DeviceCheckFn deviceCheckFn );
// The command line parser used by runTestHarness to break up parameters into calls to callTestFunctions
extern int parseAndCallCommandLineTests( int argc, const char *argv[], cl_device_id device, int testNum,
test_definition testList[], int forceNoContextCreation,
cl_command_queue_properties queueProps, int num_elements );
// Call this function if you need to do all the setup work yourself, and just need the function list called/
// managed.
// testList is the data structure that contains test functions and its names
// selectedTestList is an array of integers (treated as bools) which tell which function is to be called,
// each element at index i, corresponds to the element in testList at index i
// resultTestList is an array of statuses which contain the result of each selected test
// testNum is the number of tests in testList, selectedTestList and resultTestList
// contextProps are used to create a testing context for each test
// deviceToUse and numElementsToUse are all just passed to each test function
extern void callTestFunctions( test_definition testList[], unsigned char selectedTestList[], test_status resultTestList[],
int testNum, cl_device_id deviceToUse, int forceNoContextCreation, int numElementsToUse,
cl_command_queue_properties queueProps); cl_command_queue_properties queueProps);
// This function is called by callTestFunctions, once per function, to do setup, call, logging and cleanup // Device checking function. See runTestHarnessWithCheck. If this function
extern test_status callSingleTestFunction( test_definition test, cl_device_id deviceToUse, int forceNoContextCreation, // returns anything other than TEST_PASS, the harness exits.
int numElementsToUse, cl_command_queue_properties queueProps ); typedef test_status (*DeviceCheckFn)(cl_device_id device);
// Same as runTestHarness, but also supplies a function that checks the created
// device for required functionality. Returns EXIT_SUCCESS iff all tests
// succeeded or the tests were listed, otherwise return EXIT_FAILURE.
extern int runTestHarnessWithCheck(int argc, const char *argv[], int testNum,
test_definition testList[],
int forceNoContextCreation,
cl_command_queue_properties queueProps,
DeviceCheckFn deviceCheckFn);
// The command line parser used by runTestHarness to break up parameters into
// calls to callTestFunctions
extern int parseAndCallCommandLineTests(int argc, const char *argv[],
cl_device_id device, int testNum,
test_definition testList[],
int forceNoContextCreation,
cl_command_queue_properties queueProps,
int num_elements);
// Call this function if you need to do all the setup work yourself, and just
// need the function list called/ managed.
// testList is the data structure that contains test functions and its names
// selectedTestList is an array of integers (treated as bools) which tell
// which function is to be called,
// each element at index i, corresponds to the element in testList at
// index i
// resultTestList is an array of statuses which contain the result of each
// selected test testNum is the number of tests in testList, selectedTestList
// and resultTestList contextProps are used to create a testing context for
// each test deviceToUse and numElementsToUse are all just passed to each
// test function
extern void callTestFunctions(test_definition testList[],
unsigned char selectedTestList[],
test_status resultTestList[], int testNum,
cl_device_id deviceToUse,
int forceNoContextCreation, int numElementsToUse,
cl_command_queue_properties queueProps);
// This function is called by callTestFunctions, once per function, to do setup,
// call, logging and cleanup
extern test_status
callSingleTestFunction(test_definition test, cl_device_id deviceToUse,
int forceNoContextCreation, int numElementsToUse,
cl_command_queue_properties queueProps);
///// Miscellaneous steps ///// Miscellaneous steps
// standard callback function for context pfn_notify // standard callback function for context pfn_notify
extern void CL_CALLBACK notify_callback(const char *errinfo, const void *private_info, size_t cb, void *user_data); extern void CL_CALLBACK notify_callback(const char *errinfo,
const void *private_info, size_t cb,
void *user_data);
extern cl_device_type GetDeviceType(cl_device_id); extern cl_device_type GetDeviceType(cl_device_id);
// Given a device (most likely passed in by the harness, but not required), will attempt to find // Given a device (most likely passed in by the harness, but not required), will
// a DIFFERENT device and return it. Useful for finding another device to run multi-device tests against. // attempt to find a DIFFERENT device and return it. Useful for finding another
// Note that returning NULL means an error was hit, but if no error was hit and the device passed in // device to run multi-device tests against. Note that returning NULL means an
// is the only device available, the SAME device is returned, so check! // error was hit, but if no error was hit and the device passed in is the only
// device available, the SAME device is returned, so check!
extern cl_device_id GetOpposingDevice(cl_device_id device); extern cl_device_id GetOpposingDevice(cl_device_id device);
Version get_device_spirv_il_version(cl_device_id device); Version get_device_spirv_il_version(cl_device_id device);
@@ -143,10 +172,13 @@ void version_expected_info(const char *test_name, const char *api_name,
test_status check_spirv_compilation_readiness(cl_device_id device); test_status check_spirv_compilation_readiness(cl_device_id device);
extern int gFlushDenormsToZero; // This is set to 1 if the device does not support denorms (CL_FP_DENORM) extern int gFlushDenormsToZero; // This is set to 1 if the device does not
extern int gInfNanSupport; // This is set to 1 if the device supports infinities and NaNs // support denorms (CL_FP_DENORM)
extern int gInfNanSupport; // This is set to 1 if the device supports infinities
// and NaNs
extern int gIsEmbedded; // This is set to 1 if the device is an embedded device extern int gIsEmbedded; // This is set to 1 if the device is an embedded device
extern int gHasLong; // This is set to 1 if the device suppots long and ulong types in OpenCL C. extern int gHasLong; // This is set to 1 if the device suppots long and ulong
// types in OpenCL C.
extern bool gCoreILProgram; extern bool gCoreILProgram;
#if !defined(__APPLE__) #if !defined(__APPLE__)
@@ -157,5 +189,3 @@ extern void PrintArch(void);
#endif // _testHarness_h #endif // _testHarness_h

View File

@@ -20,10 +20,11 @@ int main( void )
{ {
MTdata d = init_genrand(42); MTdata d = init_genrand(42);
int i; int i;
const cl_uint reference[16] = { 0x5fe1dc66, 0x8b255210, 0x0380b0c8, 0xc87d2ce4, const cl_uint reference[16] = {
0x55c31f24, 0x8bcd21ab, 0x14d5fef5, 0x9416d2b6, 0x5fe1dc66, 0x8b255210, 0x0380b0c8, 0xc87d2ce4, 0x55c31f24, 0x8bcd21ab,
0xdf875de9, 0x00517d76, 0xd861c944, 0xa7676404, 0x14d5fef5, 0x9416d2b6, 0xdf875de9, 0x00517d76, 0xd861c944, 0xa7676404,
0x5491aff4, 0x67616209, 0xc368b3fb, 0x929dfc92 }; 0x5491aff4, 0x67616209, 0xc368b3fb, 0x929dfc92
};
int errcount = 0; int errcount = 0;
for (i = 0; i < 65536; i++) for (i = 0; i < 65536; i++)
@@ -33,7 +34,8 @@ int main( void )
{ {
if (u != reference[i >> 12]) if (u != reference[i >> 12])
{ {
printf("ERROR: expected *0x%8.8x at %d. Got 0x%8.8x\n", reference[i>>12], i, u ); printf("ERROR: expected *0x%8.8x at %d. Got 0x%8.8x\n",
reference[i >> 12], i, u);
errcount++; errcount++;
} }
} }

View File

@@ -96,5 +96,3 @@ int test_threaded_function( basefn fnToTest, cl_device_id device, cl_context con
return (int)((intptr_t)retVal); return (int)((intptr_t)retVal);
} }
#endif #endif

View File

@@ -25,9 +25,10 @@
#define TEST_NOT_IMPLEMENTED -99 #define TEST_NOT_IMPLEMENTED -99
#define TEST_SKIPPED_ITSELF -100 #define TEST_SKIPPED_ITSELF -100
typedef int (*basefn)(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements); typedef int (*basefn)(cl_device_id deviceID, cl_context context,
extern int test_threaded_function( basefn fnToTest, cl_device_id device, cl_context context, cl_command_queue queue, int numElements ); cl_command_queue queue, int num_elements);
extern int test_threaded_function(basefn fnToTest, cl_device_id device,
cl_context context, cl_command_queue queue,
int numElements);
#endif // _threadTesting_h #endif // _threadTesting_h

View File

@@ -19,7 +19,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "clImageHelper.h" #include "clImageHelper.h"
#define ROUND_SIZE_UP( _size, _align ) (((size_t)(_size) + (size_t)(_align) - 1) & -((size_t)(_align))) #define ROUND_SIZE_UP(_size, _align) \
(((size_t)(_size) + (size_t)(_align)-1) & -((size_t)(_align)))
#if defined(__APPLE__) #if defined(__APPLE__)
#define kPageSize 4096 #define kPageSize 4096
@@ -30,42 +31,50 @@
#define kPageSize (getpagesize()) #define kPageSize (getpagesize())
#endif #endif
clProtectedImage::clProtectedImage( cl_context context, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, cl_int *errcode_ret ) clProtectedImage::clProtectedImage(cl_context context, cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
cl_int *errcode_ret)
{ {
cl_int err = Create(context, mem_flags, fmt, width); cl_int err = Create(context, mem_flags, fmt, width);
if( errcode_ret != NULL ) if (errcode_ret != NULL) *errcode_ret = err;
*errcode_ret = err;
} }
cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width ) cl_int clProtectedImage::Create(cl_context context, cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width)
{ {
cl_int error; cl_int error;
#if defined(__APPLE__) #if defined(__APPLE__)
int protect_pages = 1; int protect_pages = 1;
cl_device_id devices[16]; cl_device_id devices[16];
size_t number_of_devices; size_t number_of_devices;
error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices), devices, &number_of_devices); error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices),
devices, &number_of_devices);
test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed"); test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed");
number_of_devices /= sizeof(cl_device_id); number_of_devices /= sizeof(cl_device_id);
for (int i=0; i<(int)number_of_devices; i++) { for (int i = 0; i < (int)number_of_devices; i++)
{
cl_device_type type; cl_device_type type;
error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type, NULL); error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type,
NULL);
test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed"); test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed");
if (type == CL_DEVICE_TYPE_GPU) { if (type == CL_DEVICE_TYPE_GPU)
{
protect_pages = 0; protect_pages = 0;
break; break;
} }
} }
if (protect_pages) { if (protect_pages)
{
size_t pixelBytes = get_pixel_bytes(fmt); size_t pixelBytes = get_pixel_bytes(fmt);
size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize); size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize);
size_t rowStride = rowBytes + kPageSize; size_t rowStride = rowBytes + kPageSize;
// create backing store // create backing store
backingStoreSize = rowStride + 8 * rowStride; backingStoreSize = rowStride + 8 * rowStride;
backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0); backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, 0, 0);
// add guard pages // add guard pages
size_t row; size_t row;
@@ -73,14 +82,17 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
char *imagePtr = (char *)backingStore + 4 * rowStride; char *imagePtr = (char *)backingStore + 4 * rowStride;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
p += rowBytes; p += rowBytes;
mprotect( p, kPageSize, PROT_NONE ); p += rowStride; mprotect(p, kPageSize, PROT_NONE);
p += rowStride;
p -= rowBytes; p -= rowBytes;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
if (getenv("CL_ALIGN_RIGHT")) if (getenv("CL_ALIGN_RIGHT"))
@@ -88,64 +100,78 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
static int spewEnv = 1; static int spewEnv = 1;
if (spewEnv) if (spewEnv)
{ {
log_info( "***CL_ALIGN_RIGHT is set. Aligning images at right edge of page\n" ); log_info("***CL_ALIGN_RIGHT is set. Aligning images at right "
"edge of page\n");
spewEnv = 0; spewEnv = 0;
} }
imagePtr += rowBytes - pixelBytes * width; imagePtr += rowBytes - pixelBytes * width;
} }
image = create_image_1d( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, rowStride, imagePtr, NULL, &error ); image = create_image_1d(context, mem_flags | CL_MEM_USE_HOST_PTR, fmt,
} else { width, rowStride, imagePtr, NULL, &error);
}
else
{
backingStore = NULL; backingStore = NULL;
image = create_image_1d( context, mem_flags, fmt, width, 0, NULL, NULL, &error ); image = create_image_1d(context, mem_flags, fmt, width, 0, NULL, NULL,
&error);
} }
#else #else
backingStore = NULL; backingStore = NULL;
image = create_image_1d( context, mem_flags, fmt, width, 0, NULL, NULL, &error ); image =
create_image_1d(context, mem_flags, fmt, width, 0, NULL, NULL, &error);
#endif #endif
return error; return error;
} }
clProtectedImage::clProtectedImage( cl_context context, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, size_t height, cl_int *errcode_ret ) clProtectedImage::clProtectedImage(cl_context context, cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
size_t height, cl_int *errcode_ret)
{ {
cl_int err = Create(context, mem_flags, fmt, width, height); cl_int err = Create(context, mem_flags, fmt, width, height);
if( errcode_ret != NULL ) if (errcode_ret != NULL) *errcode_ret = err;
*errcode_ret = err;
} }
cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, size_t height ) cl_int clProtectedImage::Create(cl_context context, cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
size_t height)
{ {
cl_int error; cl_int error;
#if defined(__APPLE__) #if defined(__APPLE__)
int protect_pages = 1; int protect_pages = 1;
cl_device_id devices[16]; cl_device_id devices[16];
size_t number_of_devices; size_t number_of_devices;
error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices), devices, &number_of_devices); error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices),
devices, &number_of_devices);
test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed"); test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed");
number_of_devices /= sizeof(cl_device_id); number_of_devices /= sizeof(cl_device_id);
for (int i=0; i<(int)number_of_devices; i++) { for (int i = 0; i < (int)number_of_devices; i++)
{
cl_device_type type; cl_device_type type;
error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type, NULL); error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type,
NULL);
test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed"); test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed");
if (type == CL_DEVICE_TYPE_GPU) { if (type == CL_DEVICE_TYPE_GPU)
{
protect_pages = 0; protect_pages = 0;
break; break;
} }
} }
if (protect_pages) { if (protect_pages)
{
size_t pixelBytes = get_pixel_bytes(fmt); size_t pixelBytes = get_pixel_bytes(fmt);
size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize); size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize);
size_t rowStride = rowBytes + kPageSize; size_t rowStride = rowBytes + kPageSize;
// create backing store // create backing store
backingStoreSize = height * rowStride + 8 * rowStride; backingStoreSize = height * rowStride + 8 * rowStride;
backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0); backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, 0, 0);
// add guard pages // add guard pages
size_t row; size_t row;
@@ -153,17 +179,20 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
char *imagePtr = (char *)backingStore + 4 * rowStride; char *imagePtr = (char *)backingStore + 4 * rowStride;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
p += rowBytes; p += rowBytes;
for (row = 0; row < height; row++) for (row = 0; row < height; row++)
{ {
mprotect( p, kPageSize, PROT_NONE ); p += rowStride; mprotect(p, kPageSize, PROT_NONE);
p += rowStride;
} }
p -= rowBytes; p -= rowBytes;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
if (getenv("CL_ALIGN_RIGHT")) if (getenv("CL_ALIGN_RIGHT"))
@@ -171,35 +200,44 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
static int spewEnv = 1; static int spewEnv = 1;
if (spewEnv) if (spewEnv)
{ {
log_info( "***CL_ALIGN_RIGHT is set. Aligning images at right edge of page\n" ); log_info("***CL_ALIGN_RIGHT is set. Aligning images at right "
"edge of page\n");
spewEnv = 0; spewEnv = 0;
} }
imagePtr += rowBytes - pixelBytes * width; imagePtr += rowBytes - pixelBytes * width;
} }
image = create_image_2d( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, height, rowStride, imagePtr, &error ); image = create_image_2d(context, mem_flags | CL_MEM_USE_HOST_PTR, fmt,
} else { width, height, rowStride, imagePtr, &error);
}
else
{
backingStore = NULL; backingStore = NULL;
image = create_image_2d( context, mem_flags, fmt, width, height, 0, NULL, &error ); image = create_image_2d(context, mem_flags, fmt, width, height, 0, NULL,
&error);
} }
#else #else
backingStore = NULL; backingStore = NULL;
image = create_image_2d( context, mem_flags, fmt, width, height, 0, NULL, &error ); image = create_image_2d(context, mem_flags, fmt, width, height, 0, NULL,
&error);
#endif #endif
return error; return error;
} }
clProtectedImage::clProtectedImage( cl_context context, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth, cl_int *errcode_ret ) clProtectedImage::clProtectedImage(cl_context context, cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
size_t height, size_t depth,
cl_int *errcode_ret)
{ {
cl_int err = Create(context, mem_flags, fmt, width, height, depth); cl_int err = Create(context, mem_flags, fmt, width, height, depth);
if( errcode_ret != NULL ) if (errcode_ret != NULL) *errcode_ret = err;
*errcode_ret = err;
} }
cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth ) cl_int clProtectedImage::Create(cl_context context, cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
size_t height, size_t depth)
{ {
cl_int error; cl_int error;
@@ -207,28 +245,34 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
int protect_pages = 1; int protect_pages = 1;
cl_device_id devices[16]; cl_device_id devices[16];
size_t number_of_devices; size_t number_of_devices;
error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices), devices, &number_of_devices); error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices),
devices, &number_of_devices);
test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed"); test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed");
number_of_devices /= sizeof(cl_device_id); number_of_devices /= sizeof(cl_device_id);
for (int i=0; i<(int)number_of_devices; i++) { for (int i = 0; i < (int)number_of_devices; i++)
{
cl_device_type type; cl_device_type type;
error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type, NULL); error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type,
NULL);
test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed"); test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed");
if (type == CL_DEVICE_TYPE_GPU) { if (type == CL_DEVICE_TYPE_GPU)
{
protect_pages = 0; protect_pages = 0;
break; break;
} }
} }
if (protect_pages) { if (protect_pages)
{
size_t pixelBytes = get_pixel_bytes(fmt); size_t pixelBytes = get_pixel_bytes(fmt);
size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize); size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize);
size_t rowStride = rowBytes + kPageSize; size_t rowStride = rowBytes + kPageSize;
// create backing store // create backing store
backingStoreSize = height * depth * rowStride + 8 * rowStride; backingStoreSize = height * depth * rowStride + 8 * rowStride;
backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0); backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, 0, 0);
// add guard pages // add guard pages
size_t row; size_t row;
@@ -236,17 +280,20 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
char *imagePtr = (char *)backingStore + 4 * rowStride; char *imagePtr = (char *)backingStore + 4 * rowStride;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
p += rowBytes; p += rowBytes;
for (row = 0; row < height * depth; row++) for (row = 0; row < height * depth; row++)
{ {
mprotect( p, kPageSize, PROT_NONE ); p += rowStride; mprotect(p, kPageSize, PROT_NONE);
p += rowStride;
} }
p -= rowBytes; p -= rowBytes;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
if (getenv("CL_ALIGN_RIGHT")) if (getenv("CL_ALIGN_RIGHT"))
@@ -254,21 +301,28 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
static int spewEnv = 1; static int spewEnv = 1;
if (spewEnv) if (spewEnv)
{ {
log_info( "***CL_ALIGN_RIGHT is set. Aligning images at right edge of page\n" ); log_info("***CL_ALIGN_RIGHT is set. Aligning images at right "
"edge of page\n");
spewEnv = 0; spewEnv = 0;
} }
imagePtr += rowBytes - pixelBytes * width; imagePtr += rowBytes - pixelBytes * width;
} }
image = create_image_3d( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, height, depth, rowStride, height*rowStride, imagePtr, &error ); image = create_image_3d(context, mem_flags | CL_MEM_USE_HOST_PTR, fmt,
} else { width, height, depth, rowStride,
height * rowStride, imagePtr, &error);
}
else
{
backingStore = NULL; backingStore = NULL;
image = create_image_3d( context, mem_flags, fmt, width, height, depth, 0, 0, NULL, &error ); image = create_image_3d(context, mem_flags, fmt, width, height, depth,
0, 0, NULL, &error);
} }
#else #else
backingStore = NULL; backingStore = NULL;
image = create_image_3d( context, mem_flags, fmt, width, height, depth, 0, 0, NULL, &error ); image = create_image_3d(context, mem_flags, fmt, width, height, depth, 0, 0,
NULL, &error);
#endif #endif
@@ -276,35 +330,49 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_flags mem_flags, con
} }
clProtectedImage::clProtectedImage( cl_context context, cl_mem_object_type imageType, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth, size_t arraySize, cl_int *errcode_ret ) clProtectedImage::clProtectedImage(cl_context context,
cl_mem_object_type imageType,
cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
size_t height, size_t depth,
size_t arraySize, cl_int *errcode_ret)
{ {
cl_int err = Create( context, imageType, mem_flags, fmt, width, height, depth, arraySize ); cl_int err = Create(context, imageType, mem_flags, fmt, width, height,
if( errcode_ret != NULL ) depth, arraySize);
*errcode_ret = err; if (errcode_ret != NULL) *errcode_ret = err;
} }
cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageType, cl_mem_flags mem_flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth, size_t arraySize ) cl_int clProtectedImage::Create(cl_context context,
cl_mem_object_type imageType,
cl_mem_flags mem_flags,
const cl_image_format *fmt, size_t width,
size_t height, size_t depth, size_t arraySize)
{ {
cl_int error; cl_int error;
#if defined(__APPLE__) #if defined(__APPLE__)
int protect_pages = 1; int protect_pages = 1;
cl_device_id devices[16]; cl_device_id devices[16];
size_t number_of_devices; size_t number_of_devices;
error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices), devices, &number_of_devices); error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices),
devices, &number_of_devices);
test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed"); test_error(error, "clGetContextInfo for CL_CONTEXT_DEVICES failed");
number_of_devices /= sizeof(cl_device_id); number_of_devices /= sizeof(cl_device_id);
for (int i=0; i<(int)number_of_devices; i++) { for (int i = 0; i < (int)number_of_devices; i++)
{
cl_device_type type; cl_device_type type;
error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type, NULL); error = clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(type), &type,
NULL);
test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed"); test_error(error, "clGetDeviceInfo for CL_DEVICE_TYPE failed");
if (type == CL_DEVICE_TYPE_GPU) { if (type == CL_DEVICE_TYPE_GPU)
{
protect_pages = 0; protect_pages = 0;
break; break;
} }
} }
if (protect_pages) { if (protect_pages)
{
size_t pixelBytes = get_pixel_bytes(fmt); size_t pixelBytes = get_pixel_bytes(fmt);
size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize); size_t rowBytes = ROUND_SIZE_UP(width * pixelBytes, kPageSize);
size_t rowStride = rowBytes + kPageSize; size_t rowStride = rowBytes + kPageSize;
@@ -325,10 +393,12 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageTyp
backingStoreSize = arraySize * rowStride + 8 * rowStride; backingStoreSize = arraySize * rowStride + 8 * rowStride;
break; break;
case CL_MEM_OBJECT_IMAGE2D_ARRAY: case CL_MEM_OBJECT_IMAGE2D_ARRAY:
backingStoreSize = height * arraySize * rowStride + 8 * rowStride; backingStoreSize =
height * arraySize * rowStride + 8 * rowStride;
break; break;
} }
backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0); backingStore = mmap(0, backingStoreSize, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, 0, 0);
// add guard pages // add guard pages
size_t row; size_t row;
@@ -336,18 +406,22 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageTyp
char *imagePtr = (char *)backingStore + 4 * rowStride; char *imagePtr = (char *)backingStore + 4 * rowStride;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
p += rowBytes; p += rowBytes;
size_t sz = (height > 0 ? height : 1) * (depth > 0 ? depth : 1) * (arraySize > 0 ? arraySize : 1); size_t sz = (height > 0 ? height : 1) * (depth > 0 ? depth : 1)
* (arraySize > 0 ? arraySize : 1);
for (row = 0; row < sz; row++) for (row = 0; row < sz; row++)
{ {
mprotect( p, kPageSize, PROT_NONE ); p += rowStride; mprotect(p, kPageSize, PROT_NONE);
p += rowStride;
} }
p -= rowBytes; p -= rowBytes;
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
mprotect( p, rowStride, PROT_NONE ); p += rowStride; mprotect(p, rowStride, PROT_NONE);
p += rowStride;
} }
if (getenv("CL_ALIGN_RIGHT")) if (getenv("CL_ALIGN_RIGHT"))
@@ -355,7 +429,8 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageTyp
static int spewEnv = 1; static int spewEnv = 1;
if (spewEnv) if (spewEnv)
{ {
log_info( "***CL_ALIGN_RIGHT is set. Aligning images at right edge of page\n" ); log_info("***CL_ALIGN_RIGHT is set. Aligning images at right "
"edge of page\n");
spewEnv = 0; spewEnv = 0;
} }
imagePtr += rowBytes - pixelBytes * width; imagePtr += rowBytes - pixelBytes * width;
@@ -364,43 +439,61 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageTyp
switch (imageType) switch (imageType)
{ {
case CL_MEM_OBJECT_IMAGE1D: case CL_MEM_OBJECT_IMAGE1D:
image = create_image_1d( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, rowStride, imagePtr, NULL, &error ); image = create_image_1d(
context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width,
rowStride, imagePtr, NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE2D: case CL_MEM_OBJECT_IMAGE2D:
image = create_image_2d( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, height, rowStride, imagePtr, &error ); image = create_image_2d(
context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width,
height, rowStride, imagePtr, &error);
break; break;
case CL_MEM_OBJECT_IMAGE3D: case CL_MEM_OBJECT_IMAGE3D:
image = create_image_3d( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, height, depth, rowStride, height*rowStride, imagePtr, &error ); image =
create_image_3d(context, mem_flags | CL_MEM_USE_HOST_PTR,
fmt, width, height, depth, rowStride,
height * rowStride, imagePtr, &error);
break; break;
case CL_MEM_OBJECT_IMAGE1D_ARRAY: case CL_MEM_OBJECT_IMAGE1D_ARRAY:
image = create_image_1d_array( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, arraySize, rowStride, rowStride, imagePtr, &error ); image = create_image_1d_array(
context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width,
arraySize, rowStride, rowStride, imagePtr, &error);
break; break;
case CL_MEM_OBJECT_IMAGE2D_ARRAY: case CL_MEM_OBJECT_IMAGE2D_ARRAY:
image = create_image_2d_array( context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width, height, arraySize, rowStride, height*rowStride, imagePtr, &error ); image = create_image_2d_array(
context, mem_flags | CL_MEM_USE_HOST_PTR, fmt, width,
height, arraySize, rowStride, height * rowStride, imagePtr,
&error);
break; break;
} }
} else { }
else
{
backingStore = NULL; backingStore = NULL;
switch (imageType) switch (imageType)
{ {
case CL_MEM_OBJECT_IMAGE1D: case CL_MEM_OBJECT_IMAGE1D:
image = create_image_1d( context, mem_flags, fmt, width, 0, NULL, NULL, &error ); image = create_image_1d(context, mem_flags, fmt, width, 0, NULL,
NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE2D: case CL_MEM_OBJECT_IMAGE2D:
image = create_image_2d( context, mem_flags, fmt, width, height, 0, NULL, &error ); image = create_image_2d(context, mem_flags, fmt, width, height,
0, NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE3D: case CL_MEM_OBJECT_IMAGE3D:
image = create_image_3d(context, mem_flags, fmt, width, height, image = create_image_3d(context, mem_flags, fmt, width, height,
depth, 0, 0, NULL, &error); depth, 0, 0, NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE1D_ARRAY: case CL_MEM_OBJECT_IMAGE1D_ARRAY:
image = create_image_1d_array( context, mem_flags, fmt, width, arraySize, 0, 0, NULL, &error ); image = create_image_1d_array(context, mem_flags, fmt, width,
arraySize, 0, 0, NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE2D_ARRAY: case CL_MEM_OBJECT_IMAGE2D_ARRAY:
image = create_image_2d_array( context, mem_flags, fmt, width, height, arraySize, 0, 0, NULL, &error ); image = create_image_2d_array(context, mem_flags, fmt, width,
height, arraySize, 0, 0, NULL,
&error);
break; break;
} }
} }
#else #else
@@ -408,20 +501,25 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageTyp
switch (imageType) switch (imageType)
{ {
case CL_MEM_OBJECT_IMAGE1D: case CL_MEM_OBJECT_IMAGE1D:
image = create_image_1d( context, mem_flags, fmt, width, 0, NULL, NULL, &error ); image = create_image_1d(context, mem_flags, fmt, width, 0, NULL,
NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE2D: case CL_MEM_OBJECT_IMAGE2D:
image = create_image_2d( context, mem_flags, fmt, width, height, 0, NULL, &error ); image = create_image_2d(context, mem_flags, fmt, width, height, 0,
NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE3D: case CL_MEM_OBJECT_IMAGE3D:
image = create_image_3d(context, mem_flags, fmt, width, height, image = create_image_3d(context, mem_flags, fmt, width, height,
depth, 0, 0, NULL, &error); depth, 0, 0, NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE1D_ARRAY: case CL_MEM_OBJECT_IMAGE1D_ARRAY:
image = create_image_1d_array( context, mem_flags, fmt, width, arraySize, 0, 0, NULL, &error ); image = create_image_1d_array(context, mem_flags, fmt, width,
arraySize, 0, 0, NULL, &error);
break; break;
case CL_MEM_OBJECT_IMAGE2D_ARRAY: case CL_MEM_OBJECT_IMAGE2D_ARRAY:
image = create_image_2d_array( context, mem_flags, fmt, width, height, arraySize, 0, 0, NULL, &error ); image =
create_image_2d_array(context, mem_flags, fmt, width, height,
arraySize, 0, 0, NULL, &error);
break; break;
} }
#endif #endif
@@ -429,14 +527,10 @@ cl_int clProtectedImage::Create( cl_context context, cl_mem_object_type imageTyp
} }
/******* /*******
* clProtectedArray implementation * clProtectedArray implementation
*******/ *******/
clProtectedArray::clProtectedArray() clProtectedArray::clProtectedArray() { mBuffer = mValidBuffer = NULL; }
{
mBuffer = mValidBuffer = NULL;
}
clProtectedArray::clProtectedArray(size_t sizeInBytes) clProtectedArray::clProtectedArray(size_t sizeInBytes)
{ {
@@ -446,7 +540,8 @@ clProtectedArray::clProtectedArray( size_t sizeInBytes )
clProtectedArray::~clProtectedArray() clProtectedArray::~clProtectedArray()
{ {
if( mBuffer != NULL ) { if (mBuffer != NULL)
{
#if defined(__APPLE__) #if defined(__APPLE__)
int error = munmap(mBuffer, mRealSize); int error = munmap(mBuffer, mRealSize);
if (error) log_error("WARNING: munmap failed in clProtectedArray.\n"); if (error) log_error("WARNING: munmap failed in clProtectedArray.\n");
@@ -461,13 +556,15 @@ void clProtectedArray::Allocate( size_t sizeInBytes )
#if defined(__APPLE__) #if defined(__APPLE__)
// Allocate enough space to: round up our actual allocation to an even number of pages // Allocate enough space to: round up our actual allocation to an even
// and allocate two pages on either side // number of pages and allocate two pages on either side
mRoundedSize = ROUND_SIZE_UP(sizeInBytes, kPageSize); mRoundedSize = ROUND_SIZE_UP(sizeInBytes, kPageSize);
mRealSize = mRoundedSize + kPageSize * 2; mRealSize = mRoundedSize + kPageSize * 2;
// Use mmap here to ensure we start on a page boundary, so the mprotect calls will work OK // Use mmap here to ensure we start on a page boundary, so the mprotect
mBuffer = (char *)mmap(0, mRealSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0); // calls will work OK
mBuffer = (char *)mmap(0, mRealSize, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, 0, 0);
mValidBuffer = mBuffer + kPageSize; mValidBuffer = mBuffer + kPageSize;
@@ -479,5 +576,3 @@ void clProtectedArray::Allocate( size_t sizeInBytes )
mBuffer = mValidBuffer = (char *)calloc(1, mRealSize); mBuffer = mValidBuffer = (char *)calloc(1, mRealSize);
#endif #endif
} }

View File

@@ -31,14 +31,20 @@
/* cl_context wrapper */ /* cl_context wrapper */
class clContextWrapper class clContextWrapper {
{
public: public:
clContextWrapper() { mContext = NULL; } clContextWrapper() { mContext = NULL; }
clContextWrapper(cl_context program) { mContext = program; } clContextWrapper(cl_context program) { mContext = program; }
~clContextWrapper() { if( mContext != NULL ) clReleaseContext( mContext ); } ~clContextWrapper()
{
if (mContext != NULL) clReleaseContext(mContext);
}
clContextWrapper & operator=( const cl_context &rhs ) { mContext = rhs; return *this; } clContextWrapper &operator=(const cl_context &rhs)
{
mContext = rhs;
return *this;
}
operator cl_context() const { return mContext; } operator cl_context() const { return mContext; }
cl_context *operator&() { return &mContext; } cl_context *operator&() { return &mContext; }
@@ -46,20 +52,25 @@ class clContextWrapper
bool operator==(const cl_context &rhs) { return mContext == rhs; } bool operator==(const cl_context &rhs) { return mContext == rhs; }
protected: protected:
cl_context mContext; cl_context mContext;
}; };
/* cl_program wrapper */ /* cl_program wrapper */
class clProgramWrapper class clProgramWrapper {
{
public: public:
clProgramWrapper() { mProgram = NULL; } clProgramWrapper() { mProgram = NULL; }
clProgramWrapper(cl_program program) { mProgram = program; } clProgramWrapper(cl_program program) { mProgram = program; }
~clProgramWrapper() { if( mProgram != NULL ) clReleaseProgram( mProgram ); } ~clProgramWrapper()
{
if (mProgram != NULL) clReleaseProgram(mProgram);
}
clProgramWrapper & operator=( const cl_program &rhs ) { mProgram = rhs; return *this; } clProgramWrapper &operator=(const cl_program &rhs)
{
mProgram = rhs;
return *this;
}
operator cl_program() const { return mProgram; } operator cl_program() const { return mProgram; }
cl_program *operator&() { return &mProgram; } cl_program *operator&() { return &mProgram; }
@@ -67,20 +78,25 @@ class clProgramWrapper
bool operator==(const cl_program &rhs) { return mProgram == rhs; } bool operator==(const cl_program &rhs) { return mProgram == rhs; }
protected: protected:
cl_program mProgram; cl_program mProgram;
}; };
/* cl_kernel wrapper */ /* cl_kernel wrapper */
class clKernelWrapper class clKernelWrapper {
{
public: public:
clKernelWrapper() { mKernel = NULL; } clKernelWrapper() { mKernel = NULL; }
clKernelWrapper(cl_kernel kernel) { mKernel = kernel; } clKernelWrapper(cl_kernel kernel) { mKernel = kernel; }
~clKernelWrapper() { if( mKernel != NULL ) clReleaseKernel( mKernel ); } ~clKernelWrapper()
{
if (mKernel != NULL) clReleaseKernel(mKernel);
}
clKernelWrapper & operator=( const cl_kernel &rhs ) { mKernel = rhs; return *this; } clKernelWrapper &operator=(const cl_kernel &rhs)
{
mKernel = rhs;
return *this;
}
operator cl_kernel() const { return mKernel; } operator cl_kernel() const { return mKernel; }
cl_kernel *operator&() { return &mKernel; } cl_kernel *operator&() { return &mKernel; }
@@ -88,20 +104,25 @@ class clKernelWrapper
bool operator==(const cl_kernel &rhs) { return mKernel == rhs; } bool operator==(const cl_kernel &rhs) { return mKernel == rhs; }
protected: protected:
cl_kernel mKernel; cl_kernel mKernel;
}; };
/* cl_mem (stream) wrapper */ /* cl_mem (stream) wrapper */
class clMemWrapper class clMemWrapper {
{
public: public:
clMemWrapper() { mMem = NULL; } clMemWrapper() { mMem = NULL; }
clMemWrapper(cl_mem mem) { mMem = mem; } clMemWrapper(cl_mem mem) { mMem = mem; }
~clMemWrapper() { if( mMem != NULL ) clReleaseMemObject( mMem ); } ~clMemWrapper()
{
if (mMem != NULL) clReleaseMemObject(mMem);
}
clMemWrapper & operator=( const cl_mem &rhs ) { mMem = rhs; return *this; } clMemWrapper &operator=(const cl_mem &rhs)
{
mMem = rhs;
return *this;
}
operator cl_mem() const { return mMem; } operator cl_mem() const { return mMem; }
cl_mem *operator&() { return &mMem; } cl_mem *operator&() { return &mMem; }
@@ -109,35 +130,55 @@ class clMemWrapper
bool operator==(const cl_mem &rhs) { return mMem == rhs; } bool operator==(const cl_mem &rhs) { return mMem == rhs; }
protected: protected:
cl_mem mMem; cl_mem mMem;
}; };
class clProtectedImage class clProtectedImage {
{
public: public:
clProtectedImage() { image = NULL; backingStore = NULL; } clProtectedImage()
clProtectedImage( cl_context context, cl_mem_flags flags, const cl_image_format *fmt, size_t width, cl_int *errcode_ret ); {
clProtectedImage( cl_context context, cl_mem_flags flags, const cl_image_format *fmt, size_t width, size_t height, cl_int *errcode_ret ); image = NULL;
clProtectedImage( cl_context context, cl_mem_flags flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth, cl_int *errcode_ret ); backingStore = NULL;
clProtectedImage( cl_context context, cl_mem_object_type imageType, cl_mem_flags flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth, size_t arraySize, cl_int *errcode_ret ); }
clProtectedImage(cl_context context, cl_mem_flags flags,
const cl_image_format *fmt, size_t width,
cl_int *errcode_ret);
clProtectedImage(cl_context context, cl_mem_flags flags,
const cl_image_format *fmt, size_t width, size_t height,
cl_int *errcode_ret);
clProtectedImage(cl_context context, cl_mem_flags flags,
const cl_image_format *fmt, size_t width, size_t height,
size_t depth, cl_int *errcode_ret);
clProtectedImage(cl_context context, cl_mem_object_type imageType,
cl_mem_flags flags, const cl_image_format *fmt,
size_t width, size_t height, size_t depth,
size_t arraySize, cl_int *errcode_ret);
~clProtectedImage() ~clProtectedImage()
{ {
if( image != NULL ) if (image != NULL) clReleaseMemObject(image);
clReleaseMemObject( image );
#if defined(__APPLE__) #if defined(__APPLE__)
if(backingStore) if (backingStore) munmap(backingStore, backingStoreSize);
munmap(backingStore, backingStoreSize);
#endif #endif
} }
cl_int Create( cl_context context, cl_mem_flags flags, const cl_image_format *fmt, size_t width ); cl_int Create(cl_context context, cl_mem_flags flags,
cl_int Create( cl_context context, cl_mem_flags flags, const cl_image_format *fmt, size_t width, size_t height ); const cl_image_format *fmt, size_t width);
cl_int Create( cl_context context, cl_mem_flags flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth ); cl_int Create(cl_context context, cl_mem_flags flags,
cl_int Create( cl_context context, cl_mem_object_type imageType, cl_mem_flags flags, const cl_image_format *fmt, size_t width, size_t height, size_t depth, size_t arraySize ); const cl_image_format *fmt, size_t width, size_t height);
cl_int Create(cl_context context, cl_mem_flags flags,
const cl_image_format *fmt, size_t width, size_t height,
size_t depth);
cl_int Create(cl_context context, cl_mem_object_type imageType,
cl_mem_flags flags, const cl_image_format *fmt, size_t width,
size_t height, size_t depth, size_t arraySize);
clProtectedImage & operator=( const cl_mem &rhs ) { image = rhs; backingStore = NULL; return *this; } clProtectedImage &operator=(const cl_mem &rhs)
{
image = rhs;
backingStore = NULL;
return *this;
}
operator cl_mem() { return image; } operator cl_mem() { return image; }
cl_mem *operator&() { return &image; } cl_mem *operator&() { return &image; }
@@ -151,14 +192,23 @@ class clProtectedImage
}; };
/* cl_command_queue wrapper */ /* cl_command_queue wrapper */
class clCommandQueueWrapper class clCommandQueueWrapper {
{
public: public:
clCommandQueueWrapper() { mMem = NULL; } clCommandQueueWrapper() { mMem = NULL; }
clCommandQueueWrapper(cl_command_queue mem) { mMem = mem; } clCommandQueueWrapper(cl_command_queue mem) { mMem = mem; }
~clCommandQueueWrapper() { if( mMem != NULL ) { clReleaseCommandQueue( mMem ); } } ~clCommandQueueWrapper()
{
if (mMem != NULL)
{
clReleaseCommandQueue(mMem);
}
}
clCommandQueueWrapper & operator=( const cl_command_queue &rhs ) { mMem = rhs; return *this; } clCommandQueueWrapper &operator=(const cl_command_queue &rhs)
{
mMem = rhs;
return *this;
}
operator cl_command_queue() const { return mMem; } operator cl_command_queue() const { return mMem; }
cl_command_queue *operator&() { return &mMem; } cl_command_queue *operator&() { return &mMem; }
@@ -166,19 +216,24 @@ class clCommandQueueWrapper
bool operator==(const cl_command_queue &rhs) { return mMem == rhs; } bool operator==(const cl_command_queue &rhs) { return mMem == rhs; }
protected: protected:
cl_command_queue mMem; cl_command_queue mMem;
}; };
/* cl_sampler wrapper */ /* cl_sampler wrapper */
class clSamplerWrapper class clSamplerWrapper {
{
public: public:
clSamplerWrapper() { mMem = NULL; } clSamplerWrapper() { mMem = NULL; }
clSamplerWrapper(cl_sampler mem) { mMem = mem; } clSamplerWrapper(cl_sampler mem) { mMem = mem; }
~clSamplerWrapper() { if( mMem != NULL ) clReleaseSampler( mMem ); } ~clSamplerWrapper()
{
if (mMem != NULL) clReleaseSampler(mMem);
}
clSamplerWrapper & operator=( const cl_sampler &rhs ) { mMem = rhs; return *this; } clSamplerWrapper &operator=(const cl_sampler &rhs)
{
mMem = rhs;
return *this;
}
operator cl_sampler() const { return mMem; } operator cl_sampler() const { return mMem; }
cl_sampler *operator&() { return &mMem; } cl_sampler *operator&() { return &mMem; }
@@ -186,19 +241,24 @@ class clSamplerWrapper
bool operator==(const cl_sampler &rhs) { return mMem == rhs; } bool operator==(const cl_sampler &rhs) { return mMem == rhs; }
protected: protected:
cl_sampler mMem; cl_sampler mMem;
}; };
/* cl_event wrapper */ /* cl_event wrapper */
class clEventWrapper class clEventWrapper {
{
public: public:
clEventWrapper() { mMem = NULL; } clEventWrapper() { mMem = NULL; }
clEventWrapper(cl_event mem) { mMem = mem; } clEventWrapper(cl_event mem) { mMem = mem; }
~clEventWrapper() { if( mMem != NULL ) clReleaseEvent( mMem ); } ~clEventWrapper()
{
if (mMem != NULL) clReleaseEvent(mMem);
}
clEventWrapper & operator=( const cl_event &rhs ) { mMem = rhs; return *this; } clEventWrapper &operator=(const cl_event &rhs)
{
mMem = rhs;
return *this;
}
operator cl_event() const { return mMem; } operator cl_event() const { return mMem; }
cl_event *operator&() { return &mMem; } cl_event *operator&() { return &mMem; }
@@ -206,13 +266,11 @@ class clEventWrapper
bool operator==(const cl_event &rhs) { return mMem == rhs; } bool operator==(const cl_event &rhs) { return mMem == rhs; }
protected: protected:
cl_event mMem; cl_event mMem;
}; };
/* Generic protected memory buffer, for verifying access within bounds */ /* Generic protected memory buffer, for verifying access within bounds */
class clProtectedArray class clProtectedArray {
{
public: public:
clProtectedArray(); clProtectedArray();
clProtectedArray(size_t sizeInBytes); clProtectedArray(size_t sizeInBytes);
@@ -224,20 +282,21 @@ class clProtectedArray
operator const void *() const { return (const void *)mValidBuffer; } operator const void *() const { return (const void *)mValidBuffer; }
protected: protected:
char *mBuffer; char *mBuffer;
char *mValidBuffer; char *mValidBuffer;
size_t mRealSize, mRoundedSize; size_t mRealSize, mRoundedSize;
}; };
class RandomSeed class RandomSeed {
{
public: public:
RandomSeed( cl_uint seed ){ if(seed) log_info( "(seed = %10.10u) ", seed ); mtData = init_genrand(seed); } RandomSeed(cl_uint seed)
{
if (seed) log_info("(seed = %10.10u) ", seed);
mtData = init_genrand(seed);
}
~RandomSeed() ~RandomSeed()
{ {
if( gReSeed ) if (gReSeed) gRandomSeed = genrand_int32(mtData);
gRandomSeed = genrand_int32( mtData );
free_mtdata(mtData); free_mtdata(mtData);
} }
@@ -248,36 +307,46 @@ class RandomSeed
}; };
template <typename T> class BufferOwningPtr template <typename T> class BufferOwningPtr {
{
BufferOwningPtr(BufferOwningPtr const &); // do not implement BufferOwningPtr(BufferOwningPtr const &); // do not implement
void operator=(BufferOwningPtr const &); // do not implement void operator=(BufferOwningPtr const &); // do not implement
void *ptr; void *ptr;
void *map; void *map;
size_t mapsize; // Bytes allocated total, pointed to by map. // Bytes allocated total, pointed to by map:
size_t allocsize; // Bytes allocated in unprotected pages, pointed to by ptr. size_t mapsize;
// Bytes allocated in unprotected pages, pointed to by ptr:
size_t allocsize;
bool aligned; bool aligned;
public: public:
explicit BufferOwningPtr(void *p = 0) : ptr(p), map(0), mapsize(0), allocsize(0), aligned(false) {} explicit BufferOwningPtr(void *p = 0)
: ptr(p), map(0), mapsize(0), allocsize(0), aligned(false)
{}
explicit BufferOwningPtr(void *p, void *m, size_t s) explicit BufferOwningPtr(void *p, void *m, size_t s)
: ptr(p), map(m), mapsize(s), allocsize(0), aligned(false) : ptr(p), map(m), mapsize(s), allocsize(0), aligned(false)
{ {
#if !defined(__APPLE__) #if !defined(__APPLE__)
if (m) if (m)
{ {
log_error( "ERROR: unhandled code path. BufferOwningPtr allocated with mapped buffer!" ); log_error("ERROR: unhandled code path. BufferOwningPtr allocated "
"with mapped buffer!");
abort(); abort();
} }
#endif #endif
} }
~BufferOwningPtr() { ~BufferOwningPtr()
if (map) { {
if (map)
{
#if defined(__APPLE__) #if defined(__APPLE__)
int error = munmap(map, mapsize); int error = munmap(map, mapsize);
if (error) log_error("WARNING: munmap failed in BufferOwningPtr.\n"); if (error)
log_error("WARNING: munmap failed in BufferOwningPtr.\n");
#endif #endif
} else { }
else
{
if (aligned) if (aligned)
{ {
align_free(ptr); align_free(ptr);
@@ -288,16 +357,23 @@ template <typename T> class BufferOwningPtr
} }
} }
} }
void reset(void *p, void *m = 0, size_t mapsize_ = 0, size_t allocsize_ = 0, bool aligned_ = false) { void reset(void *p, void *m = 0, size_t mapsize_ = 0, size_t allocsize_ = 0,
if (map){ bool aligned_ = false)
{
if (map)
{
#if defined(__APPLE__) #if defined(__APPLE__)
int error = munmap(map, mapsize); int error = munmap(map, mapsize);
if (error) log_error("WARNING: munmap failed in BufferOwningPtr.\n"); if (error)
log_error("WARNING: munmap failed in BufferOwningPtr.\n");
#else #else
log_error( "ERROR: unhandled code path. BufferOwningPtr reset with mapped buffer!" ); log_error("ERROR: unhandled code path. BufferOwningPtr reset with "
"mapped buffer!");
abort(); abort();
#endif #endif
} else { }
else
{
if (aligned) if (aligned)
{ {
align_free(ptr); align_free(ptr);
@@ -310,12 +386,14 @@ template <typename T> class BufferOwningPtr
ptr = p; ptr = p;
map = m; map = m;
mapsize = mapsize_; mapsize = mapsize_;
allocsize = (ptr != NULL) ? allocsize_ : 0; // Force allocsize to zero if ptr is NULL. // Force allocsize to zero if ptr is NULL:
allocsize = (ptr != NULL) ? allocsize_ : 0;
aligned = aligned_; aligned = aligned_;
#if !defined(__APPLE__) #if !defined(__APPLE__)
if (m) if (m)
{ {
log_error( "ERROR: unhandled code path. BufferOwningPtr allocated with mapped buffer!" ); log_error("ERROR: unhandled code path. BufferOwningPtr allocated "
"with mapped buffer!");
abort(); abort();
} }
#endif #endif
@@ -326,4 +404,3 @@ template <typename T> class BufferOwningPtr
}; };
#endif // _typeWrappers_h #endif // _typeWrappers_h