* [PATCH] Workaround for ffs() on LP64 targets
@ 2017-07-27 8:06 Sebastian Huber
2017-07-27 8:29 ` Kito Cheng
2017-07-27 11:13 ` Eric Blake
0 siblings, 2 replies; 11+ messages in thread
From: Sebastian Huber @ 2017-07-27 8:06 UTC (permalink / raw)
To: newlib
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
---
newlib/libc/misc/ffs.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
index ba5700920..a09cbd3bb 100644
--- a/newlib/libc/misc/ffs.c
+++ b/newlib/libc/misc/ffs.c
@@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
int
ffs(int i)
{
+#ifdef __LP64__
+ /* GCC would expand the __builtin_ffs() to ffs() in this case */
+ int bit;
+
+ if (i == 0)
+ return (0);
+ for (bit = 1; !(i & 1); bit++)
+ i = (unsigned int)i >> 1;
+ return (bit);
+#else
return (__builtin_ffs(i));
+#endif
}
--
2.12.3
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 8:06 [PATCH] Workaround for ffs() on LP64 targets Sebastian Huber
@ 2017-07-27 8:29 ` Kito Cheng
2017-07-27 8:40 ` Sebastian Huber
2017-07-27 11:13 ` Eric Blake
1 sibling, 1 reply; 11+ messages in thread
From: Kito Cheng @ 2017-07-27 8:29 UTC (permalink / raw)
To: Sebastian Huber; +Cc: newlib
it's will make aarch64 gen worse code (compare to __builtin_ffs
version) since aarch64 have clz, but I don't have better idea too...
On Thu, Jul 27, 2017 at 4:06 PM, Sebastian Huber
<sebastian.huber@embedded-brains.de> wrote:
> Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
> ---
> newlib/libc/misc/ffs.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
> index ba5700920..a09cbd3bb 100644
> --- a/newlib/libc/misc/ffs.c
> +++ b/newlib/libc/misc/ffs.c
> @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
> int
> ffs(int i)
> {
> +#ifdef __LP64__
> + /* GCC would expand the __builtin_ffs() to ffs() in this case */
> + int bit;
> +
> + if (i == 0)
> + return (0);
> + for (bit = 1; !(i & 1); bit++)
> + i = (unsigned int)i >> 1;
> + return (bit);
> +#else
>
> return (__builtin_ffs(i));
> +#endif
> }
> --
> 2.12.3
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 8:29 ` Kito Cheng
@ 2017-07-27 8:40 ` Sebastian Huber
2017-07-27 9:01 ` Kito Cheng
0 siblings, 1 reply; 11+ messages in thread
From: Sebastian Huber @ 2017-07-27 8:40 UTC (permalink / raw)
To: Kito Cheng; +Cc: newlib
On 27/07/17 10:29, Kito Cheng wrote:
> it's will make aarch64 gen worse code (compare to __builtin_ffs
> version) since aarch64 have clz, but I don't have better idea too...
Maybe we should add a defined(__riscv__). A count leading zeros
instruction is quite common.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 8:40 ` Sebastian Huber
@ 2017-07-27 9:01 ` Kito Cheng
0 siblings, 0 replies; 11+ messages in thread
From: Kito Cheng @ 2017-07-27 9:01 UTC (permalink / raw)
To: Sebastian Huber; +Cc: newlib
Hi Sebastian:
You mean look like this? I am ok for this if you feel this solution is
acceptable :)
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
---
newlib/libc/misc/ffs.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
index ba5700920..a09cbd3bb 100644
--- a/newlib/libc/misc/ffs.c
+++ b/newlib/libc/misc/ffs.c
@@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
int
ffs(int i)
{
+#if defined(__LP64__) && defined(__riscv)
+ /* GCC would expand the __builtin_ffs() to ffs() in this case */
+ int bit;
+
+ if (i == 0)
+ return (0);
+ for (bit = 1; !(i & 1); bit++)
+ i = (unsigned int)i >> 1;
+ return (bit);
+#else
return (__builtin_ffs(i));
+#endif
}
On Thu, Jul 27, 2017 at 4:40 PM, Sebastian Huber
<sebastian.huber@embedded-brains.de> wrote:
> On 27/07/17 10:29, Kito Cheng wrote:
>
>> it's will make aarch64 gen worse code (compare to __builtin_ffs
>> version) since aarch64 have clz, but I don't have better idea too...
>
>
> Maybe we should add a defined(__riscv__). A count leading zeros instruction
> is quite common.
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone : +49 89 189 47 41-16
> Fax : +49 89 189 47 41-09
> E-Mail : sebastian.huber@embedded-brains.de
> PGP : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 8:06 [PATCH] Workaround for ffs() on LP64 targets Sebastian Huber
2017-07-27 8:29 ` Kito Cheng
@ 2017-07-27 11:13 ` Eric Blake
2017-07-27 11:25 ` Sebastian Huber
1 sibling, 1 reply; 11+ messages in thread
From: Eric Blake @ 2017-07-27 11:13 UTC (permalink / raw)
To: Sebastian Huber, newlib
[-- Attachment #1.1: Type: text/plain, Size: 1144 bytes --]
On 07/27/2017 03:06 AM, Sebastian Huber wrote:
> Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
> ---
> newlib/libc/misc/ffs.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
> index ba5700920..a09cbd3bb 100644
> --- a/newlib/libc/misc/ffs.c
> +++ b/newlib/libc/misc/ffs.c
> @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
> int
> ffs(int i)
> {
> +#ifdef __LP64__
> + /* GCC would expand the __builtin_ffs() to ffs() in this case */
> + int bit;
> +
> + if (i == 0)
> + return (0);
> + for (bit = 1; !(i & 1); bit++)
> + i = (unsigned int)i >> 1;
> + return (bit);
If we're going to open-code it to work around the compiler creating an
infloop recursion to ffs(), at least code a straight-line version
without branches, rather than the painfully slow bit-by-bit loop.
There's plenty of examples on the web of writing ffs() by using
bit-twiddling without branching.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 11:13 ` Eric Blake
@ 2017-07-27 11:25 ` Sebastian Huber
2017-07-27 12:27 ` Corinna Vinschen
2017-07-27 21:03 ` Brian Inglis
0 siblings, 2 replies; 11+ messages in thread
From: Sebastian Huber @ 2017-07-27 11:25 UTC (permalink / raw)
To: Eric Blake, newlib
On 27/07/17 13:13, Eric Blake wrote:
> On 07/27/2017 03:06 AM, Sebastian Huber wrote:
>> Signed-off-by: Sebastian Huber<sebastian.huber@embedded-brains.de>
>> ---
>> newlib/libc/misc/ffs.c | 11 +++++++++++
>> 1 file changed, 11 insertions(+)
>>
>> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
>> index ba5700920..a09cbd3bb 100644
>> --- a/newlib/libc/misc/ffs.c
>> +++ b/newlib/libc/misc/ffs.c
>> @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
>> int
>> ffs(int i)
>> {
>> +#ifdef __LP64__
>> + /* GCC would expand the __builtin_ffs() to ffs() in this case */
>> + int bit;
>> +
>> + if (i == 0)
>> + return (0);
>> + for (bit = 1; !(i & 1); bit++)
>> + i = (unsigned int)i >> 1;
>> + return (bit);
> If we're going to open-code it to work around the compiler creating an
> infloop recursion to ffs(), at least code a straight-line version
> without branches, rather than the painfully slow bit-by-bit loop.
> There's plenty of examples on the web of writing ffs() by using
> bit-twiddling without branching.
This is roughly the same implementation we had before. I do not intend
to optimize this.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 11:25 ` Sebastian Huber
@ 2017-07-27 12:27 ` Corinna Vinschen
2017-07-27 12:34 ` Sebastian Huber
2017-07-27 21:03 ` Brian Inglis
1 sibling, 1 reply; 11+ messages in thread
From: Corinna Vinschen @ 2017-07-27 12:27 UTC (permalink / raw)
To: newlib
[-- Attachment #1: Type: text/plain, Size: 1619 bytes --]
On Jul 27 13:24, Sebastian Huber wrote:
> On 27/07/17 13:13, Eric Blake wrote:
>
> > On 07/27/2017 03:06 AM, Sebastian Huber wrote:
> > > Signed-off-by: Sebastian Huber<sebastian.huber@embedded-brains.de>
> > > ---
> > > newlib/libc/misc/ffs.c | 11 +++++++++++
> > > 1 file changed, 11 insertions(+)
> > >
> > > diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
> > > index ba5700920..a09cbd3bb 100644
> > > --- a/newlib/libc/misc/ffs.c
> > > +++ b/newlib/libc/misc/ffs.c
> > > @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
> > > int
> > > ffs(int i)
> > > {
> > > +#ifdef __LP64__
> > > + /* GCC would expand the __builtin_ffs() to ffs() in this case */
> > > + int bit;
> > > +
> > > + if (i == 0)
> > > + return (0);
> > > + for (bit = 1; !(i & 1); bit++)
> > > + i = (unsigned int)i >> 1;
> > > + return (bit);
> > If we're going to open-code it to work around the compiler creating an
> > infloop recursion to ffs(), at least code a straight-line version
> > without branches, rather than the painfully slow bit-by-bit loop.
> > There's plenty of examples on the web of writing ffs() by using
> > bit-twiddling without branching.
>
> This is roughly the same implementation we had before. I do not intend to
> optimize this.
Still, __LP64__ is unacceptable. Cygwin would be affected by this as
well and would have to revert to its former own ffs implementation.
Reverting to a C-based implementation should only be performed on a
case-by-case basis.
Thanks,
Corinna
--
Corinna Vinschen
Cygwin Maintainer
Red Hat
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 12:27 ` Corinna Vinschen
@ 2017-07-27 12:34 ` Sebastian Huber
2017-07-27 12:49 ` Corinna Vinschen
0 siblings, 1 reply; 11+ messages in thread
From: Sebastian Huber @ 2017-07-27 12:34 UTC (permalink / raw)
To: newlib
On 27/07/17 14:27, Corinna Vinschen wrote:
> On Jul 27 13:24, Sebastian Huber wrote:
>> On 27/07/17 13:13, Eric Blake wrote:
>>
>>> On 07/27/2017 03:06 AM, Sebastian Huber wrote:
>>>> Signed-off-by: Sebastian Huber<sebastian.huber@embedded-brains.de>
>>>> ---
>>>> newlib/libc/misc/ffs.c | 11 +++++++++++
>>>> 1 file changed, 11 insertions(+)
>>>>
>>>> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
>>>> index ba5700920..a09cbd3bb 100644
>>>> --- a/newlib/libc/misc/ffs.c
>>>> +++ b/newlib/libc/misc/ffs.c
>>>> @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
>>>> int
>>>> ffs(int i)
>>>> {
>>>> +#ifdef __LP64__
>>>> + /* GCC would expand the __builtin_ffs() to ffs() in this case */
>>>> + int bit;
>>>> +
>>>> + if (i == 0)
>>>> + return (0);
>>>> + for (bit = 1; !(i & 1); bit++)
>>>> + i = (unsigned int)i >> 1;
>>>> + return (bit);
>>> If we're going to open-code it to work around the compiler creating an
>>> infloop recursion to ffs(), at least code a straight-line version
>>> without branches, rather than the painfully slow bit-by-bit loop.
>>> There's plenty of examples on the web of writing ffs() by using
>>> bit-twiddling without branching.
>> This is roughly the same implementation we had before. I do not intend to
>> optimize this.
> Still, __LP64__ is unacceptable. Cygwin would be affected by this as
> well and would have to revert to its former own ffs implementation.
>
> Reverting to a C-based implementation should only be performed on a
> case-by-case basis.
Yes, so maybe something like this
#if defined(__LP64__) && defined(__riscv)
or a target-specific ffs.c file similar to memcpy.c, etc.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 12:34 ` Sebastian Huber
@ 2017-07-27 12:49 ` Corinna Vinschen
2017-07-27 12:53 ` Sebastian Huber
0 siblings, 1 reply; 11+ messages in thread
From: Corinna Vinschen @ 2017-07-27 12:49 UTC (permalink / raw)
To: newlib
[-- Attachment #1: Type: text/plain, Size: 2130 bytes --]
On Jul 27 14:33, Sebastian Huber wrote:
> On 27/07/17 14:27, Corinna Vinschen wrote:
>
> > On Jul 27 13:24, Sebastian Huber wrote:
> > > On 27/07/17 13:13, Eric Blake wrote:
> > >
> > > > On 07/27/2017 03:06 AM, Sebastian Huber wrote:
> > > > > Signed-off-by: Sebastian Huber<sebastian.huber@embedded-brains.de>
> > > > > ---
> > > > > newlib/libc/misc/ffs.c | 11 +++++++++++
> > > > > 1 file changed, 11 insertions(+)
> > > > >
> > > > > diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
> > > > > index ba5700920..a09cbd3bb 100644
> > > > > --- a/newlib/libc/misc/ffs.c
> > > > > +++ b/newlib/libc/misc/ffs.c
> > > > > @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
> > > > > int
> > > > > ffs(int i)
> > > > > {
> > > > > +#ifdef __LP64__
> > > > > + /* GCC would expand the __builtin_ffs() to ffs() in this case */
> > > > > + int bit;
> > > > > +
> > > > > + if (i == 0)
> > > > > + return (0);
> > > > > + for (bit = 1; !(i & 1); bit++)
> > > > > + i = (unsigned int)i >> 1;
> > > > > + return (bit);
> > > > If we're going to open-code it to work around the compiler creating an
> > > > infloop recursion to ffs(), at least code a straight-line version
> > > > without branches, rather than the painfully slow bit-by-bit loop.
> > > > There's plenty of examples on the web of writing ffs() by using
> > > > bit-twiddling without branching.
> > > This is roughly the same implementation we had before. I do not intend to
> > > optimize this.
> > Still, __LP64__ is unacceptable. Cygwin would be affected by this as
> > well and would have to revert to its former own ffs implementation.
> >
> > Reverting to a C-based implementation should only be performed on a
> > case-by-case basis.
>
> Yes, so maybe something like this
>
> #if defined(__LP64__) && defined(__riscv)
>
> or a target-specific ffs.c file similar to memcpy.c, etc.
I'm inclined to favor a target-specific file. This would also allow
to implement the replacement in assembler easily.
Corinna
--
Corinna Vinschen
Cygwin Maintainer
Red Hat
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 12:49 ` Corinna Vinschen
@ 2017-07-27 12:53 ` Sebastian Huber
0 siblings, 0 replies; 11+ messages in thread
From: Sebastian Huber @ 2017-07-27 12:53 UTC (permalink / raw)
To: newlib; +Cc: Kito Cheng
On 27/07/17 14:48, Corinna Vinschen wrote:
> On Jul 27 14:33, Sebastian Huber wrote:
>> On 27/07/17 14:27, Corinna Vinschen wrote:
>>
>>> On Jul 27 13:24, Sebastian Huber wrote:
>>>> On 27/07/17 13:13, Eric Blake wrote:
>>>>
>>>>> On 07/27/2017 03:06 AM, Sebastian Huber wrote:
>>>>>> Signed-off-by: Sebastian Huber<sebastian.huber@embedded-brains.de>
>>>>>> ---
>>>>>> newlib/libc/misc/ffs.c | 11 +++++++++++
>>>>>> 1 file changed, 11 insertions(+)
>>>>>>
>>>>>> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
>>>>>> index ba5700920..a09cbd3bb 100644
>>>>>> --- a/newlib/libc/misc/ffs.c
>>>>>> +++ b/newlib/libc/misc/ffs.c
>>>>>> @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
>>>>>> int
>>>>>> ffs(int i)
>>>>>> {
>>>>>> +#ifdef __LP64__
>>>>>> + /* GCC would expand the __builtin_ffs() to ffs() in this case */
>>>>>> + int bit;
>>>>>> +
>>>>>> + if (i == 0)
>>>>>> + return (0);
>>>>>> + for (bit = 1; !(i & 1); bit++)
>>>>>> + i = (unsigned int)i >> 1;
>>>>>> + return (bit);
>>>>> If we're going to open-code it to work around the compiler creating an
>>>>> infloop recursion to ffs(), at least code a straight-line version
>>>>> without branches, rather than the painfully slow bit-by-bit loop.
>>>>> There's plenty of examples on the web of writing ffs() by using
>>>>> bit-twiddling without branching.
>>>> This is roughly the same implementation we had before. I do not intend to
>>>> optimize this.
>>> Still, __LP64__ is unacceptable. Cygwin would be affected by this as
>>> well and would have to revert to its former own ffs implementation.
>>>
>>> Reverting to a C-based implementation should only be performed on a
>>> case-by-case basis.
>> Yes, so maybe something like this
>>
>> #if defined(__LP64__) && defined(__riscv)
>>
>> or a target-specific ffs.c file similar to memcpy.c, etc.
> I'm inclined to favor a target-specific file. This would also allow
> to implement the replacement in assembler easily.
Ok, good. So, this is something for the new RISC-V port.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Workaround for ffs() on LP64 targets
2017-07-27 11:25 ` Sebastian Huber
2017-07-27 12:27 ` Corinna Vinschen
@ 2017-07-27 21:03 ` Brian Inglis
1 sibling, 0 replies; 11+ messages in thread
From: Brian Inglis @ 2017-07-27 21:03 UTC (permalink / raw)
To: newlib
On 2017-07-27 05:24, Sebastian Huber wrote:
> On 27/07/17 13:13, Eric Blake wrote:
>> On 07/27/2017 03:06 AM, Sebastian Huber wrote:
>>> Signed-off-by: Sebastian Huber
>>> ---
>>> newlib/libc/misc/ffs.c | 11 +++++++++++
>>> 1 file changed, 11 insertions(+)
>>>
>>> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
>>> index ba5700920..a09cbd3bb 100644
>>> --- a/newlib/libc/misc/ffs.c
>>> +++ b/newlib/libc/misc/ffs.c
>>> @@ -31,6 +31,17 @@ No supporting OS subroutines are required. */
>>> int
>>> ffs(int i)
>>> {
>>> +#ifdef __LP64__
>>> + /* GCC would expand the __builtin_ffs() to ffs() in this case */
>>> + int bit;
>>> +
>>> + if (i == 0)
>>> + return (0);
>>> + for (bit = 1; !(i & 1); bit++)
>>> + i = (unsigned int)i >> 1;
>>> + return (bit);
>> If we're going to open-code it to work around the compiler creating an
>> infloop recursion to ffs(), at least code a straight-line version
>> without branches, rather than the painfully slow bit-by-bit loop.
>> There's plenty of examples on the web of writing ffs() by using
>> bit-twiddling without branching.
Definitive twiddling reference is now Hacker's Delight 2nd ed, Henry S. Warren,
Jr., 2013, Pearson/InformIT/AW; available in ebook formats:
https://github.com/jyfc/ebook/blob/master/02_algorithm/Hacker's%20Delight%202nd%20Edition.pdf
https://www.safaribooksonline.com/library/view/hackers-delight-second/9780133084993/
https://en.wikipedia.org/wiki/Hacker%27s_Delight
http://www.hackersdelight.org/
> This is roughly the same implementation we had before. I do not intend to
> optimize this.
Programmers using these functions expect the usage cost to be trivial and fairly
constant ~ O(log2(bits)) not O(bits); if not, they may implement their own!
Try this one, seems decently short; adjust for different word sizes; with gcc
-O3 on x86-64 compiles to 32 instructions branch free: YMMV
int
ffsll( long long in )
{
/* find first set == 1 + count trailing zeros */
int index = 64;
if (!in) return 0;
in &= -in; /* clear all but lsb set */
/*
* for ctz remove above test and add next line
* if (in) --index;
*/
if (in & 0x00000000FFFFFFFF) index -= 32;
if (in & 0x0000FFFF0000FFFF) index -= 16;
if (in & 0x00FF00FF00FF00FF) index -= 8;
if (in & 0x0F0F0F0F0F0F0F0F) index -= 4;
if (in & 0x3333333333333333) index -= 2;
if (in & 0x5555555555555555) index -= 1;
return index;
}
--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2017-07-27 21:03 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-27 8:06 [PATCH] Workaround for ffs() on LP64 targets Sebastian Huber
2017-07-27 8:29 ` Kito Cheng
2017-07-27 8:40 ` Sebastian Huber
2017-07-27 9:01 ` Kito Cheng
2017-07-27 11:13 ` Eric Blake
2017-07-27 11:25 ` Sebastian Huber
2017-07-27 12:27 ` Corinna Vinschen
2017-07-27 12:34 ` Sebastian Huber
2017-07-27 12:49 ` Corinna Vinschen
2017-07-27 12:53 ` Sebastian Huber
2017-07-27 21:03 ` Brian Inglis
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).