From c3b66566760dd44eaeed9e4df13687dc3ee69bd9 Mon Sep 17 00:00:00 2001 From: Mathias Fröhlich Date: Thu, 9 Jun 2016 06:35:34 +0200 Subject: mesa/gallium: Move u_bit_scan{,64} from gallium to util. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The functions are also useful for mesa. Introduce src/util/bitscan.{h,c}. Move ffs function implementations from src/mesa/main/imports.{h,c}. Move bit scan related functions from src/gallium/auxiliary/util/u_math.h. Merge platform handling with what is available from within mesa. v2: Try to fix MSVC compile. Reviewed-by: Brian Paul Tested-by: Brian Paul Signed-off-by: Mathias Fröhlich --- src/gallium/auxiliary/util/u_math.h | 149 +----------------------------------- 1 file changed, 1 insertion(+), 148 deletions(-) (limited to 'src/gallium/auxiliary/util') diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h index ecb1d636fcc..c94967e8a42 100644 --- a/src/gallium/auxiliary/util/u_math.h +++ b/src/gallium/auxiliary/util/u_math.h @@ -46,14 +46,7 @@ #include #include -#ifdef PIPE_OS_UNIX -#include /* for ffs */ -#endif - -#if defined(_MSC_VER) -#include -#endif - +#include "util/bitscan.h" #ifdef __cplusplus extern "C" { @@ -353,80 +346,6 @@ util_half_inf_sign(int16_t x) } -/** - * Find first bit set in word. Least significant bit is 1. - * Return 0 if no bits set. - */ -#ifndef FFS_DEFINED -#define FFS_DEFINED 1 - -#if defined(_MSC_VER) && (_M_IX86 || _M_AMD64 || _M_IA64) -static inline -unsigned long ffs( unsigned long u ) -{ - unsigned long i; - if (_BitScanForward(&i, u)) - return i + 1; - else - return 0; -} -#elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86) -static inline -unsigned ffs( unsigned u ) -{ - unsigned i; - - if (u == 0) { - return 0; - } - - __asm bsf eax, [u] - __asm inc eax - __asm mov [i], eax - - return i; -} -#elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID) || \ - defined(HAVE___BUILTIN_FFS) -#define ffs __builtin_ffs -#endif - -#ifdef HAVE___BUILTIN_FFSLL -#define ffsll __builtin_ffsll -#else -static inline int -ffsll(long long int val) -{ - int bit; - - bit = ffs((unsigned) (val & 0xffffffff)); - if (bit != 0) - return bit; - - bit = ffs((unsigned) (val >> 32)); - if (bit != 0) - return 32 + bit; - - return 0; -} -#endif - -#endif /* FFS_DEFINED */ - -/** - * Find first bit set in long long. Least significant bit is 1. - * Return 0 if no bits set. - */ -#ifndef FFSLL_DEFINED -#define FFSLL_DEFINED 1 - -#if defined(__MINGW32__) || defined(PIPE_OS_ANDROID) || \ - defined(HAVE___BUILTIN_FFSLL) -#define ffsll __builtin_ffsll -#endif - -#endif /* FFSLL_DEFINED */ - /** * Find last bit set in a word. The least significant bit is 1. * Return 0 if no bits are set. @@ -479,72 +398,6 @@ util_last_bit_signed(int i) return util_last_bit(~(unsigned)i); } -/* Destructively loop over all of the bits in a mask as in: - * - * while (mymask) { - * int i = u_bit_scan(&mymask); - * ... process element i - * } - * - */ -static inline int -u_bit_scan(unsigned *mask) -{ - int i = ffs(*mask) - 1; - *mask &= ~(1u << i); - return i; -} - -#ifndef _MSC_VER -static inline int -u_bit_scan64(uint64_t *mask) -{ - int i = ffsll(*mask) - 1; - *mask &= ~(1llu << i); - return i; -} -#endif - -/* For looping over a bitmask when you want to loop over consecutive bits - * manually, for example: - * - * while (mask) { - * int start, count, i; - * - * u_bit_scan_consecutive_range(&mask, &start, &count); - * - * for (i = 0; i < count; i++) - * ... process element (start+i) - * } - */ -static inline void -u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count) -{ - if (*mask == 0xffffffff) { - *start = 0; - *count = 32; - *mask = 0; - return; - } - *start = ffs(*mask) - 1; - *count = ffs(~(*mask >> *start)) - 1; - *mask &= ~(((1u << *count) - 1) << *start); -} - -static inline void -u_bit_scan_consecutive_range64(uint64_t *mask, int *start, int *count) -{ - if (*mask == ~0llu) { - *start = 0; - *count = 64; - *mask = 0; - return; - } - *start = ffsll(*mask) - 1; - *count = ffsll(~(*mask >> *start)) - 1; - *mask &= ~(((1llu << *count) - 1) << *start); -} - /* Returns a bitfield in which the first count bits starting at start are * set. */ -- cgit v1.2.3