Skip to content

Commit 723c796

Browse files
committed
[MOVEONLY] Move cpuid code from random & sha256 to compat/cpuid
1 parent cea3902 commit 723c796

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

src/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ BITCOIN_CORE_H = \
119119
compat.h \
120120
compat/assumptions.h \
121121
compat/byteswap.h \
122+
compat/cpuid.h \
122123
compat/endian.h \
123124
compat/sanity.h \
124125
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/crypto/sha256.cpp

+5-15
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#include <assert.h>
99
#include <string.h>
1010

11+
#include <compat/cpuid.h>
12+
1113
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
1214
#if defined(USE_ASM)
13-
#include <cpuid.h>
1415
namespace sha256_sse4
1516
{
1617
void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks);
@@ -546,18 +547,7 @@ bool SelfTest() {
546547
return true;
547548
}
548549

549-
550550
#if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__))
551-
// We can't use cpuid.h's __get_cpuid as it does not support subleafs.
552-
void inline cpuid(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
553-
{
554-
#ifdef __GNUC__
555-
__cpuid_count(leaf, subleaf, a, b, c, d);
556-
#else
557-
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
558-
#endif
559-
}
560-
561551
/** Check whether the OS has enabled AVX registers. */
562552
bool AVXEnabled()
563553
{
@@ -572,7 +562,7 @@ bool AVXEnabled()
572562
std::string SHA256AutoDetect()
573563
{
574564
std::string ret = "standard";
575-
#if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__))
565+
#if defined(USE_ASM) && defined(HAVE_GETCPUID)
576566
bool have_sse4 = false;
577567
bool have_xsave = false;
578568
bool have_avx = false;
@@ -589,15 +579,15 @@ std::string SHA256AutoDetect()
589579
(void)enabled_avx;
590580

591581
uint32_t eax, ebx, ecx, edx;
592-
cpuid(1, 0, eax, ebx, ecx, edx);
582+
GetCPUID(1, 0, eax, ebx, ecx, edx);
593583
have_sse4 = (ecx >> 19) & 1;
594584
have_xsave = (ecx >> 27) & 1;
595585
have_avx = (ecx >> 28) & 1;
596586
if (have_xsave && have_avx) {
597587
enabled_avx = AVXEnabled();
598588
}
599589
if (have_sse4) {
600-
cpuid(7, 0, eax, ebx, ecx, edx);
590+
GetCPUID(7, 0, eax, ebx, ecx, edx);
601591
have_avx2 = (ebx >> 5) & 1;
602592
have_shani = (ebx >> 29) & 1;
603593
}

src/random.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <random.h>
77

8+
#include <compat/cpuid.h>
89
#include <crypto/sha512.h>
910
#include <support/cleanse.h>
1011
#ifdef WIN32
@@ -42,11 +43,6 @@
4243
#include <sys/sysctl.h>
4344
#endif
4445

45-
46-
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
47-
#include <cpuid.h>
48-
#endif
49-
5046
#include <openssl/err.h>
5147
#include <openssl/rand.h>
5248
#include <openssl/conf.h>
@@ -77,7 +73,7 @@ static inline int64_t GetPerformanceCounter() noexcept
7773
#endif
7874
}
7975

80-
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
76+
#ifdef HAVE_GETCPUID
8177
static bool g_rdrand_supported = false;
8278
static bool g_rdseed_supported = false;
8379
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
@@ -88,15 +84,6 @@ static_assert(CPUID_F1_ECX_RDRAND == bit_RDRND, "Unexpected value for bit_RDRND"
8884
#ifdef bit_RDSEED
8985
static_assert(CPUID_F7_EBX_RDSEED == bit_RDSEED, "Unexpected value for bit_RDSEED");
9086
#endif
91-
static void inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
92-
{
93-
// We can't use __get_cpuid as it doesn't support subleafs.
94-
#ifdef __GNUC__
95-
__cpuid_count(leaf, subleaf, a, b, c, d);
96-
#else
97-
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
98-
#endif
99-
}
10087

10188
static void InitHardwareRand()
10289
{

0 commit comments

Comments
 (0)