From 09a2db89c98d9eb7ab9d528608f9cc7929468872 Mon Sep 17 00:00:00 2001 From: demostanis Date: Sat, 10 Sep 2022 18:15:57 +0200 Subject: [PATCH] LibC: Add ffs{,l,ll} Those are wrappers around AK::bit_scan_forward(). --- Userland/Libraries/LibC/strings.cpp | 19 +++++++++++++++++++ Userland/Libraries/LibC/strings.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibC/strings.cpp b/Userland/Libraries/LibC/strings.cpp index 0937cc2e1a..0614719335 100644 --- a/Userland/Libraries/LibC/strings.cpp +++ b/Userland/Libraries/LibC/strings.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -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); +} } diff --git a/Userland/Libraries/LibC/strings.h b/Userland/Libraries/LibC/strings.h index 1805bc887c..a87f8ea032 100644 --- a/Userland/Libraries/LibC/strings.h +++ b/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