]> git.droids-corp.org - dpdk.git/commitdiff
eal: provide pseudo-random floating point number
authorStephen Hemminger <stephen@networkplumber.org>
Thu, 26 May 2022 20:26:51 +0000 (13:26 -0700)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 22 Jun 2022 08:59:09 +0000 (10:59 +0200)
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>
Acked-by: Ray Kinsella <mdr@ashroe.eu>
app/test/test_rand_perf.c
doc/guides/rel_notes/release_22_07.rst
lib/eal/common/rte_random.c
lib/eal/include/rte_random.h
lib/eal/version.map

index fe797ebfa1ca8c1089d79590c5abb8f4d606bd9f..26fb1d9a586e69c50fdc2d72c27dcbc479023ce7 100644 (file)
@@ -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);
 
index 6fc044edaa93f45724aeb11689a93422261e7077..cd7d6fb182df101d2fe70fe5942245da6ca6e841 100644 (file)
@@ -70,6 +70,11 @@ New Features
   (seqlock). A seqlock allows for low overhead, parallel reads. The
   DPDK seqlock uses a spinlock to serialize multiple writing threads.
 
+* ** Added function get random floating point number.**
+
+  Added the function ``rte_drand()`` to provide a pseudo-random
+  floating point number.
+
 * **Added protocol based input color selection for meter.**
 
   Added new functions ``rte_mtr_color_in_protocol_set()``,
index 4535cc980cecdb41bce921cb8124831fa93fc784..166b0d8921c97ed47bbd0ce8aad0ca3f9f5a5a0a 100644 (file)
@@ -173,6 +173,25 @@ rte_rand_max(uint64_t upper_bound)
        return res;
 }
 
+double
+rte_drand(void)
+{
+       static const uint64_t denom = (uint64_t)1 << 53;
+       uint64_t rand64 = rte_rand();
+
+       /*
+        * The double mantissa only has 53 bits, so we uniformly mask off the
+        * high 11 bits and then floating-point divide by 2^53 to achieve a
+        * result in [0, 1).
+        *
+        * We are not allowed to emit 1.0, so denom must be one greater than
+        * the possible range of the preceding step.
+        */
+
+       rand64 &= denom - 1;
+       return (double)rand64 / denom;
+}
+
 static uint64_t
 __rte_random_initial_seed(void)
 {
index 29f5f1325a30046df4313c08ac1632d48c35f535..d90e4d219268d0c4b98c40ad508adef82bb0ba04 100644 (file)
@@ -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 non-negative 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
index ab27a4bb44f3deb3212b924df5fe767f8e03c2f0..5e6d85176bb6f83b1584a1d649af3d4577147531 100644 (file)
@@ -422,6 +422,7 @@ EXPERIMENTAL {
        rte_intr_type_set;
 
        # added in 22.07
+       rte_drand;
        rte_thread_get_affinity_by_id;
        rte_thread_get_priority;
        rte_thread_self;