Commit 595d9112 by Ben Clayton

clang-format the src/System directory

Bug: b/144825072 Change-Id: Ifc469786ac5fa2a8ed00e95069e1a2839e320e96 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39657 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarBen Clayton <bclayton@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 713b8d35
...@@ -18,22 +18,22 @@ ...@@ -18,22 +18,22 @@
// Define MEMORY_SANITIZER_ENABLED to 1 if the project was build with the memory // Define MEMORY_SANITIZER_ENABLED to 1 if the project was build with the memory
// sanitizer enabled (-fsanitize=memory). // sanitizer enabled (-fsanitize=memory).
#if defined(__SANITIZE_MEMORY__) #if defined(__SANITIZE_MEMORY__)
#define MEMORY_SANITIZER_ENABLED 1 # define MEMORY_SANITIZER_ENABLED 1
#else // defined(__SANITIZE_MEMORY__) #else // defined(__SANITIZE_MEMORY__)
#if defined(__clang__) # if defined(__clang__)
#if __has_feature(memory_sanitizer) # if __has_feature(memory_sanitizer)
#define MEMORY_SANITIZER_ENABLED 1 # define MEMORY_SANITIZER_ENABLED 1
#endif // __has_feature(memory_sanitizer) # endif // __has_feature(memory_sanitizer)
#endif // defined(__clang__) # endif // defined(__clang__)
#endif // defined(__SANITIZE_MEMORY__) #endif // defined(__SANITIZE_MEMORY__)
// MEMORY_SANITIZER_ONLY(X) resolves to X if MEMORY_SANITIZER_ENABLED is defined // MEMORY_SANITIZER_ONLY(X) resolves to X if MEMORY_SANITIZER_ENABLED is defined
// to a non-zero value, otherwise MEMORY_SANITIZER_ONLY() is stripped by the // to a non-zero value, otherwise MEMORY_SANITIZER_ONLY() is stripped by the
// preprocessor. // preprocessor.
#if MEMORY_SANITIZER_ENABLED #if MEMORY_SANITIZER_ENABLED
#define MEMORY_SANITIZER_ONLY(x) x # define MEMORY_SANITIZER_ONLY(x) x
#else #else
#define MEMORY_SANITIZER_ONLY(x) # define MEMORY_SANITIZER_ONLY(x)
#endif // MEMORY_SANITIZER_ENABLED #endif // MEMORY_SANITIZER_ENABLED
#endif // Build_hpp #endif // Build_hpp
...@@ -15,16 +15,16 @@ ...@@ -15,16 +15,16 @@
#include "CPUID.hpp" #include "CPUID.hpp"
#if defined(_WIN32) #if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN # ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
#endif # endif
#include <windows.h> # include <windows.h>
#include <intrin.h> # include <intrin.h>
#include <float.h> # include <float.h>
#else #else
#include <unistd.h> # include <unistd.h>
#include <sched.h> # include <sched.h>
#include <sys/types.h> # include <sys/types.h>
#endif #endif
namespace sw { namespace sw {
...@@ -164,18 +164,20 @@ void CPUID::setEnableSSE4_1(bool enable) ...@@ -164,18 +164,20 @@ void CPUID::setEnableSSE4_1(bool enable)
static void cpuid(int registers[4], int info) static void cpuid(int registers[4], int info)
{ {
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
#if defined(_WIN32) # if defined(_WIN32)
__cpuid(registers, info); __cpuid(registers, info);
#else # else
__asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info)); __asm volatile("cpuid"
#endif : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3])
#else : "a"(info));
registers[0] = 0; # endif
registers[1] = 0; #else
registers[2] = 0; registers[0] = 0;
registers[3] = 0; registers[1] = 0;
#endif registers[2] = 0;
registers[3] = 0;
#endif
} }
bool CPUID::detectMMX() bool CPUID::detectMMX()
...@@ -231,55 +233,55 @@ int CPUID::detectCoreCount() ...@@ -231,55 +233,55 @@ int CPUID::detectCoreCount()
{ {
int cores = 0; int cores = 0;
#if defined(_WIN32) #if defined(_WIN32)
DWORD_PTR processAffinityMask = 1; DWORD_PTR processAffinityMask = 1;
DWORD_PTR systemAffinityMask = 1; DWORD_PTR systemAffinityMask = 1;
GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask); GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
while(systemAffinityMask) while(systemAffinityMask)
{
if(systemAffinityMask & 1)
{ {
if(systemAffinityMask & 1) cores++;
{
cores++;
}
systemAffinityMask >>= 1;
} }
#else
cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif
if(cores < 1) cores = 1; systemAffinityMask >>= 1;
}
#else
cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif
if(cores < 1) cores = 1;
if(cores > 16) cores = 16; if(cores > 16) cores = 16;
return cores; // FIXME: Number of physical cores return cores; // FIXME: Number of physical cores
} }
int CPUID::detectAffinity() int CPUID::detectAffinity()
{ {
int cores = 0; int cores = 0;
#if defined(_WIN32) #if defined(_WIN32)
DWORD_PTR processAffinityMask = 1; DWORD_PTR processAffinityMask = 1;
DWORD_PTR systemAffinityMask = 1; DWORD_PTR systemAffinityMask = 1;
GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask); GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
while(processAffinityMask) while(processAffinityMask)
{
if(processAffinityMask & 1)
{ {
if(processAffinityMask & 1) cores++;
{
cores++;
}
processAffinityMask >>= 1;
} }
#else
return detectCoreCount(); // FIXME: Assumes no affinity limitation
#endif
if(cores < 1) cores = 1; processAffinityMask >>= 1;
}
#else
return detectCoreCount(); // FIXME: Assumes no affinity limitation
#endif
if(cores < 1) cores = 1;
if(cores > 16) cores = 16; if(cores > 16) cores = 16;
return cores; return cores;
...@@ -287,11 +289,11 @@ int CPUID::detectAffinity() ...@@ -287,11 +289,11 @@ int CPUID::detectAffinity()
void CPUID::setFlushToZero(bool enable) void CPUID::setFlushToZero(bool enable)
{ {
#if defined(_MSC_VER) #if defined(_MSC_VER)
_controlfp(enable ? _DN_FLUSH : _DN_SAVE, _MCW_DN); _controlfp(enable ? _DN_FLUSH : _DN_SAVE, _MCW_DN);
#else #else
// Unimplemented // Unimplemented
#endif #endif
} }
void CPUID::setDenormalsAreZero(bool enable) void CPUID::setDenormalsAreZero(bool enable)
......
...@@ -18,11 +18,11 @@ ...@@ -18,11 +18,11 @@
namespace sw { namespace sw {
#if !defined(__i386__) && defined(_M_IX86) #if !defined(__i386__) && defined(_M_IX86)
#define __i386__ 1 # define __i386__ 1
#endif #endif
#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64)) #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64))
#define __x86_64__ 1 # define __x86_64__ 1
#endif #endif
class CPUID class CPUID
...@@ -30,7 +30,7 @@ class CPUID ...@@ -30,7 +30,7 @@ class CPUID
public: public:
static bool supportsMMX(); static bool supportsMMX();
static bool supportsCMOV(); static bool supportsCMOV();
static bool supportsMMX2(); // MMX instructions added by SSE: pshufw, pmulhuw, pmovmskb, pavgw/b, pextrw, pinsrw, pmaxsw/ub, etc. static bool supportsMMX2(); // MMX instructions added by SSE: pshufw, pmulhuw, pmovmskb, pavgw/b, pextrw, pinsrw, pmaxsw/ub, etc.
static bool supportsSSE(); static bool supportsSSE();
static bool supportsSSE2(); static bool supportsSSE2();
static bool supportsSSE3(); static bool supportsSSE3();
...@@ -47,8 +47,8 @@ public: ...@@ -47,8 +47,8 @@ public:
static void setEnableSSSE3(bool enable); static void setEnableSSSE3(bool enable);
static void setEnableSSE4_1(bool enable); static void setEnableSSE4_1(bool enable);
static void setFlushToZero(bool enable); // Denormal results are written as zero static void setFlushToZero(bool enable); // Denormal results are written as zero
static void setDenormalsAreZero(bool enable); // Denormal inputs are read as zero static void setDenormalsAreZero(bool enable); // Denormal inputs are read as zero
private: private:
static bool MMX; static bool MMX;
...@@ -98,7 +98,7 @@ inline bool CPUID::supportsCMOV() ...@@ -98,7 +98,7 @@ inline bool CPUID::supportsCMOV()
inline bool CPUID::supportsMMX2() inline bool CPUID::supportsMMX2()
{ {
return supportsSSE(); // Coincides with 64-bit integer vector instructions supported by SSE return supportsSSE(); // Coincides with 64-bit integer vector instructions supported by SSE
} }
inline bool CPUID::supportsSSE() inline bool CPUID::supportsSSE()
...@@ -138,4 +138,4 @@ inline int CPUID::processAffinity() ...@@ -138,4 +138,4 @@ inline int CPUID::processAffinity()
} // namespace sw } // namespace sw
#endif // sw_CPUID_hpp #endif // sw_CPUID_hpp
...@@ -14,17 +14,17 @@ ...@@ -14,17 +14,17 @@
#include "Configurator.hpp" #include "Configurator.hpp"
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
using namespace std; using namespace std;
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#if defined(__unix__) #if defined(__unix__)
#include <unistd.h> # include <unistd.h>
#endif #endif
namespace sw { namespace sw {
...@@ -42,12 +42,12 @@ Configurator::~Configurator() ...@@ -42,12 +42,12 @@ Configurator::~Configurator()
bool Configurator::readFile() bool Configurator::readFile()
{ {
#if defined(__unix__) #if defined(__unix__)
if(access(path.c_str(), R_OK) != 0) if(access(path.c_str(), R_OK) != 0)
{ {
return false; return false;
} }
#endif #endif
fstream file(path.c_str(), ios::in); fstream file(path.c_str(), ios::in);
if(file.fail()) return false; if(file.fail()) return false;
...@@ -66,7 +66,7 @@ bool Configurator::readFile() ...@@ -66,7 +66,7 @@ bool Configurator::readFile()
if(!isprint(line[0])) if(!isprint(line[0]))
{ {
// printf("Failing on char %d\n", line[0]); // printf("Failing on char %d\n", line[0]);
file.close(); file.close();
return false; return false;
} }
...@@ -77,7 +77,7 @@ bool Configurator::readFile() ...@@ -77,7 +77,7 @@ bool Configurator::readFile()
{ {
switch(line[pLeft]) switch(line[pLeft])
{ {
case '[': case '[':
{ {
string::size_type pRight = line.find_last_of("]"); string::size_type pRight = line.find_last_of("]");
...@@ -88,17 +88,17 @@ bool Configurator::readFile() ...@@ -88,17 +88,17 @@ bool Configurator::readFile()
} }
} }
break; break;
case '=': case '=':
{ {
string valueName = line.substr(0, pLeft); string valueName = line.substr(0, pLeft);
string value = line.substr(pLeft + 1); string value = line.substr(pLeft + 1);
addValue(keyName, valueName, value); addValue(keyName, valueName, value);
} }
break; break;
case ';': case ';':
case '#': case '#':
// Ignore comments // Ignore comments
break; break;
} }
} }
} }
...@@ -116,17 +116,18 @@ bool Configurator::readFile() ...@@ -116,17 +116,18 @@ bool Configurator::readFile()
void Configurator::writeFile(std::string title) void Configurator::writeFile(std::string title)
{ {
#if defined(__unix__) #if defined(__unix__)
if(access(path.c_str(), W_OK) != 0) if(access(path.c_str(), W_OK) != 0)
{ {
return; return;
} }
#endif #endif
fstream file(path.c_str(), ios::out); fstream file(path.c_str(), ios::out);
if(file.fail()) return; if(file.fail()) return;
file << "; " << title << endl << endl; file << "; " << title << endl
<< endl;
for(unsigned int keyID = 0; keyID < sections.size(); keyID++) for(unsigned int keyID = 0; keyID < sections.size(); keyID++)
{ {
...@@ -237,18 +238,18 @@ double Configurator::getFloat(string keyName, string valueName, double defaultVa ...@@ -237,18 +238,18 @@ double Configurator::getFloat(string keyName, string valueName, double defaultVa
} }
unsigned int Configurator::getFormatted(string keyName, string valueName, char *format, unsigned int Configurator::getFormatted(string keyName, string valueName, char *format,
void *v1, void *v2, void *v3, void *v4, void *v1, void *v2, void *v3, void *v4,
void *v5, void *v6, void *v7, void *v8, void *v5, void *v6, void *v7, void *v8,
void *v9, void *v10, void *v11, void *v12, void *v9, void *v10, void *v11, void *v12,
void *v13, void *v14, void *v15, void *v16) void *v13, void *v14, void *v15, void *v16)
{ {
string value = getValue(keyName, valueName); string value = getValue(keyName, valueName);
if(!value.length()) return false; if(!value.length()) return false;
unsigned int nVals = sscanf(value.c_str(), format, unsigned int nVals = sscanf(value.c_str(), format,
v1, v2, v3, v4, v5, v6, v7, v8, v1, v2, v3, v4, v5, v6, v7, v8,
v9, v10, v11, v12, v13, v14, v15, v16); v9, v10, v11, v12, v13, v14, v15, v16);
return nVals; return nVals;
} }
......
...@@ -64,4 +64,4 @@ private: ...@@ -64,4 +64,4 @@ private:
} // namespace sw } // namespace sw
#endif // sw_Configurator_hpp #endif // sw_Configurator_hpp
...@@ -14,8 +14,8 @@ ...@@ -14,8 +14,8 @@
#include "Debug.hpp" #include "Debug.hpp"
#include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
namespace sw { namespace sw {
......
...@@ -16,44 +16,50 @@ ...@@ -16,44 +16,50 @@
#define Debug_hpp #define Debug_hpp
#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD) && !defined(ANDROID_NDK_BUILD) #if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD) && !defined(ANDROID_NDK_BUILD)
#include "DebugAndroid.hpp" # include "DebugAndroid.hpp"
#else #else
#include <assert.h> # include <assert.h>
#include <stdio.h> # include <stdio.h>
#undef min # undef min
#undef max # undef max
namespace sw { namespace sw {
void trace(const char *format, ...); void trace(const char *format, ...);
inline void trace() {} inline void trace() {}
} } // namespace sw
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) # if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define TRACE(format, ...) sw::trace("[0x%0.8X]%s(" format ")\n", this, __FUNCTION__, ##__VA_ARGS__) # define TRACE(format, ...) sw::trace("[0x%0.8X]%s(" format ")\n", this, __FUNCTION__, ##__VA_ARGS__)
#else # else
#define TRACE(...) ((void)0) # define TRACE(...) ((void)0)
#endif # endif
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define UNIMPLEMENTED(...) do { \
sw::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \
sw::trace(__VA_ARGS__); \
sw::trace("\n"); \
ASSERT(false); \
} while(0)
#else
#define UNIMPLEMENTED(...) ((void)0)
#endif
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) # if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define ASSERT(expression) {if(!(expression)) sw::trace("\t! Assert failed in %s(%d): " #expression "\n", __FUNCTION__, __LINE__); assert(expression);} # define UNIMPLEMENTED(...) \
#else do \
#define ASSERT assert { \
#endif sw::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \
sw::trace(__VA_ARGS__); \
sw::trace("\n"); \
ASSERT(false); \
} while(0)
# else
# define UNIMPLEMENTED(...) ((void)0)
# endif
# if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
# define ASSERT(expression) \
{ \
if(!(expression)) sw::trace("\t! Assert failed in %s(%d): " #expression "\n", __FUNCTION__, __LINE__); \
assert(expression); \
}
# else
# define ASSERT assert
# endif
#endif // !__ANDROID__ #endif // !__ANDROID__
#endif // Debug_hpp #endif // Debug_hpp
...@@ -14,16 +14,16 @@ ...@@ -14,16 +14,16 @@
#include "DebugAndroid.hpp" #include "DebugAndroid.hpp"
#include <cutils/properties.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <cutils/properties.h> #include <unistd.h>
void AndroidEnterDebugger() void AndroidEnterDebugger()
{ {
ALOGE(__FUNCTION__); ALOGE(__FUNCTION__);
#ifndef NDEBUG #ifndef NDEBUG
static volatile int * const makefault = nullptr; static volatile int *const makefault = nullptr;
char value[PROPERTY_VALUE_MAX]; char value[PROPERTY_VALUE_MAX];
property_get("debug.db.uid", value, "-1"); property_get("debug.db.uid", value, "-1");
int debug_uid = atoi(value); int debug_uid = atoi(value);
...@@ -31,7 +31,8 @@ void AndroidEnterDebugger() ...@@ -31,7 +31,8 @@ void AndroidEnterDebugger()
{ {
ALOGE("Waiting for debugger: gdbserver :${PORT} --attach %u. Look for thread %u", getpid(), gettid()); ALOGE("Waiting for debugger: gdbserver :${PORT} --attach %u. Look for thread %u", getpid(), gettid());
volatile int waiting = 1; volatile int waiting = 1;
while(waiting) { while(waiting)
{
sleep(1); sleep(1);
} }
} }
......
...@@ -16,11 +16,11 @@ ...@@ -16,11 +16,11 @@
#define DebugAndroid_hpp #define DebugAndroid_hpp
#if ANDROID_PLATFORM_SDK_VERSION < 27 #if ANDROID_PLATFORM_SDK_VERSION < 27
#include <cutils/log.h> # include <cutils/log.h>
#elif ANDROID_PLATFORM_SDK_VERSION >= 27 #elif ANDROID_PLATFORM_SDK_VERSION >= 27
#include <log/log.h> # include <log/log.h>
#else #else
#error "ANDROID_PLATFORM_SDK_VERSION is not defined" # error "ANDROID_PLATFORM_SDK_VERSION is not defined"
#endif #endif
#include <cassert> #include <cassert>
...@@ -48,52 +48,61 @@ ...@@ -48,52 +48,61 @@
*/ */
void AndroidEnterDebugger(); void AndroidEnterDebugger();
#define ASSERT(E) do { \ #define ASSERT(E) \
if(!(E)) { \ do \
ALOGE("badness: assertion_failed %s in %s at %s:%d", #E, \ { \
__FUNCTION__, __FILE__, __LINE__); \ if(!(E)) \
AndroidEnterDebugger(); \ { \
} \ ALOGE("badness: assertion_failed %s in %s at %s:%d", #E, \
__FUNCTION__, __FILE__, __LINE__); \
AndroidEnterDebugger(); \
} \
} while(0) } while(0)
#undef assert #undef assert
#define assert(E) ASSERT(E) #define assert(E) ASSERT(E)
#define ERR(format, ...) \ #define ERR(format, ...) \
do { \ do \
{ \
ALOGE("badness: err %s %s:%d (" format ")", __FUNCTION__, __FILE__, \ ALOGE("badness: err %s %s:%d (" format ")", __FUNCTION__, __FILE__, \
__LINE__, ##__VA_ARGS__); \ __LINE__, ##__VA_ARGS__); \
AndroidEnterDebugger(); \ AndroidEnterDebugger(); \
} while(0) } while(0)
#define FIXME(format, ...) \ #define FIXME(format, ...) \
do { \ do \
{ \
ALOGE("badness: fixme %s %s:%d (" format ")", __FUNCTION__, __FILE__, \ ALOGE("badness: fixme %s %s:%d (" format ")", __FUNCTION__, __FILE__, \
__LINE__, ##__VA_ARGS__); \ __LINE__, ##__VA_ARGS__); \
AndroidEnterDebugger(); \ AndroidEnterDebugger(); \
} while(0) } while(0)
// TODO: Handle __VA_ARGS__ (can be empty) // TODO: Handle __VA_ARGS__ (can be empty)
#define UNIMPLEMENTED(...) do { \ #define UNIMPLEMENTED(...) \
ALOGE("badness: unimplemented: %s %s:%d", \ do \
__FUNCTION__, __FILE__, __LINE__); \ { \
AndroidEnterDebugger(); \ ALOGE("badness: unimplemented: %s %s:%d", \
__FUNCTION__, __FILE__, __LINE__); \
AndroidEnterDebugger(); \
} while(0) } while(0)
#define UNREACHABLE(value) do { \ #define UNREACHABLE(value) \
do \
{ \
ALOGE("badness: unreachable case reached: %s %s:%d. %s: %d", \ ALOGE("badness: unreachable case reached: %s %s:%d. %s: %d", \
__FUNCTION__, __FILE__, __LINE__, #value, value); \ __FUNCTION__, __FILE__, __LINE__, #value, value); \
AndroidEnterDebugger(); \ AndroidEnterDebugger(); \
} while(0) } while(0)
#ifndef NDEBUG #ifndef NDEBUG
#define TRACE(format, ...) \ # define TRACE(format, ...) \
ALOGV("%s %s:%d (" format ")", __FUNCTION__, __FILE__, \ ALOGV("%s %s:%d (" format ")", __FUNCTION__, __FILE__, \
__LINE__, ##__VA_ARGS__) __LINE__, ##__VA_ARGS__)
#else #else
#define TRACE(...) ((void)0) # define TRACE(...) ((void)0)
#endif #endif
void trace(const char *format, ...); void trace(const char *format, ...);
#endif // DebugAndroid_hpp #endif // DebugAndroid_hpp
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "Debug.hpp" #include "Debug.hpp"
#ifdef HAVE_GRALLOC1 #ifdef HAVE_GRALLOC1
#include <sync/sync.h> # include <sync/sync.h>
#endif #endif
GrallocModule *GrallocModule::getInstance() GrallocModule *GrallocModule::getInstance()
...@@ -33,19 +33,19 @@ GrallocModule::GrallocModule() ...@@ -33,19 +33,19 @@ GrallocModule::GrallocModule()
m_major_version = (module->module_api_version >> 8) & 0xff; m_major_version = (module->module_api_version >> 8) & 0xff;
switch(m_major_version) switch(m_major_version)
{ {
case 0: case 0:
m_module = reinterpret_cast<const gralloc_module_t*>(module); m_module = reinterpret_cast<const gralloc_module_t *>(module);
break; break;
case 1: case 1:
#ifdef HAVE_GRALLOC1 #ifdef HAVE_GRALLOC1
gralloc1_open(module, &m_gralloc1_device); gralloc1_open(module, &m_gralloc1_device);
m_gralloc1_lock = (GRALLOC1_PFN_LOCK) m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK); m_gralloc1_lock = (GRALLOC1_PFN_LOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK);
m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK); m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK);
break; break;
#endif #endif
default: default:
TRACE("unknown gralloc major version (%d)", m_major_version); TRACE("unknown gralloc major version (%d)", m_major_version);
break; break;
} }
} }
...@@ -53,11 +53,11 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in ...@@ -53,11 +53,11 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in
{ {
switch(m_major_version) switch(m_major_version)
{ {
case 0: case 0:
{ {
return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr); return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
} }
case 1: case 1:
#ifdef HAVE_GRALLOC1 #ifdef HAVE_GRALLOC1
{ {
gralloc1_rect_t outRect{}; gralloc1_rect_t outRect{};
...@@ -68,7 +68,7 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in ...@@ -68,7 +68,7 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in
return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1); return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1);
} }
#endif #endif
default: default:
{ {
TRACE("no gralloc module to lock"); TRACE("no gralloc module to lock");
return -1; return -1;
...@@ -80,11 +80,11 @@ int GrallocModule::unlock(buffer_handle_t handle) ...@@ -80,11 +80,11 @@ int GrallocModule::unlock(buffer_handle_t handle)
{ {
switch(m_major_version) switch(m_major_version)
{ {
case 0: case 0:
{ {
return m_module->unlock(m_module, handle); return m_module->unlock(m_module, handle);
} }
case 1: case 1:
#ifdef HAVE_GRALLOC1 #ifdef HAVE_GRALLOC1
{ {
int32_t fenceFd = -1; int32_t fenceFd = -1;
...@@ -97,7 +97,7 @@ int GrallocModule::unlock(buffer_handle_t handle) ...@@ -97,7 +97,7 @@ int GrallocModule::unlock(buffer_handle_t handle)
return error; return error;
} }
#endif #endif
default: default:
{ {
TRACE("no gralloc module to unlock"); TRACE("no gralloc module to unlock");
return -1; return -1;
......
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
#include <hardware/gralloc.h> #include <hardware/gralloc.h>
#ifdef HAVE_GRALLOC1 #ifdef HAVE_GRALLOC1
#include <hardware/gralloc1.h> # include <hardware/gralloc1.h>
#endif #endif
#include <unistd.h> // for close() #include <unistd.h> // for close()
class GrallocModule class GrallocModule
{ {
......
...@@ -18,15 +18,15 @@ namespace sw { ...@@ -18,15 +18,15 @@ namespace sw {
half::half(float fp32) half::half(float fp32)
{ {
unsigned int fp32i = *(unsigned int*)&fp32; unsigned int fp32i = *(unsigned int *)&fp32;
unsigned int sign = (fp32i & 0x80000000) >> 16; unsigned int sign = (fp32i & 0x80000000) >> 16;
unsigned int abs = fp32i & 0x7FFFFFFF; unsigned int abs = fp32i & 0x7FFFFFFF;
if(abs > 0x47FFEFFF) // Infinity if(abs > 0x47FFEFFF) // Infinity
{ {
fp16i = sign | 0x7FFF; fp16i = sign | 0x7FFF;
} }
else if(abs < 0x38800000) // Denormal else if(abs < 0x38800000) // Denormal
{ {
unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000; unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
int e = 113 - (abs >> 23); int e = 113 - (abs >> 23);
...@@ -54,7 +54,7 @@ half::operator float() const ...@@ -54,7 +54,7 @@ half::operator float() const
int s = (fp16i >> 15) & 0x00000001; int s = (fp16i >> 15) & 0x00000001;
int e = (fp16i >> 10) & 0x0000001F; int e = (fp16i >> 10) & 0x0000001F;
int m = fp16i & 0x000003FF; int m = fp16i & 0x000003FF;
if(e == 0) if(e == 0)
{ {
...@@ -62,14 +62,14 @@ half::operator float() const ...@@ -62,14 +62,14 @@ half::operator float() const
{ {
fp32i = s << 31; fp32i = s << 31;
return (float&)fp32i; return (float &)fp32i;
} }
else else
{ {
while(!(m & 0x00000400)) while(!(m & 0x00000400))
{ {
m <<= 1; m <<= 1;
e -= 1; e -= 1;
} }
e += 1; e += 1;
...@@ -82,7 +82,7 @@ half::operator float() const ...@@ -82,7 +82,7 @@ half::operator float() const
fp32i = (s << 31) | (e << 23) | m; fp32i = (s << 31) | (e << 23) | m;
return (float&)fp32i; return (float &)fp32i;
} }
half &half::operator=(half h) half &half::operator=(half h)
...@@ -92,7 +92,6 @@ half &half::operator=(half h) ...@@ -92,7 +92,6 @@ half &half::operator=(half h)
return *this; return *this;
} }
half &half::operator=(float f) half &half::operator=(float f)
{ {
*this = half(f); *this = half(f);
......
...@@ -58,7 +58,8 @@ class RGB9E5 ...@@ -58,7 +58,8 @@ class RGB9E5
unsigned int E : 5; unsigned int E : 5;
public: public:
RGB9E5(float rgb[3]) : RGB9E5(rgb[0], rgb[1], rgb[2]) RGB9E5(float rgb[3])
: RGB9E5(rgb[0], rgb[1], rgb[2])
{ {
} }
...@@ -76,14 +77,14 @@ public: ...@@ -76,14 +77,14 @@ public:
constexpr int g_sharedexp_maxexponent = 31; constexpr int g_sharedexp_maxexponent = 31;
constexpr float g_sharedexp_max = constexpr float g_sharedexp_max =
((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) / ((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) /
static_cast<float>(1 << g_sharedexp_mantissabits)) * static_cast<float>(1 << g_sharedexp_mantissabits)) *
static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias)); static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias));
// Clamp components to valid range. NaN becomes 0. // Clamp components to valid range. NaN becomes 0.
const float red_c = std::min(!(r > 0) ? 0 : r, g_sharedexp_max); const float red_c = std::min(!(r > 0) ? 0 : r, g_sharedexp_max);
const float green_c = std::min(!(g > 0) ? 0 : g, g_sharedexp_max); const float green_c = std::min(!(g > 0) ? 0 : g, g_sharedexp_max);
const float blue_c = std::min(!(b > 0) ? 0 : b, g_sharedexp_max); const float blue_c = std::min(!(b > 0) ? 0 : b, g_sharedexp_max);
// We're reducing the mantissa to 9 bits, so we must round up if the next // We're reducing the mantissa to 9 bits, so we must round up if the next
// bit is 1. In other words add 0.5 to the new mantissa's position and // bit is 1. In other words add 0.5 to the new mantissa's position and
...@@ -111,12 +112,12 @@ public: ...@@ -111,12 +112,12 @@ public:
operator unsigned int() const operator unsigned int() const
{ {
return *reinterpret_cast<const unsigned int*>(this); return *reinterpret_cast<const unsigned int *>(this);
} }
void toRGB16F(half rgb[3]) const void toRGB16F(half rgb[3]) const
{ {
constexpr int offset = 24; // Exponent bias (15) + number of mantissa bits per component (9) = 24 constexpr int offset = 24; // Exponent bias (15) + number of mantissa bits per component (9) = 24
const float factor = (1u << E) * (1.0f / (1 << offset)); const float factor = (1u << E) * (1.0f / (1 << offset));
rgb[0] = half(R * factor); rgb[0] = half(R * factor);
...@@ -133,12 +134,12 @@ class R11G11B10F ...@@ -133,12 +134,12 @@ class R11G11B10F
static inline half float11ToFloat16(unsigned short fp11) static inline half float11ToFloat16(unsigned short fp11)
{ {
return shortAsHalf(fp11 << 4); // Sign bit 0 return shortAsHalf(fp11 << 4); // Sign bit 0
} }
static inline half float10ToFloat16(unsigned short fp10) static inline half float10ToFloat16(unsigned short fp10)
{ {
return shortAsHalf(fp10 << 5); // Sign bit 0 return shortAsHalf(fp10 << 5); // Sign bit 0
} }
inline unsigned short float32ToFloat11(float fp32) inline unsigned short float32ToFloat11(float fp32)
...@@ -159,7 +160,7 @@ class R11G11B10F ...@@ -159,7 +160,7 @@ class R11G11B10F
const unsigned int float32Maxfloat11 = 0x477E0000; const unsigned int float32Maxfloat11 = 0x477E0000;
const unsigned int float32Minfloat11 = 0x38800000; const unsigned int float32Minfloat11 = 0x38800000;
const unsigned int float32Bits = *reinterpret_cast<unsigned int*>(&fp32); const unsigned int float32Bits = *reinterpret_cast<unsigned int *>(&fp32);
const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask; const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
unsigned int float32Val = float32Bits & float32ValueMask; unsigned int float32Val = float32Bits & float32ValueMask;
...@@ -170,8 +171,8 @@ class R11G11B10F ...@@ -170,8 +171,8 @@ class R11G11B10F
if((float32Val & float32MantissaMask) != 0) if((float32Val & float32MantissaMask) != 0)
{ {
return float11ExponentMask | return float11ExponentMask |
(((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) &
float11MantissaMask); float11MantissaMask);
} }
else if(float32Sign) else if(float32Sign)
{ {
...@@ -200,9 +201,9 @@ class R11G11B10F ...@@ -200,9 +201,9 @@ class R11G11B10F
// The number is too small to be represented as a normalized float11 // The number is too small to be represented as a normalized float11
// Convert it to a denormalized value. // Convert it to a denormalized value.
const unsigned int shift = (float32ExponentBias - float11ExponentBias) - const unsigned int shift = (float32ExponentBias - float11ExponentBias) -
(float32Val >> float32ExponentFirstBit); (float32Val >> float32ExponentFirstBit);
float32Val = float32Val =
((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift; ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
} }
else else
{ {
...@@ -232,7 +233,7 @@ class R11G11B10F ...@@ -232,7 +233,7 @@ class R11G11B10F
const unsigned int float32Maxfloat10 = 0x477C0000; const unsigned int float32Maxfloat10 = 0x477C0000;
const unsigned int float32Minfloat10 = 0x38800000; const unsigned int float32Minfloat10 = 0x38800000;
const unsigned int float32Bits = *reinterpret_cast<unsigned int*>(&fp32); const unsigned int float32Bits = *reinterpret_cast<unsigned int *>(&fp32);
const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask; const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
unsigned int float32Val = float32Bits & float32ValueMask; unsigned int float32Val = float32Bits & float32ValueMask;
...@@ -243,8 +244,8 @@ class R11G11B10F ...@@ -243,8 +244,8 @@ class R11G11B10F
if((float32Val & float32MantissaMask) != 0) if((float32Val & float32MantissaMask) != 0)
{ {
return float10ExponentMask | return float10ExponentMask |
(((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) &
float10MantissaMask); float10MantissaMask);
} }
else if(float32Sign) else if(float32Sign)
{ {
...@@ -273,9 +274,9 @@ class R11G11B10F ...@@ -273,9 +274,9 @@ class R11G11B10F
// The number is too small to be represented as a normalized float11 // The number is too small to be represented as a normalized float11
// Convert it to a denormalized value. // Convert it to a denormalized value.
const unsigned int shift = (float32ExponentBias - float10ExponentBias) - const unsigned int shift = (float32ExponentBias - float10ExponentBias) -
(float32Val >> float32ExponentFirstBit); (float32Val >> float32ExponentFirstBit);
float32Val = float32Val =
((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift; ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
} }
else else
{ {
...@@ -297,7 +298,7 @@ public: ...@@ -297,7 +298,7 @@ public:
operator unsigned int() const operator unsigned int() const
{ {
return *reinterpret_cast<const unsigned int*>(this); return *reinterpret_cast<const unsigned int *>(this);
} }
void toRGB16F(half rgb[3]) const void toRGB16F(half rgb[3]) const
...@@ -310,4 +311,4 @@ public: ...@@ -310,4 +311,4 @@ public:
} // namespace sw } // namespace sw
#endif // sw_Half_hpp #endif // sw_Half_hpp
...@@ -22,19 +22,19 @@ ...@@ -22,19 +22,19 @@
#include <unistd.h> #include <unistd.h>
#ifndef MFD_CLOEXEC #ifndef MFD_CLOEXEC
#define MFD_CLOEXEC 0x0001U # define MFD_CLOEXEC 0x0001U
#endif #endif
#if __aarch64__ #if __aarch64__
#define __NR_memfd_create 279 # define __NR_memfd_create 279
#elif __arm__ #elif __arm__
#define __NR_memfd_create 279 # define __NR_memfd_create 279
#elif __powerpc64__ #elif __powerpc64__
#define __NR_memfd_create 360 # define __NR_memfd_create 360
#elif __i386__ #elif __i386__
#define __NR_memfd_create 356 # define __NR_memfd_create 356
#elif __x86_64__ #elif __x86_64__
#define __NR_memfd_create 319 # define __NR_memfd_create 319
#endif /* __NR_memfd_create__ */ #endif /* __NR_memfd_create__ */
LinuxMemFd::~LinuxMemFd() LinuxMemFd::~LinuxMemFd()
...@@ -59,7 +59,7 @@ int LinuxMemFd::exportFd() const ...@@ -59,7 +59,7 @@ int LinuxMemFd::exportFd() const
return ::fcntl(fd_, F_DUPFD_CLOEXEC, 0); return ::fcntl(fd_, F_DUPFD_CLOEXEC, 0);
} }
bool LinuxMemFd::allocate(const char* name, size_t size) bool LinuxMemFd::allocate(const char *name, size_t size)
{ {
close(); close();
...@@ -93,7 +93,8 @@ void LinuxMemFd::close() ...@@ -93,7 +93,8 @@ void LinuxMemFd::close()
// WARNING: Never retry on close() failure, even with EINTR, see // WARNING: Never retry on close() failure, even with EINTR, see
// https://lwn.net/Articles/576478/ for example. // https://lwn.net/Articles/576478/ for example.
int ret = ::close(fd_); int ret = ::close(fd_);
if(ret < 0) { if(ret < 0)
{
TRACE("LinuxMemFd::close() failed with: %s", strerror(errno)); TRACE("LinuxMemFd::close() failed with: %s", strerror(errno));
assert(false); assert(false);
} }
...@@ -101,14 +102,14 @@ void LinuxMemFd::close() ...@@ -101,14 +102,14 @@ void LinuxMemFd::close()
} }
} }
void* LinuxMemFd::mapReadWrite(size_t offset, size_t size) void *LinuxMemFd::mapReadWrite(size_t offset, size_t size)
{ {
void* addr = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, void *addr = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_,
static_cast<off_t>(offset)); static_cast<off_t>(offset));
return (addr == MAP_FAILED) ? nullptr : addr; return (addr == MAP_FAILED) ? nullptr : addr;
} }
bool LinuxMemFd::unmap(void* addr, size_t size) bool LinuxMemFd::unmap(void *addr, size_t size)
{ {
return ::munmap(addr, size) == 0; return ::munmap(addr, size) == 0;
} }
...@@ -22,11 +22,14 @@ ...@@ -22,11 +22,14 @@
// supported by the Linux kernel since 3.17 (good enough for Android and desktop // supported by the Linux kernel since 3.17 (good enough for Android and desktop
// Linux). // Linux).
class LinuxMemFd { class LinuxMemFd
{
public: public:
LinuxMemFd() = default; LinuxMemFd() = default;
LinuxMemFd(const char* name, size_t size) : LinuxMemFd() { LinuxMemFd(const char *name, size_t size)
: LinuxMemFd()
{
allocate(name, size); allocate(name, size);
} }
...@@ -45,19 +48,19 @@ public: ...@@ -45,19 +48,19 @@ public:
int exportFd() const; int exportFd() const;
// Implement memfd_create() through direct syscalls if possible. // Implement memfd_create() through direct syscalls if possible.
// On success, return true and sets |fd| accordingly. On failure, return // On success, return true and sets |fd| accordingly. On failure, return
// false and sets errno. // false and sets errno.
bool allocate(const char* name, size_t size); bool allocate(const char *name, size_t size);
// Map a segment of |size| bytes from |offset| from the region. // Map a segment of |size| bytes from |offset| from the region.
// Both |offset| and |size| should be page-aligned. Returns nullptr/errno // Both |offset| and |size| should be page-aligned. Returns nullptr/errno
// on failure. // on failure.
void* mapReadWrite(size_t offset, size_t size); void *mapReadWrite(size_t offset, size_t size);
// Unmap a region segment starting at |addr| of |size| bytes. // Unmap a region segment starting at |addr| of |size| bytes.
// Both |addr| and |size| should be page-aligned. Returns true on success // Both |addr| and |size| should be page-aligned. Returns true on success
// or false/errno on failure. // or false/errno on failure.
bool unmap(void* addr, size_t size); bool unmap(void *addr, size_t size);
void close(); void close();
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include <cmath> #include <cmath>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <intrin.h> # include <intrin.h>
#endif #endif
namespace sw { namespace sw {
...@@ -67,7 +67,7 @@ inline constexpr T min(T a, T b, T c, T d) ...@@ -67,7 +67,7 @@ inline constexpr T min(T a, T b, T c, T d)
return min(min(a, b), min(c, d)); return min(min(a, b), min(c, d));
} }
template <typename destType, typename sourceType> template<typename destType, typename sourceType>
destType bit_cast(const sourceType &source) destType bit_cast(const sourceType &source)
{ {
union union
...@@ -82,7 +82,7 @@ destType bit_cast(const sourceType &source) ...@@ -82,7 +82,7 @@ destType bit_cast(const sourceType &source)
inline int iround(float x) inline int iround(float x)
{ {
return (int)floor(x + 0.5f); return (int)floor(x + 0.5f);
// return _mm_cvtss_si32(_mm_load_ss(&x)); // FIXME: Demands SSE support // return _mm_cvtss_si32(_mm_load_ss(&x)); // FIXME: Demands SSE support
} }
inline int ifloor(float x) inline int ifloor(float x)
...@@ -100,52 +100,52 @@ inline int ceilInt4(int x) ...@@ -100,52 +100,52 @@ inline int ceilInt4(int x)
return (x + 0xF) >> 4; return (x + 0xF) >> 4;
} }
#define BITS(x) ( \ #define BITS(x) ( \
!!((x) & 0x80000000) + \ !!((x)&0x80000000) + \
!!((x) & 0xC0000000) + \ !!((x)&0xC0000000) + \
!!((x) & 0xE0000000) + \ !!((x)&0xE0000000) + \
!!((x) & 0xF0000000) + \ !!((x)&0xF0000000) + \
!!((x) & 0xF8000000) + \ !!((x)&0xF8000000) + \
!!((x) & 0xFC000000) + \ !!((x)&0xFC000000) + \
!!((x) & 0xFE000000) + \ !!((x)&0xFE000000) + \
!!((x) & 0xFF000000) + \ !!((x)&0xFF000000) + \
!!((x) & 0xFF800000) + \ !!((x)&0xFF800000) + \
!!((x) & 0xFFC00000) + \ !!((x)&0xFFC00000) + \
!!((x) & 0xFFE00000) + \ !!((x)&0xFFE00000) + \
!!((x) & 0xFFF00000) + \ !!((x)&0xFFF00000) + \
!!((x) & 0xFFF80000) + \ !!((x)&0xFFF80000) + \
!!((x) & 0xFFFC0000) + \ !!((x)&0xFFFC0000) + \
!!((x) & 0xFFFE0000) + \ !!((x)&0xFFFE0000) + \
!!((x) & 0xFFFF0000) + \ !!((x)&0xFFFF0000) + \
!!((x) & 0xFFFF8000) + \ !!((x)&0xFFFF8000) + \
!!((x) & 0xFFFFC000) + \ !!((x)&0xFFFFC000) + \
!!((x) & 0xFFFFE000) + \ !!((x)&0xFFFFE000) + \
!!((x) & 0xFFFFF000) + \ !!((x)&0xFFFFF000) + \
!!((x) & 0xFFFFF800) + \ !!((x)&0xFFFFF800) + \
!!((x) & 0xFFFFFC00) + \ !!((x)&0xFFFFFC00) + \
!!((x) & 0xFFFFFE00) + \ !!((x)&0xFFFFFE00) + \
!!((x) & 0xFFFFFF00) + \ !!((x)&0xFFFFFF00) + \
!!((x) & 0xFFFFFF80) + \ !!((x)&0xFFFFFF80) + \
!!((x) & 0xFFFFFFC0) + \ !!((x)&0xFFFFFFC0) + \
!!((x) & 0xFFFFFFE0) + \ !!((x)&0xFFFFFFE0) + \
!!((x) & 0xFFFFFFF0) + \ !!((x)&0xFFFFFFF0) + \
!!((x) & 0xFFFFFFF8) + \ !!((x)&0xFFFFFFF8) + \
!!((x) & 0xFFFFFFFC) + \ !!((x)&0xFFFFFFFC) + \
!!((x) & 0xFFFFFFFE) + \ !!((x)&0xFFFFFFFE) + \
!!((x) & 0xFFFFFFFF)) !!((x)&0xFFFFFFFF))
#define MAX(x, y) ((x) > (y) ? (x) : (y)) #define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y)) #define MIN(x, y) ((x) < (y) ? (x) : (y))
inline unsigned long log2i(int x) inline unsigned long log2i(int x)
{ {
#if defined(_MSC_VER) #if defined(_MSC_VER)
unsigned long y; unsigned long y;
_BitScanReverse(&y, x); _BitScanReverse(&y, x);
return y; return y;
#else #else
return 31 - __builtin_clz(x); return 31 - __builtin_clz(x);
#endif #endif
} }
inline bool isPow2(int x) inline bool isPow2(int x)
...@@ -331,11 +331,11 @@ inline float sRGBtoLinear(float c) ...@@ -331,11 +331,11 @@ inline float sRGBtoLinear(float c)
{ {
if(c <= 0.04045f) if(c <= 0.04045f)
{ {
return c * 0.07739938f; // 1.0f / 12.92f; return c * 0.07739938f; // 1.0f / 12.92f;
} }
else else
{ {
return powf((c + 0.055f) * 0.9478673f, 2.4f); // 1.0f / 1.055f return powf((c + 0.055f) * 0.9478673f, 2.4f); // 1.0f / 1.055f
} }
} }
...@@ -347,13 +347,13 @@ inline float linearToSRGB(float c) ...@@ -347,13 +347,13 @@ inline float linearToSRGB(float c)
} }
else else
{ {
return 1.055f * powf(c, 0.4166667f) - 0.055f; // 1.0f / 2.4f return 1.055f * powf(c, 0.4166667f) - 0.055f; // 1.0f / 2.4f
} }
} }
unsigned char sRGB8toLinear8(unsigned char value); unsigned char sRGB8toLinear8(unsigned char value);
uint64_t FNV_1a(const unsigned char *data, int size); // Fowler-Noll-Vo hash function uint64_t FNV_1a(const unsigned char *data, int size); // Fowler-Noll-Vo hash function
// Round up to the next multiple of alignment // Round up to the next multiple of alignment
template<typename T> template<typename T>
...@@ -374,10 +374,11 @@ inline int clampToSignedInt(unsigned int x) ...@@ -374,10 +374,11 @@ inline int clampToSignedInt(unsigned int x)
} }
// Convert floating value v to fixed point with p digits after the decimal point // Convert floating value v to fixed point with p digits after the decimal point
constexpr int toFixedPoint(float v, int p) { constexpr int toFixedPoint(float v, int p)
{
return static_cast<int>(v * (1 << p)); return static_cast<int>(v * (1 << p));
} }
} // namespace sw } // namespace sw
#endif // sw_Math_hpp #endif // sw_Math_hpp
...@@ -14,30 +14,30 @@ ...@@ -14,30 +14,30 @@
#include "Memory.hpp" #include "Memory.hpp"
#include "Types.hpp"
#include "Debug.hpp" #include "Debug.hpp"
#include "Types.hpp"
#if defined(_WIN32) #if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN # ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
#endif # endif
#include <windows.h> # include <windows.h>
#include <intrin.h> # include <intrin.h>
#else #else
#include <errno.h> # include <errno.h>
#include <sys/mman.h> # include <sys/mman.h>
#include <stdlib.h> # include <stdlib.h>
#include <unistd.h> # include <unistd.h>
#endif #endif
#include <cstring>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#undef allocate #undef allocate
#undef deallocate #undef deallocate
#if(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined (_M_X64)) && !defined(__x86__) #if(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)) && !defined(__x86__)
#define __x86__ # define __x86__
#endif #endif
namespace sw { namespace sw {
...@@ -46,45 +46,45 @@ namespace { ...@@ -46,45 +46,45 @@ namespace {
struct Allocation struct Allocation
{ {
// size_t bytes; // size_t bytes;
unsigned char *block; unsigned char *block;
}; };
void *allocateRaw(size_t bytes, size_t alignment) void *allocateRaw(size_t bytes, size_t alignment)
{ {
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment. ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(LINUX_ENABLE_NAMED_MMAP)
if(alignment < sizeof(void*)) if(alignment < sizeof(void *))
{ {
return malloc(bytes); return malloc(bytes);
} }
else else
{
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
{ {
void *allocation; errno = result;
int result = posix_memalign(&allocation, alignment, bytes); allocation = nullptr;
if(result != 0)
{
errno = result;
allocation = nullptr;
}
return allocation;
} }
#else return allocation;
unsigned char *block = (unsigned char*)malloc(bytes + sizeof(Allocation) + alignment); }
unsigned char *aligned = nullptr; #else
unsigned char *block = (unsigned char *)malloc(bytes + sizeof(Allocation) + alignment);
unsigned char *aligned = nullptr;
if(block) if(block)
{ {
aligned = (unsigned char*)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment); aligned = (unsigned char *)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation)); Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
// allocation->bytes = bytes; // allocation->bytes = bytes;
allocation->block = block; allocation->block = block;
} }
return aligned; return aligned;
#endif #endif
} }
} // anonymous namespace } // anonymous namespace
...@@ -95,13 +95,13 @@ size_t memoryPageSize() ...@@ -95,13 +95,13 @@ size_t memoryPageSize()
if(pageSize == 0) if(pageSize == 0)
{ {
#if defined(_WIN32) #if defined(_WIN32)
SYSTEM_INFO systemInfo; SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo); GetSystemInfo(&systemInfo);
pageSize = systemInfo.dwPageSize; pageSize = systemInfo.dwPageSize;
#else #else
pageSize = sysconf(_SC_PAGESIZE); pageSize = sysconf(_SC_PAGESIZE);
#endif #endif
} }
return pageSize; return pageSize;
...@@ -121,45 +121,51 @@ void *allocate(size_t bytes, size_t alignment) ...@@ -121,45 +121,51 @@ void *allocate(size_t bytes, size_t alignment)
void deallocate(void *memory) void deallocate(void *memory)
{ {
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(LINUX_ENABLE_NAMED_MMAP)
free(memory); free(memory);
#else #else
if(memory) if(memory)
{ {
unsigned char *aligned = (unsigned char*)memory; unsigned char *aligned = (unsigned char *)memory;
Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation)); Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
free(allocation->block); free(allocation->block);
} }
#endif #endif
} }
void clear(uint16_t *memory, uint16_t element, size_t count) void clear(uint16_t *memory, uint16_t element, size_t count)
{ {
#if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER) #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__stosw(memory, element, count); __stosw(memory, element, count);
#elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER) #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__asm__ __volatile__("rep stosw" : "+D"(memory), "+c"(count) : "a"(element) : "memory"); __asm__ __volatile__("rep stosw"
#else : "+D"(memory), "+c"(count)
for(size_t i = 0; i < count; i++) : "a"(element)
{ : "memory");
memory[i] = element; #else
} for(size_t i = 0; i < count; i++)
#endif {
memory[i] = element;
}
#endif
} }
void clear(uint32_t *memory, uint32_t element, size_t count) void clear(uint32_t *memory, uint32_t element, size_t count)
{ {
#if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER) #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__stosd((unsigned long*)memory, element, count); __stosd((unsigned long *)memory, element, count);
#elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER) #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__asm__ __volatile__("rep stosl" : "+D"(memory), "+c"(count) : "a"(element) : "memory"); __asm__ __volatile__("rep stosl"
#else : "+D"(memory), "+c"(count)
for(size_t i = 0; i < count; i++) : "a"(element)
{ : "memory");
memory[i] = element; #else
} for(size_t i = 0; i < count; i++)
#endif {
memory[i] = element;
}
#endif
} }
} // namespace sw } // namespace sw
...@@ -30,4 +30,4 @@ void clear(uint32_t *memory, uint32_t element, size_t count); ...@@ -30,4 +30,4 @@ void clear(uint32_t *memory, uint32_t element, size_t count);
} // namespace sw } // namespace sw
#endif // Memory_hpp #endif // Memory_hpp
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
#define SharedLibrary_hpp #define SharedLibrary_hpp
#if defined(_WIN32) #if defined(_WIN32)
#include <Windows.h> # include <Windows.h>
#else #else
#include <dlfcn.h> # include <dlfcn.h>
#endif #endif
#include <string> #include <string>
...@@ -67,105 +67,105 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], ...@@ -67,105 +67,105 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
} }
#if defined(_WIN32) #if defined(_WIN32)
inline void *loadLibrary(const char *path) inline void *loadLibrary(const char *path)
{ {
return (void*)LoadLibrary(path); return (void *)LoadLibrary(path);
} }
inline void *getLibraryHandle(const char *path) inline void *getLibraryHandle(const char *path)
{ {
HMODULE module = NULL; HMODULE module = NULL;
GetModuleHandleEx(0, path, &module); GetModuleHandleEx(0, path, &module);
return (void*)module; return (void *)module;
} }
inline void freeLibrary(void *library) inline void freeLibrary(void *library)
{ {
FreeLibrary((HMODULE)library); FreeLibrary((HMODULE)library);
} }
inline void *getProcAddress(void *library, const char *name) inline void *getProcAddress(void *library, const char *name)
{ {
return (void*)GetProcAddress((HMODULE)library, name); return (void *)GetProcAddress((HMODULE)library, name);
} }
inline std::string getModuleDirectory() inline std::string getModuleDirectory()
{ {
static int dummy_symbol = 0; static int dummy_symbol = 0;
HMODULE module = NULL; HMODULE module = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)&dummy_symbol, &module); GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)&dummy_symbol, &module);
char filename[1024]; char filename[1024];
if(module && (GetModuleFileName(module, filename, sizeof(filename)) != 0)) if(module && (GetModuleFileName(module, filename, sizeof(filename)) != 0))
{ {
std::string directory(filename); std::string directory(filename);
return directory.substr(0, directory.find_last_of("\\/") + 1).c_str(); return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
}
else
{
return "";
}
} }
#else else
inline void *loadLibrary(const char *path)
{ {
return dlopen(path, RTLD_LAZY | RTLD_LOCAL); return "";
} }
}
#else
inline void *loadLibrary(const char *path)
{
return dlopen(path, RTLD_LAZY | RTLD_LOCAL);
}
inline void *getLibraryHandle(const char *path) inline void *getLibraryHandle(const char *path)
{
# ifdef __ANDROID__
// bionic doesn't support RTLD_NOLOAD before L
return dlopen(path, RTLD_NOW | RTLD_LOCAL);
# else
void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
if(resident)
{ {
#ifdef __ANDROID__ return dlopen(path, RTLD_LAZY | RTLD_LOCAL); // Increment reference count
// bionic doesn't support RTLD_NOLOAD before L }
return dlopen(path, RTLD_NOW | RTLD_LOCAL);
#else
void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
if(resident) return nullptr;
{ # endif
return dlopen(path, RTLD_LAZY | RTLD_LOCAL); // Increment reference count }
}
return nullptr; inline void freeLibrary(void *library)
#endif {
if(library)
{
dlclose(library);
} }
}
inline void freeLibrary(void *library) inline void *getProcAddress(void *library, const char *name)
{
void *symbol = dlsym(library, name);
if(!symbol)
{ {
if(library) const char *reason = dlerror(); // Silence the error
{ (void)reason;
dlclose(library);
}
} }
inline void *getProcAddress(void *library, const char *name) return symbol;
{ }
void *symbol = dlsym(library, name);
if(!symbol) inline std::string getModuleDirectory()
{ {
const char *reason = dlerror(); // Silence the error static int dummy_symbol = 0;
(void)reason;
}
return symbol; Dl_info dl_info;
if(dladdr(&dummy_symbol, &dl_info) != 0)
{
std::string directory(dl_info.dli_fname);
return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
} }
else
inline std::string getModuleDirectory()
{ {
static int dummy_symbol = 0; return "";
Dl_info dl_info;
if(dladdr(&dummy_symbol, &dl_info) != 0)
{
std::string directory(dl_info.dli_fname);
return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
}
else
{
return "";
}
} }
}
#endif #endif
#endif // SharedLibrary_hpp #endif // SharedLibrary_hpp
...@@ -15,27 +15,28 @@ ...@@ -15,27 +15,28 @@
#include "Socket.hpp" #include "Socket.hpp"
#if defined(_WIN32) #if defined(_WIN32)
#include <ws2tcpip.h> # include <ws2tcpip.h>
#else #else
#include <unistd.h> # include <unistd.h>
#include <netdb.h> # include <netdb.h>
#include <netinet/in.h> # include <netinet/in.h>
#include <sys/select.h> # include <sys/select.h>
#endif #endif
namespace sw { namespace sw {
Socket::Socket(SOCKET socket) : socket(socket) Socket::Socket(SOCKET socket)
: socket(socket)
{ {
} }
Socket::Socket(const char *address, const char *port) Socket::Socket(const char *address, const char *port)
{ {
#if defined(_WIN32) #if defined(_WIN32)
socket = INVALID_SOCKET; socket = INVALID_SOCKET;
#else #else
socket = -1; socket = -1;
#endif #endif
addrinfo hints = {}; addrinfo hints = {};
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
...@@ -55,11 +56,11 @@ Socket::Socket(const char *address, const char *port) ...@@ -55,11 +56,11 @@ Socket::Socket(const char *address, const char *port)
Socket::~Socket() Socket::~Socket()
{ {
#if defined(_WIN32) #if defined(_WIN32)
closesocket(socket); closesocket(socket);
#else #else
close(socket); close(socket);
#endif #endif
} }
void Socket::listen(int backlog) void Socket::listen(int backlog)
...@@ -73,7 +74,7 @@ bool Socket::select(int us) ...@@ -73,7 +74,7 @@ bool Socket::select(int us)
FD_ZERO(&sockets); FD_ZERO(&sockets);
FD_SET(socket, &sockets); FD_SET(socket, &sockets);
timeval timeout = {us / 1000000, us % 1000000}; timeval timeout = { us / 1000000, us % 1000000 };
return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1; return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1;
} }
...@@ -95,17 +96,17 @@ void Socket::send(const char *buffer, int length) ...@@ -95,17 +96,17 @@ void Socket::send(const char *buffer, int length)
void Socket::startup() void Socket::startup()
{ {
#if defined(_WIN32) #if defined(_WIN32)
WSADATA winsockData; WSADATA winsockData;
WSAStartup(MAKEWORD(2, 2), &winsockData); WSAStartup(MAKEWORD(2, 2), &winsockData);
#endif #endif
} }
void Socket::cleanup() void Socket::cleanup()
{ {
#if defined(_WIN32) #if defined(_WIN32)
WSACleanup(); WSACleanup();
#endif #endif
} }
} // namespace sw } // namespace sw
...@@ -16,10 +16,10 @@ ...@@ -16,10 +16,10 @@
#define sw_Socket_hpp #define sw_Socket_hpp
#if defined(_WIN32) #if defined(_WIN32)
#include <winsock2.h> # include <winsock2.h>
#else #else
#include <sys/socket.h> # include <sys/socket.h>
typedef int SOCKET; typedef int SOCKET;
#endif #endif
namespace sw { namespace sw {
...@@ -45,6 +45,6 @@ private: ...@@ -45,6 +45,6 @@ private:
SOCKET socket; SOCKET socket;
}; };
} } // namespace sw
#endif // sw_Socket_hpp #endif // sw_Socket_hpp
...@@ -42,7 +42,11 @@ public: ...@@ -42,7 +42,11 @@ public:
// a corresponding call to start(). // a corresponding call to start().
virtual void finish() = 0; virtual void finish() = 0;
// complete() is a helper for calling start() followed by finish(). // complete() is a helper for calling start() followed by finish().
inline void complete() { start(); finish(); } inline void complete()
{
start();
finish();
}
protected: protected:
virtual ~TaskEvents() = default; virtual ~TaskEvents() = default;
...@@ -89,8 +93,8 @@ public: ...@@ -89,8 +93,8 @@ public:
// wait() blocks until all the tasks have been finished or the timeout // wait() blocks until all the tasks have been finished or the timeout
// has been reached, returning true if all tasks have been completed, or // has been reached, returning true if all tasks have been completed, or
// false if the timeout has been reached. // false if the timeout has been reached.
template <class CLOCK, class DURATION> template<class CLOCK, class DURATION>
bool wait(const std::chrono::time_point<CLOCK, DURATION>& timeout) bool wait(const std::chrono::time_point<CLOCK, DURATION> &timeout)
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
return condition.wait_until(lock, timeout, [this] { return count_ == 0; }); return condition.wait_until(lock, timeout, [this] { return count_ == 0; });
...@@ -111,14 +115,14 @@ public: ...@@ -111,14 +115,14 @@ public:
void finish() override { done(); } void finish() override { done(); }
private: private:
int32_t count_ = 0; // guarded by mutex int32_t count_ = 0; // guarded by mutex
std::mutex mutex; std::mutex mutex;
std::condition_variable condition; std::condition_variable condition;
}; };
// Chan is a thread-safe FIFO queue of type T. // Chan is a thread-safe FIFO queue of type T.
// Chan takes its name after Golang's chan. // Chan takes its name after Golang's chan.
template <typename T> template<typename T>
class Chan class Chan
{ {
public: public:
...@@ -148,10 +152,11 @@ private: ...@@ -148,10 +152,11 @@ private:
std::condition_variable added; std::condition_variable added;
}; };
template <typename T> template<typename T>
Chan<T>::Chan() {} Chan<T>::Chan()
{}
template <typename T> template<typename T>
T Chan<T>::take() T Chan<T>::take()
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
...@@ -162,7 +167,7 @@ T Chan<T>::take() ...@@ -162,7 +167,7 @@ T Chan<T>::take()
return out; return out;
} }
template <typename T> template<typename T>
std::pair<T, bool> Chan<T>::tryTake() std::pair<T, bool> Chan<T>::tryTake()
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
...@@ -175,7 +180,7 @@ std::pair<T, bool> Chan<T>::tryTake() ...@@ -175,7 +180,7 @@ std::pair<T, bool> Chan<T>::tryTake()
return std::make_pair(out, true); return std::make_pair(out, true);
} }
template <typename T> template<typename T>
void Chan<T>::put(const T &item) void Chan<T>::put(const T &item)
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
...@@ -183,7 +188,7 @@ void Chan<T>::put(const T &item) ...@@ -183,7 +188,7 @@ void Chan<T>::put(const T &item)
added.notify_one(); added.notify_one();
} }
template <typename T> template<typename T>
size_t Chan<T>::count() size_t Chan<T>::count()
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
...@@ -192,4 +197,4 @@ size_t Chan<T>::count() ...@@ -192,4 +197,4 @@ size_t Chan<T>::count()
} // namespace sw } // namespace sw
#endif // sw_Synchronization_hpp #endif // sw_Synchronization_hpp
...@@ -15,24 +15,24 @@ ...@@ -15,24 +15,24 @@
#include "Timer.hpp" #include "Timer.hpp"
#if !defined(__i386__) && defined(_M_IX86) #if !defined(__i386__) && defined(_M_IX86)
#define __i386__ 1 # define __i386__ 1
#endif #endif
#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64)) #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64))
#define __x86_64__ 1 # define __x86_64__ 1
#endif #endif
#if defined(_WIN32) #if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN # ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
#endif # endif
#include <windows.h> # include <windows.h>
#include <intrin.h> # include <intrin.h>
#else #else
#include <sys/time.h> # include <sys/time.h>
#if defined(__i386__) || defined(__x86_64__) # if defined(__i386__) || defined(__x86_64__)
#include <x86intrin.h> # include <x86intrin.h>
#endif # endif
#endif #endif
namespace sw { namespace sw {
...@@ -47,54 +47,55 @@ Timer::~Timer() ...@@ -47,54 +47,55 @@ Timer::~Timer()
double Timer::seconds() double Timer::seconds()
{ {
#if defined(_WIN32) #if defined(_WIN32)
return (double)counter() / (double)frequency(); return (double)counter() / (double)frequency();
#else #else
timeval t; timeval t;
gettimeofday(&t, 0); gettimeofday(&t, 0);
return (double)t.tv_sec + (double)t.tv_usec * 1.0e-6; return (double)t.tv_sec + (double)t.tv_usec * 1.0e-6;
#endif #endif
} }
int64_t Timer::ticks() int64_t Timer::ticks()
{ {
#if defined(_WIN32) #if defined(_WIN32)
#if defined(_M_ARM64) # if defined(_M_ARM64)
return _ReadStatusReg(ARM64_PMCCNTR_EL0); return _ReadStatusReg(ARM64_PMCCNTR_EL0);
#else # else
return __rdtsc(); return __rdtsc();
#endif # endif
#elif defined(__i386__) || defined(__x86_64__) #elif defined(__i386__) || defined(__x86_64__)
int64_t tsc; int64_t tsc;
__asm volatile("rdtsc": "=A" (tsc)); __asm volatile("rdtsc"
return tsc; : "=A"(tsc));
#else return tsc;
return 0; #else
#endif return 0;
#endif
} }
int64_t Timer::counter() int64_t Timer::counter()
{ {
#if defined(_WIN32) #if defined(_WIN32)
int64_t counter; int64_t counter;
QueryPerformanceCounter((LARGE_INTEGER*)&counter); QueryPerformanceCounter((LARGE_INTEGER *)&counter);
return counter; return counter;
#else #else
timeval t; timeval t;
gettimeofday(&t, 0); gettimeofday(&t, 0);
return t.tv_sec * 1000000 + t.tv_usec; return t.tv_sec * 1000000 + t.tv_usec;
#endif #endif
} }
int64_t Timer::frequency() int64_t Timer::frequency()
{ {
#if defined(_WIN32) #if defined(_WIN32)
int64_t frequency; int64_t frequency;
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
return frequency; return frequency;
#else #else
return 1000000; // gettimeofday uses microsecond resolution return 1000000; // gettimeofday uses microsecond resolution
#endif #endif
} }
} // namespace sw } // namespace sw
...@@ -35,4 +35,4 @@ public: ...@@ -35,4 +35,4 @@ public:
} // namespace sw } // namespace sw
#endif // sw_Timer_hpp #endif // sw_Timer_hpp
...@@ -21,25 +21,30 @@ ...@@ -21,25 +21,30 @@
// GCC warns against bitfields not fitting the entire range of an enum with a fixed underlying type of unsigned int, which gets promoted to an error with -Werror and cannot be suppressed. // GCC warns against bitfields not fitting the entire range of an enum with a fixed underlying type of unsigned int, which gets promoted to an error with -Werror and cannot be suppressed.
// However, GCC already defaults to using unsigned int as the underlying type of an unscoped enum without a fixed underlying type. So we can just omit it. // However, GCC already defaults to using unsigned int as the underlying type of an unscoped enum without a fixed underlying type. So we can just omit it.
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
namespace {enum E {}; static_assert(!std::numeric_limits<std::underlying_type<E>::type>::is_signed, "expected unscoped enum whose underlying type is not fixed to be unsigned");} namespace {
#define ENUM_UNDERLYING_TYPE_UNSIGNED_INT enum E
{
};
static_assert(!std::numeric_limits<std::underlying_type<E>::type>::is_signed, "expected unscoped enum whose underlying type is not fixed to be unsigned");
} // namespace
# define ENUM_UNDERLYING_TYPE_UNSIGNED_INT
#else #else
#define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int # define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int
#endif #endif
#if defined(_MSC_VER) #if defined(_MSC_VER)
typedef signed __int8 int8_t; typedef signed __int8 int8_t;
typedef signed __int16 int16_t; typedef signed __int16 int16_t;
typedef signed __int32 int32_t; typedef signed __int32 int32_t;
typedef signed __int64 int64_t; typedef signed __int64 int64_t;
typedef unsigned __int8 uint8_t; typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t; typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t; typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t; typedef unsigned __int64 uint64_t;
#define ALIGN(bytes, type) __declspec(align(bytes)) type # define ALIGN(bytes, type) __declspec(align(bytes)) type
#else #else
#include <stdint.h> # include <stdint.h>
#define ALIGN(bytes, type) type __attribute__((aligned(bytes))) # define ALIGN(bytes, type) type __attribute__((aligned(bytes)))
#endif #endif
namespace sw { namespace sw {
...@@ -51,7 +56,7 @@ typedef ALIGN(8, uint64_t) qword; ...@@ -51,7 +56,7 @@ typedef ALIGN(8, uint64_t) qword;
typedef ALIGN(1, int8_t) sbyte; typedef ALIGN(1, int8_t) sbyte;
template<typename T, int N> template<typename T, int N>
struct alignas(sizeof(T)* N) vec struct alignas(sizeof(T) * N) vec
{ {
vec() = default; vec() = default;
...@@ -63,9 +68,9 @@ struct alignas(sizeof(T)* N) vec ...@@ -63,9 +68,9 @@ struct alignas(sizeof(T)* N) vec
} }
} }
template<typename ... ARGS> template<typename... ARGS>
constexpr vec(T arg0, ARGS ... args) constexpr vec(T arg0, ARGS... args)
: v{ arg0, args... } : v{ arg0, args... }
{ {
} }
...@@ -91,12 +96,18 @@ struct alignas(sizeof(T) * 4) vec<T, 4> ...@@ -91,12 +96,18 @@ struct alignas(sizeof(T) * 4) vec<T, 4>
vec() = default; vec() = default;
constexpr explicit vec(T replicate) constexpr explicit vec(T replicate)
: x(replicate), y(replicate), z(replicate), w(replicate) : x(replicate)
, y(replicate)
, z(replicate)
, w(replicate)
{ {
} }
constexpr vec(T x, T y, T z, T w) constexpr vec(T x, T y, T z, T w)
: x(x), y(y), z(z), w(w) : x(x)
, y(y)
, z(z)
, w(w)
{ {
} }
...@@ -128,7 +139,7 @@ struct alignas(sizeof(T) * 4) vec<T, 4> ...@@ -128,7 +139,7 @@ struct alignas(sizeof(T) * 4) vec<T, 4>
}; };
template<typename T, int N> template<typename T, int N>
bool operator==(const vec<T, N>& a, const vec<T, N>& b) bool operator==(const vec<T, N> &a, const vec<T, N> &b)
{ {
for(int i = 0; i < N; i++) for(int i = 0; i < N; i++)
{ {
...@@ -142,15 +153,19 @@ bool operator==(const vec<T, N>& a, const vec<T, N>& b) ...@@ -142,15 +153,19 @@ bool operator==(const vec<T, N>& a, const vec<T, N>& b)
} }
template<typename T, int N> template<typename T, int N>
bool operator!=(const vec<T, N>& a, const vec<T, N>& b) bool operator!=(const vec<T, N> &a, const vec<T, N> &b)
{ {
return !(a == b); return !(a == b);
} }
template<typename T> using vec2 = vec<T, 2>; template<typename T>
template<typename T> using vec4 = vec<T, 4>; using vec2 = vec<T, 2>;
template<typename T> using vec8 = vec<T, 8>; template<typename T>
template<typename T> using vec16 = vec<T, 16>; using vec4 = vec<T, 4>;
template<typename T>
using vec8 = vec<T, 8>;
template<typename T>
using vec16 = vec<T, 16>;
using int2 = vec2<int>; using int2 = vec2<int>;
using uint2 = vec2<unsigned int>; using uint2 = vec2<unsigned int>;
...@@ -186,8 +201,8 @@ inline constexpr float4 replicate(float f) ...@@ -186,8 +201,8 @@ inline constexpr float4 replicate(float f)
return vector(f, f, f, f); return vector(f, f, f, f);
} }
#define OFFSET(s,m) (int)(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m)) #define OFFSET(s, m) (int)(size_t) & reinterpret_cast<const volatile char &>((((s *)0)->m))
} // namespace sw } // namespace sw
#endif // sw_Types_hpp #endif // sw_Types_hpp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment