From: Stephen Hemminger <stephen@networkplumber.org>
To: "Mattias Rönnblom" <mattias.ronnblom@ericsson.com>
Cc: "dev@dpdk.org" <dev@dpdk.org>, Ray Kinsella <mdr@ashroe.eu>
Subject: Re: [RFT v2 1/3] random: add rte_rand_float()
Date: Wed, 25 May 2022 08:45:47 -0700 [thread overview]
Message-ID: <20220525084547.13fd5f9f@hermes.local> (raw)
In-Reply-To: <589158f3-e42a-8e10-c5ad-5b05b4e6db04@ericsson.com>
On Wed, 25 May 2022 14:45:37 +0000
Mattias Rönnblom <mattias.ronnblom@ericsson.com> wrote:
> On 2022-05-25 00:18, Stephen Hemminger wrote:
> > 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 erand48_r in the standard library.
> >
>
> Seems like a good addition to the API.
>
> > 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 | 21 +++++++++++++++++++++
> > lib/eal/include/rte_random.h | 17 +++++++++++++++++
> > lib/eal/version.map | 1 +
> > 5 files changed, 51 insertions(+)
> >
> > diff --git a/app/test/test_rand_perf.c b/app/test/test_rand_perf.c
> > index fe797ebfa1ca..f3602da5b2d4 100644
> > --- a/app/test/test_rand_perf.c
> > +++ b/app/test/test_rand_perf.c
> > @@ -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_rand_float()]";
> > 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 += rte_rand_float() * UINT64_MAX;
> > + 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);
> >
> > diff --git a/doc/guides/rel_notes/release_22_07.rst b/doc/guides/rel_notes/release_22_07.rst
> > index e49cacecefd4..128d4fca85b3 100644
> > --- a/doc/guides/rel_notes/release_22_07.rst
> > +++ b/doc/guides/rel_notes/release_22_07.rst
> > @@ -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_rand_float()`` to provide a pseudo-random
> > + floating point number.
> > +
> >
> > Removed Items
> > -------------
> > diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> > index 4535cc980cec..024c3c41dc16 100644
> > --- a/lib/eal/common/rte_random.c
> > +++ b/lib/eal/common/rte_random.c
> > @@ -6,6 +6,7 @@
> > #include <x86intrin.h>
> > #endif
> > #include <unistd.h>
> > +#include <ieee754.h>
> >
>
> Is this API available in FreeBSD? In Windows?
>
> > #include <rte_branch_prediction.h>
> > #include <rte_cycles.h>
> > @@ -173,6 +174,26 @@ rte_rand_max(uint64_t upper_bound)
> > return res;
> > }
> >
> > +double
> > +rte_rand_float(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 */
> > + val = __rte_rand_lfsr258(state);
> > + u.ieee.mantissa0 = val >> 32; /* only 20 bits used */
> > + u.ieee.mantissa1 = (uint32_t)val;
>
> Why do you have a cast here, and not for mantissa0? Both will incur a
> 64->32 conversion, assuming unsigned int is 32-bit (which it is on all
> platforms DPDK supports?).
Yes, cast there is unnecessary. A little concerned that some compiler
might complain about loss of precision.
>
> The naive implementation (i.e., (double)rte_rand() / (double)UINT64_MAX)
> would cost a floating point multiplication. That's why you access the
> underlying IEEE754 bits directly. Correct?
Yes, avoiding floating point math, and directly setting bits keeps full precision.
next prev parent reply other threads:[~2022-05-25 15:45 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-24 18:46 [RFT 0/2] pie: floating point fixes Stephen Hemminger
2022-05-24 18:46 ` [RFT 1/2] rte_pie: remove unnecessary floating point Stephen Hemminger
2022-05-24 18:46 ` [RFT 2/2] rte_pie: fix incorrect floating point math Stephen Hemminger
2022-05-24 19:31 ` [RFT 0/2] pie: floating point fixes Morten Brørup
2022-05-24 22:18 ` [RFT v2 0/3] pie: fix random number issues Stephen Hemminger
2022-05-24 22:18 ` [RFT v2 1/3] random: add rte_rand_float() Stephen Hemminger
2022-05-25 11:55 ` Ray Kinsella
2022-05-25 14:45 ` Mattias Rönnblom
2022-05-25 15:26 ` Morten Brørup
2022-05-25 15:45 ` Stephen Hemminger [this message]
2022-05-25 15:47 ` Stephen Hemminger
2022-05-24 22:18 ` [RFT v2 2/3] rte_pie: remove unnecessary floating point Stephen Hemminger
2022-05-24 22:18 ` [RFT v2 3/3] rte_pie: fix incorrect floating point math Stephen Hemminger
2022-05-25 17:12 ` [PATCH v3 0/3] introduce random floating point function Stephen Hemminger
2022-05-25 17:12 ` [PATCH v3 1/3] random: add rte_drand() funciton Stephen Hemminger
2022-05-25 17:12 ` [PATCH v3 2/3] rte_pie: remove unnecessary floating point Stephen Hemminger
2022-05-25 17:12 ` [PATCH v3 3/3] rte_pie: fix incorrect floating point math Stephen Hemminger
2022-05-25 20:31 ` [PATCH v4 0/3] introduce random floating point function Stephen Hemminger
2022-05-25 20:31 ` [PATCH v4 1/3] random: add rte_drand() function Stephen Hemminger
2022-05-26 9:56 ` Ray Kinsella
2022-05-26 13:20 ` Mattias Rönnblom
2022-05-26 15:25 ` Stephen Hemminger
2022-05-26 15:28 ` Stephen Hemminger
2022-05-26 20:19 ` Stephen Hemminger
2022-05-25 20:31 ` [PATCH v4 2/3] rte_pie: remove unnecessary floating point Stephen Hemminger
2022-05-25 20:31 ` [PATCH v4 3/3] rte_pie: fix incorrect floating point math Stephen Hemminger
2022-05-26 7:06 ` [PATCH v4 0/3] introduce random floating point function Morten Brørup
2022-05-26 20:26 ` [PATCH v5 " Stephen Hemminger
2022-05-26 20:26 ` [PATCH v5 1/3] random: add rte_drand() function Stephen Hemminger
2022-05-26 20:26 ` [PATCH v5 2/3] rte_pie: remove unnecessary floating point Stephen Hemminger
2022-05-30 11:50 ` Dumitrescu, Cristian
2022-06-21 8:18 ` Singh, Jasvinder
2022-05-26 20:26 ` [PATCH v5 3/3] rte_pie: fix incorrect floating point math Stephen Hemminger
2022-05-30 11:50 ` Dumitrescu, Cristian
2022-06-21 8:18 ` Singh, Jasvinder
2022-06-22 9:21 ` [PATCH v5 0/3] introduce random floating point function Thomas Monjalon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220525084547.13fd5f9f@hermes.local \
--to=stephen@networkplumber.org \
--cc=dev@dpdk.org \
--cc=mattias.ronnblom@ericsson.com \
--cc=mdr@ashroe.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.