From 64f417d6e6c92f9852916f4adbc7080171da064d Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Mon, 9 Aug 2021 04:08:38 +0100 Subject: [PATCH] [util] Add bsf helper For when we know we aren't going to put in a mask of 0, we can use this and get better codegen. --- src/util/util_bit.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/util/util_bit.h b/src/util/util_bit.h index 7008bacaf..925edea8c 100644 --- a/src/util/util_bit.h +++ b/src/util/util_bit.h @@ -76,6 +76,25 @@ namespace dxvk::bit { #endif } + inline uint32_t bsf(uint32_t n) { + #if defined(_MSC_VER) && !defined(__clang__) + DWORD index; + _BitScanForward(&index, n); + return uint32_t(index); + #elif defined(__GNUC__) || defined(__clang__) + return __builtin_ctz(n); + #else + uint32_t r = 31; + n &= -n; + r -= (n & 0x0000FFFF) ? 16 : 0; + r -= (n & 0x00FF00FF) ? 8 : 0; + r -= (n & 0x0F0F0F0F) ? 4 : 0; + r -= (n & 0x33333333) ? 2 : 0; + r -= (n & 0x55555555) ? 1 : 0; + return r; + #endif + } + inline uint32_t lzcnt(uint32_t n) { #if (defined(_MSC_VER) && !defined(__clang__)) || defined(__LZCNT__) return _lzcnt_u32(n);