Re: 64-bit math in the kernel

Werner Almesberger (almesber@lrc.epfl.ch)
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   werner.almesberger@lrc.di.epfl.ch /
/_IN_R_133__Tel_+41_21_693_6621__Fax_+41_21_693_6610_____________________/