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));
# endif
#else
registers[0] = 0; registers[0] = 0;
registers[1] = 0; registers[1] = 0;
registers[2] = 0; registers[2] = 0;
registers[3] = 0; registers[3] = 0;
#endif #endif
} }
bool CPUID::detectMMX() bool CPUID::detectMMX()
...@@ -231,7 +233,7 @@ int CPUID::detectCoreCount() ...@@ -231,7 +233,7 @@ 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;
...@@ -246,9 +248,9 @@ int CPUID::detectCoreCount() ...@@ -246,9 +248,9 @@ int CPUID::detectCoreCount()
systemAffinityMask >>= 1; systemAffinityMask >>= 1;
} }
#else #else
cores = sysconf(_SC_NPROCESSORS_ONLN); cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif #endif
if(cores < 1) cores = 1; if(cores < 1) cores = 1;
if(cores > 16) cores = 16; if(cores > 16) cores = 16;
...@@ -260,7 +262,7 @@ int CPUID::detectAffinity() ...@@ -260,7 +262,7 @@ 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;
...@@ -275,9 +277,9 @@ int CPUID::detectAffinity() ...@@ -275,9 +277,9 @@ int CPUID::detectAffinity()
processAffinityMask >>= 1; processAffinityMask >>= 1;
} }
#else #else
return detectCoreCount(); // FIXME: Assumes no affinity limitation return detectCoreCount(); // FIXME: Assumes no affinity limitation
#endif #endif
if(cores < 1) cores = 1; if(cores < 1) cores = 1;
if(cores > 16) cores = 16; if(cores > 16) cores = 16;
...@@ -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
......
...@@ -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;
...@@ -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++)
{ {
......
...@@ -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) # if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define UNIMPLEMENTED(...) do { \ # define UNIMPLEMENTED(...) \
do \
{ \
sw::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \ sw::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \
sw::trace(__VA_ARGS__); \ sw::trace(__VA_ARGS__); \
sw::trace("\n"); \ sw::trace("\n"); \
ASSERT(false); \ ASSERT(false); \
} while(0) } while(0)
#else # else
#define UNIMPLEMENTED(...) ((void)0) # define UNIMPLEMENTED(...) ((void)0)
#endif # 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 ASSERT(expression) \
#else { \
#define ASSERT assert if(!(expression)) sw::trace("\t! Assert failed in %s(%d): " #expression "\n", __FUNCTION__, __LINE__); \
#endif 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,8 +48,11 @@ ...@@ -48,8 +48,11 @@
*/ */
void AndroidEnterDebugger(); void AndroidEnterDebugger();
#define ASSERT(E) do { \ #define ASSERT(E) \
if(!(E)) { \ do \
{ \
if(!(E)) \
{ \
ALOGE("badness: assertion_failed %s in %s at %s:%d", #E, \ ALOGE("badness: assertion_failed %s in %s at %s:%d", #E, \
__FUNCTION__, __FILE__, __LINE__); \ __FUNCTION__, __FILE__, __LINE__); \
AndroidEnterDebugger(); \ AndroidEnterDebugger(); \
...@@ -60,38 +63,44 @@ void AndroidEnterDebugger(); ...@@ -60,38 +63,44 @@ void AndroidEnterDebugger();
#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(...) \
do \
{ \
ALOGE("badness: unimplemented: %s %s:%d", \ ALOGE("badness: unimplemented: %s %s:%d", \
__FUNCTION__, __FILE__, __LINE__); \ __FUNCTION__, __FILE__, __LINE__); \
AndroidEnterDebugger(); \ 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, ...);
......
...@@ -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()
...@@ -34,12 +34,12 @@ GrallocModule::GrallocModule() ...@@ -34,12 +34,12 @@ GrallocModule::GrallocModule()
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
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#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()
......
...@@ -18,7 +18,7 @@ namespace sw { ...@@ -18,7 +18,7 @@ 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;
...@@ -62,7 +62,7 @@ half::operator float() const ...@@ -62,7 +62,7 @@ half::operator float() const
{ {
fp32i = s << 31; fp32i = s << 31;
return (float&)fp32i; return (float &)fp32i;
} }
else else
{ {
...@@ -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])
{ {
} }
...@@ -111,7 +112,7 @@ public: ...@@ -111,7 +112,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
...@@ -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;
...@@ -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;
...@@ -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
......
...@@ -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);
} }
...@@ -47,17 +50,17 @@ public: ...@@ -47,17 +50,17 @@ public:
// 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)
...@@ -101,51 +101,51 @@ inline int ceilInt4(int x) ...@@ -101,51 +101,51 @@ inline int ceilInt4(int x)
} }
#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)
...@@ -374,7 +374,8 @@ inline int clampToSignedInt(unsigned int x) ...@@ -374,7 +374,8 @@ 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));
} }
......
...@@ -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,7 +46,7 @@ namespace { ...@@ -46,7 +46,7 @@ namespace {
struct Allocation struct Allocation
{ {
// size_t bytes; // size_t bytes;
unsigned char *block; unsigned char *block;
}; };
...@@ -54,8 +54,8 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -54,8 +54,8 @@ 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);
} }
...@@ -70,21 +70,21 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -70,21 +70,21 @@ void *allocateRaw(size_t bytes, size_t alignment)
} }
return allocation; return allocation;
} }
#else #else
unsigned char *block = (unsigned char*)malloc(bytes + sizeof(Allocation) + alignment); unsigned char *block = (unsigned char *)malloc(bytes + sizeof(Allocation) + alignment);
unsigned char *aligned = nullptr; 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)
: "a"(element)
: "memory");
#else
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)
{ {
memory[i] = element; memory[i] = element;
} }
#endif #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)
: "a"(element)
: "memory");
#else
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)
{ {
memory[i] = element; memory[i] = element;
} }
#endif #endif
} }
} // namespace sw } // namespace sw
...@@ -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,30 +67,30 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], ...@@ -67,30 +67,30 @@ 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;
...@@ -106,19 +106,19 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], ...@@ -106,19 +106,19 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
{ {
return ""; return "";
} }
} }
#else #else
inline void *loadLibrary(const char *path) inline void *loadLibrary(const char *path)
{ {
return dlopen(path, RTLD_LAZY | RTLD_LOCAL); return dlopen(path, RTLD_LAZY | RTLD_LOCAL);
} }
inline void *getLibraryHandle(const char *path) inline void *getLibraryHandle(const char *path)
{ {
#ifdef __ANDROID__ # ifdef __ANDROID__
// bionic doesn't support RTLD_NOLOAD before L // bionic doesn't support RTLD_NOLOAD before L
return dlopen(path, RTLD_NOW | RTLD_LOCAL); return dlopen(path, RTLD_NOW | RTLD_LOCAL);
#else # else
void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL); void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
if(resident) if(resident)
...@@ -127,19 +127,19 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], ...@@ -127,19 +127,19 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
} }
return nullptr; return nullptr;
#endif # endif
} }
inline void freeLibrary(void *library) inline void freeLibrary(void *library)
{ {
if(library) if(library)
{ {
dlclose(library); dlclose(library);
} }
} }
inline void *getProcAddress(void *library, const char *name) inline void *getProcAddress(void *library, const char *name)
{ {
void *symbol = dlsym(library, name); void *symbol = dlsym(library, name);
if(!symbol) if(!symbol)
...@@ -149,10 +149,10 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], ...@@ -149,10 +149,10 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
} }
return symbol; return symbol;
} }
inline std::string getModuleDirectory() inline std::string getModuleDirectory()
{ {
static int dummy_symbol = 0; static int dummy_symbol = 0;
Dl_info dl_info; Dl_info dl_info;
...@@ -165,7 +165,7 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], ...@@ -165,7 +165,7 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
{ {
return ""; 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; });
...@@ -118,7 +122,7 @@ private: ...@@ -118,7 +122,7 @@ private:
// 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);
......
...@@ -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"
: "=A"(tsc));
return tsc; return tsc;
#else #else
return 0; return 0;
#endif #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
...@@ -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,8 +68,8 @@ struct alignas(sizeof(T)* N) vec ...@@ -63,8 +68,8 @@ 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,7 +201,7 @@ inline constexpr float4 replicate(float f) ...@@ -186,7 +201,7 @@ 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
......
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