From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 76403A046B for ; Fri, 28 Jun 2019 11:02:00 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 81B571B9B2; Fri, 28 Jun 2019 11:01:41 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id DCB9E1B99A for ; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712490; x=1564304490; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=CMG/8U/KE3q5YcCNGl5DRWSxMs9JxUK6mx9QHy4Aug8=; b=fncbLOa7z6HiFvPt0hKVNgb/btM1gGY8rIcv8BakwZfAp0mqVePpYkjnLyKEehTm JVTGPJt6OFZUQMxNMd+e81xW/MYkbuBK6K8Y3SOVwKpuFFyvPLdOO5IMM0ziVVw7 3A+KRGeiH1iHop/1psLQA92XYw1HpC1Ltl4Q4Antxso=; X-AuditID: c1b4fb3a-6f1ff7000000189f-f7-5d15d76ac059 Received: from ESESBMB503.ericsson.se (Unknown_Domain [153.88.183.116]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 84.64.06303.A67D51D5; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) Received: from ESESBMR506.ericsson.se (153.88.183.202) by ESESBMB503.ericsson.se (153.88.183.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from ESESBMB501.ericsson.se (153.88.183.168) by ESESBMR506.ericsson.se (153.88.183.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.184) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Fri, 28 Jun 2019 11:01:30 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x5S91SYY019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= To: CC: , , , , , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:23 +0200 Message-ID: <20190628090124.16849-5-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpkkeLIzCtJLcpLzFFi42KZGbG9RDfrumiswZ9JAhY3VtlbbF/RxWbx 7tN2JotbzSfZLBbfkbP49OAEiwObx68FS1k9Fu95yeRx7OY0do+ek/OYPN7vu8rmceX7asYA tigum5TUnMyy1CJ9uwSujAs7m9kKHihUfDxZ2MDYIt3FyMkhIWAisfb6DjYQW0jgKKPEp+VV XYxcQPY3Rol7W9sY4Zztx35AORcZJTo372GHcC4zSjxYtwasn03AU2Lyu24WEFtEQEhi6cfL YEXMAtcYJb5eesEMkhAWCJS4deAFWBGLgKrEurP3GEFsXgEniRNrdzJBHCUvsXrDAbB6TgFn ia61vewQB1ZL/DzxkA2iXlDi5MwnYHOYBTQlWrf/Zoew5SWat85mhqjXkri/5AvzBEbhWUha ZiFpmYWkZQEj8ypG0eLU4uLcdCMjvdSizOTi4vw8vbzUkk2MwIg5uOW31Q7Gg88dDzEKcDAq 8fCm7haNFWJNLCuuzD3EKMHBrCTCK3lOJFaINyWxsiq1KD++qDQntfgQozQHi5I473rvfzFC AumJJanZqakFqUUwWSYOTqkGRnv1J135mWEmR183/rzbtPhXclHm6aA7U9MOXV8X2cU5sTzl 27aXmqd7BQ527LLJK15wvUcs44jY39MOLhMdE16ZCc6Wu6+jr7Rxkc4a/vd7nnTMemi2d/ef Fe/Fb+5yPs//Z4f81L5TGw6bmM36VdLt6v7ln3v5W6lrZV8XCazlNropdLnbtU+JpTgj0VCL uag4EQCoPdn4lAIAAA== Subject: [dpdk-dev] [PATCH v4 4/5] eal: introduce random generator function with upper bound X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add a function rte_rand_max() which generates an uniformly distributed pseudo-random number less than a user-specified upper bound. The commonly used pattern rte_rand() % SOME_VALUE creates biased results (as in some values in the range are more frequently occurring than others) if SOME_VALUE is not a power of 2. Signed-off-by: Mattias Rönnblom Acked-by: Bruce Richardson --- doc/guides/rel_notes/release_19_08.rst | 5 ++- lib/librte_eal/common/include/rte_random.h | 18 ++++++++++ lib/librte_eal/common/rte_random.c | 39 ++++++++++++++++++++++ lib/librte_eal/rte_eal_version.map | 1 + 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/doc/guides/rel_notes/release_19_08.rst b/doc/guides/rel_notes/release_19_08.rst index 7080d1f47..f1c83d603 100644 --- a/doc/guides/rel_notes/release_19_08.rst +++ b/doc/guides/rel_notes/release_19_08.rst @@ -109,6 +109,10 @@ New Features higher-quality pseudo-random numbers (including full 64 bit support) and improved performance. + In addition, is extended with a new function + rte_rand_max() which supplies unbiased, bounded pseudo-random + numbers. + Removed Items ------------- @@ -125,7 +129,6 @@ Removed Items * build: armv8 crypto extension is disabled. - API Changes ----------- diff --git a/lib/librte_eal/common/include/rte_random.h b/lib/librte_eal/common/include/rte_random.h index 66dfe8ae7..939e6aaa9 100644 --- a/lib/librte_eal/common/include/rte_random.h +++ b/lib/librte_eal/common/include/rte_random.h @@ -17,6 +17,8 @@ extern "C" { #include +#include + /** * Seed the pseudo-random generator. * @@ -47,6 +49,22 @@ rte_srand(uint64_t seedval); uint64_t rte_rand(void); +/** + * Generates a pseudo-random number with an upper bound. + * + * This function returns an uniformly distributed (unbiased) random + * number less than a user-specified maximum value. + * + * If called from lcore threads, this function is thread-safe. + * + * @param upper_bound + * The upper bound of the generated number. + * @return + * A pseudo-random value between 0 and (upper_bound-1). + */ +uint64_t __rte_experimental +rte_rand_max(uint64_t upper_bound); + #ifdef __cplusplus } #endif diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index e53d96d18..3d9b9b7d8 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -137,6 +137,45 @@ rte_rand(void) return __rte_rand_lfsr258(state); } +uint64_t __rte_experimental +rte_rand_max(uint64_t upper_bound) +{ + struct rte_rand_state *state; + uint8_t ones; + uint8_t leading_zeros; + uint64_t mask = ~((uint64_t)0); + uint64_t res; + + if (unlikely(upper_bound < 2)) + return 0; + + state = __rte_rand_get_state(); + + ones = __builtin_popcountll(upper_bound); + + /* Handle power-of-2 upper_bound as a special case, since it + * has no bias issues. + */ + if (unlikely(ones == 1)) + return __rte_rand_lfsr258(state) & (upper_bound - 1); + + /* The approach to avoiding bias is to create a mask that + * stretches beyond the request value range, and up to the + * next power-of-2. In case the masked generated random value + * is equal to or greater than the upper bound, just discard + * the value and generate a new one. + */ + + leading_zeros = __builtin_clzll(upper_bound); + mask >>= leading_zeros; + + do { + res = __rte_rand_lfsr258(state) & mask; + } while (unlikely(res >= upper_bound)); + + return res; +} + static uint64_t __rte_random_initial_seed(void) { diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index 20c1a9018..a53a29a35 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -384,6 +384,7 @@ EXPERIMENTAL { rte_mp_request_async; rte_mp_sendmsg; rte_option_register; + rte_rand_max; rte_realloc_socket; rte_service_lcore_attr_get; rte_service_lcore_attr_reset_all; -- 2.17.1