mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-05-09 08:56:55 +00:00
Change the return type of function fls64 and clz64 as uint16_t; When the input is zero, INVALID_ID_INDEX is returned; Update temporary variable type and return value check of caller when it call fls64 or clz64; When input value is zero, clz64 returns 64 directly. V1-->V2: INVALID_BIT_INDEX instead of INVALID_NUMBER; Partly revert apicv_pending_intr udpates; Add type conversion as needed; Coding style fixing. V2-->V3: Correct type conversion; fls64 return INVALID_BIT_INDEX directly when the input value is zero. V3-->V4: No updates for this part in PATCH V4. Note: For instruction "bsrq", destination register value is undefined when source register value is zero. Signed-off-by: Xiangyang Wu <xiangyang.wu@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
113 lines
2.7 KiB
C
113 lines
2.7 KiB
C
/*
|
|
* Copyright (C) 2018 Intel Corporation. All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include <hv_lib.h>
|
|
|
|
static int do_udiv32(uint32_t dividend, uint32_t divisor,
|
|
struct udiv_result *res)
|
|
{
|
|
|
|
uint32_t mask;
|
|
/* dividend is always greater than or equal to the divisor. Neither
|
|
* divisor nor dividend are 0. Thus: * clz(dividend) and clz(divisor)
|
|
* are valid * clz(dividend)<=clz(divisor)
|
|
*/
|
|
|
|
mask = (uint32_t)(clz(divisor) - clz(dividend));
|
|
/* align divisor and dividend */
|
|
divisor <<= mask;
|
|
mask = 1U << mask;
|
|
/* division loop */
|
|
do {
|
|
if (dividend >= divisor) {
|
|
dividend -= divisor;
|
|
res->q.dwords.low |= mask;
|
|
}
|
|
divisor >>= 1U;
|
|
} while (((mask >>= 1U) != 0U) && (dividend != 0U));
|
|
/* dividend now contains the reminder */
|
|
res->r.dwords.low = dividend;
|
|
return 0;
|
|
}
|
|
|
|
int udiv32(uint32_t dividend, uint32_t divisor, struct udiv_result *res)
|
|
{
|
|
|
|
/* initialize the result */
|
|
res->q.dwords.low = res->r.dwords.low = 0;
|
|
/* test for "division by 0" condition */
|
|
if (divisor == 0) {
|
|
res->q.dwords.low = 0xffffffff;
|
|
return !0;
|
|
}
|
|
/* trivial case: divisor==dividend */
|
|
if (divisor == dividend) {
|
|
res->q.dwords.low = 1;
|
|
return 0;
|
|
}
|
|
/* trivial case: divisor>dividend */
|
|
if (divisor > dividend) {
|
|
res->r.dwords.low = dividend;
|
|
return 0;
|
|
}
|
|
/* now that the trivial cases are eliminated we can call the generic
|
|
* function.
|
|
*/
|
|
return do_udiv32(dividend, divisor, res);
|
|
}
|
|
|
|
int udiv64(uint64_t dividend, uint64_t divisor, struct udiv_result *res)
|
|
{
|
|
|
|
uint64_t mask;
|
|
uint64_t bits;
|
|
|
|
/* initialize the result */
|
|
res->q.qword = res->r.qword = 0;
|
|
/* test for "division by 0" condition */
|
|
if (divisor == 0) {
|
|
res->q.qword = 0xffffffffffffffffull;
|
|
return -1;
|
|
}
|
|
/* trivial case: divisor==dividend */
|
|
if (divisor == dividend) {
|
|
res->q.qword = 1;
|
|
return 0;
|
|
}
|
|
/* trivial case: divisor>dividend */
|
|
if (divisor > dividend) {
|
|
res->r.qword = dividend;
|
|
return 0;
|
|
}
|
|
/* simplified case: only 32 bit operands Note that the preconditions
|
|
* for do_udiv32() are fulfilled, since the tests were made above.
|
|
*/
|
|
if (((divisor >> 32) == 0) && ((dividend >> 32) == 0))
|
|
return do_udiv32((uint32_t) dividend, (uint32_t) divisor, res);
|
|
|
|
/* dividend is always greater than or equal to the divisor. Neither
|
|
* divisor nor dividend are 0. Thus: * clz(dividend) and clz(divisor)
|
|
* are valid * clz(dividend)<=clz(divisor)
|
|
*/
|
|
|
|
/* align divisor and dividend. */
|
|
bits = (uint64_t)(clz64(divisor) - clz64(dividend));
|
|
divisor <<= bits;
|
|
mask = 1UL << bits;
|
|
/* division loop */
|
|
do {
|
|
if (dividend >= divisor) {
|
|
dividend -= divisor;
|
|
res->q.qword |= mask;
|
|
}
|
|
divisor >>= 1;
|
|
mask >>= 1;
|
|
} while ((bits-- != 0) && (dividend != 0));
|
|
|
|
res->r.qword = dividend;
|
|
return 0;
|
|
}
|