ldiv: Make rem < 0 if a < 0.

* lib/mes/div.c (ldiv): Make rem < 0 if a < 0.
This commit is contained in:
Danny Milosavljevic 2019-03-11 22:02:19 +01:00 committed by Jan Nieuwenhuizen
parent b1090422c3
commit 554da18fac
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
1 changed files with 8 additions and 3 deletions

View File

@ -60,7 +60,9 @@ unsigned long __mesabi_uldiv(unsigned long a, unsigned long b, unsigned long* re
}
}
/* Note: Rounds towards zero */
/* Note: Rounds towards zero.
Maintainer: Be careful to satisfy quot * b + rem == a.
That means that rem can be negative. */
ldiv_t ldiv(long a, long b)
{
ldiv_t result;
@ -68,13 +70,16 @@ ldiv_t ldiv(long a, long b)
assert(b != LONG_MIN);
if (a != LONG_MIN)
{
if (a < 0)
int negative_a = (a < 0);
if (negative_a)
a = -a;
if (b < 0)
b = -b;
result.quot = __mesabi_uldiv(a, b, &result.rem);
if (negate_result)
result.quot = -result.quot;
if (negative_a)
result.rem = -result.rem;
return result;
}
else
@ -95,7 +100,7 @@ ldiv_t ldiv(long a, long b)
long x;
for (x = 0; a <= -b; a += b)
++x;
result.rem = -a;
result.rem = a; /* negative */
result.quot = x;
}
if (negate_result)