diff mbox series

eal/reciprocal: fix off by one when divisor is 32bit

Message ID 20190903191645.1700-1-pbhagavatula@marvell.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers show
Series eal/reciprocal: fix off by one when divisor is 32bit | expand

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/iol-dpdk_compile_ovs success Compile Testing PASS
ci/iol-dpdk_compile_spdk success Compile Testing PASS
ci/iol-dpdk_compile success Compile Testing PASS
ci/intel-Performance success Performance Testing PASS
ci/mellanox-Performance success Performance Testing PASS

Commit Message

Pavan Nikhilesh Bhagavatula Sept. 3, 2019, 7:16 p.m. UTC
From: Pavan Nikhilesh <pbhagavatula@marvell.com>

Fix off by one error in 64bit reciprocal division when divisor is 32bit.

Fixes: 6d45659eacb8 ("eal: add u64-bit variant for reciprocal divide")
Cc: stable@dpdk.org

Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
---

Example:
   Division failed, 17358247066007716387/244 =
   		expected 71140356827900476 result 71140356827900477
   Division failed, 17541123788887206374/41475 =
   		expected 422932460250444 result 422932460250445

 lib/librte_eal/common/rte_reciprocal.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

--
2.23.0

Comments

David Marchand Oct. 15, 2019, 8:45 a.m. UTC | #1
On Tue, Sep 3, 2019 at 9:17 PM <pbhagavatula@marvell.com> wrote:
>
> From: Pavan Nikhilesh <pbhagavatula@marvell.com>
>
> Fix off by one error in 64bit reciprocal division when divisor is 32bit.
>
> Fixes: 6d45659eacb8 ("eal: add u64-bit variant for reciprocal divide")
> Cc: stable@dpdk.org
>
> Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>

Any review?

Are we missing an update in the unit test to catch this issue?
Thanks.
Pavan Nikhilesh Bhagavatula Oct. 15, 2019, 8:56 a.m. UTC | #2
>-----Original Message-----
>From: David Marchand <david.marchand@redhat.com>
>Sent: Tuesday, October 15, 2019 2:16 PM
>To: Jerin Jacob Kollanukkaran <jerinj@marvell.com>; Stephen
>Hemminger <stephen@networkplumber.org>
>Cc: dev <dev@dpdk.org>; Pavan Nikhilesh Bhagavatula
><pbhagavatula@marvell.com>; dpdk stable <stable@dpdk.org>; Aaron
>Conole <aconole@redhat.com>
>Subject: Re: [dpdk-dev] [PATCH] eal/reciprocal: fix off by one
>when divisor is 32bit
>On Tue, Sep 3, 2019 at 9:17 PM <pbhagavatula@marvell.com> wrote:
>>
>> From: Pavan Nikhilesh <pbhagavatula@marvell.com>
>>
>> Fix off by one error in 64bit reciprocal division when divisor is 32bit.
>>
>> Fixes: 6d45659eacb8 ("eal: add u64-bit variant for reciprocal divide")
>> Cc: stable@dpdk.org
>>
>> Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
>
>Any review?
>
>Are we missing an update in the unit test to catch this issue?
>Thanks.

We actually caught it in a unit test
>test_reciprocal_division

>
>--
>David Marchand
David Marchand Oct. 15, 2019, 9:06 a.m. UTC | #3
On Tue, Oct 15, 2019 at 10:56 AM Pavan Nikhilesh Bhagavatula
<pbhagavatula@marvell.com> wrote:
>
>
>
> >-----Original Message-----
> >From: David Marchand <david.marchand@redhat.com>
> >Sent: Tuesday, October 15, 2019 2:16 PM
> >To: Jerin Jacob Kollanukkaran <jerinj@marvell.com>; Stephen
> >Hemminger <stephen@networkplumber.org>
> >Cc: dev <dev@dpdk.org>; Pavan Nikhilesh Bhagavatula
> ><pbhagavatula@marvell.com>; dpdk stable <stable@dpdk.org>; Aaron
> >Conole <aconole@redhat.com>
> >Subject: Re: [dpdk-dev] [PATCH] eal/reciprocal: fix off by one
> >when divisor is 32bit
> >On Tue, Sep 3, 2019 at 9:17 PM <pbhagavatula@marvell.com> wrote:
> >>
> >> From: Pavan Nikhilesh <pbhagavatula@marvell.com>
> >>
> >> Fix off by one error in 64bit reciprocal division when divisor is 32bit.
> >>
> >> Fixes: 6d45659eacb8 ("eal: add u64-bit variant for reciprocal divide")
> >> Cc: stable@dpdk.org
> >>
> >> Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
> >
> >Any review?
> >
> >Are we missing an update in the unit test to catch this issue?
> >Thanks.
>
> We actually caught it in a unit test
> >test_reciprocal_division

We had this problem since the very start then.

Both reciprocal_division and reciprocal_division_perf are in the "perf" list.
Can they be promoted to the standard list?
David Marchand Oct. 26, 2019, 2:33 p.m. UTC | #4
On Tue, Sep 3, 2019 at 9:17 PM <pbhagavatula@marvell.com> wrote:
>
> From: Pavan Nikhilesh <pbhagavatula@marvell.com>
>
> Fix off by one error in 64bit reciprocal division when divisor is 32bit.

Added log from unit test:

    RTE>>reciprocal_division
    Validating unsigned 32bit division.
    Validating unsigned 64bit division.
    Validating unsigned 64bit division with 32bit divisor.
    Division failed, 16983222950483802557/819 = expected 20736535959076681
    result 20736535959076682
    Validating division by power of 2.
    Test Failed

>
> Fixes: 6d45659eacb8 ("eal: add u64-bit variant for reciprocal divide")
> Cc: stable@dpdk.org
>
> Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>

Please continue working on enhancing the functional test so that it
can be part of the standard unit test list.
Thanks.
diff mbox series

Patch

diff --git a/lib/librte_eal/common/rte_reciprocal.c b/lib/librte_eal/common/rte_reciprocal.c
index f017d0c28..1c6d10e73 100644
--- a/lib/librte_eal/common/rte_reciprocal.c
+++ b/lib/librte_eal/common/rte_reciprocal.c
@@ -133,12 +133,15 @@  rte_reciprocal_value_u64(uint64_t d)
 {
 	struct rte_reciprocal_u64 R;
 	uint64_t m;
+	uint64_t r;
 	int l;

 	l = 63 - __builtin_clzll(d);

-	m = divide_128_div_64_to_64((1ULL << l), 0, d, NULL) << 1;
-	m = (1ULL << l) - d ? m + 2 : 1;
+	m = divide_128_div_64_to_64((1ULL << l), 0, d, &r) << 1;
+	if (r << 1 < r || r << 1 >= d)
+		m++;
+	m = (1ULL << l) - d ? m + 1 : 1;
 	R.m = m;

 	R.sh1 = l > 1 ? 1 : l;