From e3407385ab9e74179453d018b587afdb90e87a51 Mon Sep 17 00:00:00 2001 From: enWILLYado Date: Wed, 7 Feb 2018 22:57:59 +0100 Subject: [PATCH] Code syntaxis Auto code style in some files and update VS project properties. --- src/Cpu.cpp | 116 +++++++++++++++------------- src/Cpu.h | 75 ++++++++++++------ src/Cpu_unix.cpp | 35 +++++---- src/Cpu_win.cpp | 22 +++--- src/Mem.cpp | 54 +++++++------ src/crypto/soft_aes.h | 7 ++ src/interfaces/IJobResultListener.h | 4 +- src/interfaces/IWorker.h | 8 +- src/net/JobResult.h | 63 +++++++-------- src/net/SubmitResult.h | 16 ++-- src/workers/DoubleWorker.h | 20 ++--- src/workers/Handle.h | 48 ++++++++---- src/workers/SingleWorker.h | 16 ++-- vs/xmrig.vcxproj | 8 +- vs/xmrig.vcxproj.filters | 36 ++++----- 15 files changed, 300 insertions(+), 228 deletions(-) diff --git a/src/Cpu.cpp b/src/Cpu.cpp index ff6f49e9..ae627f7d 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -41,78 +41,90 @@ int Cpu::m_totalThreads = 0; int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage) { - if (m_totalThreads == 1) { - return 1; - } + if(m_totalThreads == 1) + { + return 1; + } - int cache = 0; - if (m_l3_cache) { - cache = m_l2_exclusive ? (m_l2_cache + m_l3_cache) : m_l3_cache; - } - else { - cache = m_l2_cache; - } + int cache = 0; + if(m_l3_cache) + { + cache = m_l2_exclusive ? (m_l2_cache + m_l3_cache) : m_l3_cache; + } + else + { + cache = m_l2_cache; + } - int count = 0; - const int size = (algo ? 1024 : 2048) * (doubleHash ? 2 : 1); + int count = 0; + const int size = (algo ? 1024 : 2048) * (doubleHash ? 2 : 1); - if (cache) { - count = cache / size; - } - else { - count = m_totalThreads / 2; - } + if(cache) + { + count = cache / size; + } + else + { + count = m_totalThreads / 2; + } - if (count > m_totalThreads) { - count = m_totalThreads; - } + if(count > m_totalThreads) + { + count = m_totalThreads; + } - if (((float) count / m_totalThreads * 100) > maxCpuUsage) { - count = (int) ceil((float) m_totalThreads * (maxCpuUsage / 100.0)); - } + if(((float) count / m_totalThreads * 100) > maxCpuUsage) + { + count = (int) ceil((float) m_totalThreads * (maxCpuUsage / 100.0)); + } - return count < 1 ? 1 : count; + return count < 1 ? 1 : count; } void Cpu::initCommon() { - struct cpu_raw_data_t raw = { 0 }; - struct cpu_id_t data = { 0 }; + struct cpu_raw_data_t raw = { 0 }; + struct cpu_id_t data = { 0 }; - cpuid_get_raw_data(&raw); - cpu_identify(&raw, &data); + cpuid_get_raw_data(&raw); + cpu_identify(&raw, &data); - strncpy(m_brand, data.brand_str, sizeof(m_brand) - 1); + strncpy(m_brand, data.brand_str, sizeof(m_brand) - 1); - m_totalThreads = data.total_logical_cpus; - m_sockets = m_totalThreads / data.num_logical_cpus; + m_totalThreads = data.total_logical_cpus; + m_sockets = m_totalThreads / data.num_logical_cpus; - if (m_sockets == 0) { - m_sockets = 1; - } + if(m_sockets == 0) + { + m_sockets = 1; + } - m_totalCores = data.num_cores * m_sockets; - m_l3_cache = data.l3_cache > 0 ? data.l3_cache * m_sockets : 0; + m_totalCores = data.num_cores * m_sockets; + m_l3_cache = data.l3_cache > 0 ? data.l3_cache * m_sockets : 0; - // Workaround for AMD CPUs https://github.com/anrieff/libcpuid/issues/97 - if (data.vendor == VENDOR_AMD && data.ext_family >= 0x15 && data.ext_family < 0x17) { - m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; - m_l2_exclusive = true; - } - else { - m_l2_cache = data.l2_cache > 0 ? data.l2_cache * m_totalCores * m_sockets : 0; - } + // Workaround for AMD CPUs https://github.com/anrieff/libcpuid/issues/97 + if(data.vendor == VENDOR_AMD && data.ext_family >= 0x15 && data.ext_family < 0x17) + { + m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; + m_l2_exclusive = true; + } + else + { + m_l2_cache = data.l2_cache > 0 ? data.l2_cache * m_totalCores * m_sockets : 0; + } # if defined(__x86_64__) || defined(_M_AMD64) - m_flags |= X86_64; + m_flags |= X86_64; # endif - if (data.flags[CPU_FEATURE_AES]) { - m_flags |= AES; - } + if(data.flags[CPU_FEATURE_AES]) + { + m_flags |= AES; + } - if (data.flags[CPU_FEATURE_BMI2]) { - m_flags |= BMI2; - } + if(data.flags[CPU_FEATURE_BMI2]) + { + m_flags |= BMI2; + } } diff --git a/src/Cpu.h b/src/Cpu.h index 9444274d..4a69f91d 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -31,36 +31,61 @@ class Cpu { public: - enum Flags { - X86_64 = 1, - AES = 2, - BMI2 = 4 - }; + enum Flags + { + X86_64 = 1, + AES = 2, + BMI2 = 4 + }; - static int optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage); - static void init(); - static void setAffinity(int id, uint64_t mask); + static int optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage); + static void init(); + static void setAffinity(int id, uint64_t mask); - static inline bool hasAES() { return (m_flags & AES) != 0; } - static inline bool isX64() { return (m_flags & X86_64) != 0; } - static inline const char *brand() { return m_brand; } - static inline int cores() { return m_totalCores; } - static inline int l2() { return m_l2_cache; } - static inline int l3() { return m_l3_cache; } - static inline int sockets() { return m_sockets; } - static inline int threads() { return m_totalThreads; } + static inline bool hasAES() + { + return (m_flags & AES) != 0; + } + static inline bool isX64() + { + return (m_flags & X86_64) != 0; + } + static inline const char* brand() + { + return m_brand; + } + static inline int cores() + { + return m_totalCores; + } + static inline int l2() + { + return m_l2_cache; + } + static inline int l3() + { + return m_l3_cache; + } + static inline int sockets() + { + return m_sockets; + } + static inline int threads() + { + return m_totalThreads; + } private: - static void initCommon(); + static void initCommon(); - static bool m_l2_exclusive; - static char m_brand[64]; - static int m_flags; - static int m_l2_cache; - static int m_l3_cache; - static int m_sockets; - static int m_totalCores; - static int m_totalThreads; + static bool m_l2_exclusive; + static char m_brand[64]; + static int m_flags; + static int m_l2_cache; + static int m_l3_cache; + static int m_sockets; + static int m_totalCores; + static int m_totalThreads; }; diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index 9d560a94..33fe0ea1 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -56,27 +56,30 @@ void Cpu::init() void Cpu::setAffinity(int id, uint64_t mask) { - /* cpu_set_t set; CPU_ZERO(&set); - for (int i = 0; i < m_totalThreads; i++) { - if (mask & (1UL << i)) { - CPU_SET(i, &set); - } + for(int i = 0; i < m_totalThreads; i++) + { + if(mask & (1UL << i)) + { + CPU_SET(i, &set); + } } - if (id == -1) { - # ifndef __FreeBSD__ - sched_setaffinity(0, sizeof(&set), &set); - # endif - } else { - # ifndef __ANDROID__ - pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); - # else - sched_setaffinity(gettid(), sizeof(&set), &set); - # endif + if(id == -1) + { +# ifndef __FreeBSD__ + sched_setaffinity(0, sizeof(&set), &set); +# endif + } + else + { +# ifndef __ANDROID__ + pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); +# else + sched_setaffinity(gettid(), sizeof(&set), &set); +# endif } - */ } #endif \ No newline at end of file diff --git a/src/Cpu_win.cpp b/src/Cpu_win.cpp index 13113a17..3c9a7bc0 100644 --- a/src/Cpu_win.cpp +++ b/src/Cpu_win.cpp @@ -31,22 +31,24 @@ void Cpu::init() { # ifdef XMRIG_NO_LIBCPUID - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); - m_totalThreads = sysinfo.dwNumberOfProcessors; + m_totalThreads = sysinfo.dwNumberOfProcessors; # endif - initCommon(); + initCommon(); } void Cpu::setAffinity(int id, uint64_t mask) { - if (id == -1) { - SetProcessAffinityMask(GetCurrentProcess(), mask); - } - else { - SetThreadAffinityMask(GetCurrentThread(), mask); - } + if(id == -1) + { + SetProcessAffinityMask(GetCurrentProcess(), mask); + } + else + { + SetThreadAffinityMask(GetCurrentThread(), mask); + } } diff --git a/src/Mem.cpp b/src/Mem.cpp index 4437c673..76cbffe8 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -35,53 +35,57 @@ int Mem::m_algo = 0; int Mem::m_flags = 0; int Mem::m_threads = 0; size_t Mem::m_offset = 0; -uint8_t *Mem::m_memory = nullptr; +uint8_t* Mem::m_memory = nullptr; -cryptonight_ctx *Mem::create(int threadId) +cryptonight_ctx* Mem::create(int threadId) { # ifndef XMRIG_NO_AEON - if (m_algo == Options::ALGO_CRYPTONIGHT_LITE) { - return createLite(threadId); - } + if(m_algo == Options::ALGO_CRYPTONIGHT_LITE) + { + return createLite(threadId); + } # endif - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + cryptonight_ctx* ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * + (threadId + 1)]); - const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[MEMORY * (threadId * ratio + 1)]; + const int ratio = m_doubleHash ? 2 : 1; + ctx->memory = &m_memory[MEMORY * (threadId * ratio + 1)]; - return ctx; + return ctx; } -void *Mem::calloc(size_t num, size_t size) +void* Mem::calloc(size_t num, size_t size) { - void *mem = &m_memory[m_offset]; - m_offset += (num * size); + void* mem = &m_memory[m_offset]; + m_offset += (num * size); - memset(mem, 0, num * size); + memset(mem, 0, num * size); - return mem; + return mem; } #ifndef XMRIG_NO_AEON -cryptonight_ctx *Mem::createLite(int threadId) { - cryptonight_ctx *ctx; +cryptonight_ctx* Mem::createLite(int threadId) +{ + cryptonight_ctx* ctx; - if (!m_doubleHash) { - const size_t offset = MEMORY * (threadId + 1); + if(!m_doubleHash) + { + const size_t offset = MEMORY * (threadId + 1); - ctx = reinterpret_cast(&m_memory[offset + MEMORY_LITE]); - ctx->memory = &m_memory[offset]; - return ctx; - } + ctx = reinterpret_cast(&m_memory[offset + MEMORY_LITE]); + ctx->memory = &m_memory[offset]; + return ctx; + } - ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); - ctx->memory = &m_memory[MEMORY * (threadId + 1)]; + ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + ctx->memory = &m_memory[MEMORY * (threadId + 1)]; - return ctx; + return ctx; } #endif diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index 69607345..4db7ece3 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -91,10 +91,17 @@ const uint8_t saes_sbox[256] = saes_data(saes_h0); static inline __m128i soft_aesenc(__m128i in, __m128i key) { +#if defined(_MSC_VER) + const uint32_t x0 = in.m128i_u32[0]; + const uint32_t x1 = in.m128i_u32[1]; + const uint32_t x2 = in.m128i_u32[2]; + const uint32_t x3 = in.m128i_u32[3]; +#else const uint32_t x0 = _mm_cvtsi128_si32(in); const uint32_t x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); const uint32_t x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); const uint32_t x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); +#endif __m128i out = _mm_set_epi32( (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ diff --git a/src/interfaces/IJobResultListener.h b/src/interfaces/IJobResultListener.h index 483a2062..4bde82cf 100644 --- a/src/interfaces/IJobResultListener.h +++ b/src/interfaces/IJobResultListener.h @@ -32,9 +32,9 @@ class JobResult; class IJobResultListener { public: - virtual ~IJobResultListener() {} + virtual ~IJobResultListener() {} - virtual void onJobResult(const JobResult &result) = 0; + virtual void onJobResult(const JobResult & result) = 0; }; diff --git a/src/interfaces/IWorker.h b/src/interfaces/IWorker.h index b9b6eb0a..b613c5db 100644 --- a/src/interfaces/IWorker.h +++ b/src/interfaces/IWorker.h @@ -31,11 +31,11 @@ class IWorker { public: - virtual ~IWorker() {} + virtual ~IWorker() {} - virtual uint64_t hashCount() const = 0; - virtual uint64_t timestamp() const = 0; - virtual void start() = 0; + virtual uint64_t hashCount() const = 0; + virtual uint64_t timestamp() const = 0; + virtual void start() = 0; }; diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 520022a7..409c038c 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -35,46 +35,47 @@ class JobResult { public: - inline JobResult() : poolId(0), diff(0), nonce(0) {} - inline JobResult(int poolId, const JobId &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : - poolId(poolId), - jobId(jobId), - diff(diff), - nonce(nonce) - { - memcpy(this->result, result, sizeof(this->result)); - } + inline JobResult() : poolId(0), diff(0), nonce(0) {} + inline JobResult(int poolId, const JobId & jobId, uint32_t nonce, const uint8_t* result, uint32_t diff) : + poolId(poolId), + jobId(jobId), + diff(diff), + nonce(nonce) + { + memcpy(this->result, result, sizeof(this->result)); + } - inline JobResult(const Job &job) : poolId(0), diff(0), nonce(0) - { - jobId = job.id(); - poolId = job.poolId(); - diff = job.diff(); - nonce = *job.nonce(); - } + inline JobResult(const Job & job) : poolId(0), diff(0), nonce(0) + { + jobId = job.id(); + poolId = job.poolId(); + diff = job.diff(); + nonce = *job.nonce(); + } - inline JobResult &operator=(const Job &job) { - jobId = job.id(); - poolId = job.poolId(); - diff = job.diff(); + inline JobResult & operator=(const Job & job) + { + jobId = job.id(); + poolId = job.poolId(); + diff = job.diff(); - return *this; - } + return *this; + } - inline uint64_t actualDiff() const - { - return Job::toDiff(reinterpret_cast(result)[3]); - } + inline uint64_t actualDiff() const + { + return Job::toDiff(reinterpret_cast(result)[3]); + } - int poolId; - JobId jobId; - uint32_t diff; - uint32_t nonce; - uint8_t result[32]; + int poolId; + JobId jobId; + uint32_t diff; + uint32_t nonce; + uint8_t result[32]; }; #endif /* __JOBRESULT_H__ */ diff --git a/src/net/SubmitResult.h b/src/net/SubmitResult.h index 8eddef89..66daa7e8 100644 --- a/src/net/SubmitResult.h +++ b/src/net/SubmitResult.h @@ -31,18 +31,18 @@ class SubmitResult { public: - inline SubmitResult() : seq(0), diff(0), actualDiff(0), elapsed(0), start(0) {} - SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff); + inline SubmitResult() : seq(0), diff(0), actualDiff(0), elapsed(0), start(0) {} + SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff); - void done(); + void done(); - int64_t seq; - uint32_t diff; - uint64_t actualDiff; - uint64_t elapsed; + int64_t seq; + uint32_t diff; + uint64_t actualDiff; + uint64_t elapsed; private: - uint64_t start; + uint64_t start; }; #endif /* __SUBMITRESULT_H__ */ diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h index 711f4bd1..267ae2f0 100644 --- a/src/workers/DoubleWorker.h +++ b/src/workers/DoubleWorker.h @@ -37,21 +37,21 @@ class Handle; class DoubleWorker : public Worker { public: - DoubleWorker(Handle *handle); - ~DoubleWorker(); + DoubleWorker(Handle* handle); + ~DoubleWorker(); - void start() override; + void start() override; private: - bool resume(const Job &job); - void consumeJob(); - void save(const Job &job); + bool resume(const Job & job); + void consumeJob(); + void save(const Job & job); - class State; + class State; - uint8_t m_hash[64]; - State *m_state; - State *m_pausedState; + uint8_t m_hash[64]; + State* m_state; + State* m_pausedState; }; diff --git a/src/workers/Handle.h b/src/workers/Handle.h index 9faae0d0..f95dfdda 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -35,24 +35,42 @@ class IWorker; class Handle { public: - Handle(int threadId, int threads, int64_t affinity, int priority); - void join(); - void start(void (*callback) (void *)); + Handle(int threadId, int threads, int64_t affinity, int priority); + void join(); + void start(void (*callback)(void*)); - inline int priority() const { return m_priority; } - inline int threadId() const { return m_threadId; } - inline int threads() const { return m_threads; } - inline int64_t affinity() const { return m_affinity; } - inline IWorker *worker() const { return m_worker; } - inline void setWorker(IWorker *worker) { m_worker = worker; } + inline int priority() const + { + return m_priority; + } + inline int threadId() const + { + return m_threadId; + } + inline int threads() const + { + return m_threads; + } + inline int64_t affinity() const + { + return m_affinity; + } + inline IWorker* worker() const + { + return m_worker; + } + inline void setWorker(IWorker* worker) + { + m_worker = worker; + } private: - int m_priority; - int m_threadId; - int m_threads; - int64_t m_affinity; - IWorker *m_worker; - uv_thread_t m_thread; + int m_priority; + int m_threadId; + int m_threads; + int64_t m_affinity; + IWorker* m_worker; + uv_thread_t m_thread; }; diff --git a/src/workers/SingleWorker.h b/src/workers/SingleWorker.h index 08ab1857..76c1a36b 100644 --- a/src/workers/SingleWorker.h +++ b/src/workers/SingleWorker.h @@ -36,18 +36,18 @@ class Handle; class SingleWorker : public Worker { public: - SingleWorker(Handle *handle); + SingleWorker(Handle* handle); - void start() override; + void start() override; private: - bool resume(const Job &job); - void consumeJob(); - void save(const Job &job); + bool resume(const Job & job); + void consumeJob(); + void save(const Job & job); - Job m_job; - Job m_paused; - JobResult m_result; + Job m_job; + Job m_paused; + JobResult m_result; }; diff --git a/vs/xmrig.vcxproj b/vs/xmrig.vcxproj index 2dc14455..e0c114b3 100644 --- a/vs/xmrig.vcxproj +++ b/vs/xmrig.vcxproj @@ -308,8 +308,8 @@ true true $(ProjectName)$(Platform)d.pdb - true true + UseLinkTimeCodeGeneration @@ -328,7 +328,7 @@ $(SolutionDir)\..\..\libuv-1.x\Release\$(Platform)\lib;%(AdditionalLibraryDirectories) libuv.lib;advapi32.lib;iphlpapi.lib;psapi.lib;shell32.lib;user32.lib;userenv.lib;ws2_32.lib;%(AdditionalDependencies) LIBCMTD;LIBCMT - true + UseLinkTimeCodeGeneration @@ -342,12 +342,12 @@ $(SolutionDir)\..\..\libuv-1.x\Release\$(Platform)\lib;%(AdditionalLibraryDirectories) LIBCMTD;LIBCMT libuv.lib;advapi32.lib;iphlpapi.lib;psapi.lib;shell32.lib;user32.lib;userenv.lib;ws2_32.lib;%(AdditionalDependencies) - true $(ProjectName)$(Platform)d.pdb true true true true + UseLinkTimeCodeGeneration @@ -367,7 +367,7 @@ $(SolutionDir)\..\..\libuv-1.x\Release\$(Platform)\lib;%(AdditionalLibraryDirectories) libuv.lib;advapi32.lib;iphlpapi.lib;psapi.lib;shell32.lib;user32.lib;userenv.lib;ws2_32.lib;%(AdditionalDependencies) LIBCMTD;LIBCMT - true + UseLinkTimeCodeGeneration diff --git a/vs/xmrig.vcxproj.filters b/vs/xmrig.vcxproj.filters index 7c712abf..9eed6a87 100644 --- a/vs/xmrig.vcxproj.filters +++ b/vs/xmrig.vcxproj.filters @@ -519,24 +519,6 @@ Archivos de encabezado\net\strategies - - Archivos de encabezado\workers - - - Archivos de encabezado\workers - - - Archivos de encabezado\workers - - - Archivos de encabezado\workers - - - Archivos de encabezado\workers - - - Archivos de encabezado\workers - Archivos de encabezado\3rdparty\libcpuid @@ -570,6 +552,24 @@ Archivos de encabezado\3rdparty\libcpuid + + Archivos de encabezado\workers + + + Archivos de encabezado\workers + + + Archivos de encabezado\workers + + + Archivos de encabezado\workers + + + Archivos de encabezado\workers + + + Archivos de encabezado\workers +