Skip to content

Commit

Permalink
[MOVEONLY] Move cpuid code from random to compat/cpuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Apr 14, 2021
1 parent 52b5336 commit 7bde8b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ BITCOIN_CORE_H = \
coins.h \
compat.h \
compat/byteswap.h \
compat/cpuid.h \
compat/endian.h \
compat/sanity.h \
compressor.h \
Expand Down
24 changes: 24 additions & 0 deletions src/compat/cpuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2017-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_COMPAT_CPUID_H
#define BITCOIN_COMPAT_CPUID_H

#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
#define HAVE_GETCPUID

#include <cpuid.h>

// We can't use cpuid.h's __get_cpuid as it does not support subleafs.
void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
{
#ifdef __GNUC__
__cpuid_count(leaf, subleaf, a, b, c, d);
#else
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
#endif
}

#endif // defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
#endif // BITCOIN_COMPAT_CPUID_H
20 changes: 3 additions & 17 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "random.h"

#include "allocators.h"
#include "compat/cpuid.h"
#include "crypto/sha512.h"
#include "support/cleanse.h"
#ifdef WIN32
Expand All @@ -23,8 +25,6 @@

#include "randomenv.h"

#include "allocators.h"

#ifndef WIN32
#include <fcntl.h>
#include <sys/time.h>
Expand All @@ -47,11 +47,6 @@

#include <mutex>

#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
#include <atomic>
#include <cpuid.h>
#endif

#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/conf.h>
Expand Down Expand Up @@ -82,7 +77,7 @@ static inline int64_t GetPerformanceCounter() noexcept
#endif
}

#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
#ifdef HAVE_GETCPUID
static bool g_rdrand_supported = false;
static bool g_rdseed_supported = false;
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
Expand All @@ -93,15 +88,6 @@ static_assert(CPUID_F1_ECX_RDRAND == bit_RDRND, "Unexpected value for bit_RDRND"
#ifdef bit_RDSEED
static_assert(CPUID_F7_EBX_RDSEED == bit_RDSEED, "Unexpected value for bit_RDSEED");
#endif
static void inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
{
// We can't use __get_cpuid as it doesn't support subleafs.
#ifdef __GNUC__
__cpuid_count(leaf, subleaf, a, b, c, d);
#else
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
#endif
}

static void InitHardwareRand()
{
Expand Down

0 comments on commit 7bde8b7

Please sign in to comment.