[v3,1/3] random: add rte_drand() funciton
Checks
Commit Message
The PIE code and other applications can benefit from having a
fast way to get a random floating point value. This new function
is equivalent to drand() in the standard library.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
app/test/test_rand_perf.c | 7 +++++++
doc/guides/rel_notes/release_22_07.rst | 5 +++++
lib/eal/common/rte_random.c | 24 ++++++++++++++++++++++++
lib/eal/include/rte_random.h | 18 ++++++++++++++++++
lib/eal/version.map | 1 +
5 files changed, 55 insertions(+)
@@ -20,6 +20,7 @@ static volatile uint64_t vsum;
enum rand_type {
rand_type_64,
+ rand_type_float,
rand_type_bounded_best_case,
rand_type_bounded_worst_case
};
@@ -30,6 +31,8 @@ rand_type_desc(enum rand_type rand_type)
switch (rand_type) {
case rand_type_64:
return "Full 64-bit [rte_rand()]";
+ case rand_type_float:
+ return "Floating point [rte_drand()]";
case rand_type_bounded_best_case:
return "Bounded average best-case [rte_rand_max()]";
case rand_type_bounded_worst_case:
@@ -55,6 +58,9 @@ test_rand_perf_type(enum rand_type rand_type)
case rand_type_64:
sum += rte_rand();
break;
+ case rand_type_float:
+ sum += 1000. * rte_drand();
+ break;
case rand_type_bounded_best_case:
sum += rte_rand_max(BEST_CASE_BOUND);
break;
@@ -83,6 +89,7 @@ test_rand_perf(void)
printf("Pseudo-random number generation latencies:\n");
test_rand_perf_type(rand_type_64);
+ test_rand_perf_type(rand_type_float);
test_rand_perf_type(rand_type_bounded_best_case);
test_rand_perf_type(rand_type_bounded_worst_case);
@@ -104,6 +104,11 @@ New Features
* ``RTE_EVENT_QUEUE_ATTR_WEIGHT``
* ``RTE_EVENT_QUEUE_ATTR_AFFINITY``
+* ** Added function get random floating point number.**
+
+ Added the function ``rte_drand()`` to provide a pseudo-random
+ floating point number.
+
Removed Items
-------------
@@ -6,6 +6,7 @@
#include <x86intrin.h>
#endif
#include <unistd.h>
+#include <ieee754.h>
#include <rte_branch_prediction.h>
#include <rte_cycles.h>
@@ -173,6 +174,29 @@ rte_rand_max(uint64_t upper_bound)
return res;
}
+double
+rte_drand(void)
+{
+ struct rte_rand_state *state = __rte_rand_get_state();
+ union ieee754_double u = {
+ .ieee = {
+ .negative = 0,
+ .exponent = IEEE754_DOUBLE_BIAS,
+ },
+ };
+ uint64_t val;
+
+ /* Take 64 bit random value and put it into the mantissa
+ * This uses direct access to IEEE format to avoid doing
+ * any direct floating point math here.
+ */
+ val = __rte_rand_lfsr258(state);
+ u.ieee.mantissa0 = val >> 32;
+ u.ieee.mantissa1 = val;
+
+ return u.d - 1.0;
+}
+
static uint64_t
__rte_random_initial_seed(void)
{
@@ -65,6 +65,24 @@ rte_rand(void);
uint64_t
rte_rand_max(uint64_t upper_bound);
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Generates a pseudo-random floating point number.
+ *
+ * This function returns a nonnegative double-precision floating random
+ * number uniformly distributed over the interval [0.0, 1.0).
+ *
+ * The generator is not cryptographically secure.
+ * If called from lcore threads, this function is thread-safe.
+ *
+ * @return
+ * A pseudo-random value between 0 and 1.0.
+ */
+__rte_experimental
+double rte_drand(void);
+
#ifdef __cplusplus
}
#endif
@@ -422,6 +422,7 @@ EXPERIMENTAL {
rte_intr_type_set;
# added in 22.07
+ rte_drand;
rte_thread_get_affinity_by_id;
rte_thread_self;
rte_thread_set_affinity_by_id;