Re: unresolved symbol: __udivdi3

Chris Caputo
2002-04-20T19:02:45Z

On Fri, 19 Apr 2002, Ben Greear wrote:
> Also, for what it's worth, do_div on x86 seems to corrupt arguments
> given to it, and may do other screwy things. I'm just going to
> go back to casting and let user-space do any precise division.

Or consider the code from:

http://nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html

Adapted as follows...

Chris

---

#define DIV 0
#define REM 1

// Function copied/adapted/optimized from:
//
// nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
//
// Copyright 1994, University of Cambridge Computer Laboratory
// All Rights Reserved.
//
// TODO: When running on a 64-bit CPU platform, this should no longer be
// TODO: necessary.
//
s64 divremdi3(s64 x,
s64 y,
int type)
{
u64 a = (x < 0) ? -x : x;
u64 b = (y < 0) ? -y : y;
u64 res = 0, d = 1;

if (b > 0) while (b < a) b <<= 1, d <<= 1;

do
{
if ( a >= b ) a -= b, res += d;
b >>= 1;
d >>= 1;
}
while (d);

if (DIV == type)
{
return (((x ^ y) & (1ll<<63)) == 0) ? res : -(s64)res;
}
else
{
return ((x & (1ll<<63)) == 0) ? a : -(s64)a;
}
}