public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Kito Cheng <kito.cheng@gmail.com>
To: Sebastian Huber <sebastian.huber@embedded-brains.de>
Cc: newlib@sourceware.org
Subject: Re: [PATCH] Workaround for ffs() on LP64 targets
Date: Thu, 27 Jul 2017 09:01:00 -0000	[thread overview]
Message-ID: <CA+yXCZDPLKZfqPtFWPKyfHzJSvWG3LMf2y=3p5EkVdky-LMiCg@mail.gmail.com> (raw)
In-Reply-To: <d2568bac-3098-e2a3-422f-091e76d96538@embedded-brains.de>

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

  reply	other threads:[~2017-07-27  9:01 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-27  8:06 Sebastian Huber
2017-07-27  8:29 ` Kito Cheng
2017-07-27  8:40   ` Sebastian Huber
2017-07-27  9:01     ` Kito Cheng [this message]
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

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='CA+yXCZDPLKZfqPtFWPKyfHzJSvWG3LMf2y=3p5EkVdky-LMiCg@mail.gmail.com' \
    --to=kito.cheng@gmail.com \
    --cc=newlib@sourceware.org \
    --cc=sebastian.huber@embedded-brains.de \
    /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).