From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-DM3-obe.outbound.protection.outlook.com (mail-dm3nam03on0078.outbound.protection.outlook.com [104.47.41.78]) by dpdk.org (Postfix) with ESMTP id DA6E71C152 for ; Wed, 4 Apr 2018 15:20:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=CAVIUMNETWORKS.onmicrosoft.com; s=selector1-cavium-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=UttcQhGZ1PdbMI+xGgYXSP3kdZ/6tD/zZEl0DJSzoEY=; b=fEUZ52QNgs0+um966H+ivNShxz6j2mKp5IP7nz0mg7iHcKPDZonR73hGM32FB/GY6x8IHT31dheZe4KxJo1h+W3ZjRWhihnm0r/zH4x06QK5n82GmA7JxO7VIOntCiE+vNQlpYQ7EHOLEcEfOKkClg9nC3xPMUugGuBQrZrUNzQ= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Pavan.Bhagavatula@cavium.com; Received: from ltp-pvn.caveonetworks.com (111.93.218.67) by DM5PR07MB3467.namprd07.prod.outlook.com (2603:10b6:4:67::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.631.10; Wed, 4 Apr 2018 13:20:34 +0000 From: Pavan Nikhilesh To: jerin.jacob@caviumnetworks.com, matan@mellanox.com, keith.wiles@intel.com, thomas@monjalon.net Cc: dev@dpdk.org, Pavan Nikhilesh Date: Wed, 4 Apr 2018 18:50:16 +0530 Message-Id: <20180404132016.13754-1-pbhagavatula@caviumnetworks.com> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180217104934.17291-1-pbhagavatula@caviumnetworks.com> References: <20180217104934.17291-1-pbhagavatula@caviumnetworks.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [111.93.218.67] X-ClientProxiedBy: CO2PR04CA0123.namprd04.prod.outlook.com (2603:10b6:104:7::25) To DM5PR07MB3467.namprd07.prod.outlook.com (2603:10b6:4:67::22) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 50b3cc60-a2d4-45ca-75ad-08d59a2eda39 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(2017052603328)(7153060)(7193020); SRVR:DM5PR07MB3467; X-Microsoft-Exchange-Diagnostics: 1; DM5PR07MB3467; 3:XrCZNKcyK6oJcJX5fIzPdcDx4cCGp+/aFvPK5QtrC50trcEQABQ7bAGAvB0dtdesAw7Saig0CHpZiIs47uoPhMOhiZjV5fLjJTcRb7uv8i9fECDfARpYC1u/OaBKn0yMld4i5UrhFfxNobollZ6AgRstyd8PUXLB7tP/Zn3BXtS3QYbvs3cKdy1o6D8Jtfa0AGMcuXbTywC6FhRbdwK47FObJZPctLq7SkCWubdIGOnR/TWBJRMavs1EI/XrW0/L; 25:16IQtuW9e3wagRDJ6gkCmanEK7gSHWvxVp6WKeR20lGlXWBzI7N6wUC5MUbG4/DiawDoXLDHZ0rJeeKILbaZHkgbqfEPJ0XNceRrJmKurrsf2s7denwlWE69KEDACDophkXZONfolGR+6MDCZd5wxwzQTvE52IxF3qEMPVNV/7ScqGGDAiEeuvXVjvk6Yi+Su7aBcFHXzvDj4SbrUT8MOMoriUuIaxcR03azJjCDMUHWGtxp25UdLXr9fcawu+/L1TwRQCAqWHmZz6IbQtoUFEOEnlntDLgFhPMleyMpmJRks9iDmZbnucPaFjv1Hi7/Ul2+UsaioRkuKrJOScrGOQ==; 31:jz8kochxVUuyPemNf6yQv+PgpQT8xVhXZLGzcFL7hkfqZG/bZWbNYiDZVzawVsECBJDH7zXrZcdA+OjMmxx6EzSAGc6sJwb376T0ejSBC34IoH/xWNggmszVn+BJzExfk9oNhrKUJ5ynJe/CKb8OE0i56+yxukvsd1wYx4WPLiZ1EzV8AOQ4aWp7/xfwTdi5R/ZRvyQIJ2t6p+b4Y+aBadouSRcmKtaRz0nU91+dlPo= X-MS-TrafficTypeDiagnostic: DM5PR07MB3467: X-Microsoft-Exchange-Diagnostics: 1; DM5PR07MB3467; 20:Gb48VNHrqCClyrpk2/JchT/4LjPlUCgRpRAaHOmimUU2Jfr8zCVEqgoq7WNZ2NZfiYsTCUJEcRYmAeGvPzFicSwzewvwQ4NUUy6B1vqsPVLZ/fHJcDQwigXtY9tN8Npc0gVLd9ruWo6tsd9eY013J3x3Vubsk3qUz2/+RDVsGZ5H5Y4FJRWcfreMSMVJ7hUQLs0KC3X6AetKM/NR0vAofLOZsUxGfbDOVpK8QKYeMd1ZsyUYLOvJRv70JVZlYuVnF5UHpVaIWaosLCUF7hL7r6N2LHsAsaUMRU5N1OjkWhC/ctM55+vzQVaFcrKHyoYZ7wG0YPzi6DXfn/78ONnthflxMKvr5twtYhX6MHmL4fAII76ky5n5TjvuCvYkwmCn+CZNAmGzuXyxLNonWu/hKoFW90dx5eRRwGkSiQuDee0QVLA+yEZhM1XDbbOZOyHJF3fMzOXTimwry8UFGMgYbHz3TVsPdpGf+2AdpXxLSirfrRq0Zi3v01YOYQlAjJqZLRHXYwV1sU/2NdKYcGTo2MbIcdTXeqHG/E3p4z3SDe8dugxtonscIk+teIlQZsi//XW4N8MISazlmJr1Ev55vFS7nxw8YsQh6U4pFGSYr6o=; 4:PCWU9BtqqlQzNLatF6T+VJbrQ9rdz8nlf+1spKpUnNxy4Tvs+9kl/ZIlaXkCrAAnMY9hrGOqKaHvOf3IkCx2K33C4i7q7zTYQe4HzaLxs38aj8+K8cz/rkpjRFbCojcF1DPES5sZLTL7HmIutI3dd0nlP0d3DJP5RzpoSMudmL1b+Y44DvWwx0XZKrvxORTp4eXwLNnp6ekmmeYYFhfBI2cid7DJDxqHp6AGAGdhfwnhqtVly5Rt5bl9GoL/QtJfOCsrX+QPRVe2QiTomTQBoA== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(93006095)(3002001)(10201501046)(3231221)(944501327)(52105095)(6041310)(20161123560045)(20161123558120)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(6072148)(201708071742011); SRVR:DM5PR07MB3467; BCL:0; PCL:0; RULEID:; SRVR:DM5PR07MB3467; X-Forefront-PRVS: 0632519F33 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(39860400002)(39380400002)(396003)(376002)(346002)(366004)(199004)(189003)(106356001)(305945005)(5660300001)(51416003)(52116002)(476003)(48376002)(8936002)(59450400001)(81156014)(81166006)(2616005)(1857600001)(8676002)(446003)(107886003)(50466002)(7736002)(386003)(16586007)(42882007)(50226002)(76176011)(486006)(316002)(36756003)(4326008)(26005)(6506007)(68736007)(11346002)(956004)(6116002)(3846002)(72206003)(6512007)(53936002)(97736004)(1076002)(16526019)(66066001)(2906002)(47776003)(25786009)(6666003)(478600001)(53416004)(5009440100003)(6486002)(69596002)(105586002)(42262002); DIR:OUT; SFP:1101; SCL:1; SRVR:DM5PR07MB3467; H:ltp-pvn.caveonetworks.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: None (protection.outlook.com: cavium.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DM5PR07MB3467; 23:jSeFml+V8GGhJsUiq7pB2j+dq4NYvz3ETRaeLWV/J?= =?us-ascii?Q?fdzoBlYwbpOJ8Wipm+dg6uihK7qrwQj2BgAwXVxi/sTZaVdeHesNzn9zVleX?= =?us-ascii?Q?qgerwnaZI4UNzQMDS0omj23WKUSpbid3usjYbMCVuSBdFCOIhhG/6/3jIPT5?= =?us-ascii?Q?f7PRTwu7VuyzG3WagS3jc0g+faNLbBkZfcY2P2X9X16zrU+xaMRxm1alpZJD?= =?us-ascii?Q?N8xoJBT5EXBV922toSjjUjE9vdJwbUBRFLIEuaoA+nsVjoV6/UUWSvf/j9L+?= =?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B6XFEvDErTgyIds9rMVccfp5L?= =?us-ascii?Q?dww099x4MikGca9ozUr4ay6xVnAMQNGDZADRinx2N1MG8Sz9Fgfy1pS9sYTl?= =?us-ascii?Q?+OCtL/JfnBUGBH8RRI5Ti6psWu0Kv4fyQWOHVs7YpmZyCs0jaR/tdho7SDZm?= =?us-ascii?Q?73sDqo6L8G5qvIEWSKITPIhOFZH5VLA9U8KMpRUq6OnDBa9xr6Oud566NPOV?= =?us-ascii?Q?UrIGA=3D=3D?= X-Microsoft-Antispam-Message-Info: d/vMVPCy0EW8g3nLJD0UUP0HsqlAXPiR4b8dR6+lw0UhunaNhj5y5HMuE2eGZh2l+P8pT8Qu7u58kxaxyykmh028SkatxVw0/POP6QSGrBnmCv2MaQyUwDIW3M8M7536cD+AhRuFWcz9L0EXrn+dPPlUUiHpEf2/Q+j/xghN6AgiYg3C5HKYEal669PE8UdN X-Microsoft-Exchange-Diagnostics: 1; DM5PR07MB3467; 6:RGWjN4b0xYyuO8RYWedB5ikEAryOWfRjCOoXL5cUKxHC/aB2E06snFaUUHCLMYbFkEuAdr9eC5RxMovd/ogx5gB1zwN1fK8QKKokThpbc+dNZ/OWxVWKseerj2oaBprXj0W2lm/iK4+UleMpX+JiUVwlWTrnud5T/KtYut2WZk4G1M/o7CxSSQuXvSFIEJircNK1p3ue8GWwRMJbOz0Uq+ndGMbJqJ/rCb060izBw1UgfdCzKZITm6B2f3P5gfUZhmqdYQ/NxFgkf8vZK9gBKFP8B5NeCAjpau1ziExlxa3dDk9MG3oRft2zrjTX1UVdi0bQ4nxmEud+AEBlUbFNiKJ1zLa9HBer/GTEIIghlsPAWbRyUEuFaRJImsgPHSfqzvZyTSOGEIwvQxOtq1vDLj8Fj9khiXSEGYTfdgemYEaendPeWWen4QIyuhGcxd32E6B0uP6ucGNiG0H9TYVurw==; 5:mxdeYTY0r5lCe9r3DHacX/8mFJAP3cHrGSovYiYUmiskjocI+4jgByyCseXlqdqGvrvLnjAE3OqbC3WLtt+xIGgqAklyIMZku9EeUOGM49UF/7+kssOULTFgTrfQKS4UsACIpHBOkEYE7rmenIEBX5m6EgS4Cyp2rp794un+F4k=; 24:RVRPeLXpE6EKPmPINT1mqcwAZybHXRmkBQShmQhPZcXChJ4NR7WSrnNL4tMJf6ZhD+1KIEJoxmh578qGVt2VrEFXazZ9d2IJZfb5KLQOpyE= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM5PR07MB3467; 7:3aslgVL77IQkd6btJdepxo18yHdJSsz5gdm8kRhmui/xJtzQq73NC6U1vVNICmBTpw3kXrAXCkfXWGBv5ajQU90g6GKWbJMLWeSjYijQElez/TmzQtiQ1GIaFxuSsdY/eAKDfpwR+c1xwPGmrtF8q+I7EblkLNkTX4WtNYKYvSAmsLcfs5611ongEaG2Ps5823OJSv/lqNHjErrhTQEL+duashe9UboNxDIqUANkUH5NfHOwKJcgHtkcBD+x1ynm X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2018 13:20:34.0664 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 50b3cc60-a2d4-45ca-75ad-08d59a2eda39 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR07MB3467 Subject: [dpdk-dev] [PATCH v4] eal: add API to align integer to previous power of 2 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: , X-List-Received-Date: Wed, 04 Apr 2018 13:20:39 -0000 Add 32b and 64b API's to align the given integer to the previous power of 2. Update common auto test to include test for previous power of 2 for both 32 and 64bit integers. Signed-off-by: Pavan Nikhilesh --- v4 Changes: - Squash patchset into a single patch. - Use %PRIu64 instead of %lu lib/librte_eal/common/include/rte_common.h | 92 ++++++++++++++++++++++++++---- test/test/test_common.c | 26 +++++++++ 2 files changed, 107 insertions(+), 11 deletions(-) diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index c7803e41c..7e147dcf2 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -223,6 +223,51 @@ extern int RTE_BUILD_BUG_ON_detected_error; } while(0) #endif +/** + * Combines 32b inputs most significant set bits into the least + * significant bits to construct a value with the same MSBs as x + * but all 1's under it. + * + * @param x + * The integer whose MSBs need to be combined with its LSBs + * @return + * The combined value. + */ +static inline uint32_t +rte_combine32ms1b(register uint32_t x) +{ + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + + return x; +} + +/** + * Combines 64b inputs most significant set bits into the least + * significant bits to construct a value with the same MSBs as x + * but all 1's under it. + * + * @param v + * The integer whose MSBs need to be combined with its LSBs + * @return + * The combined value. + */ +static inline uint64_t +rte_combine64ms1b(register uint64_t v) +{ + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + + return v; +} + /*********** Macros to work with powers of 2 ********/ /** @@ -250,15 +295,28 @@ static inline uint32_t rte_align32pow2(uint32_t x) { x--; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; + x = rte_combine32ms1b(x); return x + 1; } +/** + * Aligns input parameter to the previous power of 2 + * + * @param x + * The integer value to algin + * + * @return + * Input parameter aligned to the previous power of 2 + */ +static inline uint32_t +rte_align32prevpow2(uint32_t x) +{ + x = rte_combine32ms1b(x); + + return x - (x >> 1); +} + /** * Aligns 64b input parameter to the next power of 2 * @@ -272,16 +330,28 @@ static inline uint64_t rte_align64pow2(uint64_t v) { v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v |= v >> 32; + v = rte_combine64ms1b(v); return v + 1; } +/** + * Aligns 64b input parameter to the previous power of 2 + * + * @param v + * The 64b value to align + * + * @return + * Input parameter aligned to the previous power of 2 + */ +static inline uint64_t +rte_align64prevpow2(uint64_t v) +{ + v = rte_combine64ms1b(v); + + return v - (v >> 1); +} + /*********** Macros for calculating min and max **********/ /** diff --git a/test/test/test_common.c b/test/test/test_common.c index d0342430f..7361693b8 100644 --- a/test/test/test_common.c +++ b/test/test/test_common.c @@ -3,6 +3,7 @@ */ #include +#include #include #include #include @@ -70,6 +71,9 @@ test_align(void) #define FAIL_ALIGN(x, i, p)\ {printf(x "() test failed: %u %u\n", i, p);\ return -1;} +#define FAIL_ALIGN64(x, j, q)\ + {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\ + return -1; } #define ERROR_FLOOR(res, i, pow) \ (res % pow) || /* check if not aligned */ \ ((res / pow) != (i / pow)) /* check if correct alignment */ @@ -80,6 +84,7 @@ test_align(void) val / pow != (i / pow) + 1) /* if not aligned, hence +1 */ uint32_t i, p, val; + uint64_t j, q; for (i = 1, p = 1; i <= MAX_NUM; i ++) { if (rte_align32pow2(i) != p) @@ -88,6 +93,27 @@ test_align(void) p <<= 1; } + for (i = 1, p = 1; i <= MAX_NUM; i++) { + if (rte_align32prevpow2(i) != p) + FAIL_ALIGN("rte_align32prevpow2", i, p); + if (rte_is_power_of_2(i + 1)) + p = i + 1; + } + + for (j = 1, q = 1; j <= MAX_NUM ; j++) { + if (rte_align64pow2(j) != q) + FAIL_ALIGN64("rte_align64pow2", j, q); + if (j == q) + q <<= 1; + } + + for (j = 1, q = 1; j <= MAX_NUM ; j++) { + if (rte_align64prevpow2(j) != q) + FAIL_ALIGN64("rte_align64prevpow2", j, q); + if (rte_is_power_of_2(j + 1)) + q = j + 1; + } + for (p = 2; p <= MAX_NUM; p <<= 1) { if (!rte_is_power_of_2(p)) -- 2.16.3