Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.ffs
libc.src.strings.ffsl
libc.src.strings.ffsll
libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.ffs
libc.src.strings.ffsl
libc.src.strings.ffsll
libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
Expand Down
18 changes: 18 additions & 0 deletions libc/include/strings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ functions:
arguments:
- type: void *
- type: size_t
- name: ffs
standards:
- POSIX
return_type: int
arguments:
- type: int
- name: ffsl
standards:
- POSIX
return_type: int
arguments:
- type: long
- name: ffsll
standards:
- POSIX
return_type: int
arguments:
- type: long long
- name: index
standards:
- BSDExtensions
Expand Down
24 changes: 24 additions & 0 deletions libc/src/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ add_entrypoint_object(
bcopy.h
)

add_entrypoint_object(
ffs
SRCS
ffs.cpp
HDRS
ffs.h
)

add_entrypoint_object(
ffsl
SRCS
ffsl.cpp
HDRS
ffsl.h
)

add_entrypoint_object(
ffsll
SRCS
ffsll.cpp
HDRS
ffsll.h
)

add_entrypoint_object(
index
SRCS
Expand Down
17 changes: 17 additions & 0 deletions libc/src/strings/ffs.cpp
Original file line number Diff line number Diff line change
@@ -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); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't these functions the same spec as stdc_first_trailing_one? If so then reuse its implementation at https://github.com/llvm/llvm-project/blob/main/libc/src/stdbit/stdc_first_trailing_one_ui.cpp#L18, in case the builtins are not available, or making callback to the function itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the ctz builtins and the find first one function seems to just invert the mask and use ctz. ffs is basically just ctz + 1 AFAIK.

Copy link
Contributor Author

@c8ef c8ef Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @jhuber6 points out, first_trailing_one calls cpp::countr_zero, which in turn calls __builtin_ctzg. Ultimately, we're using built-in functions, so perhaps we could directly use the ffs built-in here instead?

Okay, I see that it uses __has_builtin to check if the built-in is available. In that case, maybe we can use cpp::countr_zero(i) + 1 here instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just be aware the codegen is slightly different. I would guess that the builtin is always faster, but I haven't benchmarked anything https://godbolt.org/z/8KohTj5Md.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other constraints that make them different? If not then it seems like some missed optimization by the compiler(s).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the codegen is still not very good:

   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   8d 4f 01                lea    0x1(%rdi),%ecx
   7:   85 ff                   test   %edi,%edi
   9:   74 06                   je     11 <_ZN22__llvm_libc_21_0_0_git3ffsEi+0x11>
   b:   f3 0f bc d7             tzcnt  %edi,%edx
   f:   eb 05                   jmp    16 <_ZN22__llvm_libc_21_0_0_git3ffsEi+0x16>
  11:   ba 20 00 00 00          mov    $0x20,%edx
  16:   ff c2                   inc    %edx
  18:   31 c0                   xor    %eax,%eax
  1a:   83 f9 02                cmp    $0x2,%ecx
  1d:   0f 43 c2                cmovae %edx,%eax
  20:   5d                      pop    %rbp
  21:   c3                      ret    

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@c8ef c8ef Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #130155, we need to handle two special cases: when the input is 0 and when the input is the unsigned maximum value. This requirement codegen less elegant than we'd like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you follow the godbolt link https://godbolt.org/z/xafvqha4c , (value == 0) ? 0 : countr_zero(value) + 1 is identical to __builtin_ffs in both 0 and unsigned + signed maximum values.

What I really meant is that our current implementations and test cases for stdc_first_trailing_one are wrong on both 0 and unsigned maximum values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it finally. Sorry for being careless. I'll update this tonight.


} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/src/strings/ffs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for ffs ---------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STRINGS_FFS_H
#define LLVM_LIBC_SRC_STRINGS_FFS_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int ffs(int i);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRINGS_FFS_H
17 changes: 17 additions & 0 deletions libc/src/strings/ffsl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Implementation of ffsl --------------------------------------------===//
//
// 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/ffsl.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, ffsl, (long i)) { return __builtin_ffsl(i); }

} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/src/strings/ffsl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for ffsl --------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STRINGS_FFSL_H
#define LLVM_LIBC_SRC_STRINGS_FFSL_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int ffsl(long i);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRINGS_FFSL_H
17 changes: 17 additions & 0 deletions libc/src/strings/ffsll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Implementation of ffsll -------------------------------------------===//
//
// 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/ffsll.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, ffsll, (long long i)) { return __builtin_ffsll(i); }

} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/src/strings/ffsll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for ffsll -------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STRINGS_FFSLL_H
#define LLVM_LIBC_SRC_STRINGS_FFSLL_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int ffsll(long long i);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRINGS_FFSLL_H
30 changes: 30 additions & 0 deletions libc/test/src/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ add_libc_test(
LibcMemoryHelpers
)

add_libc_test(
ffs_test
SUITE
libc-strings-tests
SRCS
ffs_test.cpp
DEPENDS
libc.src.strings.ffs
)

add_libc_test(
ffsl_test
SUITE
libc-strings-tests
SRCS
ffsl_test.cpp
DEPENDS
libc.src.strings.ffsl
)

add_libc_test(
ffsll_test
SUITE
libc-strings-tests
SRCS
ffsll_test.cpp
DEPENDS
libc.src.strings.ffsll
)

add_libc_test(
index_test
SUITE
Expand Down
25 changes: 25 additions & 0 deletions libc/test/src/strings/ffs_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Unittests for 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/macros/config.h"
#include "test/UnitTest/Test.h"

namespace LIBC_NAMESPACE_DECL {

TEST(LlvmLibcFfsTest, SimpleFfs) {
ASSERT_EQ(ffs(0), 0);
ASSERT_EQ(ffs(1), 1);
ASSERT_EQ(ffs(0xfbe71), 1);
ASSERT_EQ(ffs(0xfbe70), 5);
ASSERT_EQ(ffs(0x10), 5);
ASSERT_EQ(ffs(0x100), 9);
}

} // namespace LIBC_NAMESPACE_DECL
25 changes: 25 additions & 0 deletions libc/test/src/strings/ffsl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Unittests for ffsl ------------------------------------------------===//
//
// 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/ffsl.h"

#include "src/__support/macros/config.h"
#include "test/UnitTest/Test.h"

namespace LIBC_NAMESPACE_DECL {

TEST(LlvmLibcFfslTest, SimpleFfsl) {
ASSERT_EQ(ffsl(0L), 0);
ASSERT_EQ(ffsl(1L), 1);
ASSERT_EQ(ffsl(0xfbe71L), 1);
ASSERT_EQ(ffsl(0xfbe70L), 5);
ASSERT_EQ(ffsl(0x10L), 5);
ASSERT_EQ(ffsl(0x100L), 9);
}

} // namespace LIBC_NAMESPACE_DECL
25 changes: 25 additions & 0 deletions libc/test/src/strings/ffsll_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Unittests for ffsll -----------------------------------------------===//
//
// 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/ffsll.h"

#include "src/__support/macros/config.h"
#include "test/UnitTest/Test.h"

namespace LIBC_NAMESPACE_DECL {

TEST(LlvmLibcFfsllTest, SimpleFfsll) {
ASSERT_EQ(ffsll(0LL), 0);
ASSERT_EQ(ffsll(1LL), 1);
ASSERT_EQ(ffsll(0xfbe71LL), 1);
ASSERT_EQ(ffsll(0xfbe70LL), 5);
ASSERT_EQ(ffsll(0x10LL), 5);
ASSERT_EQ(ffsll(0x100LL), 9);
}

} // namespace LIBC_NAMESPACE_DECL
Loading