From: Keir Fraser Date: Thu, 28 May 2009 08:41:59 +0000 (+0100) Subject: minios: implement ffs, ffsl and ffsll. X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=7a7d0a3db9562c6442a7c5eff8b26a9ca67f2803;p=people%2Fliuw%2Flibxenctrl-split%2Fmini-os.git minios: implement ffs, ffsl and ffsll. The first function is compiled only in case minios is compiled without newlib, since newlib already provides an implementation for ffs. On the other hand ffsl and ffsll are always compiled because newlib misses those functions. This patch also provides an implementation for __ffsti2 and __ffsdi2 because they are needed by gcc in order to successfully link ffsll. Signed-off-by: Stefano Stabellini --- diff --git a/include/posix/strings.h b/include/posix/strings.h index 8619ba2..4957c41 100644 --- a/include/posix/strings.h +++ b/include/posix/strings.h @@ -5,4 +5,8 @@ #define bzero(ptr, size) (memset((ptr), '\0', (size)), (void) 0) +int ffs (int i); +int ffsl (long int li); +int ffsll (long long int lli); + #endif /* _POSIX_STRINGS_H */ diff --git a/lib/string.c b/lib/string.c index f6dfa60..a6f2abe 100644 --- a/lib/string.c +++ b/lib/string.c @@ -18,6 +18,43 @@ **************************************************************************** */ +#include + +/* newlib defines ffs but not ffsll or ffsl */ +int __ffsti2 (long long int lli) +{ + int i, num, t, tmpint, len; + + num = sizeof(long long int) / sizeof(int); + if (num == 1) return (ffs((int) lli)); + len = sizeof(int) * 8; + + for (i = 0; i < num; i++) { + tmpint = (int) (((lli >> len) << len) ^ lli); + + t = ffs(tmpint); + if (t) + return (t + i * len); + lli = lli >> len; + } + return 0; +} + +int __ffsdi2 (long int li) +{ + return __ffsti2 ((long long int) li); +} + +int ffsl (long int li) +{ + return __ffsti2 ((long long int) li); +} + +int ffsll (long long int lli) +{ + return __ffsti2 (lli); +} + #if !defined HAVE_LIBC #include @@ -175,4 +212,17 @@ char *strdup(const char *x) return res; } +int ffs(int i) +{ + int c = 1; + + do { + if (i & 1) + return (c); + i = i >> 1; + c++; + } while (i); + return 0; +} + #endif