On 30. 11. 22 8:23, bootmgr--- via Gcc-help wrote:
Greetings,
I'm not sure if it's appropriate to ask a question here, so please understand.
I trying to write a function like strlen to calculate the length of a null-terminated string. Here is a code
snippet.
[[nodiscard]] inline std::size_t strlen(const char *str) noexcept {
const auto uint_ptr = reinterpret_cast<std::uintptr_t>(str);
using v16qi [[gnu::vector_size(16)]] = char;
constexpr std::uintptr_t mask = 0xF;
auto xmm_word = reinterpret_cast<const v16qi *>(uint_ptr & ~mask);
v16qi xmm0;
__builtin_memset(std::addressof(xmm0), 0, sizeof(xmm0));
unsigned long result;
if (_BitScanForward(std::addressof(result),
__builtin_ia32_pmovmskb128(*xmm_word == xmm0) >> (uint_ptr & mask)))
return result;
/* ... */
}
And I want to port it to Linux, so I am looking for any functions like _BitScanForward. I trying to use
__builtin_ctz instead _BitScanForward, but it is undefined when x is 0 and it can't
determine whether x is 0 or not. Then I found __bsfd in x86intrin.h, but it's the same as
__builtin_ctz, which can't determine x is 0.
So is there any solution, preferably cross-platform?
Isn't it something like `(*index = __builtin_ffs(value)) != 0` what you
want? See https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
--
VH