Skip to content

Commit a5588b6

Browse files
authored
[libc] implement strings/ffs (llvm#129892)
This patch adds the `strings/ffs` function. ref: https://pubs.opengroup.org/onlinepubs/9799919799/functions/ffs.html Closes: llvm#122054.
1 parent 15869a8 commit a5588b6

File tree

14 files changed

+318
-0
lines changed

14 files changed

+318
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ set(TARGET_LIBC_ENTRYPOINTS
9393
libc.src.strings.bcmp
9494
libc.src.strings.bcopy
9595
libc.src.strings.bzero
96+
libc.src.strings.ffs
97+
libc.src.strings.ffsl
98+
libc.src.strings.ffsll
9699
libc.src.strings.index
97100
libc.src.strings.rindex
98101
libc.src.strings.strcasecmp

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ set(TARGET_LIBC_ENTRYPOINTS
9393
libc.src.strings.bcmp
9494
libc.src.strings.bcopy
9595
libc.src.strings.bzero
96+
libc.src.strings.ffs
97+
libc.src.strings.ffsl
98+
libc.src.strings.ffsll
9699
libc.src.strings.index
97100
libc.src.strings.rindex
98101
libc.src.strings.strcasecmp

libc/include/strings.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ functions:
2929
arguments:
3030
- type: void *
3131
- type: size_t
32+
- name: ffs
33+
standards:
34+
- POSIX
35+
return_type: int
36+
arguments:
37+
- type: int
38+
- name: ffsl
39+
standards:
40+
- POSIX
41+
return_type: int
42+
arguments:
43+
- type: long
44+
- name: ffsll
45+
standards:
46+
- POSIX
47+
return_type: int
48+
arguments:
49+
- type: long long
3250
- name: index
3351
standards:
3452
- BSDExtensions

libc/src/strings/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ add_entrypoint_object(
5454
bcopy.h
5555
)
5656

57+
add_entrypoint_object(
58+
ffs
59+
SRCS
60+
ffs.cpp
61+
HDRS
62+
ffs.h
63+
DEPENDS
64+
libc.src.__support.math_extras
65+
)
66+
67+
add_entrypoint_object(
68+
ffsl
69+
SRCS
70+
ffsl.cpp
71+
HDRS
72+
ffsl.h
73+
DEPENDS
74+
libc.src.__support.math_extras
75+
)
76+
77+
add_entrypoint_object(
78+
ffsll
79+
SRCS
80+
ffsll.cpp
81+
HDRS
82+
ffsll.h
83+
DEPENDS
84+
libc.src.__support.math_extras
85+
)
86+
5787
add_entrypoint_object(
5888
index
5989
SRCS

libc/src/strings/ffs.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of ffs ---------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/strings/ffs.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/__support/math_extras.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(int, ffs, (int i)) {
17+
return first_trailing_one(static_cast<unsigned>(i));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE_DECL

libc/src/strings/ffs.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for ffs ---------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRINGS_FFS_H
10+
#define LLVM_LIBC_SRC_STRINGS_FFS_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int ffs(int i);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_STRINGS_FFS_H

libc/src/strings/ffsl.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of ffsl --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/strings/ffsl.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/__support/math_extras.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(int, ffsl, (long i)) {
17+
return first_trailing_one(static_cast<unsigned long>(i));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE_DECL

libc/src/strings/ffsl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for ffsl --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRINGS_FFSL_H
10+
#define LLVM_LIBC_SRC_STRINGS_FFSL_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int ffsl(long i);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_STRINGS_FFSL_H

libc/src/strings/ffsll.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of ffsll -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/strings/ffsll.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/__support/math_extras.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(int, ffsll, (long long i)) {
17+
return first_trailing_one(static_cast<unsigned long long>(i));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE_DECL

libc/src/strings/ffsll.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for ffsll -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRINGS_FFSLL_H
10+
#define LLVM_LIBC_SRC_STRINGS_FFSLL_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int ffsll(long long i);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_STRINGS_FFSLL_H

0 commit comments

Comments
 (0)