X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fadt%2Fbitfiddle.h;h=3be8ef060336dc81bc2901ed06b3fc2170117e03;hb=26b9008643da89a023fd6839ded5b9abf21ef328;hp=29ff4387e733c7d74db14aa3ceb38a846fd3febc;hpb=972eca70744a2c9a0e0d472db4f62250cdeee01c;p=libfirm diff --git a/ir/adt/bitfiddle.h b/ir/adt/bitfiddle.h index 29ff4387e..3be8ef060 100644 --- a/ir/adt/bitfiddle.h +++ b/ir/adt/bitfiddle.h @@ -22,7 +22,6 @@ * @date 28.9.2004 * @brief Functions from hackers delight. * @author Sebastian Hack, Matthias Braun - * @version $Id$ */ #ifndef FIRM_ADT_BITFIDDLE_H #define FIRM_ADT_BITFIDDLE_H @@ -45,8 +44,7 @@ COMPILETIME_ASSERT(UINT_MAX == 4294967295U, uintmax) * * @note See hacker's delight, page 27. */ -static inline -int add_saturated(int x, int y) +static inline int add_saturated(int x, int y) { int sum = x + y; /* @@ -74,14 +72,18 @@ int add_saturated(int x, int y) * @param x A 32-bit word. * @return The number of bits set in x. */ -static inline -unsigned popcnt(unsigned x) { +static inline unsigned popcount(unsigned x) +{ +#if defined(__GNUC__) && __GNUC__ >= 4 + return __builtin_popcount(x); +#else x -= ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); x = (x + (x >> 4)) & 0x0f0f0f0f; x += x >> 8; x += x >> 16; return x & 0x3f; +#endif } /** @@ -89,17 +91,12 @@ unsigned popcnt(unsigned x) { * @param x The word. * @return The number of leading (from the most significant bit) zeros. */ -static inline -unsigned nlz(unsigned x) { -#ifdef USE_X86_ASSEMBLY - unsigned res; +static inline unsigned nlz(unsigned x) +{ +#if defined(__GNUC__) && __GNUC__ >= 4 if(x == 0) return 32; - - __asm__("bsrl %1,%0" - : "=r" (res) - : "r" (x)); - return 31 - res; + return __builtin_clz(x); #else unsigned y; int n = 32; @@ -118,17 +115,12 @@ unsigned nlz(unsigned x) { * @param x The word. * @return The number of trailing zeros. */ -static inline -unsigned ntz(unsigned x) { -#ifdef USE_X86_ASSEMBLY - unsigned res; +static inline unsigned ntz(unsigned x) +{ +#if defined(__GNUC__) && __GNUC__ >= 4 if(x == 0) return 32; - - __asm__("bsfl %1,%0" - : "=r" (res) - : "r" (x)); - return res; + return __builtin_ctz(x); #else return HACKDEL_WORDSIZE - nlz(~x & (x - 1)); #endif @@ -162,10 +154,9 @@ unsigned ntz(unsigned x) { * Returns the biggest power of 2 that is equal or smaller than @p x * (see hackers delight power-of-2 boundaries, page 48) */ -static inline -unsigned floor_po2(unsigned x) +static inline unsigned floor_po2(unsigned x) { -#ifdef USE_X86_ASSEMBLY // in this case nlz is fast +#if defined(__GNUC__) && __GNUC__ >= 4 // in this case nlz is fast if(x == 0) return 0; // note that x != 0 here, so nlz(x) < 32! @@ -185,14 +176,13 @@ unsigned floor_po2(unsigned x) * @remark x has to be <= 0x8000000 of course * @note see hackers delight power-of-2 boundaries, page 48 */ -static inline -unsigned ceil_po2(unsigned x) +static inline unsigned ceil_po2(unsigned x) { if(x == 0) return 0; assert(x < (1U << 31)); -#ifdef USE_X86_ASSEMBLY // in this case nlz is fast +#if defined(__GNUC__) && __GNUC__ >= 4 // in this case nlz is fast // note that x != 0 here! return 0x80000000U >> (nlz(x-1) - 1); #else @@ -209,8 +199,7 @@ unsigned ceil_po2(unsigned x) /** * Tests whether @p x is a power of 2 */ -static inline -int is_po2(unsigned x) +static inline int is_po2(unsigned x) { return (x & (x-1)) == 0; }