[libc-commits] [libc] [libc] implement `strings/ffs` (PR #129892)

via libc-commits libc-commits at lists.llvm.org
Wed Mar 5 19:38:40 PST 2025


================
@@ -0,0 +1,17 @@
+//===-- Implementation of ffs ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/ffs.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, ffs, (int i)) { return __builtin_ffs(i); }
----------------
lntue wrote:

so `ffs` spec is identical to `stdc_first_trailing_one`, and in our `stdc_first_trailing_one` implementation, we already taken care of the edge cases: https://github.com/llvm/llvm-project/blob/main/libc/src/__support/math_extras.h#L141

Can you check if the generated code is the same with `__builtin_ffs`?  If so we can just use the same implementation, so that we don't have to add extra `has_builtin` checks.  If they are not, then you can add another implementation option when `__builtin_ffs` is available directly in `math_extras.h`, so that `stdc_first_trailing_one` can also take advantage of that builtin.

https://github.com/llvm/llvm-project/pull/129892


More information about the libc-commits mailing list