ldiv: Handle a == LONG_MIN.

* lib/mes/div.c (ldiv): Handle a == LONG_MIN.
This commit is contained in:
Danny Milosavljevic 2019-03-11 21:14:11 +01:00 committed by Jan Nieuwenhuizen
parent fba1e15f02
commit b1090422c3
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
1 changed files with 38 additions and 11 deletions

View File

@ -64,15 +64,42 @@ unsigned long __mesabi_uldiv(unsigned long a, unsigned long b, unsigned long* re
ldiv_t ldiv(long a, long b)
{
ldiv_t result;
int negative_result = (a < 0) ^ (b < 0);
assert(a != LONG_MIN && b != LONG_MIN);
if (a < 0)
a = -a;
if (b < 0)
b = -b;
result.quot = __mesabi_uldiv(a, b, &result.rem);
if (negative_result)
result.quot = -result.quot;
return result;
int negate_result = (a < 0) ^ (b < 0);
assert(b != LONG_MIN);
if (a != LONG_MIN)
{
if (a < 0)
a = -a;
if (b < 0)
b = -b;
result.quot = __mesabi_uldiv(a, b, &result.rem);
if (negate_result)
result.quot = -result.quot;
return result;
}
else
{
result.rem = 0;
if (b < 0)
b = -b;
if (b == 1)
{
result.quot = a;
/* Since result.quot is already negative, don't negate it again. */
negate_result = !negate_result;
}
else if (b == 0)
__mesabi_div0();
else
{
long x;
for (x = 0; a <= -b; a += b)
++x;
result.rem = -a;
result.quot = x;
}
if (negate_result)
result.quot = -result.quot;
return result;
}
}