From 66d62de681bde910eb7e36f3a3a58c49016f1430 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 28 Jun 2019 13:08:08 +0700 Subject: [PATCH] Merge Assembly enum and Asm class. --- cmake/asm.cmake | 6 +- src/Summary.cpp | 6 +- src/common/cpu/BasicCpuInfo.h | 2 +- src/common/interfaces/ICpuInfo.h | 6 +- src/common/xmrig.h | 10 --- src/core/config/Config.cpp | 7 +- src/core/cpu/AdvancedCpuInfo.cpp | 5 +- src/core/cpu/AdvancedCpuInfo.h | 2 +- src/crypto/cn/Asm.h | 50 ------------ src/crypto/cn/CnHash.cpp | 42 +++++----- src/crypto/cn/CnHash.h | 5 +- src/crypto/cn/CryptoNight_x86.h | 28 +++---- src/crypto/cn/r/CryptonightR_gen.cpp | 5 +- src/crypto/common/Algorithm.cpp | 1 - src/crypto/common/Algorithm.h | 2 +- .../{cn/Asm.cpp => common/Assembly.cpp} | 35 ++++---- src/crypto/common/Assembly.h | 79 +++++++++++++++++++ src/workers/CpuThread.cpp | 10 +-- src/workers/CpuThread.h | 2 +- 19 files changed, 164 insertions(+), 139 deletions(-) delete mode 100644 src/crypto/cn/Asm.h rename src/crypto/{cn/Asm.cpp => common/Assembly.cpp} (72%) create mode 100644 src/crypto/common/Assembly.h diff --git a/cmake/asm.cmake b/cmake/asm.cmake index d3010e515..e445defde 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -36,7 +36,11 @@ if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) endif() add_library(${XMRIG_ASM_LIBRARY} STATIC ${XMRIG_ASM_FILES}) - set(XMRIG_ASM_SOURCES src/crypto/cn/Asm.h src/crypto/cn/Asm.cpp src/crypto/cn/r/CryptonightR_gen.cpp) + set(XMRIG_ASM_SOURCES + src/crypto/common/Assembly.h + src/crypto/common/Assembly.cpp + src/crypto/cn/r/CryptonightR_gen.cpp + ) set_property(TARGET ${XMRIG_ASM_LIBRARY} PROPERTY LINKER_LANGUAGE C) add_definitions(/DXMRIG_FEATURE_ASM) diff --git a/src/Summary.cpp b/src/Summary.cpp index 2ba0fd571..13973c0f0 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -33,7 +33,7 @@ #include "common/cpu/Cpu.h" #include "core/config/Config.h" #include "core/Controller.h" -#include "crypto/cn/Asm.h" +#include "crypto/common/Assembly.h" #include "Mem.h" #include "Summary.h" #include "version.h" @@ -49,7 +49,7 @@ static const char *coloredAsmNames[] = { }; -inline static const char *asmName(xmrig::Assembly assembly) +inline static const char *asmName(xmrig::Assembly::Id assembly) { return coloredAsmNames[assembly]; } @@ -109,7 +109,7 @@ static void print_threads(xmrig::Config *config) } # ifdef XMRIG_FEATURE_ASM - if (config->assembly() == xmrig::ASM_AUTO) { + if (config->assembly() == xmrig::Assembly::AUTO) { const xmrig::Assembly assembly = xmrig::Cpu::info()->assembly(); xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s"), "ASSEMBLY", asmName(assembly)); diff --git a/src/common/cpu/BasicCpuInfo.h b/src/common/cpu/BasicCpuInfo.h index 95857ed27..f6daee544 100644 --- a/src/common/cpu/BasicCpuInfo.h +++ b/src/common/cpu/BasicCpuInfo.h @@ -40,7 +40,7 @@ public: protected: size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const override; - inline Assembly assembly() const override { return m_assembly; } + inline Assembly::Id assembly() const override { return m_assembly; } inline bool hasAES() const override { return m_aes; } inline bool hasAVX2() const override { return m_avx2; } inline bool isSupported() const override { return true; } diff --git a/src/common/interfaces/ICpuInfo.h b/src/common/interfaces/ICpuInfo.h index dd4034b33..907f3f63a 100644 --- a/src/common/interfaces/ICpuInfo.h +++ b/src/common/interfaces/ICpuInfo.h @@ -30,7 +30,7 @@ #include -#include "common/xmrig.h" +#include "crypto/common/Assembly.h" namespace xmrig { @@ -39,7 +39,7 @@ namespace xmrig { class ICpuInfo { public: - virtual ~ICpuInfo() {} + virtual ~ICpuInfo() = default; virtual bool hasAES() const = 0; virtual bool hasAVX2() const = 0; @@ -53,7 +53,7 @@ public: virtual int32_t sockets() const = 0; virtual int32_t threads() const = 0; virtual size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const = 0; - virtual xmrig::Assembly assembly() const = 0; + virtual Assembly::Id assembly() const = 0; }; diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 5dd418451..169c4c1fa 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -72,16 +72,6 @@ enum OclVendor { }; -enum Assembly { - ASM_NONE, - ASM_AUTO, - ASM_INTEL, - ASM_RYZEN, - ASM_BULLDOZER, - ASM_MAX -}; - - } /* namespace xmrig */ diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 69ac065fb..93bd47ff3 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -32,7 +32,7 @@ #include "base/kernel/interfaces/IJsonReader.h" #include "common/cpu/Cpu.h" #include "core/config/Config.h" -#include "crypto/cn/Asm.h" +#include "crypto/common/Assembly.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -45,7 +45,6 @@ static char affinity_tmp[20] = { 0 }; xmrig::Config::Config() : m_aesMode(AES_AUTO), m_algoVariant(AV_AUTO), - m_assembly(ASM_AUTO), m_hugePages(true), m_safe(false), m_shouldSave(false), @@ -99,7 +98,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("http", m_http.toJSON(doc), allocator); # ifdef XMRIG_FEATURE_ASM - doc.AddMember("asm", Asm::toJSON(m_assembly), allocator); + doc.AddMember("asm", m_assembly.toJSON(), allocator); # endif doc.AddMember("autosave", isAutoSave(), allocator); @@ -285,6 +284,6 @@ xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const #ifdef XMRIG_FEATURE_ASM void xmrig::Config::setAssembly(const rapidjson::Value &assembly) { - m_assembly = Asm::parse(assembly); + m_assembly = assembly; } #endif diff --git a/src/core/cpu/AdvancedCpuInfo.cpp b/src/core/cpu/AdvancedCpuInfo.cpp index df6a385ee..922e83113 100644 --- a/src/core/cpu/AdvancedCpuInfo.cpp +++ b/src/core/cpu/AdvancedCpuInfo.cpp @@ -31,7 +31,6 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : - m_assembly(ASM_NONE), m_aes(false), m_avx2(false), m_L2_exclusive(false), @@ -78,10 +77,10 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : m_aes = true; if (data.vendor == VENDOR_AMD) { - m_assembly = (data.ext_family >= 23) ? ASM_RYZEN : ASM_BULLDOZER; + m_assembly = (data.ext_family >= 23) ? Assembly::RYZEN : Assembly::BULLDOZER; } else if (data.vendor == VENDOR_INTEL) { - m_assembly = ASM_INTEL; + m_assembly = Assembly::INTEL; } } diff --git a/src/core/cpu/AdvancedCpuInfo.h b/src/core/cpu/AdvancedCpuInfo.h index 0765da339..90152640e 100644 --- a/src/core/cpu/AdvancedCpuInfo.h +++ b/src/core/cpu/AdvancedCpuInfo.h @@ -40,7 +40,7 @@ public: protected: size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const override; - inline Assembly assembly() const override { return m_assembly; } + inline Assembly::Id assembly() const override { return m_assembly; } inline bool hasAES() const override { return m_aes; } inline bool hasAVX2() const override { return m_avx2; } inline bool isSupported() const override { return true; } diff --git a/src/crypto/cn/Asm.h b/src/crypto/cn/Asm.h deleted file mode 100644 index 3b755fd64..000000000 --- a/src/crypto/cn/Asm.h +++ /dev/null @@ -1,50 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef XMRIG_ASM_H -#define XMRIG_ASM_H - - -#include "common/xmrig.h" -#include "rapidjson/fwd.h" - - -namespace xmrig { - - -class Asm -{ -public: - static Assembly parse(const char *assembly, Assembly defaultValue = ASM_AUTO); - static Assembly parse(const rapidjson::Value &value, Assembly defaultValue = ASM_AUTO); - static const char *toString(Assembly assembly); - static rapidjson::Value toJSON(Assembly assembly); - - inline static Assembly parse(bool enable) { return enable ? ASM_AUTO : ASM_NONE; } -}; - - -} /* namespace xmrig */ - - -#endif /* XMRIG_ASM_H */ diff --git a/src/crypto/cn/CnHash.cpp b/src/crypto/cn/CnHash.cpp index 61d2ea69c..d17a8e2d1 100644 --- a/src/crypto/cn/CnHash.cpp +++ b/src/crypto/cn/CnHash.cpp @@ -39,26 +39,26 @@ #define ADD_FN(algo) \ - m_map[algo][AV_SINGLE][ASM_NONE] = cryptonight_single_hash; \ - m_map[algo][AV_SINGLE_SOFT][ASM_NONE] = cryptonight_single_hash; \ - m_map[algo][AV_DOUBLE][ASM_NONE] = cryptonight_double_hash; \ - m_map[algo][AV_DOUBLE_SOFT][ASM_NONE] = cryptonight_double_hash; \ - m_map[algo][AV_TRIPLE][ASM_NONE] = cryptonight_triple_hash; \ - m_map[algo][AV_TRIPLE_SOFT][ASM_NONE] = cryptonight_triple_hash; \ - m_map[algo][AV_QUAD][ASM_NONE] = cryptonight_quad_hash; \ - m_map[algo][AV_QUAD_SOFT][ASM_NONE] = cryptonight_quad_hash; \ - m_map[algo][AV_PENTA][ASM_NONE] = cryptonight_penta_hash; \ - m_map[algo][AV_PENTA_SOFT][ASM_NONE] = cryptonight_penta_hash; + m_map[algo][AV_SINGLE][Assembly::NONE] = cryptonight_single_hash; \ + m_map[algo][AV_SINGLE_SOFT][Assembly::NONE] = cryptonight_single_hash; \ + m_map[algo][AV_DOUBLE][Assembly::NONE] = cryptonight_double_hash; \ + m_map[algo][AV_DOUBLE_SOFT][Assembly::NONE] = cryptonight_double_hash; \ + m_map[algo][AV_TRIPLE][Assembly::NONE] = cryptonight_triple_hash; \ + m_map[algo][AV_TRIPLE_SOFT][Assembly::NONE] = cryptonight_triple_hash; \ + m_map[algo][AV_QUAD][Assembly::NONE] = cryptonight_quad_hash; \ + m_map[algo][AV_QUAD_SOFT][Assembly::NONE] = cryptonight_quad_hash; \ + m_map[algo][AV_PENTA][Assembly::NONE] = cryptonight_penta_hash; \ + m_map[algo][AV_PENTA_SOFT][Assembly::NONE] = cryptonight_penta_hash; #ifdef XMRIG_FEATURE_ASM # define ADD_FN_ASM(algo) \ - m_map[algo][AV_SINGLE][ASM_INTEL] = cryptonight_single_hash_asm; \ - m_map[algo][AV_SINGLE][ASM_RYZEN] = cryptonight_single_hash_asm; \ - m_map[algo][AV_SINGLE][ASM_BULLDOZER] = cryptonight_single_hash_asm; \ - m_map[algo][AV_DOUBLE][ASM_INTEL] = cryptonight_double_hash_asm; \ - m_map[algo][AV_DOUBLE][ASM_RYZEN] = cryptonight_double_hash_asm; \ - m_map[algo][AV_DOUBLE][ASM_BULLDOZER] = cryptonight_double_hash_asm; + m_map[algo][AV_SINGLE][Assembly::INTEL] = cryptonight_single_hash_asm; \ + m_map[algo][AV_SINGLE][Assembly::RYZEN] = cryptonight_single_hash_asm; \ + m_map[algo][AV_SINGLE][Assembly::BULLDOZER] = cryptonight_single_hash_asm; \ + m_map[algo][AV_DOUBLE][Assembly::INTEL] = cryptonight_double_hash_asm; \ + m_map[algo][AV_DOUBLE][Assembly::RYZEN] = cryptonight_double_hash_asm; \ + m_map[algo][AV_DOUBLE][Assembly::BULLDOZER] = cryptonight_double_hash_asm; extern "C" void cnv2_mainloop_ivybridge_asm(cryptonight_ctx **ctx); @@ -226,8 +226,8 @@ xmrig::CnHash::CnHash() ADD_FN_ASM(Algorithm::CN_DOUBLE); # ifdef XMRIG_ALGO_CN_GPU - m_map[Algorithm::CN_GPU][AV_SINGLE][ASM_NONE] = cryptonight_single_hash_gpu; - m_map[Algorithm::CN_GPU][AV_SINGLE_SOFT][ASM_NONE] = cryptonight_single_hash_gpu; + m_map[Algorithm::CN_GPU][AV_SINGLE][Assembly::NONE] = cryptonight_single_hash_gpu; + m_map[Algorithm::CN_GPU][AV_SINGLE_SOFT][Assembly::NONE] = cryptonight_single_hash_gpu; # endif # ifdef XMRIG_ALGO_CN_LITE @@ -252,18 +252,18 @@ xmrig::CnHash::CnHash() } -xmrig::cn_hash_fun xmrig::CnHash::fn(const Algorithm &algorithm, AlgoVariant av, Assembly assembly) const +xmrig::cn_hash_fun xmrig::CnHash::fn(const Algorithm &algorithm, AlgoVariant av, Assembly::Id assembly) const { if (!algorithm.isValid()) { return nullptr; } # ifdef XMRIG_FEATURE_ASM - cn_hash_fun fun = m_map[algorithm][av][assembly == ASM_AUTO ? Cpu::info()->assembly() : assembly]; + cn_hash_fun fun = m_map[algorithm][av][assembly == Assembly::AUTO ? Cpu::info()->assembly() : assembly]; if (fun) { return fun; } # endif - return m_map[algorithm][av][ASM_NONE]; + return m_map[algorithm][av][Assembly::NONE]; } diff --git a/src/crypto/cn/CnHash.h b/src/crypto/cn/CnHash.h index 5fbf5c8a0..b57bff4cd 100644 --- a/src/crypto/cn/CnHash.h +++ b/src/crypto/cn/CnHash.h @@ -33,6 +33,7 @@ #include "common/xmrig.h" #include "crypto/cn/CnAlgo.h" +#include "crypto/common/Assembly.h" struct cryptonight_ctx; @@ -50,10 +51,10 @@ class CnHash public: CnHash(); - cn_hash_fun fn(const Algorithm &algorithm, AlgoVariant av, Assembly assembly) const; + cn_hash_fun fn(const Algorithm &algorithm, AlgoVariant av, Assembly::Id assembly) const; private: - cn_hash_fun m_map[Algorithm::MAX][AV_MAX][ASM_MAX] = {}; + cn_hash_fun m_map[Algorithm::MAX][AV_MAX][Assembly::MAX] = {}; }; diff --git a/src/crypto/cn/CryptoNight_x86.h b/src/crypto/cn/CryptoNight_x86.h index fc21c7b07..b24dea577 100644 --- a/src/crypto/cn/CryptoNight_x86.h +++ b/src/crypto/cn/CryptoNight_x86.h @@ -577,10 +577,10 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si const int code_size = v4_random_math_init(code, height); if (ALGO == Algorithm::CN_WOW) { - wow_soft_aes_compile_code(code, code_size, reinterpret_cast(ctx[0]->generated_code), ASM_NONE); + wow_soft_aes_compile_code(code, code_size, reinterpret_cast(ctx[0]->generated_code), Assembly::NONE); } else if (ALGO == Algorithm::CN_R) { - v4_soft_aes_compile_code(code, code_size, reinterpret_cast(ctx[0]->generated_code), ASM_NONE); + v4_soft_aes_compile_code(code, code_size, reinterpret_cast(ctx[0]->generated_code), Assembly::NONE); } ctx[0]->generated_code_data = { ALGO, height }; @@ -849,7 +849,7 @@ void cn_r_compile_code_double(const V4_Instruction* co namespace xmrig { -template +template inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx, uint64_t height) { constexpr CnAlgo props; @@ -866,10 +866,10 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ cn_explode_scratchpad(reinterpret_cast(ctx[0]->state), reinterpret_cast<__m128i*>(ctx[0]->memory)); if (ALGO == Algorithm::CN_2) { - if (ASM == ASM_INTEL) { + if (ASM == Assembly::INTEL) { cnv2_mainloop_ivybridge_asm(ctx); } - else if (ASM == ASM_RYZEN) { + else if (ASM == Assembly::RYZEN) { cnv2_mainloop_ryzen_asm(ctx); } else { @@ -877,10 +877,10 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ } } else if (ALGO == Algorithm::CN_HALF) { - if (ASM == ASM_INTEL) { + if (ASM == Assembly::INTEL) { cn_half_mainloop_ivybridge_asm(ctx); } - else if (ASM == ASM_RYZEN) { + else if (ASM == Assembly::RYZEN) { cn_half_mainloop_ryzen_asm(ctx); } else { @@ -889,10 +889,10 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ } # ifdef XMRIG_ALGO_CN_PICO else if (ALGO == Algorithm::CN_PICO_0) { - if (ASM == ASM_INTEL) { + if (ASM == Assembly::INTEL) { cn_trtl_mainloop_ivybridge_asm(ctx); } - else if (ASM == ASM_RYZEN) { + else if (ASM == Assembly::RYZEN) { cn_trtl_mainloop_ryzen_asm(ctx); } else { @@ -904,10 +904,10 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ cnv2_rwz_mainloop_asm(ctx); } else if (ALGO == Algorithm::CN_ZLS) { - if (ASM == ASM_INTEL) { + if (ASM == Assembly::INTEL) { cn_zls_mainloop_ivybridge_asm(ctx); } - else if (ASM == ASM_RYZEN) { + else if (ASM == Assembly::RYZEN) { cn_zls_mainloop_ryzen_asm(ctx); } else { @@ -915,10 +915,10 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ } } else if (ALGO == Algorithm::CN_DOUBLE) { - if (ASM == ASM_INTEL) { + if (ASM == Assembly::INTEL) { cn_double_mainloop_ivybridge_asm(ctx); } - else if (ASM == ASM_RYZEN) { + else if (ASM == Assembly::RYZEN) { cn_double_mainloop_ryzen_asm(ctx); } else { @@ -935,7 +935,7 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ } -template +template inline void cryptonight_double_hash_asm(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx, uint64_t height) { constexpr CnAlgo props; diff --git a/src/crypto/cn/r/CryptonightR_gen.cpp b/src/crypto/cn/r/CryptonightR_gen.cpp index 8491a33bf..3037327ab 100644 --- a/src/crypto/cn/r/CryptonightR_gen.cpp +++ b/src/crypto/cn/r/CryptonightR_gen.cpp @@ -29,6 +29,7 @@ typedef void(*void_func)(); #include "crypto/cn/asm/CryptonightR_template.h" +#include "crypto/common/Assembly.h" #include "crypto/common/VirtualMemory.h" #include "Mem.h" @@ -42,7 +43,7 @@ static inline void add_code(uint8_t* &p, void (*p1)(), void (*p2)()) } } -static inline void add_random_math(uint8_t* &p, const V4_Instruction* code, int code_size, const void_func* instructions, const void_func* instructions_mov, bool is_64_bit, xmrig::Assembly ASM) +static inline void add_random_math(uint8_t* &p, const V4_Instruction* code, int code_size, const void_func* instructions, const void_func* instructions_mov, bool is_64_bit, xmrig::Assembly::Id ASM) { uint32_t prev_rot_src = (uint32_t)(-1); @@ -76,7 +77,7 @@ static inline void add_random_math(uint8_t* &p, const V4_Instruction* code, int void_func begin = instructions[c]; - if ((ASM = xmrig::ASM_BULLDOZER) && (inst.opcode == MUL) && !is_64_bit) { + if ((ASM = xmrig::Assembly::BULLDOZER) && (inst.opcode == MUL) && !is_64_bit) { // AMD Bulldozer has latency 4 for 32-bit IMUL and 6 for 64-bit IMUL // Always use 32-bit IMUL for AMD Bulldozer in 32-bit mode - skip prefix 0x48 and change 0x49 to 0x41 uint8_t* prefix = reinterpret_cast(begin); diff --git a/src/crypto/common/Algorithm.cpp b/src/crypto/common/Algorithm.cpp index e70b0659c..66b3ddda8 100644 --- a/src/crypto/common/Algorithm.cpp +++ b/src/crypto/common/Algorithm.cpp @@ -35,7 +35,6 @@ #ifdef _MSC_VER -# define strncasecmp _strnicmp # define strcasecmp _stricmp #endif diff --git a/src/crypto/common/Algorithm.h b/src/crypto/common/Algorithm.h index ccaf7de5f..92c6f405b 100644 --- a/src/crypto/common/Algorithm.h +++ b/src/crypto/common/Algorithm.h @@ -69,7 +69,7 @@ public: CN_PICO_0, // "cn-pico" CryptoNight Turtle (TRTL) # endif # ifdef XMRIG_ALGO_RANDOMX - RX_WOW, // "rx/wow" RandomWOW + RX_WOW, // "rx/wow" RandomWOW (Wownero) # endif MAX }; diff --git a/src/crypto/cn/Asm.cpp b/src/crypto/common/Assembly.cpp similarity index 72% rename from src/crypto/cn/Asm.cpp rename to src/crypto/common/Assembly.cpp index 331c133dd..44bf0a949 100644 --- a/src/crypto/cn/Asm.cpp +++ b/src/crypto/common/Assembly.cpp @@ -6,7 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,15 +29,17 @@ #ifdef _MSC_VER -# define strncasecmp _strnicmp # define strcasecmp _stricmp #endif -#include "crypto/cn/Asm.h" +#include "crypto/common/Assembly.h" #include "rapidjson/document.h" +namespace xmrig { + + static const char *asmNames[] = { "none", "auto", @@ -46,11 +49,13 @@ static const char *asmNames[] = { }; -xmrig::Assembly xmrig::Asm::parse(const char *assembly, Assembly defaultValue) +} /* namespace xmrig */ + + +xmrig::Assembly::Id xmrig::Assembly::parse(const char *assembly, Id defaultValue) { constexpr size_t const size = sizeof(asmNames) / sizeof((asmNames)[0]); - assert(assembly != nullptr); - assert(ASM_MAX == size); + static_assert(size == MAX, "asmNames size mismatch"); if (assembly == nullptr) { return defaultValue; @@ -58,7 +63,7 @@ xmrig::Assembly xmrig::Asm::parse(const char *assembly, Assembly defaultValue) for (size_t i = 0; i < size; i++) { if (strcasecmp(assembly, asmNames[i]) == 0) { - return static_cast(i); + return static_cast(i); } } @@ -66,10 +71,10 @@ xmrig::Assembly xmrig::Asm::parse(const char *assembly, Assembly defaultValue) } -xmrig::Assembly xmrig::Asm::parse(const rapidjson::Value &value, Assembly defaultValue) +xmrig::Assembly::Id xmrig::Assembly::parse(const rapidjson::Value &value, Id defaultValue) { if (value.IsBool()) { - return parse(value.GetBool()); + return value.GetBool() ? AUTO : NONE; } if (value.IsString()) { @@ -80,23 +85,23 @@ xmrig::Assembly xmrig::Asm::parse(const rapidjson::Value &value, Assembly defaul } -const char *xmrig::Asm::toString(Assembly assembly) +const char *xmrig::Assembly::toString() const { - return asmNames[assembly]; + return asmNames[m_id]; } -rapidjson::Value xmrig::Asm::toJSON(Assembly assembly) +rapidjson::Value xmrig::Assembly::toJSON() const { using namespace rapidjson; - if (assembly == ASM_NONE) { + if (m_id == NONE) { return Value(false); } - if (assembly == ASM_AUTO) { + if (m_id == AUTO) { return Value(true); } - return Value(StringRef(toString(assembly))); + return Value(StringRef(toString())); } diff --git a/src/crypto/common/Assembly.h b/src/crypto/common/Assembly.h new file mode 100644 index 000000000..e4964d075 --- /dev/null +++ b/src/crypto/common/Assembly.h @@ -0,0 +1,79 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASSEMBLY_H +#define XMRIG_ASSEMBLY_H + + +#include "common/xmrig.h" +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class Assembly +{ +public: + enum Id : int { + NONE, + AUTO, + INTEL, + RYZEN, + BULLDOZER, + MAX + }; + + + inline Assembly() {} + inline Assembly(Id id) : m_id(id) {} + inline Assembly(const char *assembly) : m_id(parse(assembly)) {} + inline Assembly(const rapidjson::Value &value) : m_id(parse(value)) {} + + static Id parse(const char *assembly, Id defaultValue = AUTO); + static Id parse(const rapidjson::Value &value, Id defaultValue = AUTO); + + const char *toString() const; + rapidjson::Value toJSON() const; + +// inline static Assembly parse(bool enable) { return enable ? ASM_AUTO : ASM_NONE; } + + inline bool isEqual(const Assembly &other) const { return m_id == other.m_id; } + + + inline bool operator!=(const Assembly &other) const { return !isEqual(other); } + inline bool operator!=(const Assembly::Id &id) const { return m_id != id; } + inline bool operator==(const Assembly &other) const { return isEqual(other); } + inline bool operator==(const Assembly::Id &id) const { return m_id == id; } + inline operator Assembly::Id() const { return m_id; } + +private: + Id m_id = AUTO; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_ASSEMBLY_H */ diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index e26b8a0a8..9f20a35ab 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -27,8 +27,8 @@ #include "base/io/log/Log.h" #include "common/cpu/Cpu.h" -#include "crypto/cn/Asm.h" #include "crypto/cn/CnHash.h" +#include "crypto/common/Assembly.h" #include "crypto/common/VirtualMemory.h" #include "Mem.h" #include "rapidjson/document.h" @@ -36,8 +36,6 @@ - - static const xmrig::CnHash cnHash; @@ -136,7 +134,7 @@ xmrig::CpuThread::Data xmrig::CpuThread::parse(const rapidjson::Value &object) } # ifdef XMRIG_FEATURE_ASM - data.assembly = Asm::parse(object["asm"]); + data.assembly = object["asm"]; # endif return data; @@ -181,7 +179,7 @@ void xmrig::CpuThread::print() const index(), static_cast(multiway()), static_cast(m_av)); # ifdef XMRIG_FEATURE_ASM - LOG_DEBUG(" assembly: %s, affine_to_cpu: %" PRId64, Asm::toString(m_assembly), affinity()); + LOG_DEBUG(" assembly: %s, affine_to_cpu: %" PRId64, m_assembly.toString(), affinity()); # else LOG_DEBUG(" affine_to_cpu: %" PRId64, affinity()); # endif @@ -220,7 +218,7 @@ rapidjson::Value xmrig::CpuThread::toConfig(rapidjson::Document &doc) const obj.AddMember("affine_to_cpu", affinity() == -1L ? Value(kFalseType) : Value(affinity()), allocator); # ifdef XMRIG_FEATURE_ASM - obj.AddMember("asm", Asm::toJSON(m_assembly), allocator); + obj.AddMember("asm", m_assembly.toJSON(), allocator); # endif return obj; diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 08aa89cbf..a43a0c091 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -42,7 +42,7 @@ class CpuThread : public IThread public: struct Data { - inline Data() : assembly(ASM_AUTO), valid(false), affinity(-1L), multiway(SingleWay) {} + inline Data() : valid(false), affinity(-1L), multiway(SingleWay) {} inline void setMultiway(int value) {