Added ffs().
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11614 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -77,6 +77,8 @@ extern const char *strsignal(int signal);
|
|||||||
#define bcopy(source, dest, length) memcpy((dest), (source), (length))
|
#define bcopy(source, dest, length) memcpy((dest), (source), (length))
|
||||||
#define bzero(buffer, length) memset((buffer), 0, (length))
|
#define bzero(buffer, length) memset((buffer), 0, (length))
|
||||||
|
|
||||||
|
extern int ffs(int i);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2005, Ingo Weinhold, [email protected].
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
|
// find first (least significant) set bit
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user