Subject: Re: [PATCH V13 5/7] rust: Make __udivdi3() and __umoddi3() panic

From: David Laight <[email-masked]>
Date: Sun, 26 Apr 2026 23:04:08 +0100

On Sun, 26 Apr 2026 15:20:31 +0100
"Gary Guo" <[email-masked]> wrote:

> On Sun Apr 26, 2026 at 8:52 AM BST, Mukesh Kumar Chaurasiya wrote:
> > On Fri, Apr 24, 2026 at 01:43:43PM +0100, Gary Guo wrote:  
> >> On Fri Apr 24, 2026 at 6:47 AM BST, Mukesh Kumar Chaurasiya (IBM) wrote:  
> >> > From: Link Mauve <[email-masked]>
> >> >
> >> > The core crate currently depends on these two functions for i64/u64/
> >> > i128/u128/core::time::Duration formatting, but we shouldn’t use that in
> >> > the kernel so let’s panic if they are ever called.
> >> >
> >> > This doesn’t yet fix drm_panic_qr.rs, which also uses __udivdi3 when
> >> > CONFIG_CC_OPTIMIZE_FOR_SIZE=y, but at least makes the rest of the kernel
> >> > build on PPC32.  
> >> 
> >> Can we always build libcore with `-C opt-level=2` even if
> >> `CONFIG_CC_OPTIMIZE_FOR_SIZE` is specified? It feels like a better fix than
> >> stubbing things out.
> >> 
> >> Best,
> >> Gary
> >>   
> > The issue is not coming from libcore itself. It's the driver that's
> > causing this.  
> 
> Sorry. I quoted the wrong part. I was asking if compiling libcore with O2 gets
> rid of its use of the builtins, as that's what the change this commit is for.
> 
> Formatting of u64 will be needed, so we should make sure that these works as
> intended.

This code (from nolibc) will convert u64 to ascii in any base:

#define _U64TOA_RECIP(base) ((base) & 1 ? ~0ull / (base) : (1ull << 63) / ((base) / 2))
static int _u64toa_base(u64 in, char *buffer, unsigned int base, u64 recip)
{
	unsigned int digits = 0;
	unsigned int dig;
	u64 q;
	char *p;
 
	/* Generate least significant digit first */
 	do {

#if defined(__SIZEOF_INT128__) && !defined(__mips__)
		q = ((unsigned __int128)in * recip) >> 64;
#else
		u64 p = (u32)in * (recip >> 32);
		q = (in >> 32) * (recip >> 32) + (p >> 32);
		p = (u32)p + (in >> 32) * (u32)recip;
		q += p >> 32;
#endif
		dig = in - q * base;
		/* Correct for any rounding errors */
		if (dig >= base) {
			dig -= base;
			q++;
 		}

		if (dig > 9)
			dig += 'a' - '0' - 10;
		buffer[digits++] = '0' + dig;
	} while ((in = q));
 
 	buffer[digits] = 0;

	/* Order reverse to result */
	for (p = buffer + digits - 1; p > buffer; buffer++, p--) {
		dig = *buffer;
		*buffer = *p;
		*p = dig;
	}

	return digits;
}

int u64toa_r(u64 in, char *buffer)
{
	return _u64toa_base(in, buffer, 10, _U64TOA_RECIP(10));
}

Not hard to do without any divides at all.

	David