Skip to content

Commit a9e82f6

Browse files
committed
Use cpuid intrinsics instead of asm code
1 parent 7666250 commit a9e82f6

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/random.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
#include <mutex>
3838

39+
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
40+
#include <cpuid.h>
41+
#endif
42+
3943
#include <openssl/err.h>
4044
#include <openssl/rand.h>
4145

@@ -72,17 +76,8 @@ static bool rdrand_supported = false;
7276
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
7377
static void RDRandInit()
7478
{
75-
uint32_t eax, ecx, edx;
76-
#if defined(__i386__) && ( defined(__PIC__) || defined(__PIE__))
77-
// Avoid clobbering ebx, as that is used for PIC on x86.
78-
uint32_t tmp;
79-
__asm__ ("mov %%ebx, %1; cpuid; mov %1, %%ebx": "=a"(eax), "=g"(tmp), "=c"(ecx), "=d"(edx) : "a"(1));
80-
#else
81-
uint32_t ebx;
82-
__asm__ ("cpuid": "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1));
83-
#endif
84-
//! When calling cpuid function #1, ecx register will have this set if RDRAND is available.
85-
if (ecx & CPUID_F1_ECX_RDRAND) {
79+
uint32_t eax, ebx, ecx, edx;
80+
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) {
8681
LogPrintf("Using RdRand as entropy source\n");
8782
rdrand_supported = true;
8883
}

0 commit comments

Comments
 (0)