2024-03-02 20:54:53

by Uwe Kleine-König

[permalink] [raw]
Subject: [PATCH] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b

As indicated in the added comment, the algorithm works better if b is
big. As multiplication is commutative, a and b can be swapped. Do this
If a is bigger than b.

Signed-off-by: Uwe Kleine-König <[email protected]>
---
lib/math/div64.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/lib/math/div64.c b/lib/math/div64.c
index 55a81782e271..baf6f8681907 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -190,6 +190,23 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)

/* can a * b overflow ? */
if (ilog2(a) + ilog2(b) > 62) {
+ /*
+ * Note that the algorithm after the if block below might loose
+ * some precision and the result is more exact for b > a. So
+ * exchange a and b if a is bigger than b.
+ *
+ * For example with a = 43980465100800, b = 100000000, c = 1000000000
+ * the below calculation doesn't modify b at all because div == 0
+ * and then shift becomes 45 + 26 - 62 = 9 and so the result
+ * becomes 4398035251080. However with a and b swapped the exact
+ * result is calculated (i.e. 4398046510080).
+ */
+ if (a > b) {
+ u64 tmp = a;
+ a = b;
+ b = tmp;
+ }
+
/*
* (b * a) / c is equal to
*

base-commit: 1870cdc0e8dee32e3c221704a2977898ba4c10e8
--
2.43.0



2024-03-02 21:05:25

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [PATCH] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b

On 02.03.2024 21:54:27, Uwe Kleine-König wrote:
> As indicated in the added comment, the algorithm works better if b is
> big. As multiplication is commutative, a and b can be swapped. Do this
> If a is bigger than b.
>
> Signed-off-by: Uwe Kleine-König <[email protected]>
> ---
> lib/math/div64.c | 17 +++++++++++++++++
> 1 file changed, 17 insertions(+)
>
> diff --git a/lib/math/div64.c b/lib/math/div64.c
> index 55a81782e271..baf6f8681907 100644
> --- a/lib/math/div64.c
> +++ b/lib/math/div64.c
> @@ -190,6 +190,23 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
>
> /* can a * b overflow ? */
> if (ilog2(a) + ilog2(b) > 62) {
> + /*
> + * Note that the algorithm after the if block below might loose
> + * some precision and the result is more exact for b > a. So
> + * exchange a and b if a is bigger than b.
> + *
> + * For example with a = 43980465100800, b = 100000000, c = 1000000000
> + * the below calculation doesn't modify b at all because div == 0
> + * and then shift becomes 45 + 26 - 62 = 9 and so the result
> + * becomes 4398035251080. However with a and b swapped the exact
> + * result is calculated (i.e. 4398046510080).
> + */
> + if (a > b) {
> + u64 tmp = a;
> + a = b;
> + b = tmp;

You can use swap() from linux/minmax.h here.

Marc

> + }
> +
> /*
> * (b * a) / c is equal to
> *
>
> base-commit: 1870cdc0e8dee32e3c221704a2977898ba4c10e8
> --
> 2.43.0
>
>
>

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |


Attachments:
(No filename) (1.74 kB)
signature.asc (499.00 B)
Download all attachments

2024-03-02 21:14:51

by Randy Dunlap

[permalink] [raw]
Subject: Re: [PATCH] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b



On 3/2/24 13:05, Marc Kleine-Budde wrote:
> On 02.03.2024 21:54:27, Uwe Kleine-König wrote:
>> As indicated in the added comment, the algorithm works better if b is
>> big. As multiplication is commutative, a and b can be swapped. Do this
>> If a is bigger than b.
>>
>> Signed-off-by: Uwe Kleine-König <[email protected]>
>> ---
>> lib/math/div64.c | 17 +++++++++++++++++
>> 1 file changed, 17 insertions(+)
>>
>> diff --git a/lib/math/div64.c b/lib/math/div64.c
>> index 55a81782e271..baf6f8681907 100644
>> --- a/lib/math/div64.c
>> +++ b/lib/math/div64.c
>> @@ -190,6 +190,23 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
>>
>> /* can a * b overflow ? */
>> if (ilog2(a) + ilog2(b) > 62) {
>> + /*
>> + * Note that the algorithm after the if block below might loose

s/loose/lose/

>> + * some precision and the result is more exact for b > a. So
>> + * exchange a and b if a is bigger than b.
>> + *
>> + * For example with a = 43980465100800, b = 100000000, c = 1000000000
>> + * the below calculation doesn't modify b at all because div == 0
>> + * and then shift becomes 45 + 26 - 62 = 9 and so the result
>> + * becomes 4398035251080. However with a and b swapped the exact
>> + * result is calculated (i.e. 4398046510080).
>> + */
>> + if (a > b) {
>> + u64 tmp = a;
>> + a = b;
>> + b = tmp;
>
> You can use swap() from linux/minmax.h here.
>
> Marc
>
>> + }
>> +
>> /*
>> * (b * a) / c is equal to
>> *
>>
>> base-commit: 1870cdc0e8dee32e3c221704a2977898ba4c10e8
>> --
>> 2.43.0
>>
>>
>>
>

--
#Randy

2024-03-03 08:29:42

by Biju Das

[permalink] [raw]
Subject: RE: [PATCH] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b

Hi Uwe,

Thanks for the patch.

> -----Original Message-----
> From: Uwe Kleine-König <[email protected]>
> Sent: Saturday, March 2, 2024 8:54 PM
> Subject: [PATCH] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b
>
> As indicated in the added comment, the algorithm works better if b is big. As multiplication is
> commutative, a and b can be swapped. Do this If a is bigger than b.
>
> Signed-off-by: Uwe Kleine-König <[email protected]>

Tested-by: Biju Das <[email protected]>

Cheers,
Biju

> ---
> lib/math/div64.c | 17 +++++++++++++++++
> 1 file changed, 17 insertions(+)
>
> diff --git a/lib/math/div64.c b/lib/math/div64.c index 55a81782e271..baf6f8681907 100644
> --- a/lib/math/div64.c
> +++ b/lib/math/div64.c
> @@ -190,6 +190,23 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
>
> /* can a * b overflow ? */
> if (ilog2(a) + ilog2(b) > 62) {
> + /*
> + * Note that the algorithm after the if block below might loose
> + * some precision and the result is more exact for b > a. So
> + * exchange a and b if a is bigger than b.
> + *
> + * For example with a = 43980465100800, b = 100000000, c = 1000000000
> + * the below calculation doesn't modify b at all because div == 0
> + * and then shift becomes 45 + 26 - 62 = 9 and so the result
> + * becomes 4398035251080. However with a and b swapped the exact
> + * result is calculated (i.e. 4398046510080).
> + */
> + if (a > b) {
> + u64 tmp = a;
> + a = b;
> + b = tmp;
> + }
> +
> /*
> * (b * a) / c is equal to
> *
>
> base-commit: 1870cdc0e8dee32e3c221704a2977898ba4c10e8
> --
> 2.43.0