mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-28 04:21:50 -06:00
host-utils: move checks out of divu128/divs128
In preparation for changing the divu128/divs128 implementations to allow for quotients larger than 64 bits, move the div-by-zero and overflow checks to the callers. Signed-off-by: Luis Pires <luis.pires@eldorado.org.br> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-Id: <20211025191154.350831-2-luis.pires@eldorado.org.br> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
parent
1c46937358
commit
9276a31c34
4 changed files with 42 additions and 51 deletions
|
@ -324,8 +324,9 @@ static inline uint64_t clock_ns_to_ticks(const Clock *clk, uint64_t ns)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Ignore divu128() return value as we've caught div-by-zero and don't
|
* BUG: when CONFIG_INT128 is not defined, the current implementation of
|
||||||
* need different behaviour for overflow.
|
* divu128 does not return a valid truncated quotient, so the result will
|
||||||
|
* be wrong.
|
||||||
*/
|
*/
|
||||||
divu128(&lo, &hi, clk->period);
|
divu128(&lo, &hi, clk->period);
|
||||||
return lo;
|
return lo;
|
||||||
|
|
|
@ -52,36 +52,26 @@ static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
|
||||||
return (__int128_t)a * b / c;
|
return (__int128_t)a * b / c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
|
static inline void divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
|
||||||
{
|
{
|
||||||
if (divisor == 0) {
|
__uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
|
||||||
return 1;
|
__uint128_t result = dividend / divisor;
|
||||||
} else {
|
*plow = result;
|
||||||
__uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
|
*phigh = dividend % divisor;
|
||||||
__uint128_t result = dividend / divisor;
|
|
||||||
*plow = result;
|
|
||||||
*phigh = dividend % divisor;
|
|
||||||
return result > UINT64_MAX;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
|
static inline void divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
|
||||||
{
|
{
|
||||||
if (divisor == 0) {
|
__int128_t dividend = ((__int128_t)*phigh << 64) | (uint64_t)*plow;
|
||||||
return 1;
|
__int128_t result = dividend / divisor;
|
||||||
} else {
|
*plow = result;
|
||||||
__int128_t dividend = ((__int128_t)*phigh << 64) | (uint64_t)*plow;
|
*phigh = dividend % divisor;
|
||||||
__int128_t result = dividend / divisor;
|
|
||||||
*plow = result;
|
|
||||||
*phigh = dividend % divisor;
|
|
||||||
return result != *plow;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void muls64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b);
|
void muls64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b);
|
||||||
void mulu64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b);
|
void mulu64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b);
|
||||||
int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
|
void divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
|
||||||
int divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
|
void divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
|
||||||
|
|
||||||
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
|
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
|
||||||
{
|
{
|
||||||
|
|
|
@ -104,10 +104,11 @@ uint64_t helper_divdeu(CPUPPCState *env, uint64_t ra, uint64_t rb, uint32_t oe)
|
||||||
uint64_t rt = 0;
|
uint64_t rt = 0;
|
||||||
int overflow = 0;
|
int overflow = 0;
|
||||||
|
|
||||||
overflow = divu128(&rt, &ra, rb);
|
if (unlikely(rb == 0 || ra >= rb)) {
|
||||||
|
overflow = 1;
|
||||||
if (unlikely(overflow)) {
|
|
||||||
rt = 0; /* Undefined */
|
rt = 0; /* Undefined */
|
||||||
|
} else {
|
||||||
|
divu128(&rt, &ra, rb);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oe) {
|
if (oe) {
|
||||||
|
@ -122,10 +123,13 @@ uint64_t helper_divde(CPUPPCState *env, uint64_t rau, uint64_t rbu, uint32_t oe)
|
||||||
int64_t rt = 0;
|
int64_t rt = 0;
|
||||||
int64_t ra = (int64_t)rau;
|
int64_t ra = (int64_t)rau;
|
||||||
int64_t rb = (int64_t)rbu;
|
int64_t rb = (int64_t)rbu;
|
||||||
int overflow = divs128(&rt, &ra, rb);
|
int overflow = 0;
|
||||||
|
|
||||||
if (unlikely(overflow)) {
|
if (unlikely(rb == 0 || uabs64(ra) >= uabs64(rb))) {
|
||||||
|
overflow = 1;
|
||||||
rt = 0; /* Undefined */
|
rt = 0; /* Undefined */
|
||||||
|
} else {
|
||||||
|
divs128(&rt, &ra, rb);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oe) {
|
if (oe) {
|
||||||
|
|
|
@ -86,24 +86,23 @@ void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
|
||||||
*phigh = rh;
|
*phigh = rh;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unsigned 128x64 division. Returns 1 if overflow (divide by zero or */
|
/*
|
||||||
/* quotient exceeds 64 bits). Otherwise returns quotient via plow and */
|
* Unsigned 128-by-64 division. Returns quotient via plow and
|
||||||
/* remainder via phigh. */
|
* remainder via phigh.
|
||||||
int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
|
* The result must fit in 64 bits (plow) - otherwise, the result
|
||||||
|
* is undefined.
|
||||||
|
* This function will cause a division by zero if passed a zero divisor.
|
||||||
|
*/
|
||||||
|
void divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
|
||||||
{
|
{
|
||||||
uint64_t dhi = *phigh;
|
uint64_t dhi = *phigh;
|
||||||
uint64_t dlo = *plow;
|
uint64_t dlo = *plow;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
uint64_t carry = 0;
|
uint64_t carry = 0;
|
||||||
|
|
||||||
if (divisor == 0) {
|
if (divisor == 0 || dhi == 0) {
|
||||||
return 1;
|
|
||||||
} else if (dhi == 0) {
|
|
||||||
*plow = dlo / divisor;
|
*plow = dlo / divisor;
|
||||||
*phigh = dlo % divisor;
|
*phigh = dlo % divisor;
|
||||||
return 0;
|
|
||||||
} else if (dhi >= divisor) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
for (i = 0; i < 64; i++) {
|
for (i = 0; i < 64; i++) {
|
||||||
|
@ -120,15 +119,20 @@ int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
|
||||||
|
|
||||||
*plow = dlo;
|
*plow = dlo;
|
||||||
*phigh = dhi;
|
*phigh = dhi;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
|
/*
|
||||||
|
* Signed 128-by-64 division. Returns quotient via plow and
|
||||||
|
* remainder via phigh.
|
||||||
|
* The result must fit in 64 bits (plow) - otherwise, the result
|
||||||
|
* is undefined.
|
||||||
|
* This function will cause a division by zero if passed a zero divisor.
|
||||||
|
*/
|
||||||
|
void divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
|
||||||
{
|
{
|
||||||
int sgn_dvdnd = *phigh < 0;
|
int sgn_dvdnd = *phigh < 0;
|
||||||
int sgn_divsr = divisor < 0;
|
int sgn_divsr = divisor < 0;
|
||||||
int overflow = 0;
|
|
||||||
|
|
||||||
if (sgn_dvdnd) {
|
if (sgn_dvdnd) {
|
||||||
*plow = ~(*plow);
|
*plow = ~(*plow);
|
||||||
|
@ -145,19 +149,11 @@ int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
|
||||||
divisor = 0 - divisor;
|
divisor = 0 - divisor;
|
||||||
}
|
}
|
||||||
|
|
||||||
overflow = divu128((uint64_t *)plow, (uint64_t *)phigh, (uint64_t)divisor);
|
divu128((uint64_t *)plow, (uint64_t *)phigh, (uint64_t)divisor);
|
||||||
|
|
||||||
if (sgn_dvdnd ^ sgn_divsr) {
|
if (sgn_dvdnd ^ sgn_divsr) {
|
||||||
*plow = 0 - *plow;
|
*plow = 0 - *plow;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!overflow) {
|
|
||||||
if ((*plow < 0) ^ (sgn_dvdnd ^ sgn_divsr)) {
|
|
||||||
overflow = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return overflow;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue