Re: 64-bit math in the kernel

Werner Almesberger (
Tue, 26 Nov 1996 17:25:31 +0100 (MET)

Albert Cahalan wrote:
> What is the best way to divide a __u64 by a __u32 in the kernel?
> The result will fit in a __u32.

In one of my ATM drivers, I have the following non-portable and therefore
ugly solution (inspired by arch/i386/kernel/time.c:do_fast_gettimeoffset):

* zatm_dev->factor = (interval << TIMER_SHIFT)/
* (ticks-zatm_dev->last_clk);
#define S(x) #x /* "stringification" ... */
#define SX(x) S(x)
asm("movl %2,%%ebx\n\t"
"subl %3,%%ebx\n\t"
"xorl %%edx,%%edx\n\t"
"shldl $" SX(TIMER_SHIFT) ",%1,%%edx\n\t"
"shl $" SX(TIMER_SHIFT) ",%1\n\t"
"divl %%ebx\n\t"
: "=eax" (zatm_dev->factor)
: "eax" (interval-diff),"g" (ticks),
"g" (zatm_dev->last_clk)
: "ebx","edx","cc");
#undef S
#undef SX

> It seems gcc requires libgcc to do that, but libgcc is not available.

Right, I didn't dare to link the kernel with libgcc either. I think it
would be a very good idea to have basic 64 bit arithmetic available in
the kernel too.

- Werner

 / Werner Almesberger, DI-LRC,EPFL,CH /