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

Connector Switch via libc-commits libc-commits at lists.llvm.org
Fri Mar 7 08:38:39 PST 2025


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

>From 6f2d8effaf0b224dde5a3951ba6a0c18acba0c22 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 5 Mar 2025 15:37:14 +0000
Subject: [PATCH 1/4] implement ffs

---
 libc/config/linux/aarch64/entrypoints.txt |  3 +++
 libc/config/linux/x86_64/entrypoints.txt  |  3 +++
 libc/include/strings.yaml                 | 18 ++++++++++++++
 libc/src/strings/CMakeLists.txt           | 24 ++++++++++++++++++
 libc/src/strings/ffs.cpp                  | 17 +++++++++++++
 libc/src/strings/ffs.h                    | 20 +++++++++++++++
 libc/src/strings/ffsl.cpp                 | 17 +++++++++++++
 libc/src/strings/ffsl.h                   | 20 +++++++++++++++
 libc/src/strings/ffsll.cpp                | 17 +++++++++++++
 libc/src/strings/ffsll.h                  | 20 +++++++++++++++
 libc/test/src/strings/CMakeLists.txt      | 30 +++++++++++++++++++++++
 libc/test/src/strings/ffs_test.cpp        | 25 +++++++++++++++++++
 libc/test/src/strings/ffsl_test.cpp       | 25 +++++++++++++++++++
 libc/test/src/strings/ffsll_test.cpp      | 25 +++++++++++++++++++
 14 files changed, 264 insertions(+)
 create mode 100644 libc/src/strings/ffs.cpp
 create mode 100644 libc/src/strings/ffs.h
 create mode 100644 libc/src/strings/ffsl.cpp
 create mode 100644 libc/src/strings/ffsl.h
 create mode 100644 libc/src/strings/ffsll.cpp
 create mode 100644 libc/src/strings/ffsll.h
 create mode 100644 libc/test/src/strings/ffs_test.cpp
 create mode 100644 libc/test/src/strings/ffsl_test.cpp
 create mode 100644 libc/test/src/strings/ffsll_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 6c2be4d3b0b99..c7beb3ef3fdfc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -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
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 779654422e35b..12dc87bf945fd 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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
diff --git a/libc/include/strings.yaml b/libc/include/strings.yaml
index b6aa8f6d60b27..012805269f775 100644
--- a/libc/include/strings.yaml
+++ b/libc/include/strings.yaml
@@ -29,6 +29,24 @@ functions:
     arguments:
       - type: void *
       - type: size_t
+  - name: ffs
+    standards: 
+    - POSIX
+    return_type: int
+    arguments:
+      - int
+  - name: ffsl
+    standards: 
+    - POSIX
+    return_type: int
+    arguments:
+      - long
+  - name: ffsll
+    standards: 
+    - POSIX
+    return_type: int
+    arguments:
+      - long long
   - name: index
     standards:
       - BSDExtensions
diff --git a/libc/src/strings/CMakeLists.txt b/libc/src/strings/CMakeLists.txt
index 5e84c7be1f7d6..aa0f8f51a5f59 100644
--- a/libc/src/strings/CMakeLists.txt
+++ b/libc/src/strings/CMakeLists.txt
@@ -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
diff --git a/libc/src/strings/ffs.cpp b/libc/src/strings/ffs.cpp
new file mode 100644
index 0000000000000..4cf565fc335e7
--- /dev/null
+++ b/libc/src/strings/ffs.cpp
@@ -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); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/ffs.h b/libc/src/strings/ffs.h
new file mode 100644
index 0000000000000..bf43c43caedb2
--- /dev/null
+++ b/libc/src/strings/ffs.h
@@ -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
diff --git a/libc/src/strings/ffsl.cpp b/libc/src/strings/ffsl.cpp
new file mode 100644
index 0000000000000..1373699d974ef
--- /dev/null
+++ b/libc/src/strings/ffsl.cpp
@@ -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
diff --git a/libc/src/strings/ffsl.h b/libc/src/strings/ffsl.h
new file mode 100644
index 0000000000000..1feca010b2ebb
--- /dev/null
+++ b/libc/src/strings/ffsl.h
@@ -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
diff --git a/libc/src/strings/ffsll.cpp b/libc/src/strings/ffsll.cpp
new file mode 100644
index 0000000000000..a0b6858c1a9ee
--- /dev/null
+++ b/libc/src/strings/ffsll.cpp
@@ -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
diff --git a/libc/src/strings/ffsll.h b/libc/src/strings/ffsll.h
new file mode 100644
index 0000000000000..f059b8ab89b4e
--- /dev/null
+++ b/libc/src/strings/ffsll.h
@@ -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
diff --git a/libc/test/src/strings/CMakeLists.txt b/libc/test/src/strings/CMakeLists.txt
index 10f96b8531f68..baa22bb449c6c 100644
--- a/libc/test/src/strings/CMakeLists.txt
+++ b/libc/test/src/strings/CMakeLists.txt
@@ -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
diff --git a/libc/test/src/strings/ffs_test.cpp b/libc/test/src/strings/ffs_test.cpp
new file mode 100644
index 0000000000000..5270837ec2ac0
--- /dev/null
+++ b/libc/test/src/strings/ffs_test.cpp
@@ -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
diff --git a/libc/test/src/strings/ffsl_test.cpp b/libc/test/src/strings/ffsl_test.cpp
new file mode 100644
index 0000000000000..1fcd2943fbb15
--- /dev/null
+++ b/libc/test/src/strings/ffsl_test.cpp
@@ -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
diff --git a/libc/test/src/strings/ffsll_test.cpp b/libc/test/src/strings/ffsll_test.cpp
new file mode 100644
index 0000000000000..1888e3161dad6
--- /dev/null
+++ b/libc/test/src/strings/ffsll_test.cpp
@@ -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

>From a0e76ae16878b781443eac3f7737340148159818 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 5 Mar 2025 15:46:34 +0000
Subject: [PATCH 2/4] fix ci

---
 libc/include/strings.yaml | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/include/strings.yaml b/libc/include/strings.yaml
index 012805269f775..4b9535d0f014a 100644
--- a/libc/include/strings.yaml
+++ b/libc/include/strings.yaml
@@ -30,20 +30,20 @@ functions:
       - type: void *
       - type: size_t
   - name: ffs
-    standards: 
-    - POSIX
+    standards:
+      - POSIX
     return_type: int
     arguments:
       - int
   - name: ffsl
-    standards: 
-    - POSIX
+    standards:
+      - POSIX
     return_type: int
     arguments:
       - long
   - name: ffsll
-    standards: 
-    - POSIX
+    standards:
+      - POSIX
     return_type: int
     arguments:
       - long long

>From f83e2c885ea3b69d8765f3e0896962bc2aecd527 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 5 Mar 2025 15:52:28 +0000
Subject: [PATCH 3/4] fix ci

---
 libc/include/strings.yaml | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/include/strings.yaml b/libc/include/strings.yaml
index 4b9535d0f014a..802f6533585f8 100644
--- a/libc/include/strings.yaml
+++ b/libc/include/strings.yaml
@@ -34,19 +34,19 @@ functions:
       - POSIX
     return_type: int
     arguments:
-      - int
+      - type: int
   - name: ffsl
     standards:
       - POSIX
     return_type: int
     arguments:
-      - long
+      - type: long
   - name: ffsll
     standards:
       - POSIX
     return_type: int
     arguments:
-      - long long
+      - type: long long
   - name: index
     standards:
       - BSDExtensions

>From ccc7f837c79a9c6e07948ba27fa1e03fce799d93 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 7 Mar 2025 16:38:27 +0000
Subject: [PATCH 4/4] use first trailing one

---
 libc/src/strings/CMakeLists.txt      |  6 ++++++
 libc/src/strings/ffs.cpp             |  5 ++++-
 libc/src/strings/ffsl.cpp            |  5 ++++-
 libc/src/strings/ffsll.cpp           |  5 ++++-
 libc/test/src/strings/ffs_test.cpp   | 11 +++++++++--
 libc/test/src/strings/ffsl_test.cpp  | 19 +++++++++++++++++--
 libc/test/src/strings/ffsll_test.cpp | 19 +++++++++++++++++--
 7 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/libc/src/strings/CMakeLists.txt b/libc/src/strings/CMakeLists.txt
index aa0f8f51a5f59..6d86680e8e71f 100644
--- a/libc/src/strings/CMakeLists.txt
+++ b/libc/src/strings/CMakeLists.txt
@@ -60,6 +60,8 @@ add_entrypoint_object(
     ffs.cpp
   HDRS
     ffs.h
+  DEPENDS
+    libc.src.__support.math_extras
 )
 
 add_entrypoint_object(
@@ -68,6 +70,8 @@ add_entrypoint_object(
     ffsl.cpp
   HDRS
     ffsl.h
+  DEPENDS
+    libc.src.__support.math_extras
 )
 
 add_entrypoint_object(
@@ -76,6 +80,8 @@ add_entrypoint_object(
     ffsll.cpp
   HDRS
     ffsll.h
+  DEPENDS
+    libc.src.__support.math_extras
 )
 
 add_entrypoint_object(
diff --git a/libc/src/strings/ffs.cpp b/libc/src/strings/ffs.cpp
index 4cf565fc335e7..5e1efafeab56a 100644
--- a/libc/src/strings/ffs.cpp
+++ b/libc/src/strings/ffs.cpp
@@ -9,9 +9,12 @@
 #include "src/strings/ffs.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/math_extras.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(int, ffs, (int i)) { return __builtin_ffs(i); }
+LLVM_LIBC_FUNCTION(int, ffs, (int i)) {
+  return first_trailing_one(static_cast<unsigned>(i));
+}
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/ffsl.cpp b/libc/src/strings/ffsl.cpp
index 1373699d974ef..b1b25598e405b 100644
--- a/libc/src/strings/ffsl.cpp
+++ b/libc/src/strings/ffsl.cpp
@@ -9,9 +9,12 @@
 #include "src/strings/ffsl.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/math_extras.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(int, ffsl, (long i)) { return __builtin_ffsl(i); }
+LLVM_LIBC_FUNCTION(int, ffsl, (long i)) {
+  return first_trailing_one(static_cast<unsigned long>(i));
+}
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/ffsll.cpp b/libc/src/strings/ffsll.cpp
index a0b6858c1a9ee..be16e81a54711 100644
--- a/libc/src/strings/ffsll.cpp
+++ b/libc/src/strings/ffsll.cpp
@@ -9,9 +9,12 @@
 #include "src/strings/ffsll.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/math_extras.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(int, ffsll, (long long i)) { return __builtin_ffsll(i); }
+LLVM_LIBC_FUNCTION(int, ffsll, (long long i)) {
+  return first_trailing_one(static_cast<unsigned long long>(i));
+}
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/strings/ffs_test.cpp b/libc/test/src/strings/ffs_test.cpp
index 5270837ec2ac0..17cd412b2df05 100644
--- a/libc/test/src/strings/ffs_test.cpp
+++ b/libc/test/src/strings/ffs_test.cpp
@@ -14,8 +14,15 @@
 namespace LIBC_NAMESPACE_DECL {
 
 TEST(LlvmLibcFfsTest, SimpleFfs) {
-  ASSERT_EQ(ffs(0), 0);
-  ASSERT_EQ(ffs(1), 1);
+  ASSERT_EQ(ffs(0x00000000), 0);
+  ASSERT_EQ(ffs(0x00000001), 1);
+  ASSERT_EQ(ffs(0x00000020), 6);
+  ASSERT_EQ(ffs(0x00000400), 11);
+  ASSERT_EQ(ffs(0x00008000), 16);
+  ASSERT_EQ(ffs(0x00010000), 17);
+  ASSERT_EQ(ffs(0x00200000), 22);
+  ASSERT_EQ(ffs(0x04000000), 27);
+  ASSERT_EQ(ffs(0x80000000), 32);
   ASSERT_EQ(ffs(0xfbe71), 1);
   ASSERT_EQ(ffs(0xfbe70), 5);
   ASSERT_EQ(ffs(0x10), 5);
diff --git a/libc/test/src/strings/ffsl_test.cpp b/libc/test/src/strings/ffsl_test.cpp
index 1fcd2943fbb15..e015a290f0738 100644
--- a/libc/test/src/strings/ffsl_test.cpp
+++ b/libc/test/src/strings/ffsl_test.cpp
@@ -14,8 +14,23 @@
 namespace LIBC_NAMESPACE_DECL {
 
 TEST(LlvmLibcFfslTest, SimpleFfsl) {
-  ASSERT_EQ(ffsl(0L), 0);
-  ASSERT_EQ(ffsl(1L), 1);
+  ASSERT_EQ(ffsl(0x00000000L), 0);
+  ASSERT_EQ(ffsl(0x00000001L), 1);
+  ASSERT_EQ(ffsl(0x00000020L), 6);
+  ASSERT_EQ(ffsl(0x00000400L), 11);
+  ASSERT_EQ(ffsl(0x00008000L), 16);
+  ASSERT_EQ(ffsl(0x00010000L), 17);
+  ASSERT_EQ(ffsl(0x00200000L), 22);
+  ASSERT_EQ(ffsl(0x04000000L), 27);
+  ASSERT_EQ(ffsl(0x80000000L), 32);
+  ASSERT_EQ(ffsl(0x0000000100000000L), 33);
+  ASSERT_EQ(ffsl(0x0000002000000000L), 38);
+  ASSERT_EQ(ffsl(0x0000040000000000L), 43);
+  ASSERT_EQ(ffsl(0x0000800000000000L), 48);
+  ASSERT_EQ(ffsl(0x0001000000000000L), 49);
+  ASSERT_EQ(ffsl(0x0020000000000000L), 54);
+  ASSERT_EQ(ffsl(0x0400000000000000L), 59);
+  ASSERT_EQ(ffsl(0x8000000000000000L), 64);
   ASSERT_EQ(ffsl(0xfbe71L), 1);
   ASSERT_EQ(ffsl(0xfbe70L), 5);
   ASSERT_EQ(ffsl(0x10L), 5);
diff --git a/libc/test/src/strings/ffsll_test.cpp b/libc/test/src/strings/ffsll_test.cpp
index 1888e3161dad6..bdd7a2ed30f3c 100644
--- a/libc/test/src/strings/ffsll_test.cpp
+++ b/libc/test/src/strings/ffsll_test.cpp
@@ -14,8 +14,23 @@
 namespace LIBC_NAMESPACE_DECL {
 
 TEST(LlvmLibcFfsllTest, SimpleFfsll) {
-  ASSERT_EQ(ffsll(0LL), 0);
-  ASSERT_EQ(ffsll(1LL), 1);
+  ASSERT_EQ(ffsll(0x0000000000000000LL), 0);
+  ASSERT_EQ(ffsll(0x0000000000000001LL), 1);
+  ASSERT_EQ(ffsll(0x0000000000000020LL), 6);
+  ASSERT_EQ(ffsll(0x0000000000000400LL), 11);
+  ASSERT_EQ(ffsll(0x0000000000008000LL), 16);
+  ASSERT_EQ(ffsll(0x0000000000010000LL), 17);
+  ASSERT_EQ(ffsll(0x0000000000200000LL), 22);
+  ASSERT_EQ(ffsll(0x0000000004000000LL), 27);
+  ASSERT_EQ(ffsll(0x0000000080000000LL), 32);
+  ASSERT_EQ(ffsll(0x0000000100000000LL), 33);
+  ASSERT_EQ(ffsll(0x0000002000000000LL), 38);
+  ASSERT_EQ(ffsll(0x0000040000000000LL), 43);
+  ASSERT_EQ(ffsll(0x0000800000000000LL), 48);
+  ASSERT_EQ(ffsll(0x0001000000000000LL), 49);
+  ASSERT_EQ(ffsll(0x0020000000000000LL), 54);
+  ASSERT_EQ(ffsll(0x0400000000000000LL), 59);
+  ASSERT_EQ(ffsll(0x8000000000000000LL), 64);
   ASSERT_EQ(ffsll(0xfbe71LL), 1);
   ASSERT_EQ(ffsll(0xfbe70LL), 5);
   ASSERT_EQ(ffsll(0x10LL), 5);



More information about the libc-commits mailing list