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

* Re: Is there an intrinsic similar to _BitScanForward on Linux?
  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
  1 sibling, 0 replies; 3+ messages in thread
From: LIU Hao @ 2022-11-30  7:41 UTC (permalink / raw)
  To: bootmgr, gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 853 bytes --]

在 2022/11/30 15:23, bootmgr--- via Gcc-help 写道:
> 
> 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?
> 
> 
	
```
__attribute__((__gnu_inline__, __nothrow__, __pure__))
extern __inline__
bool
_BitScanForward(uint32_t* index, uint32_t value)
   {
     bool zf;
     __asm__ (
       "{ bsfl %2, %1 | bsf %1, %2 }"
       : "=@ccz"(zf), "=r"(*index) : "rm"(value)
     );
     return !zf;
   }
```

Godbolt:  https://gcc.godbolt.org/z/vzEjGj11e


-- 
Best regards,
LIU Hao


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

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

* Re: Is there an intrinsic similar to _BitScanForward on Linux?
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Václav Haisman @ 2022-12-07 20:30 UTC (permalink / raw)
  To: gcc-help

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


^ 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).