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 @@
// Define MEMORY_SANITIZER_ENABLED to 1 if the project was build with the memory
// sanitizer enabled (-fsanitize=memory).
#if defined(__SANITIZE_MEMORY__)
#define MEMORY_SANITIZER_ENABLED 1
# define MEMORY_SANITIZER_ENABLED 1
#else // defined(__SANITIZE_MEMORY__)
#if defined(__clang__)
#if __has_feature(memory_sanitizer)
#define MEMORY_SANITIZER_ENABLED 1
#endif // __has_feature(memory_sanitizer)
#endif // defined(__clang__)
# if defined(__clang__)
# if __has_feature(memory_sanitizer)
# define MEMORY_SANITIZER_ENABLED 1
# endif // __has_feature(memory_sanitizer)
# endif // defined(__clang__)
#endif // defined(__SANITIZE_MEMORY__)
// 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
// preprocessor.
#if MEMORY_SANITIZER_ENABLED
#define MEMORY_SANITIZER_ONLY(x) x
# define MEMORY_SANITIZER_ONLY(x) x
#else
#define MEMORY_SANITIZER_ONLY(x)
# define MEMORY_SANITIZER_ONLY(x)
#endif // MEMORY_SANITIZER_ENABLED
#endif // Build_hpp
......@@ -15,16 +15,16 @@
#include "CPUID.hpp"
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <intrin.h>
#include <float.h>
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <intrin.h>
# include <float.h>
#else
#include <unistd.h>
#include <sched.h>
#include <sys/types.h>
# include <unistd.h>
# include <sched.h>
# include <sys/types.h>
#endif
namespace sw {
......@@ -164,18 +164,20 @@ void CPUID::setEnableSSE4_1(bool enable)
static void cpuid(int registers[4], int info)
{
#if defined(__i386__) || defined(__x86_64__)
#if defined(_WIN32)
#if defined(__i386__) || defined(__x86_64__)
# if defined(_WIN32)
__cpuid(registers, info);
#else
__asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
#endif
#else
# else
__asm volatile("cpuid"
: "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3])
: "a"(info));
# endif
#else
registers[0] = 0;
registers[1] = 0;
registers[2] = 0;
registers[3] = 0;
#endif
#endif
}
bool CPUID::detectMMX()
......@@ -231,7 +233,7 @@ int CPUID::detectCoreCount()
{
int cores = 0;
#if defined(_WIN32)
#if defined(_WIN32)
DWORD_PTR processAffinityMask = 1;
DWORD_PTR systemAffinityMask = 1;
......@@ -246,9 +248,9 @@ int CPUID::detectCoreCount()
systemAffinityMask >>= 1;
}
#else
#else
cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif
#endif
if(cores < 1) cores = 1;
if(cores > 16) cores = 16;
......@@ -260,7 +262,7 @@ int CPUID::detectAffinity()
{
int cores = 0;
#if defined(_WIN32)
#if defined(_WIN32)
DWORD_PTR processAffinityMask = 1;
DWORD_PTR systemAffinityMask = 1;
......@@ -275,9 +277,9 @@ int CPUID::detectAffinity()
processAffinityMask >>= 1;
}
#else
#else
return detectCoreCount(); // FIXME: Assumes no affinity limitation
#endif
#endif
if(cores < 1) cores = 1;
if(cores > 16) cores = 16;
......@@ -287,11 +289,11 @@ int CPUID::detectAffinity()
void CPUID::setFlushToZero(bool enable)
{
#if defined(_MSC_VER)
#if defined(_MSC_VER)
_controlfp(enable ? _DN_FLUSH : _DN_SAVE, _MCW_DN);
#else
#else
// Unimplemented
#endif
#endif
}
void CPUID::setDenormalsAreZero(bool enable)
......
......@@ -18,11 +18,11 @@
namespace sw {
#if !defined(__i386__) && defined(_M_IX86)
#define __i386__ 1
# define __i386__ 1
#endif
#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
#define __x86_64__ 1
#if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64))
# define __x86_64__ 1
#endif
class CPUID
......
......@@ -14,17 +14,17 @@
#include "Configurator.hpp"
#include <iostream>
#include <fstream>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#if defined(__unix__)
#include <unistd.h>
# include <unistd.h>
#endif
namespace sw {
......@@ -42,12 +42,12 @@ Configurator::~Configurator()
bool Configurator::readFile()
{
#if defined(__unix__)
#if defined(__unix__)
if(access(path.c_str(), R_OK) != 0)
{
return false;
}
#endif
#endif
fstream file(path.c_str(), ios::in);
if(file.fail()) return false;
......@@ -116,17 +116,18 @@ bool Configurator::readFile()
void Configurator::writeFile(std::string title)
{
#if defined(__unix__)
#if defined(__unix__)
if(access(path.c_str(), W_OK) != 0)
{
return;
}
#endif
#endif
fstream file(path.c_str(), ios::out);
if(file.fail()) return;
file << "; " << title << endl << endl;
file << "; " << title << endl
<< endl;
for(unsigned int keyID = 0; keyID < sections.size(); keyID++)
{
......
......@@ -14,8 +14,8 @@
#include "Debug.hpp"
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
namespace sw {
......
......@@ -16,44 +16,50 @@
#define Debug_hpp
#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD) && !defined(ANDROID_NDK_BUILD)
#include "DebugAndroid.hpp"
# include "DebugAndroid.hpp"
#else
#include <assert.h>
#include <stdio.h>
# include <assert.h>
# include <stdio.h>
#undef min
#undef max
# undef min
# undef max
namespace sw {
void trace(const char *format, ...);
inline void trace() {}
}
} // namespace sw
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define TRACE(format, ...) sw::trace("[0x%0.8X]%s(" format ")\n", this, __FUNCTION__, ##__VA_ARGS__)
#else
#define TRACE(...) ((void)0)
#endif
# if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
# define TRACE(format, ...) sw::trace("[0x%0.8X]%s(" format ")\n", this, __FUNCTION__, ##__VA_ARGS__)
# else
# define TRACE(...) ((void)0)
# endif
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define UNIMPLEMENTED(...) do { \
# 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
# 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
# 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 // Debug_hpp
......@@ -14,16 +14,16 @@
#include "DebugAndroid.hpp"
#include <cutils/properties.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <cutils/properties.h>
#include <unistd.h>
void AndroidEnterDebugger()
{
ALOGE(__FUNCTION__);
#ifndef NDEBUG
static volatile int * const makefault = nullptr;
static volatile int *const makefault = nullptr;
char value[PROPERTY_VALUE_MAX];
property_get("debug.db.uid", value, "-1");
int debug_uid = atoi(value);
......@@ -31,7 +31,8 @@ void AndroidEnterDebugger()
{
ALOGE("Waiting for debugger: gdbserver :${PORT} --attach %u. Look for thread %u", getpid(), gettid());
volatile int waiting = 1;
while(waiting) {
while(waiting)
{
sleep(1);
}
}
......
......@@ -16,11 +16,11 @@
#define DebugAndroid_hpp
#if ANDROID_PLATFORM_SDK_VERSION < 27
#include <cutils/log.h>
# include <cutils/log.h>
#elif ANDROID_PLATFORM_SDK_VERSION >= 27
#include <log/log.h>
# include <log/log.h>
#else
#error "ANDROID_PLATFORM_SDK_VERSION is not defined"
# error "ANDROID_PLATFORM_SDK_VERSION is not defined"
#endif
#include <cassert>
......@@ -48,8 +48,11 @@
*/
void AndroidEnterDebugger();
#define ASSERT(E) do { \
if(!(E)) { \
#define ASSERT(E) \
do \
{ \
if(!(E)) \
{ \
ALOGE("badness: assertion_failed %s in %s at %s:%d", #E, \
__FUNCTION__, __FILE__, __LINE__); \
AndroidEnterDebugger(); \
......@@ -60,38 +63,44 @@ void AndroidEnterDebugger();
#define assert(E) ASSERT(E)
#define ERR(format, ...) \
do { \
do \
{ \
ALOGE("badness: err %s %s:%d (" format ")", __FUNCTION__, __FILE__, \
__LINE__, ##__VA_ARGS__); \
AndroidEnterDebugger(); \
} while(0)
#define FIXME(format, ...) \
do { \
do \
{ \
ALOGE("badness: fixme %s %s:%d (" format ")", __FUNCTION__, __FILE__, \
__LINE__, ##__VA_ARGS__); \
AndroidEnterDebugger(); \
} while(0)
// TODO: Handle __VA_ARGS__ (can be empty)
#define UNIMPLEMENTED(...) do { \
#define UNIMPLEMENTED(...) \
do \
{ \
ALOGE("badness: unimplemented: %s %s:%d", \
__FUNCTION__, __FILE__, __LINE__); \
AndroidEnterDebugger(); \
} while(0)
#define UNREACHABLE(value) do { \
#define UNREACHABLE(value) \
do \
{ \
ALOGE("badness: unreachable case reached: %s %s:%d. %s: %d", \
__FUNCTION__, __FILE__, __LINE__, #value, value); \
AndroidEnterDebugger(); \
} while(0)
#ifndef NDEBUG
#define TRACE(format, ...) \
# define TRACE(format, ...) \
ALOGV("%s %s:%d (" format ")", __FUNCTION__, __FILE__, \
__LINE__, ##__VA_ARGS__)
#else
#define TRACE(...) ((void)0)
# define TRACE(...) ((void)0)
#endif
void trace(const char *format, ...);
......
......@@ -16,7 +16,7 @@
#include "Debug.hpp"
#ifdef HAVE_GRALLOC1
#include <sync/sync.h>
# include <sync/sync.h>
#endif
GrallocModule *GrallocModule::getInstance()
......@@ -34,12 +34,12 @@ GrallocModule::GrallocModule()
switch(m_major_version)
{
case 0:
m_module = reinterpret_cast<const gralloc_module_t*>(module);
m_module = reinterpret_cast<const gralloc_module_t *>(module);
break;
case 1:
#ifdef HAVE_GRALLOC1
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);
break;
#endif
......
......@@ -18,7 +18,7 @@
#include <hardware/gralloc.h>
#ifdef HAVE_GRALLOC1
#include <hardware/gralloc1.h>
# include <hardware/gralloc1.h>
#endif
#include <unistd.h> // for close()
......
......@@ -18,7 +18,7 @@ namespace sw {
half::half(float fp32)
{
unsigned int fp32i = *(unsigned int*)&fp32;
unsigned int fp32i = *(unsigned int *)&fp32;
unsigned int sign = (fp32i & 0x80000000) >> 16;
unsigned int abs = fp32i & 0x7FFFFFFF;
......@@ -62,7 +62,7 @@ half::operator float() const
{
fp32i = s << 31;
return (float&)fp32i;
return (float &)fp32i;
}
else
{
......@@ -82,7 +82,7 @@ half::operator float() const
fp32i = (s << 31) | (e << 23) | m;
return (float&)fp32i;
return (float &)fp32i;
}
half &half::operator=(half h)
......@@ -92,7 +92,6 @@ half &half::operator=(half h)
return *this;
}
half &half::operator=(float f)
{
*this = half(f);
......
......@@ -58,7 +58,8 @@ class RGB9E5
unsigned int E : 5;
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:
operator unsigned int() const
{
return *reinterpret_cast<const unsigned int*>(this);
return *reinterpret_cast<const unsigned int *>(this);
}
void toRGB16F(half rgb[3]) const
......@@ -159,7 +160,7 @@ class R11G11B10F
const unsigned int float32Maxfloat11 = 0x477E0000;
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;
unsigned int float32Val = float32Bits & float32ValueMask;
......@@ -232,7 +233,7 @@ class R11G11B10F
const unsigned int float32Maxfloat10 = 0x477C0000;
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;
unsigned int float32Val = float32Bits & float32ValueMask;
......@@ -297,7 +298,7 @@ public:
operator unsigned int() const
{
return *reinterpret_cast<const unsigned int*>(this);
return *reinterpret_cast<const unsigned int *>(this);
}
void toRGB16F(half rgb[3]) const
......
......@@ -22,19 +22,19 @@
#include <unistd.h>
#ifndef MFD_CLOEXEC
#define MFD_CLOEXEC 0x0001U
# define MFD_CLOEXEC 0x0001U
#endif
#if __aarch64__
#define __NR_memfd_create 279
# define __NR_memfd_create 279
#elif __arm__
#define __NR_memfd_create 279
# define __NR_memfd_create 279
#elif __powerpc64__
#define __NR_memfd_create 360
# define __NR_memfd_create 360
#elif __i386__
#define __NR_memfd_create 356
# define __NR_memfd_create 356
#elif __x86_64__
#define __NR_memfd_create 319
# define __NR_memfd_create 319
#endif /* __NR_memfd_create__ */
LinuxMemFd::~LinuxMemFd()
......@@ -59,7 +59,7 @@ int LinuxMemFd::exportFd() const
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();
......@@ -93,7 +93,8 @@ void LinuxMemFd::close()
// WARNING: Never retry on close() failure, even with EINTR, see
// https://lwn.net/Articles/576478/ for example.
int ret = ::close(fd_);
if(ret < 0) {
if(ret < 0)
{
TRACE("LinuxMemFd::close() failed with: %s", strerror(errno));
assert(false);
}
......@@ -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));
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;
}
......@@ -22,11 +22,14 @@
// supported by the Linux kernel since 3.17 (good enough for Android and desktop
// Linux).
class LinuxMemFd {
class LinuxMemFd
{
public:
LinuxMemFd() = default;
LinuxMemFd(const char* name, size_t size) : LinuxMemFd() {
LinuxMemFd(const char *name, size_t size)
: LinuxMemFd()
{
allocate(name, size);
}
......@@ -47,17 +50,17 @@ public:
// Implement memfd_create() through direct syscalls if possible.
// On success, return true and sets |fd| accordingly. On failure, return
// 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.
// Both |offset| and |size| should be page-aligned. Returns nullptr/errno
// 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.
// Both |addr| and |size| should be page-aligned. Returns true on success
// or false/errno on failure.
bool unmap(void* addr, size_t size);
bool unmap(void *addr, size_t size);
void close();
......
......@@ -21,7 +21,7 @@
#include <cmath>
#if defined(_MSC_VER)
#include <intrin.h>
# include <intrin.h>
#endif
namespace sw {
......@@ -67,7 +67,7 @@ inline constexpr T min(T a, T b, T c, T 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)
{
union
......@@ -82,7 +82,7 @@ destType bit_cast(const sourceType &source)
inline int iround(float x)
{
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)
......@@ -101,51 +101,51 @@ inline int ceilInt4(int x)
}
#define BITS(x) ( \
!!((x) & 0x80000000) + \
!!((x) & 0xC0000000) + \
!!((x) & 0xE0000000) + \
!!((x) & 0xF0000000) + \
!!((x) & 0xF8000000) + \
!!((x) & 0xFC000000) + \
!!((x) & 0xFE000000) + \
!!((x) & 0xFF000000) + \
!!((x) & 0xFF800000) + \
!!((x) & 0xFFC00000) + \
!!((x) & 0xFFE00000) + \
!!((x) & 0xFFF00000) + \
!!((x) & 0xFFF80000) + \
!!((x) & 0xFFFC0000) + \
!!((x) & 0xFFFE0000) + \
!!((x) & 0xFFFF0000) + \
!!((x) & 0xFFFF8000) + \
!!((x) & 0xFFFFC000) + \
!!((x) & 0xFFFFE000) + \
!!((x) & 0xFFFFF000) + \
!!((x) & 0xFFFFF800) + \
!!((x) & 0xFFFFFC00) + \
!!((x) & 0xFFFFFE00) + \
!!((x) & 0xFFFFFF00) + \
!!((x) & 0xFFFFFF80) + \
!!((x) & 0xFFFFFFC0) + \
!!((x) & 0xFFFFFFE0) + \
!!((x) & 0xFFFFFFF0) + \
!!((x) & 0xFFFFFFF8) + \
!!((x) & 0xFFFFFFFC) + \
!!((x) & 0xFFFFFFFE) + \
!!((x) & 0xFFFFFFFF))
!!((x)&0x80000000) + \
!!((x)&0xC0000000) + \
!!((x)&0xE0000000) + \
!!((x)&0xF0000000) + \
!!((x)&0xF8000000) + \
!!((x)&0xFC000000) + \
!!((x)&0xFE000000) + \
!!((x)&0xFF000000) + \
!!((x)&0xFF800000) + \
!!((x)&0xFFC00000) + \
!!((x)&0xFFE00000) + \
!!((x)&0xFFF00000) + \
!!((x)&0xFFF80000) + \
!!((x)&0xFFFC0000) + \
!!((x)&0xFFFE0000) + \
!!((x)&0xFFFF0000) + \
!!((x)&0xFFFF8000) + \
!!((x)&0xFFFFC000) + \
!!((x)&0xFFFFE000) + \
!!((x)&0xFFFFF000) + \
!!((x)&0xFFFFF800) + \
!!((x)&0xFFFFFC00) + \
!!((x)&0xFFFFFE00) + \
!!((x)&0xFFFFFF00) + \
!!((x)&0xFFFFFF80) + \
!!((x)&0xFFFFFFC0) + \
!!((x)&0xFFFFFFE0) + \
!!((x)&0xFFFFFFF0) + \
!!((x)&0xFFFFFFF8) + \
!!((x)&0xFFFFFFFC) + \
!!((x)&0xFFFFFFFE) + \
!!((x)&0xFFFFFFFF))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
inline unsigned long log2i(int x)
{
#if defined(_MSC_VER)
#if defined(_MSC_VER)
unsigned long y;
_BitScanReverse(&y, x);
return y;
#else
#else
return 31 - __builtin_clz(x);
#endif
#endif
}
inline bool isPow2(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
constexpr int toFixedPoint(float v, int p) {
constexpr int toFixedPoint(float v, int p)
{
return static_cast<int>(v * (1 << p));
}
......
......@@ -14,30 +14,30 @@
#include "Memory.hpp"
#include "Types.hpp"
#include "Debug.hpp"
#include "Types.hpp"
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <intrin.h>
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <intrin.h>
#else
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
# include <errno.h>
# include <sys/mman.h>
# include <stdlib.h>
# include <unistd.h>
#endif
#include <cstring>
#include <cstdlib>
#include <cstring>
#undef allocate
#undef deallocate
#if(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined (_M_X64)) && !defined(__x86__)
#define __x86__
#if(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)) && !defined(__x86__)
# define __x86__
#endif
namespace sw {
......@@ -46,7 +46,7 @@ namespace {
struct Allocation
{
// size_t bytes;
// size_t bytes;
unsigned char *block;
};
......@@ -54,8 +54,8 @@ void *allocateRaw(size_t bytes, size_t alignment)
{
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP)
if(alignment < sizeof(void*))
#if defined(LINUX_ENABLE_NAMED_MMAP)
if(alignment < sizeof(void *))
{
return malloc(bytes);
}
......@@ -70,21 +70,21 @@ void *allocateRaw(size_t bytes, size_t alignment)
}
return allocation;
}
#else
unsigned char *block = (unsigned char*)malloc(bytes + sizeof(Allocation) + alignment);
#else
unsigned char *block = (unsigned char *)malloc(bytes + sizeof(Allocation) + alignment);
unsigned char *aligned = nullptr;
if(block)
{
aligned = (unsigned char*)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
aligned = (unsigned char *)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
// allocation->bytes = bytes;
allocation->block = block;
}
return aligned;
#endif
#endif
}
} // anonymous namespace
......@@ -95,13 +95,13 @@ size_t memoryPageSize()
if(pageSize == 0)
{
#if defined(_WIN32)
#if defined(_WIN32)
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
pageSize = systemInfo.dwPageSize;
#else
#else
pageSize = sysconf(_SC_PAGESIZE);
#endif
#endif
}
return pageSize;
......@@ -121,45 +121,51 @@ void *allocate(size_t bytes, size_t alignment)
void deallocate(void *memory)
{
#if defined(LINUX_ENABLE_NAMED_MMAP)
#if defined(LINUX_ENABLE_NAMED_MMAP)
free(memory);
#else
#else
if(memory)
{
unsigned char *aligned = (unsigned char*)memory;
Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
unsigned char *aligned = (unsigned char *)memory;
Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
free(allocation->block);
}
#endif
#endif
}
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);
#elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__asm__ __volatile__("rep stosw" : "+D"(memory), "+c"(count) : "a"(element) : "memory");
#else
#elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__asm__ __volatile__("rep stosw"
: "+D"(memory), "+c"(count)
: "a"(element)
: "memory");
#else
for(size_t i = 0; i < count; i++)
{
memory[i] = element;
}
#endif
#endif
}
void clear(uint32_t *memory, uint32_t element, size_t count)
{
#if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__stosd((unsigned long*)memory, element, count);
#elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__asm__ __volatile__("rep stosl" : "+D"(memory), "+c"(count) : "a"(element) : "memory");
#else
#if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__stosd((unsigned long *)memory, element, count);
#elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
__asm__ __volatile__("rep stosl"
: "+D"(memory), "+c"(count)
: "a"(element)
: "memory");
#else
for(size_t i = 0; i < count; i++)
{
memory[i] = element;
}
#endif
#endif
}
} // namespace sw
......@@ -16,9 +16,9 @@
#define SharedLibrary_hpp
#if defined(_WIN32)
#include <Windows.h>
# include <Windows.h>
#else
#include <dlfcn.h>
# include <dlfcn.h>
#endif
#include <string>
......@@ -67,30 +67,30 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
}
#if defined(_WIN32)
inline void *loadLibrary(const char *path)
{
return (void*)LoadLibrary(path);
}
inline void *loadLibrary(const char *path)
{
return (void *)LoadLibrary(path);
}
inline void *getLibraryHandle(const char *path)
{
inline void *getLibraryHandle(const char *path)
{
HMODULE module = NULL;
GetModuleHandleEx(0, path, &module);
return (void*)module;
}
return (void *)module;
}
inline void freeLibrary(void *library)
{
inline void freeLibrary(void *library)
{
FreeLibrary((HMODULE)library);
}
}
inline void *getProcAddress(void *library, const char *name)
{
return (void*)GetProcAddress((HMODULE)library, name);
}
inline void *getProcAddress(void *library, const char *name)
{
return (void *)GetProcAddress((HMODULE)library, name);
}
inline std::string getModuleDirectory()
{
inline std::string getModuleDirectory()
{
static int dummy_symbol = 0;
HMODULE module = NULL;
......@@ -106,19 +106,19 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
{
return "";
}
}
}
#else
inline void *loadLibrary(const char *path)
{
inline void *loadLibrary(const char *path)
{
return dlopen(path, RTLD_LAZY | RTLD_LOCAL);
}
}
inline void *getLibraryHandle(const char *path)
{
#ifdef __ANDROID__
inline void *getLibraryHandle(const char *path)
{
# ifdef __ANDROID__
// bionic doesn't support RTLD_NOLOAD before L
return dlopen(path, RTLD_NOW | RTLD_LOCAL);
#else
# else
void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
if(resident)
......@@ -127,19 +127,19 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
}
return nullptr;
#endif
}
# endif
}
inline void freeLibrary(void *library)
{
inline void freeLibrary(void *library)
{
if(library)
{
dlclose(library);
}
}
}
inline void *getProcAddress(void *library, const char *name)
{
inline void *getProcAddress(void *library, const char *name)
{
void *symbol = dlsym(library, name);
if(!symbol)
......@@ -149,10 +149,10 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
}
return symbol;
}
}
inline std::string getModuleDirectory()
{
inline std::string getModuleDirectory()
{
static int dummy_symbol = 0;
Dl_info dl_info;
......@@ -165,7 +165,7 @@ void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n],
{
return "";
}
}
}
#endif
#endif // SharedLibrary_hpp
......@@ -15,27 +15,28 @@
#include "Socket.hpp"
#if defined(_WIN32)
#include <ws2tcpip.h>
# include <ws2tcpip.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
# include <unistd.h>
# include <netdb.h>
# include <netinet/in.h>
# include <sys/select.h>
#endif
namespace sw {
Socket::Socket(SOCKET socket) : socket(socket)
Socket::Socket(SOCKET socket)
: socket(socket)
{
}
Socket::Socket(const char *address, const char *port)
{
#if defined(_WIN32)
#if defined(_WIN32)
socket = INVALID_SOCKET;
#else
#else
socket = -1;
#endif
#endif
addrinfo hints = {};
hints.ai_family = AF_INET;
......@@ -55,11 +56,11 @@ Socket::Socket(const char *address, const char *port)
Socket::~Socket()
{
#if defined(_WIN32)
#if defined(_WIN32)
closesocket(socket);
#else
#else
close(socket);
#endif
#endif
}
void Socket::listen(int backlog)
......@@ -73,7 +74,7 @@ bool Socket::select(int us)
FD_ZERO(&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;
}
......@@ -95,17 +96,17 @@ void Socket::send(const char *buffer, int length)
void Socket::startup()
{
#if defined(_WIN32)
#if defined(_WIN32)
WSADATA winsockData;
WSAStartup(MAKEWORD(2, 2), &winsockData);
#endif
#endif
}
void Socket::cleanup()
{
#if defined(_WIN32)
#if defined(_WIN32)
WSACleanup();
#endif
#endif
}
} // namespace sw
......@@ -16,10 +16,10 @@
#define sw_Socket_hpp
#if defined(_WIN32)
#include <winsock2.h>
# include <winsock2.h>
#else
#include <sys/socket.h>
typedef int SOCKET;
# include <sys/socket.h>
typedef int SOCKET;
#endif
namespace sw {
......@@ -45,6 +45,6 @@ private:
SOCKET socket;
};
}
} // namespace sw
#endif // sw_Socket_hpp
......@@ -42,7 +42,11 @@ public:
// a corresponding call to start().
virtual void finish() = 0;
// complete() is a helper for calling start() followed by finish().
inline void complete() { start(); finish(); }
inline void complete()
{
start();
finish();
}
protected:
virtual ~TaskEvents() = default;
......@@ -89,8 +93,8 @@ public:
// wait() blocks until all the tasks have been finished or the timeout
// has been reached, returning true if all tasks have been completed, or
// false if the timeout has been reached.
template <class CLOCK, class DURATION>
bool wait(const std::chrono::time_point<CLOCK, DURATION>& timeout)
template<class CLOCK, class DURATION>
bool wait(const std::chrono::time_point<CLOCK, DURATION> &timeout)
{
std::unique_lock<std::mutex> lock(mutex);
return condition.wait_until(lock, timeout, [this] { return count_ == 0; });
......@@ -118,7 +122,7 @@ private:
// Chan is a thread-safe FIFO queue of type T.
// Chan takes its name after Golang's chan.
template <typename T>
template<typename T>
class Chan
{
public:
......@@ -148,10 +152,11 @@ private:
std::condition_variable added;
};
template <typename T>
Chan<T>::Chan() {}
template<typename T>
Chan<T>::Chan()
{}
template <typename T>
template<typename T>
T Chan<T>::take()
{
std::unique_lock<std::mutex> lock(mutex);
......@@ -162,7 +167,7 @@ T Chan<T>::take()
return out;
}
template <typename T>
template<typename T>
std::pair<T, bool> Chan<T>::tryTake()
{
std::unique_lock<std::mutex> lock(mutex);
......@@ -175,7 +180,7 @@ std::pair<T, bool> Chan<T>::tryTake()
return std::make_pair(out, true);
}
template <typename T>
template<typename T>
void Chan<T>::put(const T &item)
{
std::unique_lock<std::mutex> lock(mutex);
......@@ -183,7 +188,7 @@ void Chan<T>::put(const T &item)
added.notify_one();
}
template <typename T>
template<typename T>
size_t Chan<T>::count()
{
std::unique_lock<std::mutex> lock(mutex);
......
......@@ -15,24 +15,24 @@
#include "Timer.hpp"
#if !defined(__i386__) && defined(_M_IX86)
#define __i386__ 1
# define __i386__ 1
#endif
#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
#define __x86_64__ 1
#if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64))
# define __x86_64__ 1
#endif
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <intrin.h>
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <intrin.h>
#else
#include <sys/time.h>
#if defined(__i386__) || defined(__x86_64__)
#include <x86intrin.h>
#endif
# include <sys/time.h>
# if defined(__i386__) || defined(__x86_64__)
# include <x86intrin.h>
# endif
#endif
namespace sw {
......@@ -47,54 +47,55 @@ Timer::~Timer()
double Timer::seconds()
{
#if defined(_WIN32)
#if defined(_WIN32)
return (double)counter() / (double)frequency();
#else
#else
timeval t;
gettimeofday(&t, 0);
return (double)t.tv_sec + (double)t.tv_usec * 1.0e-6;
#endif
#endif
}
int64_t Timer::ticks()
{
#if defined(_WIN32)
#if defined(_M_ARM64)
#if defined(_WIN32)
# if defined(_M_ARM64)
return _ReadStatusReg(ARM64_PMCCNTR_EL0);
#else
# else
return __rdtsc();
#endif
#elif defined(__i386__) || defined(__x86_64__)
# endif
#elif defined(__i386__) || defined(__x86_64__)
int64_t tsc;
__asm volatile("rdtsc": "=A" (tsc));
__asm volatile("rdtsc"
: "=A"(tsc));
return tsc;
#else
#else
return 0;
#endif
#endif
}
int64_t Timer::counter()
{
#if defined(_WIN32)
#if defined(_WIN32)
int64_t counter;
QueryPerformanceCounter((LARGE_INTEGER*)&counter);
QueryPerformanceCounter((LARGE_INTEGER *)&counter);
return counter;
#else
#else
timeval t;
gettimeofday(&t, 0);
return t.tv_sec * 1000000 + t.tv_usec;
#endif
#endif
}
int64_t Timer::frequency()
{
#if defined(_WIN32)
#if defined(_WIN32)
int64_t frequency;
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
return frequency;
#else
#else
return 1000000; // gettimeofday uses microsecond resolution
#endif
#endif
}
} // namespace sw
......@@ -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.
// 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__)
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");}
#define ENUM_UNDERLYING_TYPE_UNSIGNED_INT
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
#else
#define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int
# define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int
#endif
#if defined(_MSC_VER)
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef signed __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#define ALIGN(bytes, type) __declspec(align(bytes)) type
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef signed __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
# define ALIGN(bytes, type) __declspec(align(bytes)) type
#else
#include <stdint.h>
#define ALIGN(bytes, type) type __attribute__((aligned(bytes)))
# include <stdint.h>
# define ALIGN(bytes, type) type __attribute__((aligned(bytes)))
#endif
namespace sw {
......@@ -51,7 +56,7 @@ typedef ALIGN(8, uint64_t) qword;
typedef ALIGN(1, int8_t) sbyte;
template<typename T, int N>
struct alignas(sizeof(T)* N) vec
struct alignas(sizeof(T) * N) vec
{
vec() = default;
......@@ -63,8 +68,8 @@ struct alignas(sizeof(T)* N) vec
}
}
template<typename ... ARGS>
constexpr vec(T arg0, ARGS ... args)
template<typename... ARGS>
constexpr vec(T arg0, ARGS... args)
: v{ arg0, args... }
{
}
......@@ -91,12 +96,18 @@ struct alignas(sizeof(T) * 4) vec<T, 4>
vec() = default;
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)
: 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>
};
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++)
{
......@@ -142,15 +153,19 @@ bool operator==(const vec<T, N>& a, const vec<T, N>& b)
}
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);
}
template<typename T> using vec2 = vec<T, 2>;
template<typename T> using vec4 = vec<T, 4>;
template<typename T> using vec8 = vec<T, 8>;
template<typename T> using vec16 = vec<T, 16>;
template<typename T>
using vec2 = vec<T, 2>;
template<typename T>
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 uint2 = vec2<unsigned int>;
......@@ -186,7 +201,7 @@ inline constexpr float4 replicate(float 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
......
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