Skip to content

Commit 7bde8b7

Browse files
committed
[MOVEONLY] Move cpuid code from random to compat/cpuid
1 parent 52b5336 commit 7bde8b7

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ BITCOIN_CORE_H = \
165165
coins.h \
166166
compat.h \
167167
compat/byteswap.h \
168+
compat/cpuid.h \
168169
compat/endian.h \
169170
compat/sanity.h \
170171
compressor.h \

src/compat/cpuid.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2017-2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_COMPAT_CPUID_H
6+
#define BITCOIN_COMPAT_CPUID_H
7+
8+
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
9+
#define HAVE_GETCPUID
10+
11+
#include <cpuid.h>
12+
13+
// We can't use cpuid.h's __get_cpuid as it does not support subleafs.
14+
void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
15+
{
16+
#ifdef __GNUC__
17+
__cpuid_count(leaf, subleaf, a, b, c, d);
18+
#else
19+
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
20+
#endif
21+
}
22+
23+
#endif // defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
24+
#endif // BITCOIN_COMPAT_CPUID_H

src/random.cpp

+3-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "random.h"
88

9+
#include "allocators.h"
10+
#include "compat/cpuid.h"
911
#include "crypto/sha512.h"
1012
#include "support/cleanse.h"
1113
#ifdef WIN32
@@ -23,8 +25,6 @@
2325

2426
#include "randomenv.h"
2527

26-
#include "allocators.h"
27-
2828
#ifndef WIN32
2929
#include <fcntl.h>
3030
#include <sys/time.h>
@@ -47,11 +47,6 @@
4747

4848
#include <mutex>
4949

50-
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
51-
#include <atomic>
52-
#include <cpuid.h>
53-
#endif
54-
5550
#include <openssl/err.h>
5651
#include <openssl/rand.h>
5752
#include <openssl/conf.h>
@@ -82,7 +77,7 @@ static inline int64_t GetPerformanceCounter() noexcept
8277
#endif
8378
}
8479

85-
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
80+
#ifdef HAVE_GETCPUID
8681
static bool g_rdrand_supported = false;
8782
static bool g_rdseed_supported = false;
8883
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
@@ -93,15 +88,6 @@ static_assert(CPUID_F1_ECX_RDRAND == bit_RDRND, "Unexpected value for bit_RDRND"
9388
#ifdef bit_RDSEED
9489
static_assert(CPUID_F7_EBX_RDSEED == bit_RDSEED, "Unexpected value for bit_RDSEED");
9590
#endif
96-
static void inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
97-
{
98-
// We can't use __get_cpuid as it doesn't support subleafs.
99-
#ifdef __GNUC__
100-
__cpuid_count(leaf, subleaf, a, b, c, d);
101-
#else
102-
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
103-
#endif
104-
}
10591

10692
static void InitHardwareRand()
10793
{

0 commit comments

Comments
 (0)