This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Workaround for ffs() on LP64 targets


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]