Re: [PATCH v3 1/2] mul_u64_u64_div_u64: make it precise always

From: Uwe Kleine-König
Date: Mon Jul 08 2024 - 02:07:40 EST


On Sun, Jul 07, 2024 at 03:05:19PM -0400, Nicolas Pitre wrote:
> From: Nicolas Pitre <npitre@xxxxxxxxxxxx>
>
> Library facilities must always return exact results. If the caller may
> be contented with approximations then it should do the approximation on
> its own.
>
> In this particular case the comment in the code says "the algorithm
> ... below might lose some precision". Well, if you try it with e.g.:
>
> a = 18446462598732840960
> b = 18446462598732840960
> c = 18446462598732840961
>
> then the produced answer is 0 whereas the exact answer should be
> 18446462598732840959. This is _some_ precision lost indeed!
>
> Let's reimplement this function so it always produces the exact result
> regardless of its inputs while preserving existing fast paths
> when possible.
>
> Signed-off-by: Nicolas Pitre <npitre@xxxxxxxxxxxx>
> Tested-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxx>
> Tested-by: Biju Das <biju.das.jz@xxxxxxxxxxxxxx>

I would have wished that parts of the conversation about v2 made it into
code comments. Anyhow, *I* have understood the code, so:

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxx>

Best regards
Uwe

Attachment: signature.asc
Description: PGP signature