On 2026-07-30T18:40:37, Alexey Charkov <[email protected]> wrote: > lib: math64: sync <linux/math64.h> with Linux v7.1 > > U-Boot's copy was last synced with Linux in 2017 by commit 0342e335ba88 > ("lib: div64: sync with Linux"). Since then it has only been touched > twice locally, and both times in ways that increased the divergence > rather than tracking upstream. Take the file from the v7.1 tag. > > The immediate motivation is DIV64_U64_ROUND_UP(). Commit 3adc17f60bf8 > ("lib: div64: Add support for round up of div64_u64") added it inside > the #if BITS_PER_LONG == 64 branch, whereas Linux defines it > unconditionally after that block. Any 32-bit user therefore fails to > compile: > > error: implicit declaration of function 'DIV64_U64_ROUND_UP'; > did you mean 'DIV_ROUND_UP'? > > This has been surfaced while reworking fractional PLL logic for Rockchip > RK3588/RK3576, which unexpectedly failed to compile on 32-bit variants [1]. > > Newly available as a result: DIV64_U64_ROUND_UP() on 32-bit, > DIV_U64_ROUND_UP(), DIV64_U64_ROUND_CLOSEST(), DIV_U64_ROUND_CLOSEST(), > DIV_S64_ROUND_CLOSEST(), roundup_u64(), add_u64_u32() and > mul_s64_u64_shr(), plus upstream's kernel-doc for everything that was > already here and __always_inline on mul_u64_u32_shr()/mul_u64_u64_shr(). > > The file is otherwise a verbatim copy of v7.1. Four deviations: > > - The includes. U-Boot has no <linux/math.h>, no <asm/div64.h> and no > <vdso/math64.h>. do_div() comes from U-Boot's <div64.h>, > BITS_PER_LONG from <linux/bitops.h>, and abs64() from > <linux/kernel.h>, which is where U-Boot keeps the rest of what > upstream puts in <linux/math.h>. > > - mul_s64_u64_shr() uses abs64() rather than abs(), because U-Boot's > abs() evaluates its argument as int for anything that is not a long. > > - mul_u64_add_u64_div_u64() and the mul_u64_u64_div_u64() and > mul_u64_u64_div_u64_roundup() macros built on it are not ported. > They would need ~110 lines of lib/math/div64.c brought along, plus a > u128 type and OPTIMIZER_HIDE_VAR(), and U-Boot has no users for them > yet. > > - abs_diff(), added locally by commit f1f4a1d1d835 ("compat: math64: > add abs_diff()"), is kept. Upstream has it in <linux/math.h>. > > Build tested for evb-rk3288-rk808 (32-bit LE), generic-rk3576 (64-bit > LE), malta (32-bit BE, which is what compiles the __BIG_ENDIAN union > paths in mul_u64_u64_shr() and mul_u64_u32_div()), sandbox and > tools-only, with both gcc and clang. All six DIV*_ROUND_* macros plus > roundup_u64(), mul_s64_u64_shr(), add_u64_u32() and abs_diff() were > additionally instantiated on 32-bit ARM to confirm they now resolve. > > Closes: > https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/#3731361 > [1] > Signed-off-by: Alexey Charkov <[email protected]> > > include/linux/math64.h | 182 ++++++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 171 insertions(+), 11 deletions(-)
Reviewed-by: Simon Glass <[email protected]>
