public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: LIU Hao <lh_mouse@126.com>
To: "bootmgr@163.com" <bootmgr@163.com>, gcc-help@gcc.gnu.org
Subject: Re: Is there an intrinsic similar to _BitScanForward on Linux?
Date: Wed, 30 Nov 2022 15:41:56 +0800	[thread overview]
Message-ID: <e13135d6-ff5a-1b91-79f0-e96703e48ca3@126.com> (raw)
In-Reply-To: <132592a1.3db9.184c76afb9c.Coremail.bootmgr@163.com>


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

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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-30  7:23 bootmgr
2022-11-30  7:41 ` LIU Hao [this message]
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=e13135d6-ff5a-1b91-79f0-e96703e48ca3@126.com \
    --to=lh_mouse@126.com \
    --cc=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).