How to split two 64-bit numbers in a Linux kernel?

Some code that rounds off a division to demonstrate (C syntax):

#define SINT64 long long int
#define SINT32 long int

SINT64 divRound(SINT64 dividend, SINT64 divisor)
{
  SINT32 quotient1 = dividend / divisor;

  SINT32 modResult = dividend % divisor;
  SINT32 multResult = modResult * 2;
  SINT32 quotient2 = multResult / divisor;

  SINT64 result = quotient1 + quotient2;

  return ( result );
}

Now, if it were User-space, we probably would not even notice that our compiler generates code for these operators (for example, divdi3 () for division). Most likely, we contact "libgcc" without even knowing about it. The problem is that the kernel space is different (for example, there is no libgcc). What to do?

Crawl Google for a while, note that almost everyone accesses an unsigned option:

#define UINT64 long long int
#define UINT32 long int

UINT64 divRound(UINT64 dividend, UINT64 divisor)
{
  UINT32 quotient1 = dividend / divisor;

  UINT32 modResult = dividend % divisor;
  UINT32 multResult = modResult * 2;
  UINT32 quotient2 = multResult / divisor;

  UINT64 result = quotient1 + quotient2;

  return ( result );
}

I know how to fix this: override udivdi3 () and umoddi3 () with _do_div () _ from asm / div64.h. Correctly? Wrong. Signed does not match unsigned, sdivdi3 () _ does not just call udivdi3 (), they are separate functions for some reason.

? , ? , , .

,

+5
4

. .

, sign(dividend) ^ sign(divisor). ( * /, 1 -1, false true. , , , , , .)

unsigned division . .

P.S. , __divdi3 libgcc2.c ( GCC 4.2.3, , Ubuntu). .: -)

+4
+2

ldiv?

: , . , , .

0

( , ) , do_div() . , , , __ divdi3().

__ divdi3(), , , do_div().

It might seem that I'm leaning backwards and should just come up with an algorithm for doing 64-bit / 32-bit division, which I really need. However, the added complication is that I have a bunch of numerical code using the "/" operator, and you will need to go through that code and replace each "/" with my function calls.

I get desperation to do this though.

Thanks for any follow up Chad

0
source

All Articles