coreboot/payloads/libpayload/drivers
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
..
i8042 libpayload: Fix clang fallthrough warning 2023-08-28 02:12:25 +00:00
serial libpayload: Fix compiler warnings 2022-11-21 21:08:30 +00:00
storage libpayload: Add x86_64 (64-bit) support 2024-05-26 01:26:31 +00:00
timer libpayload: timer: Revert timer_hz() return type to 64-bits 2024-02-08 21:07:13 +00:00
udc libpayload: Fix compiler warnings 2022-11-21 21:08:30 +00:00
usb libpayload: usbmsc: Correctly deal with disks larger than 2TB 2025-05-02 23:24:25 +00:00
video cbgfx: Prevent divide-by-zero edge case in Lanczos kernel 2025-05-06 21:04:04 +00:00
cbmem_console.c libpayload/drivers/cbmem_console: Use C99 flexible arrays 2023-08-01 12:40:07 +00:00
hid.c
Makefile.mk payloads: Rename Makefiles from .inc to .mk 2024-01-24 10:15:10 +00:00
mouse_cursor.c
nvram.c payloads: Drop unneeded empty lines 2020-09-21 16:20:57 +00:00
options.c libpayload: use PRIu64 type to print u64 2020-09-30 10:16:44 +00:00
pci_io_ops.c libpayload/pci: Add support for bus mapping 2022-05-20 02:51:33 +00:00
pci_map_bus_ops.c libpayload/pci: Add support for bus mapping 2022-05-20 02:51:33 +00:00
pci_ops.c libpayload/pci: Add support for bus mapping 2022-05-20 02:51:33 +00:00
pci_qcom.c libpayload/pci: Add pci_map_bus function for Qualcomm platform 2022-08-01 18:04:54 +00:00
pcie_mediatek.c libpayload/pci: Add pci_map_bus function for MediaTek platform 2022-05-20 02:53:35 +00:00
speaker.c