Browse Source

LibC: Add ffs{,l,ll}

Those are wrappers around AK::bit_scan_forward().
demostanis 2 years ago
parent
commit
09a2db89c9
2 changed files with 22 additions and 0 deletions
  1. 19 0
      Userland/Libraries/LibC/strings.cpp
  2. 3 0
      Userland/Libraries/LibC/strings.h

+ 19 - 0
Userland/Libraries/LibC/strings.cpp

@@ -4,6 +4,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <AK/BuiltinWrappers.h>
 #include <assert.h>
 #include <ctype.h>
 #include <string.h>
@@ -51,4 +52,22 @@ int strncasecmp(char const* s1, char const* s2, size_t n)
     } while (--n);
     return 0;
 }
+
+// https://pubs.opengroup.org/onlinepubs/009696799/functions/ffs.html
+int ffs(int i)
+{
+    return bit_scan_forward(i);
+}
+
+// https://linux.die.net/man/3/ffsl (GNU extension)
+int ffsl(long int i)
+{
+    return bit_scan_forward(i);
+}
+
+// https://linux.die.net/man/3/ffsll (GNU extension)
+int ffsll(long long int i)
+{
+    return bit_scan_forward(i);
+}
 }

+ 3 - 0
Userland/Libraries/LibC/strings.h

@@ -15,5 +15,8 @@ int strcasecmp(char const*, char const*);
 int strncasecmp(char const*, char const*, size_t);
 void bzero(void*, size_t);
 void bcopy(void const*, void*, size_t);
+int ffs(int);
+int ffsl(long int);
+int ffsll(long long int);
 
 __END_DECLS