From: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
To: Josef Schlehofer <pepe.schlehofer@gmail.com>,
u-boot@lists.u-boot-project.org,
Bradley Morgan <bradley.morgan@linux.dev>
Cc: "Tom Rini" <trini@konsulko.com>, "Marek Behún" <kabel@kernel.org>,
"Marek Mojík" <marek.mojik@nic.cz>, "Peng Fan" <peng.fan@nxp.com>,
"Priyanka Jain" <priyanka.jain@nxp.com>,
"Qiang Zhao" <qiang.zhao@nxp.com>,
"Shengzhou Liu" <Shengzhou.Liu@nxp.com>,
"Mario Six" <mario.six@gdsys.cc>,
"Dario Binacchi" <dario.binacchi@amarulasolutions.com>,
"Michael Trimarchi" <michael@amarulasolutions.com>,
"Heiko Schocher" <hs@nabladev.com>,
"Holger Brunck" <holger.brunck@hitachienergy.com>,
"Simon Glass" <sjg@chromium.org>,
"Bradley Morgan" <brads@mainlining.org>
Subject: Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc
Date: Thu, 19 Nov 2026 15:21:14 +0100 [thread overview]
Message-ID: <0576c843-db7d-4875-a974-0c497cf69059@kernel.org> (raw)
In-Reply-To: <20261119133310.22732-5-pepe.schlehofer@gmail.com>
Le 19/11/2026 à 14:33, Josef Schlehofer a écrit :
> On 32-bit PowerPC, GCC lowers '/' and '%' on unsigned 64-bit operands
> to calls to __udivdi3() and __umoddi3(). U-Boot's private libgcc lacks
> these routines, so turris_1x_sdcard fails to link with
> CONFIG_USE_PRIVATE_LIBGCC=y:
>
> undefined reference to `__udivdi3'
Why do we need __udivdi3() at all ? Can't we just avoid / and % on
64-bit operands and use __div64_32() or equivalent instead ?
Christophe
>
> Implement both with a copy of __div64_32() from lib/div64.c whose
> divisor is widened to 64 bits. The copy keeps the private libgcc
> independent of lib/, which not every xPL phase links.
>
> Build the file without LTO, as ARM does for eabi_compat.o: LTO can
> drop these functions before GCC generates the calls that need them.
>
> Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com>
> Reviewed-by: Bradley Morgan <brads@mainlining.org>
> Cc: Tom Rini <trini@konsulko.com>
> Cc: Marek Behún <kabel@kernel.org>
> ---
> Changes in v3:
> - name lib/div64.c as the source of udivmod64() more clearly
> - explain in the comment why the high word is only divided first when
> the divisor fits into 32 bits
>
> Changes in v2:
> - new patch
>
> At -Os the two take 300 bytes, against 1036 and 992 bytes of
> _udivdi3.o and _umoddi3.o in Debian's libgcc 14.2.
> ---
> arch/powerpc/lib/Makefile | 3 +-
> arch/powerpc/lib/udivdi3.c | 85 ++++++++++++++++++++++++++++++++++++++
> 2 files changed, 87 insertions(+), 1 deletion(-)
> create mode 100644 arch/powerpc/lib/udivdi3.c
>
> diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
> index 90f0e1e5fa9..23e62996701 100644
> --- a/arch/powerpc/lib/Makefile
> +++ b/arch/powerpc/lib/Makefile
> @@ -6,7 +6,8 @@
> ## Build a couple of necessary functions into a private libgcc
> ## if the user asked for it
> lib-$(CONFIG_USE_PRIVATE_LIBGCC) += _ashldi3.o _ashrdi3.o _lshrdi3.o \
> - crtsavres.o
> + crtsavres.o udivdi3.o
> +CFLAGS_REMOVE_udivdi3.o := $(LTO_CFLAGS)
>
> MINIMAL=
>
> diff --git a/arch/powerpc/lib/udivdi3.c b/arch/powerpc/lib/udivdi3.c
> new file mode 100644
> index 00000000000..4b02c61bcba
> --- /dev/null
> +++ b/arch/powerpc/lib/udivdi3.c
> @@ -0,0 +1,85 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * 64-bit unsigned division and modulo for the private libgcc
> + *
> + * GCC calls __udivdi3() and __umoddi3() for '/' and '%' on unsigned 64-bit
> + * operands on 32-bit PowerPC.
> + *
> + * udivmod64() is a copy of __div64_32() from lib/div64.c, changed to take a
> + * 64-bit divisor. Its shift-and-subtract loop already works with one. Like
> + * __div64_32(), the functions are not instrumented, as tracing code may
> + * divide. The copyright notices below are those of lib/div64.c.
> + *
> + * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
> + *
> + * Based on former do_div() implementation from asm-parisc/div64.h:
> + * Copyright (C) 1999 Hewlett-Packard Co
> + * Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
> + */
> +
> +#include <linux/types.h>
> +
> +static notrace u64 udivmod64(u64 n, u64 base, u64 *rem)
> +{
> + u64 b = base;
> + u64 res, d = 1;
> + u32 high = n >> 32;
> +
> + /*
> + * Divide the high word first, as __div64_32() does. This is a 32-bit
> + * division, so it is only valid when the divisor fits into 32 bits.
> + */
> + res = 0;
> + if (!(base >> 32) && high >= base) {
> + high /= (u32)base;
> + res = (u64)high << 32;
> + n -= (u64)(high * (u32)base) << 32;
> + }
> +
> + while ((s64)b > 0 && b < n) {
> + b = b + b;
> + d = d + d;
> + }
> +
> + do {
> + if (n >= b) {
> + n -= b;
> + res += d;
> + }
> + b >>= 1;
> + d >>= 1;
> + } while (d);
> +
> + *rem = n;
> + return res;
> +}
> +
> +/**
> + * __udivdi3() - divide two 64-bit unsigned integers
> + *
> + * @n: dividend
> + * @d: divisor
> + * Return: quotient
> + */
> +notrace u64 __udivdi3(u64 n, u64 d)
> +{
> + u64 rem;
> +
> + return udivmod64(n, d, &rem);
> +}
> +
> +/**
> + * __umoddi3() - remainder of the division of two 64-bit unsigned integers
> + *
> + * @n: dividend
> + * @d: divisor
> + * Return: remainder
> + */
> +notrace u64 __umoddi3(u64 n, u64 d)
> +{
> + u64 rem;
> +
> + udivmod64(n, d, &rem);
> +
> + return rem;
> +}
next prev parent reply other threads:[~2026-11-19 14:21 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 13:32 [PATCH v3 00/14] powerpc: Enable LTO on mpc85xx Josef Schlehofer
2026-11-19 13:32 ` [PATCH v3 01/14] board: nxp: p1_p2_rdb_pc, p1010rdb: Drop unused SPL gd copy Josef Schlehofer
2026-11-19 13:32 ` [PATCH v3 02/14] powerpc: Make the global data pointer LTO safe Josef Schlehofer
2026-11-19 13:32 ` [PATCH v3 03/14] powerpc: lib: Add GPR save and restore routines to the private libgcc Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 04/14] powerpc: lib: Add 64-bit division " Josef Schlehofer
2026-11-19 14:21 ` Christophe Leroy (CS GROUP) [this message]
2026-11-19 16:01 ` Bradley Morgan
2026-11-19 18:42 ` Josef Schlehofer
2026-11-19 18:55 ` Bradley Morgan
2026-11-20 9:08 ` Josef Schlehofer
2026-11-20 16:04 ` Bradley Morgan
2026-11-20 16:16 ` Tom Rini
2026-11-20 16:18 ` Bradley Morgan
2026-11-19 20:09 ` Tom Rini
2026-11-19 13:33 ` [PATCH v3 05/14] powerpc: mpc85xx: Use the private libgcc with LTO Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 06/14] powerpc: mpc85xx: Discard LTO keep-syms sections Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 07/14] powerpc: Drop -ffunction-sections and -fdata-sections with LTO Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 08/14] powerpc: mpc85xx: Allow LTO Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 09/14] xpl: Pass the make jobserver to the xPL link Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 10/14] configs: turris_1x: Enable LTO Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 11/14] mtd: rawnand: fsl_elbc: Select NAND_ECC_BCH Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 12/14] board: CZ.NIC: turris_1x: Make NVMe and SCSI boot targets conditional Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 13/14] configs: turris_1x_nor: Enable CMD_UBI, drop NVMe and SCSI Josef Schlehofer
2026-11-19 13:33 ` [PATCH v3 14/14] board: CZ.NIC: turris_1x: Fix up MACs and serial number from OTP Josef Schlehofer
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=0576c843-db7d-4875-a974-0c497cf69059@kernel.org \
--to=chleroy@kernel.org \
--cc=Shengzhou.Liu@nxp.com \
--cc=bradley.morgan@linux.dev \
--cc=brads@mainlining.org \
--cc=dario.binacchi@amarulasolutions.com \
--cc=holger.brunck@hitachienergy.com \
--cc=hs@nabladev.com \
--cc=kabel@kernel.org \
--cc=marek.mojik@nic.cz \
--cc=mario.six@gdsys.cc \
--cc=michael@amarulasolutions.com \
--cc=peng.fan@nxp.com \
--cc=pepe.schlehofer@gmail.com \
--cc=priyanka.jain@nxp.com \
--cc=qiang.zhao@nxp.com \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.u-boot-project.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