why does libstdc++ std::random_device hate AMD CPUs?
sotrdg sotrdg
euloanty@live.com
Wed Oct 17 05:02:00 GMT 2018
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B11/random.cc
Why does GCC hate AMD? amd ryzen has a fully support for rdseed and rdrand instructions and you guys hate amd.
And why not just use intrinsic assembly? We are using C++ not using C. C APIs are always terrible please don’t use them. Also instructions like rdseed requires calling asm volatile(“pause”) after a failed rdseed call from intel’s manual.
// Check availability of cpuid and, for now at least, also the
// CPU signature for Intel's WHY WHY WHY ???????
if (__get_cpuid_max(0, &ebx) > 0 && ebx == signature_INTEL_ebx)
if (ecx & bit_RDRND)
{
_M_file = nullptr;
return;
}
}
Why do you guys love pointers with useless register pointer function calls? Why do you guys enjoy using shit C APIs. C is a garbage language and should be avoided at all.
What’s wrong with assembly code as simple like this?
uint32_t __b;
__asm__ __volatile__("cpuid":"=b"(__b):"a"(7),"c"(0):"%edx");
if(!(__b&(1<<18)))
throw runtime_error("std::random_device: Your CPU does not support the rdseed instruction.");
Also Who designed this shit? Abusing goto.
if (!_M_file)
goto fail;
Why do you enjoy using shit apis like fopen/fclose? Fopen/fclose has internal buffers which highly chance to be exploited.
const int fd = fileno(static_cast<FILE*>(_M_file));
LOL. Why not just use open/close +buffer directly in order to avoid all issues.
The entire implementation is completely unacceptable from the beginning. Now you guys are doing terrible changes to bits/random.h which potentially breaks ABI.
This class in header is terrible and even worse than libc++’s implementation. Libc++ just uses constructors and destructors as abi and never introduces any other abis.
class random_device
{
public:
/** The type of the generated random value. */
typedef unsigned int result_type;
// constructors, destructors and member functions
#ifdef _GLIBCXX_USE_DEV_RANDOM
random_device() { _M_init("default"); }
explicit
random_device(const std::string& __token) { _M_init(__token); }
~random_device()
{ _M_fini(); }
#else
random_device() { _M_init_pretr1("mt19937"); }
explicit
random_device(const std::string& __token)
{ _M_init_pretr1(__token); }
#endif
static constexpr result_type
min()
{ return std::numeric_limits<result_type>::min(); }
static constexpr result_type
max()
{ return std::numeric_limits<result_type>::max(); }
double
entropy() const noexcept
{
#ifdef _GLIBCXX_USE_DEV_RANDOM
return this->_M_getentropy();
#else
return 0.0;
#endif
}
result_type
operator()()
{
#ifdef _GLIBCXX_USE_DEV_RANDOM
return this->_M_getval();
#else
return this->_M_getval_pretr1();
#endif
}
// No copy functions.
random_device(const random_device&) = delete;
void operator=(const random_device&) = delete;
private:
void _M_init(const std::string& __token);
void _M_init_pretr1(const std::string& __token);
void _M_fini();
result_type _M_getval();
result_type _M_getval_pretr1();
double _M_getentropy() const noexcept;
union
{
void* _M_file;
mt19937 _M_mt;
};
};
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
More information about the Libstdc++
mailing list