Optimize ffs() implementation

- Use gcc builtin
- Define as a static inline function in the .h so no function call overhead is needed
- Keep the function in libroot for backwards compatibility
- Remove a duplicate implementation in the freebsd compatibility layer

gcc2 does not document the builtin, but it is in fact already available
there as well.

Fixes #3281.

Change-Id: I94f8a2548637aa70e85febbfab06f07c1a427005
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2605
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Adrien Destugues
2020-05-08 14:01:53 +00:00
committed by waddlesplash
parent 0cc9a12ae0
commit 2dc597d50d
3 changed files with 4 additions and 33 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
extern "C" {
#endif
extern int ffs(int i);
static inline int ffs(int i) { return __builtin_ffs(i); }
extern int strcasecmp(const char *string1, const char *string2);
extern int strncasecmp(const char *string1, const char *string2,
-17
View File
@@ -624,23 +624,6 @@ printf(const char *format, ...)
}
#ifndef __clang__
int
ffs(int value)
{
int i = 1;
if (value == 0)
return 0;
for (; !(value & 1); i++)
value >>= 1;
return i;
}
#endif
int
resource_int_value(const char *name, int unit, const char *resname,
int *result)
+3 -15
View File
@@ -1,24 +1,12 @@
/*
* Copyright 2005, Ingo Weinhold, [email protected].
* Copyright 2020, Adrien Destugues <[email protected]>.
* Distributed under the terms of the MIT License.
*/
#include <strings.h>
// find first (least significant) set bit
int
extern "C" int
ffs(int value)
{
if (!value)
return 0;
// ToDo: This can certainly be optimized (e.g. by binary search). Or not
// unlikely there's a single assembler instruction...
for (int i = 1; i <= (int)sizeof(value) * 8; i++, value >>= 1) {
if (value & 1)
return i;
}
// never gets here
return 0;
return __builtin_ffs(value);
}