Skip to content

Commit

Permalink
Generate random integers with uniform_int_distribution
Browse files Browse the repository at this point in the history
Signed-off-by: Clayton Smith <[email protected]>
  • Loading branch information
argilo committed Oct 23, 2023
1 parent 42f57cd commit 297ddf6
Showing 1 changed file with 62 additions and 37 deletions.
99 changes: 62 additions & 37 deletions lib/qa_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,69 @@ void load_random_data(void* data, volk_type_t type, unsigned int n)
random_floats<float>(data, n, rnd_engine);
}
} else {
float int_max = float(uint64_t(2) << (type.size * 8));
if (type.is_signed)
int_max /= 2.0;
std::uniform_real_distribution<float> uniform_dist(-int_max, int_max);
for (unsigned int i = 0; i < n; i++) {
float scaled_rand = uniform_dist(rnd_engine);
// man i really don't know how to do this in a more clever way, you have to
// cast down at some point
switch (type.size) {
case 8:
if (type.is_signed)
((int64_t*)data)[i] = (int64_t)scaled_rand;
else
((uint64_t*)data)[i] = (uint64_t)scaled_rand;
break;
case 4:
if (type.is_signed)
((int32_t*)data)[i] = (int32_t)scaled_rand;
else
((uint32_t*)data)[i] = (uint32_t)scaled_rand;
break;
case 2:
if (type.is_signed)
((int16_t*)data)[i] = (int16_t)((int16_t)scaled_rand % 8);
else
((uint16_t*)data)[i] = (uint16_t)((int16_t)scaled_rand % 8);
break;
case 1:
if (type.is_signed)
((int8_t*)data)[i] = (int8_t)scaled_rand;
else
((uint8_t*)data)[i] = (uint8_t)scaled_rand;
break;
default:
throw "load_random_data: no support for data size > 8 or < 1"; // no
// shenanigans
// here
switch (type.size) {
case 8:
if (type.is_signed) {
std::uniform_int_distribution<int64_t> uniform_dist(
std::numeric_limits<int64_t>::min(),
std::numeric_limits<int64_t>::max());
for (unsigned int i = 0; i < n; i++)
((int64_t*)data)[i] = uniform_dist(rnd_engine);
} else {
std::uniform_int_distribution<uint64_t> uniform_dist(
std::numeric_limits<uint64_t>::min(),
std::numeric_limits<uint64_t>::max());
for (unsigned int i = 0; i < n; i++)
((uint64_t*)data)[i] = uniform_dist(rnd_engine);
}
break;
case 4:
if (type.is_signed) {
std::uniform_int_distribution<int32_t> uniform_dist(
std::numeric_limits<int32_t>::min(),
std::numeric_limits<int32_t>::max());
for (unsigned int i = 0; i < n; i++)
((int32_t*)data)[i] = uniform_dist(rnd_engine);
} else {
std::uniform_int_distribution<uint32_t> uniform_dist(
std::numeric_limits<uint32_t>::min(),
std::numeric_limits<uint32_t>::max());
for (unsigned int i = 0; i < n; i++)
((uint32_t*)data)[i] = uniform_dist(rnd_engine);
}
break;
case 2:
if (type.is_signed) {
std::uniform_int_distribution<int16_t> uniform_dist(-7, 7);
for (unsigned int i = 0; i < n; i++)
((int16_t*)data)[i] = uniform_dist(rnd_engine);
} else {
std::uniform_int_distribution<uint16_t> uniform_dist(
std::numeric_limits<uint16_t>::min(),
std::numeric_limits<uint16_t>::max());
for (unsigned int i = 0; i < n; i++)
((uint16_t*)data)[i] = uniform_dist(rnd_engine);
}
break;
case 1:
if (type.is_signed) {
std::uniform_int_distribution<int16_t> uniform_dist(
std::numeric_limits<int8_t>::min(),
std::numeric_limits<int8_t>::max());
for (unsigned int i = 0; i < n; i++)
((int8_t*)data)[i] = uniform_dist(rnd_engine);
} else {
std::uniform_int_distribution<uint16_t> uniform_dist(
std::numeric_limits<uint8_t>::min(),
std::numeric_limits<uint8_t>::max());
for (unsigned int i = 0; i < n; i++)
((uint8_t*)data)[i] = uniform_dist(rnd_engine);
}
break;
default:
throw "load_random_data: no support for data size > 8 or < 1"; // no
// shenanigans
// here
}
}
}
Expand Down

0 comments on commit 297ddf6

Please sign in to comment.