hv: clean up div related code

- replace udiv64 with direct integer divide
- remove lib/div.c

Tracked-On: #861
Signed-off-by: Shiqing Gao <shiqing.gao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Shiqing Gao 2018-09-12 13:31:42 +08:00 committed by lijinxia
parent 1d2ed1adee
commit 7cab77dace
4 changed files with 2 additions and 132 deletions

View File

@ -154,7 +154,6 @@ C_SRCS += arch/x86/guest/pm.c
C_SRCS += lib/spinlock.c C_SRCS += lib/spinlock.c
C_SRCS += lib/udelay.c C_SRCS += lib/udelay.c
C_SRCS += lib/mdelay.c C_SRCS += lib/mdelay.c
C_SRCS += lib/div.c
C_SRCS += lib/string.c C_SRCS += lib/string.c
C_SRCS += lib/memory.c C_SRCS += lib/memory.c
C_SRCS += lib/crypto/hkdf_wrap.c C_SRCS += lib/crypto/hkdf_wrap.c

View File

@ -19,12 +19,6 @@ union u_qword {
}; };
struct udiv_result {
union u_qword q;
union u_qword r;
};
/* Function prototypes */ /* Function prototypes */
void udelay(uint32_t us); void udelay(uint32_t us);
void *memchr(const void *void_s, int c, size_t n); void *memchr(const void *void_s, int c, size_t n);
@ -37,8 +31,6 @@ void mdelay(uint32_t loop_count_arg);
size_t strnlen_s(const char *str_arg, size_t maxlen_arg); size_t strnlen_s(const char *str_arg, size_t maxlen_arg);
void *memset(void *base, uint8_t v, size_t n); void *memset(void *base, uint8_t v, size_t n);
void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg); void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg);
int udiv64(uint64_t dividend_arg, uint64_t divisor_arg, struct udiv_result *res);
int udiv32(uint32_t dividend, uint32_t divisor, struct udiv_result *res);
int atoi(const char *str); int atoi(const char *str);
long strtol_deci(const char *nptr); long strtol_deci(const char *nptr);
uint64_t strtoul_hex(const char *nptr); uint64_t strtoul_hex(const char *nptr);

View File

@ -1,118 +0,0 @@
/*
* 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_arg, uint32_t divisor_arg,
struct udiv_result *res)
{
uint32_t dividend = dividend_arg;
uint32_t divisor = divisor_arg;
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;
mask >>= 1U;
} while ((mask != 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 = 0U;
res->r.dwords.low = 0U;
/* test for "division by 0" condition */
if (divisor == 0U) {
res->q.dwords.low = 0xffffffffU;
return 1;
}
/* trivial case: divisor==dividend */
if (divisor == dividend) {
res->q.dwords.low = 1U;
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_arg, uint64_t divisor_arg, struct udiv_result *res)
{
uint64_t dividend = dividend_arg;
uint64_t divisor = divisor_arg;
uint64_t mask;
uint64_t bits;
/* initialize the result */
res->q.qword = 0UL;
res->r.qword = 0UL;
/* test for "division by 0" condition */
if (divisor == 0UL) {
res->q.qword = 0xffffffffffffffffUL;
return -1;
}
/* trivial case: divisor==dividend */
if (divisor == dividend) {
res->q.qword = 1UL;
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 >> 32UL) == 0UL) && ((dividend >> 32UL) == 0UL)) {
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 >>= 1UL;
mask >>= 1UL;
} while (((bits--) != 0UL) && (dividend != 0UL));
res->r.qword = dividend;
return 0;
}

View File

@ -348,8 +348,6 @@ static int print_decimal(struct print_param *param, int64_t value)
union u_qword v; union u_qword v;
/* next value in 32/64 bit */ /* next value in 32/64 bit */
union u_qword nv; union u_qword nv;
/* helper union for division result */
struct udiv_result d;
int ret; int ret;
/* assume an unsigned 64 bit value */ /* assume an unsigned 64 bit value */
@ -381,10 +379,9 @@ static int print_decimal(struct print_param *param, int64_t value)
/* process 64 bit value as long as needed */ /* process 64 bit value as long as needed */
while (v.dwords.high != 0U) { while (v.dwords.high != 0U) {
/* determine digits from right to left */ /* determine digits from right to left */
udiv64(v.qword, 10U, &d);
pos--; pos--;
*pos = d.r.dwords.low + '0'; *pos = (char)(v.qword % 10UL) + '0';
v.qword = d.q.qword; v.qword = v.qword / 10UL;
} }
/* process 32 bit (or reduced 64 bit) value */ /* process 32 bit (or reduced 64 bit) value */