public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Is there an intrinsic similar to _BitScanForward on Linux?
@ 2022-11-30  7:23 bootmgr
  2022-11-30  7:41 ` LIU Hao
  2022-12-07 20:30 ` Václav Haisman
  0 siblings, 2 replies; 3+ messages in thread
From: bootmgr @ 2022-11-30  7:23 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1135 bytes --]

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?



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-07 20:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30  7:23 Is there an intrinsic similar to _BitScanForward on Linux? bootmgr
2022-11-30  7:41 ` LIU Hao
2022-12-07 20:30 ` Václav Haisman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).