coreboot/payloads
Julius Werner c1bcb43f7c cbgfx: Prevent divide-by-zero edge case in Lanczos kernel
The existing lanczos_weight() implementation naively follows the purely
mathematical definition for the `x == 0` special case. However, the
point of defining that special case is obviously to prevent division by
zero in the general case formula. Unfortunately we are still doing some
multiplications with `x` before we get to the division step, and our
fpmath library loses precision during multiplication. This can lead to
edge cases where `x` is not zero but `x_times_pi` later ends up being 0,
which causes the division to throw an exception after all. (I guess
we've just been lucky to not see this case in practice for now... it
requires the output pixel coordinate to be extremely close to but not
quite on the next input pixel coordinate, which may be rare in practice
with our scaling algorithms.)

This patch fixes the issue by implementing the special case later and
checking if `x_times_pi` is zero instead. Note that as long as we pass
this check, we can be confident that the division cannot fail even
though fpdiv() also truncates the divisor: this is because `x_times_pi`
was calculated from an fpmul() call with the constant fppi(), which has
34 significant bits. Even if x is the smallest possible non-zero value
after scaling for multiplication, the result `x_times_pi` must still
have 18 significant bits. That means it can be scaled down a further 16
bits for division without becoming zero.

Also add a simple unit test forcing exactly this condition to ensure the
code will not regress.

Change-Id: I2f212ee5df38252e97ec55aba3d2d25320c4b102
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/87532
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Reviewed-by: Jakub "Kuba" Czapiga <czapiga@google.com>
2025-05-06 21:04:04 +00:00
..
coreinfo libpayload: Set reasonable arch defaults 2024-08-27 09:06:58 +00:00
external payloads/external/edk2: Set StatusD register to work around failing AMD boot 2025-04-27 21:32:00 +00:00
libpayload cbgfx: Prevent divide-by-zero edge case in Lanczos kernel 2025-05-06 21:04:04 +00:00
linuxcheck treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
nvramcui libpayload: Set reasonable arch defaults 2024-08-27 09:06:58 +00:00
Kconfig payloads/Kconfig: Fix dependencies of PAYLOAD_FIT_SUPPORT 2024-08-27 11:32:05 +00:00
Makefile.mk payloads: Add leanefi payload 2024-06-04 00:26:14 +00:00