mirror of https://gitee.com/openkylin/qemu.git
Optimize some host-utils function with gcc builtins
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net> git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5464 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
parent
bc7b5f873b
commit
7d01998021
40
host-utils.h
40
host-utils.h
|
@ -47,14 +47,16 @@ void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
|
|||
void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
|
||||
#endif
|
||||
|
||||
/* Note that some of those functions may end up calling libgcc functions,
|
||||
depending on the host machine. It is up to the target emulation to
|
||||
cope with that. */
|
||||
|
||||
/* Binary search for leading zeros. */
|
||||
|
||||
static always_inline int clz32(uint32_t val)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
if (val)
|
||||
return __builtin_clz(val);
|
||||
else
|
||||
return 32;
|
||||
#else
|
||||
int cnt = 0;
|
||||
|
||||
if (!(val & 0xFFFF0000U)) {
|
||||
|
@ -81,6 +83,7 @@ static always_inline int clz32(uint32_t val)
|
|||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
#endif
|
||||
}
|
||||
|
||||
static always_inline int clo32(uint32_t val)
|
||||
|
@ -90,6 +93,12 @@ static always_inline int clo32(uint32_t val)
|
|||
|
||||
static always_inline int clz64(uint64_t val)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
if (val)
|
||||
return __builtin_clzll(val);
|
||||
else
|
||||
return 64;
|
||||
#else
|
||||
int cnt = 0;
|
||||
|
||||
if (!(val >> 32)) {
|
||||
|
@ -99,6 +108,7 @@ static always_inline int clz64(uint64_t val)
|
|||
}
|
||||
|
||||
return cnt + clz32(val);
|
||||
#endif
|
||||
}
|
||||
|
||||
static always_inline int clo64(uint64_t val)
|
||||
|
@ -108,6 +118,12 @@ static always_inline int clo64(uint64_t val)
|
|||
|
||||
static always_inline int ctz32 (uint32_t val)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
if (val)
|
||||
return __builtin_ctz(val);
|
||||
else
|
||||
return 32;
|
||||
#else
|
||||
int cnt;
|
||||
|
||||
cnt = 0;
|
||||
|
@ -136,6 +152,7 @@ static always_inline int ctz32 (uint32_t val)
|
|||
}
|
||||
|
||||
return cnt;
|
||||
#endif
|
||||
}
|
||||
|
||||
static always_inline int cto32 (uint32_t val)
|
||||
|
@ -145,6 +162,12 @@ static always_inline int cto32 (uint32_t val)
|
|||
|
||||
static always_inline int ctz64 (uint64_t val)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
if (val)
|
||||
return __builtin_ctz(val);
|
||||
else
|
||||
return 64;
|
||||
#else
|
||||
int cnt;
|
||||
|
||||
cnt = 0;
|
||||
|
@ -154,6 +177,7 @@ static always_inline int ctz64 (uint64_t val)
|
|||
}
|
||||
|
||||
return cnt + ctz32(val);
|
||||
#endif
|
||||
}
|
||||
|
||||
static always_inline int cto64 (uint64_t val)
|
||||
|
@ -182,6 +206,9 @@ static always_inline int ctpop16 (uint16_t val)
|
|||
|
||||
static always_inline int ctpop32 (uint32_t val)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
return __builtin_popcount(val);
|
||||
#else
|
||||
val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
|
||||
val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
|
||||
val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
|
||||
|
@ -189,10 +216,14 @@ static always_inline int ctpop32 (uint32_t val)
|
|||
val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
static always_inline int ctpop64 (uint64_t val)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
return __builtin_popcountll(val);
|
||||
#else
|
||||
val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
|
||||
val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
|
||||
val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
|
||||
|
@ -201,4 +232,5 @@ static always_inline int ctpop64 (uint64_t val)
|
|||
val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue