Re: [PATCH] m68k: delay: Fix rounding of short udelay() and ndelay()
From: Graeme Harker
Date: Sat Dec 05 2026 - 03:28:14 EST
Hi Geert,
Finn has measured the patch on his LC 630 (thanks again, Finn) and has
left the remaining choice to you, so here is what I would like to send
as v2, if you are happy with it.
His numbers bear out the udelay() half: every udelay() is longer than
before and close to what was asked, the remaining 2% shortfall with
interrupts off being calibrate_delay(), which this patch does not touch.
For ndelay(), the patch is faster than the old code from 100 ns to 4 us
(1.17 against 2.49 us at 100 ns, 5.63 against 6.07 us at 4 us), and
never short, but it has one real flaw: the constant 5 that I copied from
x86 is 16.4% more than 2^32 / 10^9, so long ndelay()s run about 16% long
(46.9 against 39.2 us at 40 us). With his loops_per_jiffy of 107776 the
patch's formula predicts 46.6 us there.
For v2 I would keep udelay() as it is and change only the ndelay()
constant, using shifts so that it adds no multiply:
static inline void ndelay(unsigned long nsec)
{
/* 4 + 1/4 + 1/32 + 1/64 = 4.296875, close to 2^32 / 10^9 */
__xdelay(nsec * 4 + (nsec >> 2) + (nsec >> 5) + (nsec >> 6));
}
That is 0.044% over 2^32 / 10^9 rather than 16.4%. The shifts truncate,
so a very short ndelay() can ask for a fraction of a loop less than the
exact figure, under a nanosecond; __delay()'s extra loop covers that.
I have checked the loop counts on the host against the exact figure for
loops_per_jiffy of 17792 (my 68030), 107776 (Finn's LC040) and
170000000. As before, ndelay() is good up to about 250 ms, where
xloops * 4 overflows.
Short ndelay()s still pay for the 64-bit multiply in __xdelay(): on the
LC040 ndelay(20) went from 0.40 to 0.61 us. If you would rather avoid
that, the other choice is to drop the ndelay() change altogether and
send v2 as a udelay() fix only.
Would you like v2 with the shift form, or udelay() only? I'll wait for
your answer before sending either.
Graeme