refactor to avoid using union
This commit is contained in:
@@ -267,22 +267,14 @@ arma_rng::set_seed_random()
|
||||
{
|
||||
try
|
||||
{
|
||||
// TODO: replace union-based conversion with method based on std::memcpy() or C++20 bit_cast
|
||||
|
||||
union
|
||||
{
|
||||
seed_type a;
|
||||
unsigned char b[sizeof(seed_type)];
|
||||
} tmp;
|
||||
|
||||
tmp.a = seed_type(0);
|
||||
char tmp[sizeof(seed_type)] = {};
|
||||
|
||||
std::ifstream f("/dev/urandom", std::ifstream::binary);
|
||||
|
||||
if(f.good()) { f.read((char*)(&(tmp.b[0])), sizeof(seed_type)); }
|
||||
if(f.good()) { f.read(&(tmp[0]), sizeof(seed_type)); }
|
||||
|
||||
if(f.good()) { std::memcpy(&seed2, &(tmp[0]), sizeof(seed_type)); }
|
||||
|
||||
if(f.good()) { seed2 = tmp.a; }
|
||||
|
||||
have_seed = (seed2 != seed_type(0));
|
||||
}
|
||||
catch(...) {}
|
||||
@@ -299,21 +291,17 @@ arma_rng::set_seed_random()
|
||||
|
||||
seed3 = static_cast<seed_type>( since_epoch_usec & 0xFFFF );
|
||||
|
||||
// TODO: replace union-based conversion with method based on std::memcpy() or C++20 bit_cast
|
||||
unsigned char* a = (unsigned char*)std::malloc(std::size_t(4096));
|
||||
|
||||
union
|
||||
unsigned char b[sizeof(unsigned char*)] = {};
|
||||
|
||||
if(a != nullptr)
|
||||
{
|
||||
uword* a;
|
||||
unsigned char b[sizeof(uword*)];
|
||||
} tmp;
|
||||
|
||||
tmp.a = (uword*)malloc(sizeof(uword));
|
||||
|
||||
if(tmp.a != nullptr)
|
||||
{
|
||||
for(size_t i=0; i<sizeof(uword*); ++i) { seed4 += seed_type(tmp.b[i]); }
|
||||
std::memcpy(&(b[0]), &a, sizeof(unsigned char*));
|
||||
|
||||
free(tmp.a);
|
||||
for(size_t i=0; i<sizeof(unsigned char*); ++i) { seed4 += seed_type(b[i]); }
|
||||
|
||||
std::free(a);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user