summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2019-08-13 21:59:26 -0700
committerThiago Macieira <thiago.macieira@intel.com>2019-10-09 07:31:28 -0700
commit780137d585344bf9de906a285a50498104c0c66e (patch)
tree12ba9d636b58091a4e0935fe1b9375297fde5a20 /src
parent74858dc4af2c70b1c1540a7eda550620227badac (diff)
QRandom: add support for RDSEED
The Intel whitepaer[1] recommends using the RDSEED over RDRAND whenever present. libstdc++ from GCC 10 will also use it in std::random_device. [ChangeLog][QtCore][QRandomGenerator] The system() random generator will now use the RDSEED instruction on x86 processors whenever available as the first source of random data. It will fall back to RDRAND and then to the system functions, in that order. [1] https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide Change-Id: I907a43cd9a714da288a2fffd15bab176e54e1975 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qsimd.cpp36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/corelib/tools/qsimd.cpp b/src/corelib/tools/qsimd.cpp
index fb9b5996f6..6e3b0f9faf 100644
--- a/src/corelib/tools/qsimd.cpp
+++ b/src/corelib/tools/qsimd.cpp
@@ -626,8 +626,40 @@ void qDumpCPUFeatures()
# ifdef Q_PROCESSOR_X86_64
# define _rdrandXX_step _rdrand64_step
+# define _rdseedXX_step _rdseed64_step
# else
# define _rdrandXX_step _rdrand32_step
+# define _rdseedXX_step _rdseed32_step
+# endif
+
+# if QT_COMPILER_SUPPORTS_HERE(RDSEED)
+static QT_FUNCTION_TARGET(RDSEED) unsigned *qt_random_rdseed(unsigned *ptr, unsigned *end) noexcept
+{
+ // Unlike for the RDRAND code below, the Intel whitepaper describing the
+ // use of the RDSEED instruction indicates we should not retry in a loop.
+ // If the independent bit generator used by RDSEED is out of entropy, it
+ // may take time to replenish.
+ // https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
+ while (ptr + sizeof(qregisteruint)/sizeof(*ptr) <= end) {
+ if (_rdseedXX_step(reinterpret_cast<qregisteruint *>(ptr)) == 0)
+ goto out;
+ ptr += sizeof(qregisteruint)/sizeof(*ptr);
+ }
+
+ if (sizeof(*ptr) != sizeof(qregisteruint) && ptr != end) {
+ if (_rdseed32_step(ptr) == 0)
+ goto out;
+ ++ptr;
+ }
+
+out:
+ return ptr;
+}
+# else
+static unsigned *qt_random_rdseed(unsigned *ptr, unsigned *)
+{
+ return ptr;
+}
# endif
QT_FUNCTION_TARGET(RDRND) qsizetype qRandomCpu(void *buffer, qsizetype count) noexcept
@@ -636,6 +668,9 @@ QT_FUNCTION_TARGET(RDRND) qsizetype qRandomCpu(void *buffer, qsizetype count) no
unsigned *end = ptr + count;
int retries = 10;
+ if (qCpuHasFeature(RDSEED))
+ ptr = qt_random_rdseed(ptr, end);
+
while (ptr + sizeof(qregisteruint)/sizeof(*ptr) <= end) {
if (_rdrandXX_step(reinterpret_cast<qregisteruint *>(ptr)))
ptr += sizeof(qregisteruint)/sizeof(*ptr);
@@ -657,5 +692,4 @@ out:
}
#endif
-
QT_END_NAMESPACE