-
Notifications
You must be signed in to change notification settings - Fork 340
Automatic detection of CPU capabilities #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e0484df
ebbe769
fc892fc
abf2a50
4296c35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| Copyright (c) 2019, tevador <tevador@gmail.com> | ||
|
|
||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are met: | ||
| * Redistributions of source code must retain the above copyright | ||
| notice, this list of conditions and the following disclaimer. | ||
| * Redistributions in binary form must reproduce the above copyright | ||
| notice, this list of conditions and the following disclaimer in the | ||
| documentation and/or other materials provided with the distribution. | ||
| * Neither the name of the copyright holder nor the | ||
| names of its contributors may be used to endorse or promote products | ||
| derived from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #include "cpu.hpp" | ||
|
|
||
| #if defined(_M_X64) || defined(__x86_64__) | ||
| #define HAVE_CPUID | ||
| #ifdef _WIN32 | ||
| #include <intrin.h> | ||
| #define cpuid(info, x) __cpuidex(info, x, 0) | ||
| #else //GCC | ||
| #include <cpuid.h> | ||
| void cpuid(int info[4], int InfoType) { | ||
| __cpuid_count(InfoType, 0, info[0], info[1], info[2], info[3]); | ||
| } | ||
| #endif | ||
| #endif | ||
|
|
||
| #if defined(HAVE_HWCAP) | ||
| #include <sys/auxv.h> | ||
| #include <asm/hwcap.h> | ||
| #endif | ||
|
|
||
| namespace randomx { | ||
|
|
||
| Cpu::Cpu() : aes_(false), ssse3_(false), avx2_(false) { | ||
| #ifdef HAVE_CPUID | ||
| int info[4]; | ||
| cpuid(info, 0); | ||
| int nIds = info[0]; | ||
| if (nIds >= 0x00000001) { | ||
| cpuid(info, 0x00000001); | ||
| ssse3_ = (info[2] & (1 << 9)) != 0; | ||
| aes_ = (info[2] & (1 << 25)) != 0; | ||
| } | ||
| if (nIds >= 0x00000007) { | ||
| cpuid(info, 0x00000007); | ||
| avx2_ = (info[1] & (1 << 5)) != 0; | ||
| } | ||
| #elif defined(__aarch64__) && defined(HWCAP_AES) | ||
| long hwcaps = getauxval(AT_HWCAP); | ||
| aes_ = (hwcaps & HWCAP_AES) != 0; | ||
| #endif | ||
| //TODO POWER8 AES | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| Copyright (c) 2019, tevador <tevador@gmail.com> | ||
|
|
||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are met: | ||
| * Redistributions of source code must retain the above copyright | ||
| notice, this list of conditions and the following disclaimer. | ||
| * Redistributions in binary form must reproduce the above copyright | ||
| notice, this list of conditions and the following disclaimer in the | ||
| documentation and/or other materials provided with the distribution. | ||
| * Neither the name of the copyright holder nor the | ||
| names of its contributors may be used to endorse or promote products | ||
| derived from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace randomx { | ||
|
|
||
| class Cpu { | ||
| public: | ||
| Cpu(); | ||
| bool hasAes() const { | ||
| return aes_; | ||
| } | ||
| bool hasSsse3() const { | ||
| return ssse3_; | ||
| } | ||
| bool hasAvx2() const { | ||
| return avx2_; | ||
| } | ||
| private: | ||
| bool aes_, ssse3_, avx2_; | ||
| }; | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,18 +33,38 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| #include "vm_compiled.hpp" | ||
| #include "vm_compiled_light.hpp" | ||
| #include "blake2/blake2.h" | ||
| #include "cpu.hpp" | ||
| #include <cassert> | ||
| #include <limits> | ||
|
|
||
| extern "C" { | ||
|
|
||
| randomx_flags randomx_get_flags() { | ||
| randomx_flags flags = RANDOMX_HAVE_COMPILER ? RANDOMX_FLAG_JIT : RANDOMX_FLAG_DEFAULT; | ||
| randomx::Cpu cpu; | ||
| if (HAVE_AES && cpu.hasAes()) { | ||
| flags |= RANDOMX_FLAG_HARD_AES; | ||
| } | ||
| if (randomx_argon2_impl_avx2() != nullptr && cpu.hasAvx2()) { | ||
| flags |= RANDOMX_FLAG_ARGON2_AVX2; | ||
| } | ||
| if (randomx_argon2_impl_ssse3() != nullptr && cpu.hasSsse3()) { | ||
| flags |= RANDOMX_FLAG_ARGON2_SSSE3; | ||
| } | ||
| return flags; | ||
| } | ||
|
|
||
| randomx_cache *randomx_alloc_cache(randomx_flags flags) { | ||
| randomx_cache *cache = nullptr; | ||
| auto impl = randomx::selectArgonImpl(flags); | ||
| if (impl == nullptr) { | ||
| return cache; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably a bad idea to return uninitialized cache here even if impl can't be nullptr with current code. It should output some error and abort, or throw an exception.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then it's ok, I somehow thought cache was already allocated at this point. |
||
| } | ||
|
|
||
| try { | ||
| cache = new randomx_cache(); | ||
| cache->argonImpl = randomx::selectArgonImpl(flags); | ||
| switch (flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)) { | ||
| cache->argonImpl = impl; | ||
| switch ((int)(flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES))) { | ||
| case RANDOMX_FLAG_DEFAULT: | ||
| cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>; | ||
| cache->jit = nullptr; | ||
|
|
@@ -173,7 +193,7 @@ extern "C" { | |
| randomx_vm *vm = nullptr; | ||
|
|
||
| try { | ||
| switch (flags & (RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES)) { | ||
| switch ((int)(flags & (RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES))) { | ||
| case RANDOMX_FLAG_DEFAULT: | ||
| vm = new randomx::InterpretedLightVmDefault(); | ||
| break; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to wrap it into
#if HAVE_AES == 1 ... #endif, no need to make it compile-time when preprocessor can handle it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
HAVE_AES == 0, it will be optimized out during compilation. I find the code more readable this way.