public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "bootmgr@163.com" <bootmgr@163.com>
To: gcc-help@gcc.gnu.org
Subject: Is there an intrinsic similar to _BitScanForward on Linux?
Date: Wed, 30 Nov 2022 15:23:08 +0800 (CST)	[thread overview]
Message-ID: <132592a1.3db9.184c76afb9c.Coremail.bootmgr@163.com> (raw)

[-- 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?



             reply	other threads:[~2022-11-30  7:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-30  7:23 bootmgr [this message]
2022-11-30  7:41 ` LIU Hao
2022-12-07 20:30 ` Václav Haisman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=132592a1.3db9.184c76afb9c.Coremail.bootmgr@163.com \
    --to=bootmgr@163.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).