From: Andrew Morton <akpm@linux-foundation.org>
To: Alexandru Ardelean <aardelean@baylibre.com>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
jic23@kernel.org, bartosz.golaszewski@linaro.org,
gregkh@linuxfoundation.org
Subject: Re: [PATCH v2 1/2] util_macros.h: fix/rework find_closest() macros
Date: Tue, 5 Nov 2024 15:08:26 -0800 [thread overview]
Message-ID: <20241105150826.86b0a8f2c0df2a4822b07757@linux-foundation.org> (raw)
In-Reply-To: <20241105145406.554365-1-aardelean@baylibre.com>
On Tue, 5 Nov 2024 16:54:05 +0200 Alexandru Ardelean <aardelean@baylibre.com> wrote:
> A bug was found in the find_closest() (find_closest_descending() is also
> affected after some testing), where for certain values with small
> progressions, the rounding (done by averaging 2 values) causes an incorrect
> index to be returned.
Convincing changelog, thanks.
> -#define find_closest(x, a, as) __find_closest(x, a, as, <=)
> +#define find_closest(x, a, as) \
> +({ \
> + typeof(as) __fc_i, __fc_as = (as) - 1; \
> + long __fc_mid_x, __fc_x = (x); \
> + long __fc_left, __fc_right; \
> + typeof(*a) const *__fc_a = (a); \
> + for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \
> + __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2; \
> + if (__fc_x <= __fc_mid_x) { \
> + __fc_left = __fc_x - __fc_a[__fc_i]; \
> + __fc_right = __fc_a[__fc_i + 1] - __fc_x; \
> + if (__fc_right < __fc_left) \
> + __fc_i++; \
> + break; \
> + } \
> + } \
> + (__fc_i); \
> +})
>
> ...
>
> +#define find_closest_descending(x, a, as) \
Boy these things are hard to read. They're also bloaty and I'm
counting 36ish callsites!
Can we fix both issues by just giving up on the macro approach and
reimplement them in out-of-line C code? All the sites I looked at are
using 32-bit quantities - a mix of signed and unsigned.
It's separate from this bugfix of course, but would it be feasible for
someone to go switch all callers to use u32's then reimplement these in
lib/find_closest.c?
next prev parent reply other threads:[~2024-11-05 23:08 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-05 14:54 [PATCH v2 1/2] util_macros.h: fix/rework find_closest() macros Alexandru Ardelean
2024-11-05 14:54 ` [PATCH v2 2/2] lib: util_macros_kunit: add kunit test for util_macros.h Alexandru Ardelean
2024-11-05 23:08 ` Andrew Morton [this message]
2024-11-06 14:03 ` [PATCH v2 1/2] util_macros.h: fix/rework find_closest() macros Alexandru Ardelean
2024-11-06 20:08 ` Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241105150826.86b0a8f2c0df2a4822b07757@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=aardelean@baylibre.com \
--cc=bartosz.golaszewski@linaro.org \
--cc=gregkh@linuxfoundation.org \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox