From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM01-BY2-obe.outbound.protection.outlook.com (mail-by2nam01on0081.outbound.protection.outlook.com [104.47.34.81]) by dpdk.org (Postfix) with ESMTP id 3868C7D04 for ; Mon, 4 Dec 2017 14:23:07 +0100 (CET) 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=ez3z8uonR4pJpM1RKDFueyuZkAn/MFnYGlqAlzXyK+o=; b=oVv0SGX+HvyeTkWBsgj07t2J1PtGFVpSyiZu/6XoXympLVlGZLmoPP0JtxYGXHzEoA4BJeAxh1DiG2rVwMQrWEB99kTilpC8nrM4KVagK5lfTM5NdDLKPScx2EYwoRwgckb3DefwaeIzVgS6bhY6CTTVvhj005eAinu3e3UOPSc= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Pavan.Bhagavatula@cavium.com; Received: from Pavan-LT.caveonetworks.com (111.93.218.67) by CY4PR07MB3461.namprd07.prod.outlook.com (10.171.252.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.282.5; Mon, 4 Dec 2017 13:23:03 +0000 From: Pavan Nikhilesh To: cristian.dumitrescu@intel.com, stephen@networkplumber.org, ktraynor@redhat.com Cc: dev@dpdk.org, Pavan Nikhilesh Date: Mon, 4 Dec 2017 18:52:31 +0530 Message-Id: <20171204132231.29975-3-pbhagavatula@caviumnetworks.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171204132231.29975-1-pbhagavatula@caviumnetworks.com> References: <1504032378-5483-1-git-send-email-pbhagavatula@caviumnetworks.com> <20171204132231.29975-1-pbhagavatula@caviumnetworks.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [111.93.218.67] X-ClientProxiedBy: DM5PR13CA0042.namprd13.prod.outlook.com (10.168.240.156) To CY4PR07MB3461.namprd07.prod.outlook.com (10.171.252.142) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d28f31b1-4679-4d60-db5c-08d53b1a273b X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603286); SRVR:CY4PR07MB3461; X-Microsoft-Exchange-Diagnostics: 1; CY4PR07MB3461; 3:R7DABHPJWHh60FMB4yBG9nSAqX4tDeeLSpo46JfP7QknBdY+J+SQvjwf/vTF6MeFwRkjo6cdrxF0E1edYEk1MzR+kEWSSEsBw3Lj7h9/CIQbfSgaK8ZExUoNeZEUPxIgh5JnDxwU8myw2cCkQ7F/bL5EfnWFrLTyPBaOo0tmwQqNuTT6Acf/tiUZo5g1OwbMA+1Jj3bNf9rgnGjeec4QMfvS12LADDEfJsjJe60SZf7NWU8tHeepvSzcorMrc7UF; 25:Iwx5tNTflLG6SnpCWqBbfLR4NnQ7RkbGQYhbujYVesIuZpeVbT9+4Auz0B6xy4LxgjaGw4gtphOaabW3plJiMApu7SjFIEARW0lk+Wsi5xCublcC4RW8S44bgDM1svDYocljda4y4edIhxl70MoNAnhSI9wqwdYmjafUpe8CT7i1OAnKYuAHLbgVyH+qInqFOo2vh83BEIL0cQsanwVutMd4NyIekP88dOkw+CmPyKCD9CJPhO1pTJxpqARJumh4Cp3VTODwnIaAC4BLmMqAgx8Z4RwHpXrDgUARLZPiNf+RJ0UoI5I2q8N4xFPQlvJhRyKM4V0vfbp/v/6TBEXHiA==; 31:O3TE5ujfTiuqQtFl9PtfsxTcageX/R8Kl00+62jtLPRhgTLxRHtuxuHkOXrzr4YouBrDnr+rBMRDk5FBiPtzdCn8hRUqsEO+dbSGqxpQy9cPt1UYaCKnZQ+tGSAVYQ7QsJbhIbAfo8Ph5bs9MKY8aCojAhunKHPZYl8KOPb8CBOSJ37k2vQtvym4YaHD6iUsOd7LSCG7KANojar0Xg3LcAK4SpjIokSJIfujenRNC6k= X-MS-TrafficTypeDiagnostic: CY4PR07MB3461: X-Microsoft-Exchange-Diagnostics: 1; CY4PR07MB3461; 20:aKZJeNDrPYtOYqd3FNGpVFWEGoDxMVZU4xE2U90sbxDb71CvfMTpTjEDA6QKOjz5zyecmH03iZGy7ie/lxN9euYs/TqOk0Pg8RbVS4wZtAOzB+/ueqqpm3nzSL2LrEshsEc9Ut5+p6M35cFJzhFeN6gLQeF6GRT1hGbBO+T1TLr1B2iw20HIGqpvrp4VESFguFI8RfPCnmsaNVYgY943br/1cpnoKixNiT2JTgxZQ1jk0RiUGV2PtSL2aCZLdj9XzCexL3/+x7dBitGbmblH2mEYeg6aq9A2YilWd7Y9WRcmAuMtK1Atuq+O9iC5QiIKCHT60P85+9tGaO+MgRcF0ZiLnfjoJg/Hk3sCUU35EK3xaCDpoyFisf+ZyspjJYYwI11cBrwXw/g4ZI6WpJXRtojknnhvKgLK9avaTcZ9vuFtyVeilYa7HPQrQprsn7gr+bpj/7EFpxFZP+7E8NBsjqb9I5gPajzgCnPEvv0JiG9FBRXKuen1n7sNKz1fvrdzfXWX6njJ6jxszphNf6bnS0ZGlnwp4ISARndaLIWKd3w/6u7vAfPzy7z8Me6LUzhm+Dt6eJefkkdXWU2yl2/+ademiJjcSZjSugJ1Ba3FyeI=; 4:nAnii7noROXuntrFhVP80nqLjk/9kS2TK/QIvYu0Vd+QYbzNGHQfz9lND1a79ydk1MdcSaFua4VTOW+w1tXOLiTpsNIMz4l4nv2E0yWcTsY9++3vKIAOAK7bG0V0TUsUPVOMmzGuDrWao7qjmjOlsDrZU1EpYOkOA0XXBavqvn0xWE3eGiVWXV6GNKQduY5quwGSYMp0SdtmchzWrTFSxQLG1Zet+5z7NtNZt6M75TdGXTucNVfyKj/6usFqiW4PSa94vwF4MMA/DUb7dNzuWg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(2401047)(8121501046)(5005006)(10201501046)(3231022)(3002001)(93006095)(6041248)(20161123555025)(20161123562025)(20161123560025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(6072148)(201708071742011); SRVR:CY4PR07MB3461; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:CY4PR07MB3461; X-Forefront-PRVS: 051158ECBB X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(979002)(6009001)(346002)(376002)(366004)(189002)(199003)(76176011)(48376002)(72206003)(3846002)(50466002)(2950100002)(42882006)(7736002)(52116002)(6666003)(51416003)(6116002)(6512007)(16586007)(16526018)(53936002)(101416001)(69596002)(68736007)(33646002)(107886003)(5009440100003)(316002)(305945005)(1076002)(97736004)(5660300001)(4326008)(53416004)(8936002)(189998001)(2906002)(105586002)(106356001)(6486002)(81166006)(81156014)(8676002)(478600001)(6506006)(36756003)(25786009)(50226002)(66066001)(47776003)(42262002)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR07MB3461; H:Pavan-LT.caveonetworks.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: cavium.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY4PR07MB3461; 23:phkYFeQqfyh6lKKhjqEhpDweZM0qSdl9RkqZhYTy4?= =?us-ascii?Q?ZVM5rq6V3f2HUYYVyejgRLobv2VuTBv8tL+v0vMRgk9ccFK+oQ/19COahyt7?= =?us-ascii?Q?sJnvGaBrsjh/X1DcMGMKtVlEKjdt/CrVwFwgHIwXSPmg6+GfadUw5V1FiAcA?= =?us-ascii?Q?QKPIEcyJIQ2bXUjtI3FOH4AX6u1RUIAOuji0U1SQPH90N0gM4WnqXdn3FrMA?= =?us-ascii?Q?/t5VkO/seu/GFabZCKRPwPV4duwfRz9V4uRgZOASvjFiFEsA5bVzLchn5KWP?= =?us-ascii?Q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HvfNRLixx+C/352psqyXp7fdk3B5RG0PgHP995wJZuHfGvGXwdqbWgbQSoK6nAKKXlBACZgEMx3PUGFasecvzRnA41tRxZzkIBQ4vRXopZ8JD3ODVq32hSgIl8KtKtMqLGqB6Oqewd6iMT485aTkNMfz0jhA7ix9CB2ckojvsyxfkfvDRM03mFvzdh SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2017 13:23:03.7621 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d28f31b1-4679-4d60-db5c-08d53b1a273b X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR07MB3461 Subject: [dpdk-dev] [PATCH v7 3/3] test: add tests for reciprocal based division 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: Mon, 04 Dec 2017 13:23:08 -0000 This commit provides a set of tests for verifying the correctness and performance of both unsigned 32 and 64bit reciprocal based division. Signed-off-by: Pavan Nikhilesh --- test/test/Makefile | 2 + test/test/test_reciprocal_division.c | 190 +++++++++++++++++++++++++ test/test/test_reciprocal_division_perf.c | 229 ++++++++++++++++++++++++++++++ 3 files changed, 421 insertions(+) create mode 100644 test/test/test_reciprocal_division.c create mode 100644 test/test/test_reciprocal_division_perf.c diff --git a/test/test/Makefile b/test/test/Makefile index bb54c9808..7f4aa67b9 100644 --- a/test/test/Makefile +++ b/test/test/Makefile @@ -95,6 +95,8 @@ SRCS-y += test_spinlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c +SRCS-y += test_reciprocal_division.c +SRCS-y += test_reciprocal_division_perf.c SRCS-y += test_ring.c SRCS-y += test_ring_perf.c diff --git a/test/test/test_reciprocal_division.c b/test/test/test_reciprocal_division.c new file mode 100644 index 000000000..75313c486 --- /dev/null +++ b/test/test/test_reciprocal_division.c @@ -0,0 +1,190 @@ +/* + * BSD LICENSE + * + * Copyright (C) Cavium, Inc. 2017. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Cavium, Inc nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "test.h" + +#include +#include +#include + +#include +#include +#include +#include + +#define MAX_ITERATIONS (1ULL << 32) +#define DIVIDE_ITER (100) + +static int +test_reciprocal(void) +{ + int result = 0; + uint32_t divisor_u32 = 0; + uint32_t dividend_u32; + uint32_t nresult_u32; + uint32_t rresult_u32; + uint64_t i; + uint64_t divisor_u64 = 0; + uint64_t dividend_u64; + uint64_t nresult_u64; + uint64_t rresult_u64; + struct rte_reciprocal reci_u32 = {0}; + struct rte_reciprocal_u64 reci_u64 = {0}; + + rte_srand(rte_rdtsc()); + printf("Validating unsigned 32bit division.\n"); + for (i = 0; i < MAX_ITERATIONS; i++) { + /* Change divisor every DIVIDE_ITER iterations. */ + if (i % DIVIDE_ITER == 0) { + divisor_u32 = rte_rand(); + reci_u32 = rte_reciprocal_value(divisor_u32); + } + + dividend_u32 = rte_rand(); + nresult_u32 = dividend_u32 / divisor_u32; + rresult_u32 = rte_reciprocal_divide(dividend_u32, + reci_u32); + if (nresult_u32 != rresult_u32) { + printf("Division failed, %"PRIu32"/%"PRIu32" = " + "expected %"PRIu32" result %"PRIu32"\n", + dividend_u32, divisor_u32, + nresult_u32, rresult_u32); + result = 1; + break; + } + } + + printf("Validating unsigned 64bit division.\n"); + for (i = 0; i < MAX_ITERATIONS; i++) { + /* Change divisor every DIVIDE_ITER iterations. */ + if (i % DIVIDE_ITER == 0) { + divisor_u64 = rte_rand(); + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + } + + dividend_u64 = rte_rand(); + nresult_u64 = dividend_u64 / divisor_u64; + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + if (nresult_u64 != rresult_u64) { + printf("Division failed, %"PRIu64"/%"PRIu64" = " + "expected %"PRIu64" result %"PRIu64"\n", + dividend_u64, divisor_u64, + nresult_u64, rresult_u64); + result = 1; + break; + } + } + + printf("Validating unsigned 64bit division with 32bit divisor.\n"); + for (i = 0; i < MAX_ITERATIONS; i++) { + /* Change divisor every DIVIDE_ITER iterations. */ + if (i % DIVIDE_ITER == 0) { + divisor_u64 = rte_rand() >> 32; + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + } + + dividend_u64 = rte_rand(); + + nresult_u64 = dividend_u64 / divisor_u64; + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + + if (nresult_u64 != rresult_u64) { + printf("Division failed, %"PRIu64"/%"PRIu64" = " + "expected %"PRIu64" result %"PRIu64"\n", + dividend_u64, divisor_u64, + nresult_u64, rresult_u64); + result = 1; + break; + } + } + + printf("Validating division by power of 2.\n"); + for (i = 0; i < 32; i++) { + divisor_u64 = 1ull << i; + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + reci_u32 = rte_reciprocal_value((uint32_t)divisor_u64); + + dividend_u64 = rte_rand(); + + nresult_u64 = dividend_u64 / divisor_u64; + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + + if (nresult_u64 != rresult_u64) { + printf("Division 64 failed, %"PRIu64"/%"PRIu64" = " + "expected %"PRIu64" result %"PRIu64"\n", + dividend_u64, divisor_u64, + nresult_u64, rresult_u64); + result = 1; + } + + nresult_u32 = (dividend_u64 >> 32) / divisor_u64; + rresult_u32 = rte_reciprocal_divide((dividend_u64 >> 32), + reci_u32); + + if (nresult_u32 != rresult_u32) { + printf("Division 32 failed, %"PRIu64"/%"PRIu64" = " + "expected %"PRIu64" result %"PRIu64"\n", + dividend_u64 >> 32, divisor_u64, + nresult_u64, rresult_u64); + result = 1; + break; + } + } + + for (; i < 64; i++) { + divisor_u64 = 1ull << i; + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + + dividend_u64 = rte_rand(); + + nresult_u64 = dividend_u64 / divisor_u64; + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + + if (nresult_u64 != rresult_u64) { + printf("Division failed, %"PRIu64"/%"PRIu64" = " + "expected %"PRIu64" result %"PRIu64"\n", + dividend_u64, divisor_u64, + nresult_u64, rresult_u64); + result = 1; + break; + } + + } + + return result; +} + +REGISTER_TEST_COMMAND(reciprocal_division, test_reciprocal); diff --git a/test/test/test_reciprocal_division_perf.c b/test/test/test_reciprocal_division_perf.c new file mode 100644 index 000000000..e759331de --- /dev/null +++ b/test/test/test_reciprocal_division_perf.c @@ -0,0 +1,229 @@ +/* + * BSD LICENSE + * + * Copyright (C) Cavium, Inc. 2017. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Cavium, Inc nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "test.h" + +#include +#include +#include + +#include +#include +#include +#include + +#define MAX_ITERATIONS (1ULL << 32) +#define DIVIDE_ITER (1ULL << 28) + +static int +test_reciprocal_division_perf(void) +{ + int result = 0; + uint32_t divisor_u32 = 0; + uint32_t dividend_u32; + uint64_t divisor_u64 = 0; + uint64_t dividend_u64; + volatile uint32_t nresult_u32; + volatile uint32_t rresult_u32; + volatile uint64_t nresult_u64; + volatile uint64_t rresult_u64; + uint64_t start_cyc; + uint64_t split_cyc; + uint64_t end_cyc; + uint64_t tot_cyc_n = 0; + uint64_t tot_cyc_r = 0; + uint64_t i; + struct rte_reciprocal reci_u32 = {0}; + struct rte_reciprocal_u64 reci_u64 = {0}; + + rte_srand(rte_rdtsc()); + + printf("Validating unsigned 32bit division.\n"); + for (i = 0; i < MAX_ITERATIONS; i++) { + /* Change divisor every DIVIDE_ITER iterations. */ + if (i % DIVIDE_ITER == 0) { + divisor_u32 = rte_rand(); + reci_u32 = rte_reciprocal_value(divisor_u32); + } + + dividend_u32 = rte_rand(); + + start_cyc = rte_rdtsc(); + nresult_u32 = dividend_u32 / divisor_u32; + split_cyc = rte_rdtsc(); + rresult_u32 = rte_reciprocal_divide(dividend_u32, + reci_u32); + end_cyc = rte_rdtsc(); + + tot_cyc_n += split_cyc - start_cyc; + tot_cyc_r += end_cyc - split_cyc; + if (nresult_u32 != rresult_u32) { + printf("Division failed, expected %"PRIu32" " + "result %"PRIu32"", + nresult_u32, rresult_u32); + result = 1; + break; + } + } + printf("32bit Division results:\n"); + printf("Total number of cycles normal division : %"PRIu64"\n", + tot_cyc_n); + printf("Total number of cycles reciprocal division : %"PRIu64"\n", + tot_cyc_r); + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n\n", + ((double)tot_cyc_r)/i); + + tot_cyc_n = 0; + tot_cyc_r = 0; + + printf("Validating unsigned 64bit division.\n"); + for (i = 0; i < MAX_ITERATIONS; i++) { + /* Change divisor every DIVIDE_ITER iterations. */ + if (i % DIVIDE_ITER == 0) { + divisor_u64 = rte_rand(); + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + } + + dividend_u64 = rte_rand(); + + start_cyc = rte_rdtsc(); + nresult_u64 = dividend_u64 / divisor_u64; + split_cyc = rte_rdtsc(); + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + end_cyc = rte_rdtsc(); + + tot_cyc_n += split_cyc - start_cyc; + tot_cyc_r += end_cyc - split_cyc; + if (nresult_u64 != rresult_u64) { + printf("Division failed, expected %"PRIu64" " + "result %"PRIu64"", + nresult_u64, rresult_u64); + result = 1; + break; + } + } + printf("64bit Division results:\n"); + printf("Total number of cycles normal division : %"PRIu64"\n", + tot_cyc_n); + printf("Total number of cycles reciprocal division : %"PRIu64"\n", + tot_cyc_r); + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n\n", + ((double)tot_cyc_r)/i); + + tot_cyc_n = 0; + tot_cyc_r = 0; + + printf("Validating unsigned 64bit division with 32bit divisor.\n"); + for (i = 0; i < MAX_ITERATIONS; i++) { + /* Change divisor every DIVIDE_ITER iterations. */ + if (i % DIVIDE_ITER == 0) { + divisor_u64 = rte_rand() >> 32; + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + } + + dividend_u64 = rte_rand(); + + start_cyc = rte_rdtsc(); + nresult_u64 = dividend_u64 / divisor_u64; + split_cyc = rte_rdtsc(); + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + end_cyc = rte_rdtsc(); + + tot_cyc_n += split_cyc - start_cyc; + tot_cyc_r += end_cyc - split_cyc; + if (nresult_u64 != rresult_u64) { + printf("Division failed, expected %"PRIu64" " + "result %"PRIu64"", + nresult_u64, rresult_u64); + result = 1; + break; + } + } + + printf("64bit Division results:\n"); + printf("Total number of cycles normal division : %"PRIu64"\n", + tot_cyc_n); + printf("Total number of cycles reciprocal division : %"PRIu64"\n", + tot_cyc_r); + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n\n", + ((double)tot_cyc_r)/i); + + tot_cyc_n = 0; + tot_cyc_r = 0; + + printf("Validating division by power of 2.\n"); + for (i = 0; i < 64; i++) { + divisor_u64 = 1ull << i; + reci_u64 = rte_reciprocal_value_u64(divisor_u64); + + dividend_u64 = rte_rand(); + + start_cyc = rte_rdtsc(); + nresult_u64 = dividend_u64 / divisor_u64; + split_cyc = rte_rdtsc(); + rresult_u64 = rte_reciprocal_divide_u64(dividend_u64, + &reci_u64); + end_cyc = rte_rdtsc(); + + tot_cyc_n += split_cyc - start_cyc; + tot_cyc_r += end_cyc - split_cyc; + if (nresult_u64 != rresult_u64) { + printf("Division 64 failed, %"PRIu64"/%"PRIu64" = " + "expected %"PRIu64" result %"PRIu64"\n", + dividend_u64, divisor_u64, + nresult_u64, rresult_u64); + result = 1; + break; + } + } + printf("64bit Division results:\n"); + printf("Total number of cycles normal division : %"PRIu64"\n", + tot_cyc_n); + printf("Total number of cycles reciprocal division : %"PRIu64"\n", + tot_cyc_r); + printf("Cycles per division(normal) : %3.2f\n", + ((double)tot_cyc_n)/i); + printf("Cycles per division(reciprocal) : %3.2f\n", + ((double)tot_cyc_r)/i); + + return result; +} + +REGISTER_TEST_COMMAND(reciprocal_division_perf, test_reciprocal_division_perf); -- 2.14.1