Skip to content

Commit ccc7f83

Browse files
committed
use first trailing one
1 parent 5409f31 commit ccc7f83

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

libc/src/strings/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ add_entrypoint_object(
6060
ffs.cpp
6161
HDRS
6262
ffs.h
63+
DEPENDS
64+
libc.src.__support.math_extras
6365
)
6466

6567
add_entrypoint_object(
@@ -68,6 +70,8 @@ add_entrypoint_object(
6870
ffsl.cpp
6971
HDRS
7072
ffsl.h
73+
DEPENDS
74+
libc.src.__support.math_extras
7175
)
7276

7377
add_entrypoint_object(
@@ -76,6 +80,8 @@ add_entrypoint_object(
7680
ffsll.cpp
7781
HDRS
7882
ffsll.h
83+
DEPENDS
84+
libc.src.__support.math_extras
7985
)
8086

8187
add_entrypoint_object(

libc/src/strings/ffs.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#include "src/strings/ffs.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/math_extras.h"
1213

1314
namespace LIBC_NAMESPACE_DECL {
1415

15-
LLVM_LIBC_FUNCTION(int, ffs, (int i)) { return __builtin_ffs(i); }
16+
LLVM_LIBC_FUNCTION(int, ffs, (int i)) {
17+
return first_trailing_one(static_cast<unsigned>(i));
18+
}
1619

1720
} // namespace LIBC_NAMESPACE_DECL

libc/src/strings/ffsl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#include "src/strings/ffsl.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/math_extras.h"
1213

1314
namespace LIBC_NAMESPACE_DECL {
1415

15-
LLVM_LIBC_FUNCTION(int, ffsl, (long i)) { return __builtin_ffsl(i); }
16+
LLVM_LIBC_FUNCTION(int, ffsl, (long i)) {
17+
return first_trailing_one(static_cast<unsigned long>(i));
18+
}
1619

1720
} // namespace LIBC_NAMESPACE_DECL

libc/src/strings/ffsll.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#include "src/strings/ffsll.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/math_extras.h"
1213

1314
namespace LIBC_NAMESPACE_DECL {
1415

15-
LLVM_LIBC_FUNCTION(int, ffsll, (long long i)) { return __builtin_ffsll(i); }
16+
LLVM_LIBC_FUNCTION(int, ffsll, (long long i)) {
17+
return first_trailing_one(static_cast<unsigned long long>(i));
18+
}
1619

1720
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/strings/ffs_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
TEST(LlvmLibcFfsTest, SimpleFfs) {
17-
ASSERT_EQ(ffs(0), 0);
18-
ASSERT_EQ(ffs(1), 1);
17+
ASSERT_EQ(ffs(0x00000000), 0);
18+
ASSERT_EQ(ffs(0x00000001), 1);
19+
ASSERT_EQ(ffs(0x00000020), 6);
20+
ASSERT_EQ(ffs(0x00000400), 11);
21+
ASSERT_EQ(ffs(0x00008000), 16);
22+
ASSERT_EQ(ffs(0x00010000), 17);
23+
ASSERT_EQ(ffs(0x00200000), 22);
24+
ASSERT_EQ(ffs(0x04000000), 27);
25+
ASSERT_EQ(ffs(0x80000000), 32);
1926
ASSERT_EQ(ffs(0xfbe71), 1);
2027
ASSERT_EQ(ffs(0xfbe70), 5);
2128
ASSERT_EQ(ffs(0x10), 5);

libc/test/src/strings/ffsl_test.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
TEST(LlvmLibcFfslTest, SimpleFfsl) {
17-
ASSERT_EQ(ffsl(0L), 0);
18-
ASSERT_EQ(ffsl(1L), 1);
17+
ASSERT_EQ(ffsl(0x00000000L), 0);
18+
ASSERT_EQ(ffsl(0x00000001L), 1);
19+
ASSERT_EQ(ffsl(0x00000020L), 6);
20+
ASSERT_EQ(ffsl(0x00000400L), 11);
21+
ASSERT_EQ(ffsl(0x00008000L), 16);
22+
ASSERT_EQ(ffsl(0x00010000L), 17);
23+
ASSERT_EQ(ffsl(0x00200000L), 22);
24+
ASSERT_EQ(ffsl(0x04000000L), 27);
25+
ASSERT_EQ(ffsl(0x80000000L), 32);
26+
ASSERT_EQ(ffsl(0x0000000100000000L), 33);
27+
ASSERT_EQ(ffsl(0x0000002000000000L), 38);
28+
ASSERT_EQ(ffsl(0x0000040000000000L), 43);
29+
ASSERT_EQ(ffsl(0x0000800000000000L), 48);
30+
ASSERT_EQ(ffsl(0x0001000000000000L), 49);
31+
ASSERT_EQ(ffsl(0x0020000000000000L), 54);
32+
ASSERT_EQ(ffsl(0x0400000000000000L), 59);
33+
ASSERT_EQ(ffsl(0x8000000000000000L), 64);
1934
ASSERT_EQ(ffsl(0xfbe71L), 1);
2035
ASSERT_EQ(ffsl(0xfbe70L), 5);
2136
ASSERT_EQ(ffsl(0x10L), 5);

libc/test/src/strings/ffsll_test.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
TEST(LlvmLibcFfsllTest, SimpleFfsll) {
17-
ASSERT_EQ(ffsll(0LL), 0);
18-
ASSERT_EQ(ffsll(1LL), 1);
17+
ASSERT_EQ(ffsll(0x0000000000000000LL), 0);
18+
ASSERT_EQ(ffsll(0x0000000000000001LL), 1);
19+
ASSERT_EQ(ffsll(0x0000000000000020LL), 6);
20+
ASSERT_EQ(ffsll(0x0000000000000400LL), 11);
21+
ASSERT_EQ(ffsll(0x0000000000008000LL), 16);
22+
ASSERT_EQ(ffsll(0x0000000000010000LL), 17);
23+
ASSERT_EQ(ffsll(0x0000000000200000LL), 22);
24+
ASSERT_EQ(ffsll(0x0000000004000000LL), 27);
25+
ASSERT_EQ(ffsll(0x0000000080000000LL), 32);
26+
ASSERT_EQ(ffsll(0x0000000100000000LL), 33);
27+
ASSERT_EQ(ffsll(0x0000002000000000LL), 38);
28+
ASSERT_EQ(ffsll(0x0000040000000000LL), 43);
29+
ASSERT_EQ(ffsll(0x0000800000000000LL), 48);
30+
ASSERT_EQ(ffsll(0x0001000000000000LL), 49);
31+
ASSERT_EQ(ffsll(0x0020000000000000LL), 54);
32+
ASSERT_EQ(ffsll(0x0400000000000000LL), 59);
33+
ASSERT_EQ(ffsll(0x8000000000000000LL), 64);
1934
ASSERT_EQ(ffsll(0xfbe71LL), 1);
2035
ASSERT_EQ(ffsll(0xfbe70LL), 5);
2136
ASSERT_EQ(ffsll(0x10LL), 5);

0 commit comments

Comments
 (0)