<p>Some code that rounds up the division to demonstrate (C-syntax):</p>

<pre><code>#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 );
}
</code></pre>

<p>Now, if this were User-space we probably wouldn't even notice that our compiler is generating code for those operators (e.g. <code>divdi3()</code> for division). Chances are we link with <code>libgcc</code> without even knowing it. The problem is that Kernel-space is different (e.g. no <code>libgcc</code>). What to do?</p>

<p>Crawl Google for a while, notice that pretty much everyone addresses the unsigned variant:</p>

<pre><code>#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 );
}
</code></pre>

<p>I know how to fix this one: Override <code>udivdi3()</code> and <code>umoddi3()</code> with <code>do_div()</code> from <em>asm/div64.h</em>. Done right? Wrong. Signed is not the same as unsigned, <code>sdivdi3()</code> does not simply call <code>udivdi3()</code>, they are separate functions for a reason.</p>

<p>Have you solved this problem? Do you know of a library that will help me do this? I'm really stuck so whatever you might see here that I just don't right now would be really helpful.</p>

<p>Thanks,
Chad</p>

[{'answer_id': 35468, 'author': 'Mat Noguchi', 'author_id': 1799, 'author_profile': 'https://Stackoverflow.com/users/1799', 'pm_score': 0, 'selected': False, 'text': '<p><code>ldiv</code> ?</p>\n\n<p>Edit: reread title, so you might want to ignore this. Or not, depending on if it has an appropriate non-library version.</p>\n'}, {'answer_id': 35472, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 2, 'selected': False, 'text': "<p>Here's my really naive solution. Your mileage may vary.</p>\n\n<p>Keep a sign bit, which is <code>sign(dividend) ^ sign(divisor)</code>. (Or <code>*</code>, or <code>/</code>, if you're storing your sign as 1 and -1, as opposed to false and true. Basically, negative if either one is negative, positive if none or both are negative.)</p>\n\n<p>Then, call the unsigned division function on the absolute values of both. Then tack the sign back onto the result.</p>\n\n<p>P.S. That is actually how <code>__divdi3</code> is implemented in <code>libgcc2.c</code> (from GCC 4.2.3, the version that's installed on my Ubuntu system). I just checked. :-)</p>\n"}, {'answer_id': 39889, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t think (at least can\'t find a way to make) <a href="https://stackoverflow.com/questions/35463/how-to-divide-two-64-bit-numbers-in-linux-kernel#35472">Chris\' answer</a> work in this case because <strong><em>do_div()</em></strong> actually changes the dividend in-place. Getting the absolute value implies a temporary variable whose value will change the way I require but can\'t be passed out of my <strong><em>__divdi3()</em></strong> override. </p>\n\n<p>I don\'t see a way around the parameter-by-value signature of <strong><em>__divdi3()</em></strong> at this point except to mimic the technique used by <strong><em>do_div()</em></strong>.</p>\n\n<p>It might seem like I\'m bending over backwards here and should just come up with an algorithm to do the 64-bit/32-bit division I actually need. The added complication here though is that I have a bunch of numerical code using the \'/\' operator and would need to go through that code and replace every \'/\' with my function calls.</p>\n\n<p>I\'m getting desperate enough to do just that though.</p>\n\n<p>Thanks for any follow-up,\nChad</p>\n'}, {'answer_id': 196172, 'author': 'colgur', 'author_id': 1059, 'author_profile': 'https://Stackoverflow.com/users/1059', 'pm_score': 3, 'selected': False, 'text': '<p>This functionality is introduced in <a href="http://lxr.linux.no/linux+v2.6.22/lib/div64.c" rel="noreferrer">/linux/lib/div64.c</a> as early as kernel v2.6.22.</p>\n'}]
['https://Stackoverflow.com/questions/35463', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']