diff options
author | aurel32 <aurel32@c046a42c-6fe2-441c-8c8c-71466251a162> | 2008-10-12 00:53:08 +0000 |
---|---|---|
committer | aurel32 <aurel32@c046a42c-6fe2-441c-8c8c-71466251a162> | 2008-10-12 00:53:08 +0000 |
commit | 7d01998021e38363d53bc93f198023a05d8a52bf (patch) | |
tree | 2b9349d8c9aeda85bf4e668e75f1f616a9a1d6c3 /host-utils.h | |
parent | bc7b5f873b10b8c2e4a1b247a541ff66d6eaca35 (diff) |
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
Diffstat (limited to 'host-utils.h')
-rw-r--r-- | host-utils.h | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/host-utils.h b/host-utils.h index b1e799ed3b..c907fe839a 100644 --- a/host-utils.h +++ b/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 } |