34,10 → 34,11 |
|
#include <genarch/softint/division.h> |
|
#define ABSVAL(x) ( (x) > 0 ? (x) : -(x)) |
#define SGN(x) ( (x) >= 0 ? 1 : 0 ) |
#define ABSVAL(x) ((x) > 0 ? (x) : -(x)) |
#define SGN(x) ((x) >= 0 ? 1 : 0) |
|
static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder) |
static unsigned int divandmod32(unsigned int a, unsigned int b, |
unsigned int *remainder) |
{ |
unsigned int result; |
int steps = sizeof(unsigned int) * 8; |
50,19 → 51,19 |
return 0; |
} |
|
if ( a < b) { |
if (a < b) { |
*remainder = a; |
return 0; |
} |
|
for ( ; steps > 0; steps--) { |
for (; steps > 0; steps--) { |
/* shift one bit to remainder */ |
*remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1); |
*remainder = ((*remainder) << 1) | (( a >> 31) & 0x1); |
result <<= 1; |
|
if (*remainder >= b) { |
*remainder -= b; |
result |= 0x1; |
*remainder -= b; |
result |= 0x1; |
} |
a <<= 1; |
} |
71,7 → 72,8 |
} |
|
|
static unsigned long long divandmod64(unsigned long long a, unsigned long long b, unsigned long long *remainder) |
static unsigned long long divandmod64(unsigned long long a, |
unsigned long long b, unsigned long long *remainder) |
{ |
unsigned long long result; |
int steps = sizeof(unsigned long long) * 8; |
84,19 → 86,19 |
return 0; |
} |
|
if ( a < b) { |
if (a < b) { |
*remainder = a; |
return 0; |
} |
|
for ( ; steps > 0; steps--) { |
for (; steps > 0; steps--) { |
/* shift one bit to remainder */ |
*remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1); |
*remainder = ((*remainder) << 1) | ((a >> 63) & 0x1); |
result <<= 1; |
|
if (*remainder >= b) { |
*remainder -= b; |
result |= 0x1; |
*remainder -= b; |
result |= 0x1; |
} |
a <<= 1; |
} |
110,9 → 112,10 |
unsigned int rem; |
int result; |
|
result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem); |
result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem); |
|
if ( SGN(a) == SGN(b)) return result; |
if (SGN(a) == SGN(b)) |
return result; |
return -result; |
} |
|
122,9 → 125,10 |
unsigned long long rem; |
long long result; |
|
result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem); |
result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem); |
|
if ( SGN(a) == SGN(b)) return result; |
if (SGN(a) == SGN(b)) |
return result; |
return -result; |
} |
|
150,10 → 154,10 |
|
/* if divident is negative, remainder must be too */ |
if (!(SGN(a))) { |
return -((int)rem); |
return -((int) rem); |
} |
|
return (int)rem; |
return (int) rem; |
} |
|
/* 64bit remainder of the signed division */ |
164,10 → 168,10 |
|
/* if divident is negative, remainder must be too */ |
if (!(SGN(a))) { |
return -((long long)rem); |
return -((long long) rem); |
} |
|
return (long long)rem; |
return (long long) rem; |
} |
|
/* 32bit remainder of the unsigned division */ |
186,7 → 190,8 |
return rem; |
} |
|
unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c) |
unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, |
unsigned long long *c) |
{ |
return divandmod64(a, b, c); |
} |