[libc-commits] [libc] [libc][stdbit] implement stdc_first_trailing_zero (C23) (PR #81526)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Feb 12 12:32:12 PST 2024


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/81526

>From 221d267aeaf00db63098a35af69a5cf9b0d50f4f Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 12 Feb 2024 09:40:01 -0800
Subject: [PATCH 1/4] [libc][stdbit] implement stdc_first_leading_one (C23)

---
 libc/config/linux/x86_64/entrypoints.txt      |  5 +++
 libc/docs/stdbit.rst                          | 40 +++++++++----------
 libc/include/llvm-libc-macros/stdbit-macros.h | 22 ++++++++++
 libc/spec/stdc.td                             | 10 ++++-
 libc/src/__support/CPP/bit.h                  | 29 ++++++++++++++
 libc/src/stdbit/CMakeLists.txt                |  1 +
 libc/src/stdbit/stdc_first_leading_one_uc.cpp | 20 ++++++++++
 libc/src/stdbit/stdc_first_leading_one_uc.h   | 18 +++++++++
 libc/src/stdbit/stdc_first_leading_one_ui.cpp | 20 ++++++++++
 libc/src/stdbit/stdc_first_leading_one_ui.h   | 18 +++++++++
 libc/src/stdbit/stdc_first_leading_one_ul.cpp | 20 ++++++++++
 libc/src/stdbit/stdc_first_leading_one_ul.h   | 18 +++++++++
 .../src/stdbit/stdc_first_leading_one_ull.cpp | 21 ++++++++++
 libc/src/stdbit/stdc_first_leading_one_ull.h  | 18 +++++++++
 libc/src/stdbit/stdc_first_leading_one_us.cpp | 21 ++++++++++
 libc/src/stdbit/stdc_first_leading_one_us.h   | 18 +++++++++
 libc/test/include/stdbit_test.cpp             | 15 +++++++
 libc/test/src/__support/CPP/bit_test.cpp      |  7 ++++
 libc/test/src/stdbit/CMakeLists.txt           |  1 +
 .../stdbit/stdc_first_leading_one_uc_test.cpp | 21 ++++++++++
 .../stdbit/stdc_first_leading_one_ui_test.cpp | 21 ++++++++++
 .../stdbit/stdc_first_leading_one_ul_test.cpp | 21 ++++++++++
 .../stdc_first_leading_one_ull_test.cpp       | 21 ++++++++++
 .../stdbit/stdc_first_leading_one_us_test.cpp | 21 ++++++++++
 24 files changed, 405 insertions(+), 22 deletions(-)
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_uc.cpp
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_uc.h
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_ui.cpp
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_ui.h
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_ul.cpp
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_ul.h
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_ull.cpp
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_ull.h
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_us.cpp
 create mode 100644 libc/src/stdbit/stdc_first_leading_one_us.h
 create mode 100644 libc/test/src/stdbit/stdc_first_leading_one_uc_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_leading_one_ui_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_leading_one_ul_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_leading_one_ull_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_leading_one_us_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 75e39ae51fdb06..fc30bcf56665c7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -117,6 +117,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_first_leading_zero_ui
     libc.src.stdbit.stdc_first_leading_zero_ul
     libc.src.stdbit.stdc_first_leading_zero_ull
+    libc.src.stdbit.stdc_first_leading_one_uc
+    libc.src.stdbit.stdc_first_leading_one_us
+    libc.src.stdbit.stdc_first_leading_one_ui
+    libc.src.stdbit.stdc_first_leading_one_ul
+    libc.src.stdbit.stdc_first_leading_one_ull
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index 4f242d21f8b842..c4e49a8228683e 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -41,26 +41,26 @@ stdc_leading_ones_us         |check|
 stdc_leading_ones_ui         |check|
 stdc_leading_ones_ul         |check|
 stdc_leading_ones_ull        |check|
-stdc_trailing_zeros_uc
-stdc_trailing_zeros_us
-stdc_trailing_zeros_ui
-stdc_trailing_zeros_ul
-stdc_trailing_zeros_ull
-stdc_trailing_ones_uc
-stdc_trailing_ones_us
-stdc_trailing_ones_ui
-stdc_trailing_ones_ul
-stdc_trailing_ones_ull
-stdc_first_leading_zero_uc
-stdc_first_leading_zero_us
-stdc_first_leading_zero_ui
-stdc_first_leading_zero_ul
-stdc_first_leading_zero_ull
-stdc_first_leading_one_uc
-stdc_first_leading_one_us
-stdc_first_leading_one_ui
-stdc_first_leading_one_ul
-stdc_first_leading_one_ull
+stdc_trailing_zeros_uc       |check|
+stdc_trailing_zeros_us       |check|
+stdc_trailing_zeros_ui       |check|
+stdc_trailing_zeros_ul       |check|
+stdc_trailing_zeros_ull      |check|
+stdc_trailing_ones_uc        |check|
+stdc_trailing_ones_us        |check|
+stdc_trailing_ones_ui        |check|
+stdc_trailing_ones_ul        |check|
+stdc_trailing_ones_ull       |check|
+stdc_first_leading_zero_uc   |check|
+stdc_first_leading_zero_us   |check|
+stdc_first_leading_zero_ui   |check|
+stdc_first_leading_zero_ul   |check|
+stdc_first_leading_zero_ull  |check|
+stdc_first_leading_one_uc    |check|
+stdc_first_leading_one_us    |check|
+stdc_first_leading_one_ui    |check|
+stdc_first_leading_one_ul    |check|
+stdc_first_leading_one_ull   |check|
 stdc_first_trailing_zero_uc
 stdc_first_trailing_zero_us
 stdc_first_trailing_zero_ui
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 693a45e63419ff..87c590e61e3999 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -86,6 +86,21 @@ inline unsigned stdc_first_leading_zero(unsigned long x) {
 inline unsigned stdc_first_leading_zero(unsigned long long x) {
   return stdc_first_leading_zero_ull(x);
 }
+inline unsigned stdc_first_leading_one(unsigned char x) {
+  return stdc_first_leading_one_uc(x);
+}
+inline unsigned stdc_first_leading_one(unsigned short x) {
+  return stdc_first_leading_one_us(x);
+}
+inline unsigned stdc_first_leading_one(unsigned x) {
+  return stdc_first_leading_one_ui(x);
+}
+inline unsigned stdc_first_leading_one(unsigned long x) {
+  return stdc_first_leading_one_ul(x);
+}
+inline unsigned stdc_first_leading_one(unsigned long long x) {
+  return stdc_first_leading_one_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -122,6 +137,13 @@ inline unsigned stdc_first_leading_zero(unsigned long long x) {
       unsigned: stdc_first_leading_zero_ui,                                    \
       unsigned long: stdc_first_leading_zero_ul,                               \
       unsigned long long: stdc_first_leading_zero_ull)(x)
+#define stdc_first_leading_one(x)                                              \
+  _Generic((x),                                                                \
+      unsigned char: stdc_first_leading_one_uc,                                \
+      unsigned short: stdc_first_leading_one_us,                               \
+      unsigned: stdc_first_leading_one_ui,                                     \
+      unsigned long: stdc_first_leading_one_ul,                                \
+      unsigned long long: stdc_first_leading_one_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 1720a4a3c3aaf9..5e87831b907fb5 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -781,7 +781,8 @@ def StdC : StandardSpec<"stdc"> {
         Macro<"stdc_leading_ones">,
         Macro<"stdc_trailing_zeros">,
         Macro<"stdc_trailing_ones">,
-        Macro<"stdc_first_leading_zero">
+        Macro<"stdc_first_leading_zero">,
+        Macro<"stdc_first_leading_one">
       ], // Macros
       [], // Types
       [], // Enumerations
@@ -810,7 +811,12 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"stdc_first_leading_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_first_leading_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_first_leading_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_first_leading_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_first_leading_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_first_leading_one_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_first_leading_one_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_first_leading_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_first_leading_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 392fbe248138ae..579a2b514a6ddf 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -268,6 +268,35 @@ SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
 
 #undef SPECIALIZE_FLZ
 
+#define SPECIALIZE_FLO(NAME, TYPE, BUILTIN)                                    \
+  template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
+    static_assert(cpp::is_unsigned_v<TYPE>);                                   \
+    return value == static_cast<TYPE>(0)                                       \
+               ? 0                                                             \
+               : BUILTIN(static_cast<TYPE>(value)) + 1;                        \
+  }
+
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr int first_leading_one(T value) {
+  return value == static_cast<T>(0) ? 0
+                                    : countl_zero<T>(static_cast<T>(value)) + 1;
+}
+
+#if LIBC_HAS_BUILTIN(__builtin_clzs)
+SPECIALIZE_FLO(first_leading_one, unsigned short, __builtin_clzs)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clz)
+SPECIALIZE_FLO(first_leading_one, unsigned int, __builtin_clz)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzl)
+SPECIALIZE_FLO(first_leading_one, unsigned long, __builtin_clzl)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzll)
+SPECIALIZE_FLO(first_leading_one, unsigned long long, __builtin_clzll)
+#endif
+
+#undef SPECIALIZE_FLO
+
 } // namespace LIBC_NAMESPACE::cpp
 
 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 65d5f344307475..6ee93861b8db40 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -4,6 +4,7 @@ set(prefixes
   trailing_zeros
   trailing_ones
   first_leading_zero
+  first_leading_one
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_first_leading_one_uc.cpp b/libc/src/stdbit/stdc_first_leading_one_uc.cpp
new file mode 100644
index 00000000000000..02871595fdb6b8
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_first_leading_one_uc -----------------------===//
+//
+// 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/stdbit/stdc_first_leading_one_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_uc, (unsigned char value)) {
+  return static_cast<unsigned>(cpp::first_leading_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_leading_one_uc.h b/libc/src/stdbit/stdc_first_leading_one_uc.h
new file mode 100644
index 00000000000000..58892c3f0ff298
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_leading_one_uc -----*- 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_STDBIT_STDC_FIRST_LEADING_ONE_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_leading_one_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UC_H
diff --git a/libc/src/stdbit/stdc_first_leading_one_ui.cpp b/libc/src/stdbit/stdc_first_leading_one_ui.cpp
new file mode 100644
index 00000000000000..a6c7ef5a833914
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_first_leading_one_ui -----------------------===//
+//
+// 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/stdbit/stdc_first_leading_one_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::first_leading_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_leading_one_ui.h b/libc/src/stdbit/stdc_first_leading_one_ui.h
new file mode 100644
index 00000000000000..613adf4e1ff762
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_leading_one_ui -----*- 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_STDBIT_STDC_FIRST_LEADING_ONE_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_leading_one_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UI_H
diff --git a/libc/src/stdbit/stdc_first_leading_one_ul.cpp b/libc/src/stdbit/stdc_first_leading_one_ul.cpp
new file mode 100644
index 00000000000000..d1bcab5dda02a9
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_first_leading_one_ul -----------------------===//
+//
+// 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/stdbit/stdc_first_leading_one_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_ul, (unsigned long value)) {
+  return static_cast<unsigned>(cpp::first_leading_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_leading_one_ul.h b/libc/src/stdbit/stdc_first_leading_one_ul.h
new file mode 100644
index 00000000000000..47c179f3fbacd1
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_leading_one_ul -----*- 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_STDBIT_STDC_FIRST_LEADING_ONE_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_leading_one_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UL_H
diff --git a/libc/src/stdbit/stdc_first_leading_one_ull.cpp b/libc/src/stdbit/stdc_first_leading_one_ull.cpp
new file mode 100644
index 00000000000000..7be8f1051ec231
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_first_leading_one_ull ----------------------===//
+//
+// 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/stdbit/stdc_first_leading_one_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_ull,
+                   (unsigned long long value)) {
+  return static_cast<unsigned>(cpp::first_leading_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_leading_one_ull.h b/libc/src/stdbit/stdc_first_leading_one_ull.h
new file mode 100644
index 00000000000000..344d03f7100f47
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_leading_one_ull ----*- 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_STDBIT_STDC_FIRST_LEADING_ONE_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_leading_one_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_ULL_H
diff --git a/libc/src/stdbit/stdc_first_leading_one_us.cpp b/libc/src/stdbit/stdc_first_leading_one_us.cpp
new file mode 100644
index 00000000000000..7a4c7e673f367f
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_us.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_first_leading_one_us -----------------------===//
+//
+// 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/stdbit/stdc_first_leading_one_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_us,
+                   (unsigned short value)) {
+  return static_cast<unsigned>(cpp::first_leading_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_leading_one_us.h b/libc/src/stdbit/stdc_first_leading_one_us.h
new file mode 100644
index 00000000000000..9d5feaf1e92f08
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_leading_one_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_leading_one_us -----*- 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_STDBIT_STDC_FIRST_LEADING_ONE_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_leading_one_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 9a66a76de20bc5..c2fbcb8ce2d321 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -50,6 +50,13 @@ unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
 unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
   return 0xEFU;
 }
+unsigned stdc_first_leading_one_uc(unsigned char) noexcept { return 0xFAU; }
+unsigned stdc_first_leading_one_us(unsigned short) noexcept { return 0xFBU; }
+unsigned stdc_first_leading_one_ui(unsigned) noexcept { return 0xFCU; }
+unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
+unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
+  return 0xFFU;
+}
 }
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
@@ -93,3 +100,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingZero) {
   EXPECT_EQ(stdc_first_leading_zero(0UL), 0xEDU);
   EXPECT_EQ(stdc_first_leading_zero(0ULL), 0xEFU);
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingOne) {
+  EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned char>(0U)), 0xFAU);
+  EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned short>(0U)), 0xFBU);
+  EXPECT_EQ(stdc_first_leading_one(0U), 0xFCU);
+  EXPECT_EQ(stdc_first_leading_one(0UL), 0xFDU);
+  EXPECT_EQ(stdc_first_leading_one(0ULL), 0xFFU);
+}
diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index 00d8ca5d293ace..a70726d4feb241 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -213,4 +213,11 @@ TYPED_TEST(LlvmLibcBitTest, FirstLeadingZero, UnsignedTypes) {
               cpp::numeric_limits<T>::digits - i);
 }
 
+TYPED_TEST(LlvmLibcBitTest, FirstLeadingOne, UnsignedTypes) {
+  EXPECT_EQ(first_leading_one<T>(static_cast<T>(0)), 0);
+  for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
+    EXPECT_EQ(first_leading_one<T>(T(1) << i),
+              cpp::numeric_limits<T>::digits - i);
+}
+
 } // namespace LIBC_NAMESPACE::cpp
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index bc7e49d186a0d5..e32663f88e8620 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -6,6 +6,7 @@ set(prefixes
   trailing_zeros
   trailing_ones
   first_leading_zero
+  first_leading_one
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_first_leading_one_uc_test.cpp b/libc/test/src/stdbit/stdc_first_leading_one_uc_test.cpp
new file mode 100644
index 00000000000000..b8c8db587098e4
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_leading_one_uc_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_first_leading_one_uc ---------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_leading_one_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstLeadingOneUcTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_uc(0U), 0U);
+}
+
+TEST(LlvmLibcStdcFirstLeadingOneUcTest, OneHot) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_uc(1U << i),
+              UCHAR_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_first_leading_one_ui_test.cpp b/libc/test/src/stdbit/stdc_first_leading_one_ui_test.cpp
new file mode 100644
index 00000000000000..319d7482c50f9e
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_leading_one_ui_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_first_leading_one_ui ---------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_leading_one_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstLeadingOneUiTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_ui(0U), 0U);
+}
+
+TEST(LlvmLibcStdcFirstLeadingOneUiTest, OneHot) {
+  for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_ui(1U << i),
+              UINT_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_first_leading_one_ul_test.cpp b/libc/test/src/stdbit/stdc_first_leading_one_ul_test.cpp
new file mode 100644
index 00000000000000..5884cec418ce2b
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_leading_one_ul_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_first_leading_one_ul ---------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_leading_one_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstLeadingOneUlTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_ul(0UL), 0U);
+}
+
+TEST(LlvmLibcStdcFirstLeadingOneUlTest, OneHot) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_ul(1UL << i),
+              ULONG_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_first_leading_one_ull_test.cpp b/libc/test/src/stdbit/stdc_first_leading_one_ull_test.cpp
new file mode 100644
index 00000000000000..bf57f16c1dcaaa
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_leading_one_ull_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_first_leading_one_ull --------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_leading_one_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstLeadingOneUllTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_ull(0ULL), 0U);
+}
+
+TEST(LlvmLibcStdcFirstLeadingOneUllTest, OneHot) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_ull(1ULL << i),
+              ULLONG_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_first_leading_one_us_test.cpp b/libc/test/src/stdbit/stdc_first_leading_one_us_test.cpp
new file mode 100644
index 00000000000000..e9488335d9b008
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_leading_one_us_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_first_leading_one_us ---------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_leading_one_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstLeadingOneUsTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_us(0U), 0U);
+}
+
+TEST(LlvmLibcStdcFirstLeadingOneUsTest, OneHot) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_one_us(1U << i),
+              USHRT_WIDTH - i);
+}

>From 2b7f787afc381d082c8116fbc67dee03cf2b8a4e Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 12 Feb 2024 10:23:05 -0800
Subject: [PATCH 2/4] add tg macros to doc

---
 libc/docs/stdbit.rst | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index c4e49a8228683e..5ff36dcb0f5929 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -116,10 +116,10 @@ __STDC_ENDIAN_BIG__
 __STDC_ENDIAN_NATIVE__
 stdc_leading_zeros         |check|
 stdc_leading_ones          |check|
-stdc_trailing_zeros
-stdc_trailing_ones
-stdc_first_leading_zero
-stdc_first_leading_one
+stdc_trailing_zeros        |check|
+stdc_trailing_ones         |check|
+stdc_first_leading_zero    |check|
+stdc_first_leading_one     |check|
 stdc_first_trailing_zero
 stdc_first_trailing_one
 stdc_count_zeros

>From be816525a7bb42297c625a58fb55af5e90aa1425 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 12 Feb 2024 10:22:12 -0800
Subject: [PATCH 3/4] [libc][stdbit] implement stdc_first_trailing_zero (C23)

---
 libc/config/linux/x86_64/entrypoints.txt      |  5 ++++
 libc/docs/stdbit.rst                          | 12 ++++----
 libc/include/llvm-libc-macros/stdbit-macros.h | 22 ++++++++++++++
 libc/spec/stdc.td                             | 10 +++++--
 libc/src/__support/CPP/bit.h                  | 30 +++++++++++++++++++
 libc/src/stdbit/CMakeLists.txt                |  1 +
 .../stdbit/stdc_first_trailing_zero_uc.cpp    | 21 +++++++++++++
 libc/src/stdbit/stdc_first_trailing_zero_uc.h | 18 +++++++++++
 .../stdbit/stdc_first_trailing_zero_ui.cpp    | 20 +++++++++++++
 libc/src/stdbit/stdc_first_trailing_zero_ui.h | 18 +++++++++++
 .../stdbit/stdc_first_trailing_zero_ul.cpp    | 21 +++++++++++++
 libc/src/stdbit/stdc_first_trailing_zero_ul.h | 18 +++++++++++
 .../stdbit/stdc_first_trailing_zero_ull.cpp   | 21 +++++++++++++
 .../src/stdbit/stdc_first_trailing_zero_ull.h | 18 +++++++++++
 .../stdbit/stdc_first_trailing_zero_us.cpp    | 21 +++++++++++++
 libc/src/stdbit/stdc_first_trailing_zero_us.h | 18 +++++++++++
 libc/test/include/stdbit_test.cpp             | 15 ++++++++++
 libc/test/src/__support/CPP/bit_test.cpp      |  6 ++++
 libc/test/src/stdbit/CMakeLists.txt           |  1 +
 .../stdc_first_trailing_zero_uc_test.cpp      | 20 +++++++++++++
 .../stdc_first_trailing_zero_ui_test.cpp      | 20 +++++++++++++
 .../stdc_first_trailing_zero_ul_test.cpp      | 20 +++++++++++++
 .../stdc_first_trailing_zero_ull_test.cpp     | 20 +++++++++++++
 .../stdc_first_trailing_zero_us_test.cpp      | 20 +++++++++++++
 24 files changed, 388 insertions(+), 8 deletions(-)
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_uc.cpp
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_uc.h
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_ui.cpp
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_ui.h
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_ul.cpp
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_ul.h
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_ull.cpp
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_ull.h
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_us.cpp
 create mode 100644 libc/src/stdbit/stdc_first_trailing_zero_us.h
 create mode 100644 libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index fc30bcf56665c7..d53b225e4d87e2 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -122,6 +122,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_first_leading_one_ui
     libc.src.stdbit.stdc_first_leading_one_ul
     libc.src.stdbit.stdc_first_leading_one_ull
+    libc.src.stdbit.stdc_first_trailing_zero_uc
+    libc.src.stdbit.stdc_first_trailing_zero_us
+    libc.src.stdbit.stdc_first_trailing_zero_ui
+    libc.src.stdbit.stdc_first_trailing_zero_ul
+    libc.src.stdbit.stdc_first_trailing_zero_ull
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index 5ff36dcb0f5929..790a747baaa2da 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -61,11 +61,11 @@ stdc_first_leading_one_us    |check|
 stdc_first_leading_one_ui    |check|
 stdc_first_leading_one_ul    |check|
 stdc_first_leading_one_ull   |check|
-stdc_first_trailing_zero_uc
-stdc_first_trailing_zero_us
-stdc_first_trailing_zero_ui
-stdc_first_trailing_zero_ul
-stdc_first_trailing_zero_ull
+stdc_first_trailing_zero_uc  |check|
+stdc_first_trailing_zero_us  |check|
+stdc_first_trailing_zero_ui  |check|
+stdc_first_trailing_zero_ul  |check|
+stdc_first_trailing_zero_ull |check|
 stdc_first_trailing_one_uc
 stdc_first_trailing_one_us
 stdc_first_trailing_one_ui
@@ -120,7 +120,7 @@ stdc_trailing_zeros        |check|
 stdc_trailing_ones         |check|
 stdc_first_leading_zero    |check|
 stdc_first_leading_one     |check|
-stdc_first_trailing_zero
+stdc_first_trailing_zero   |check|
 stdc_first_trailing_one
 stdc_count_zeros
 stdc_count_ones
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 87c590e61e3999..19b800f342e8ff 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -101,6 +101,21 @@ inline unsigned stdc_first_leading_one(unsigned long x) {
 inline unsigned stdc_first_leading_one(unsigned long long x) {
   return stdc_first_leading_one_ull(x);
 }
+inline unsigned stdc_first_trailing_zero(unsigned char x) {
+  return stdc_first_trailing_zero_uc(x);
+}
+inline unsigned stdc_first_trailing_zero(unsigned short x) {
+  return stdc_first_trailing_zero_us(x);
+}
+inline unsigned stdc_first_trailing_zero(unsigned x) {
+  return stdc_first_trailing_zero_ui(x);
+}
+inline unsigned stdc_first_trailing_zero(unsigned long x) {
+  return stdc_first_trailing_zero_ul(x);
+}
+inline unsigned stdc_first_trailing_zero(unsigned long long x) {
+  return stdc_first_trailing_zero_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -144,6 +159,13 @@ inline unsigned stdc_first_leading_one(unsigned long long x) {
       unsigned: stdc_first_leading_one_ui,                                     \
       unsigned long: stdc_first_leading_one_ul,                                \
       unsigned long long: stdc_first_leading_one_ull)(x)
+#define stdc_first_trailing_zero(x)                                             \
+  _Generic((x),                                                                \
+      unsigned char: stdc_first_trailing_zero_uc,                               \
+      unsigned short: stdc_first_trailing_zero_us,                              \
+      unsigned: stdc_first_trailing_zero_ui,                                    \
+      unsigned long: stdc_first_trailing_zero_ul,                               \
+      unsigned long long: stdc_first_trailing_zero_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 5e87831b907fb5..b97e90d1a8ae34 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -782,7 +782,8 @@ def StdC : StandardSpec<"stdc"> {
         Macro<"stdc_trailing_zeros">,
         Macro<"stdc_trailing_ones">,
         Macro<"stdc_first_leading_zero">,
-        Macro<"stdc_first_leading_one">
+        Macro<"stdc_first_leading_one">,
+        Macro<"stdc_first_trailing_zero">
       ], // Macros
       [], // Types
       [], // Enumerations
@@ -816,7 +817,12 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"stdc_first_leading_one_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_first_leading_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_first_leading_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_first_trailing_zero_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_first_trailing_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_first_trailing_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_first_trailing_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_first_trailing_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
       ] // Functions
   >;
 
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 579a2b514a6ddf..596b1fb530bf93 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -297,6 +297,36 @@ SPECIALIZE_FLO(first_leading_one, unsigned long long, __builtin_clzll)
 
 #undef SPECIALIZE_FLO
 
+#define SPECIALIZE_FTZ(NAME, TYPE, BUILTIN)                                    \
+  template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
+    static_assert(cpp::is_unsigned_v<TYPE>);                                   \
+    return value == cpp::numeric_limits<TYPE>::max()                                       \
+               ? 0                                                             \
+               : BUILTIN(static_cast<TYPE>(~value)) + 1;                        \
+  }
+
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr int first_trailing_zero(T value) {
+  return value == cpp::numeric_limits<T>::max()
+             ? 0
+             : countr_zero(static_cast<T>(~value)) + 1;
+}
+
+#if LIBC_HAS_BUILTIN(__builtin_clzs)
+SPECIALIZE_FTZ(first_trailing_zero, unsigned short, __builtin_ctzs)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clz)
+SPECIALIZE_FTZ(first_trailing_zero, unsigned int, __builtin_ctz)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzl)
+SPECIALIZE_FTZ(first_trailing_zero, unsigned long, __builtin_ctzl)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzll)
+SPECIALIZE_FTZ(first_trailing_zero, unsigned long long, __builtin_ctzll)
+#endif
+
+#undef SPECIALIZE_FTZ
+
 } // namespace LIBC_NAMESPACE::cpp
 
 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 6ee93861b8db40..fa68d4a90dbb7e 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -5,6 +5,7 @@ set(prefixes
   trailing_ones
   first_leading_zero
   first_leading_one
+  first_trailing_zero
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_uc.cpp b/libc/src/stdbit/stdc_first_trailing_zero_uc.cpp
new file mode 100644
index 00000000000000..5825d5d441c591
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_uc.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_first_trailing_zero_uc ---------------------===//
+//
+// 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/stdbit/stdc_first_trailing_zero_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_zero_uc,
+                   (unsigned char value)) {
+  return static_cast<unsigned>(cpp::first_trailing_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_uc.h b/libc/src/stdbit/stdc_first_trailing_zero_uc.h
new file mode 100644
index 00000000000000..242472ae34f2e6
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_trailing_zero_uc ---*- 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_STDBIT_STDC_FIRST_TRAILING_ZERO_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_trailing_zero_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UC_H
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ui.cpp b/libc/src/stdbit/stdc_first_trailing_zero_ui.cpp
new file mode 100644
index 00000000000000..3b51b5fa22c324
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_first_trailing_zero_ui ---------------------===//
+//
+// 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/stdbit/stdc_first_trailing_zero_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_zero_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::first_trailing_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ui.h b/libc/src/stdbit/stdc_first_trailing_zero_ui.h
new file mode 100644
index 00000000000000..cc308f762b2b64
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_trailing_zero_ui ---*- 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_STDBIT_STDC_FIRST_TRAILING_ZERO_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_trailing_zero_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UI_H
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ul.cpp b/libc/src/stdbit/stdc_first_trailing_zero_ul.cpp
new file mode 100644
index 00000000000000..abf122944a76a4
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ul.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_first_trailing_zero_ul ---------------------===//
+//
+// 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/stdbit/stdc_first_trailing_zero_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_zero_ul,
+                   (unsigned long value)) {
+  return static_cast<unsigned>(cpp::first_trailing_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ul.h b/libc/src/stdbit/stdc_first_trailing_zero_ul.h
new file mode 100644
index 00000000000000..82413373341748
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_trailing_zero_ul ---*- 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_STDBIT_STDC_FIRST_TRAILING_ZERO_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_trailing_zero_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UL_H
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp b/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp
new file mode 100644
index 00000000000000..e17df2b40d845f
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_first_trailing_zero_ull ---------------------===//
+//
+// 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/stdbit/stdc_first_trailing_zero_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_zero_ull,
+                   (unsigned long long value)) {
+  return static_cast<unsigned>(cpp::first_trailing_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ull.h b/libc/src/stdbit/stdc_first_trailing_zero_ull.h
new file mode 100644
index 00000000000000..03a0f40026a26f
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_trailing_zero_ull ---*- 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_STDBIT_STDC_FIRST_TRAILING_ZERO_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_trailing_zero_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_ULL_H
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_us.cpp b/libc/src/stdbit/stdc_first_trailing_zero_us.cpp
new file mode 100644
index 00000000000000..b7d05047b2721a
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_us.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_first_trailing_zero_us ---------------------===//
+//
+// 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/stdbit/stdc_first_trailing_zero_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_zero_us,
+                   (unsigned short value)) {
+  return static_cast<unsigned>(cpp::first_trailing_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_us.h b/libc/src/stdbit/stdc_first_trailing_zero_us.h
new file mode 100644
index 00000000000000..608b0522969642
--- /dev/null
+++ b/libc/src/stdbit/stdc_first_trailing_zero_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_first_trailing_zero_us ---*- 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_STDBIT_STDC_FIRST_TRAILING_ZERO_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_first_trailing_zero_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index c2fbcb8ce2d321..238c2d44fac45a 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -57,6 +57,13 @@ unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
 unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
   return 0xFFU;
 }
+unsigned stdc_first_trailing_zero_uc(unsigned char) noexcept { return 0x0AU; }
+unsigned stdc_first_trailing_zero_us(unsigned short) noexcept { return 0x0BU; }
+unsigned stdc_first_trailing_zero_ui(unsigned) noexcept { return 0x0CU; }
+unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
+unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
+  return 0x0FU;
+}
 }
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
@@ -108,3 +115,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingOne) {
   EXPECT_EQ(stdc_first_leading_one(0UL), 0xFDU);
   EXPECT_EQ(stdc_first_leading_one(0ULL), 0xFFU);
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingZero) {
+  EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned char>(0U)), 0x0AU);
+  EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned short>(0U)), 0x0BU);
+  EXPECT_EQ(stdc_first_trailing_zero(0U), 0x0CU);
+  EXPECT_EQ(stdc_first_trailing_zero(0UL), 0x0DU);
+  EXPECT_EQ(stdc_first_trailing_zero(0ULL), 0x0FU);
+}
diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index a70726d4feb241..914c6ca823c8ef 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -220,4 +220,10 @@ TYPED_TEST(LlvmLibcBitTest, FirstLeadingOne, UnsignedTypes) {
               cpp::numeric_limits<T>::digits - i);
 }
 
+TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypes) {
+  EXPECT_EQ(first_trailing_zero<T>(cpp::numeric_limits<T>::max()), 0);
+  for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
+    EXPECT_EQ(first_trailing_zero<T>(~(T(1) << i)), i + 1);
+}
+
 } // namespace LIBC_NAMESPACE::cpp
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index e32663f88e8620..d5896ad83dbfab 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -7,6 +7,7 @@ set(prefixes
   trailing_ones
   first_leading_zero
   first_leading_one
+  first_trailing_zero
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp
new file mode 100644
index 00000000000000..8b0168c76fdce2
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for stdc_first_trailing_zero_uc --------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_trailing_zero_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstTrailingZeroUcTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_uc(UCHAR_MAX), 0U);
+}
+
+TEST(LlvmLibcStdcFirstTrailingZeroUcTest, ZeroHot) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_uc(~(1U << i)), i + 1);
+}
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp
new file mode 100644
index 00000000000000..c259d5aee966c2
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for stdc_first_trailing_zero_ui --------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_trailing_zero_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstTrailingZeroUiTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ui(UINT_MAX), 0U);
+}
+
+TEST(LlvmLibcStdcFirstTrailingZeroUiTest, ZeroHot) {
+  for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ui(~(1U << i)), i + 1);
+}
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp
new file mode 100644
index 00000000000000..61881a7c2186a8
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for stdc_first_trailing_zero_ul --------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_trailing_zero_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstTrailingZeroUlTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ul(ULONG_MAX), 0U);
+}
+
+TEST(LlvmLibcStdcFirstTrailingZeroUlTest, ZeroHot) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ul(~(1UL << i)), i + 1);
+}
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp
new file mode 100644
index 00000000000000..96e7209c0e3f66
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for stdc_first_trailing_zero_ull --------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_trailing_zero_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstTrailingZeroUllTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ull(ULLONG_MAX), 0U);
+}
+
+TEST(LlvmLibcStdcFirstTrailingZeroUllTest, ZeroHot) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ull(~(1ULL << i)), i + 1);
+}
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp
new file mode 100644
index 00000000000000..792dd502bea1e3
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for stdc_first_trailing_zero_us --------------------------===//
+//
+// 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/__support/CPP/limits.h"
+#include "src/stdbit/stdc_first_trailing_zero_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcFirstTrailingZeroUsTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_us(USHRT_MAX), 0U);
+}
+
+TEST(LlvmLibcStdcFirstTrailingZeroUsTest, ZeroHot) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_us(~(1U << i)), i + 1);
+}

>From 66943defec3520700a767f697e3a09f632d8981a Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 12 Feb 2024 12:32:00 -0800
Subject: [PATCH 4/4] reformat

---
 libc/include/llvm-libc-macros/stdbit-macros.h          | 10 +++++-----
 libc/src/__support/CPP/bit.h                           |  4 ++--
 libc/src/stdbit/stdc_first_trailing_zero_ull.cpp       |  2 +-
 libc/src/stdbit/stdc_first_trailing_zero_ull.h         |  2 +-
 .../src/stdbit/stdc_first_trailing_zero_uc_test.cpp    |  2 +-
 .../src/stdbit/stdc_first_trailing_zero_ui_test.cpp    |  2 +-
 .../src/stdbit/stdc_first_trailing_zero_ul_test.cpp    |  2 +-
 .../src/stdbit/stdc_first_trailing_zero_ull_test.cpp   |  5 +++--
 .../src/stdbit/stdc_first_trailing_zero_us_test.cpp    |  2 +-
 9 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 19b800f342e8ff..0ec8aef10d3e0a 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -159,12 +159,12 @@ inline unsigned stdc_first_trailing_zero(unsigned long long x) {
       unsigned: stdc_first_leading_one_ui,                                     \
       unsigned long: stdc_first_leading_one_ul,                                \
       unsigned long long: stdc_first_leading_one_ull)(x)
-#define stdc_first_trailing_zero(x)                                             \
+#define stdc_first_trailing_zero(x)                                            \
   _Generic((x),                                                                \
-      unsigned char: stdc_first_trailing_zero_uc,                               \
-      unsigned short: stdc_first_trailing_zero_us,                              \
-      unsigned: stdc_first_trailing_zero_ui,                                    \
-      unsigned long: stdc_first_trailing_zero_ul,                               \
+      unsigned char: stdc_first_trailing_zero_uc,                              \
+      unsigned short: stdc_first_trailing_zero_us,                             \
+      unsigned: stdc_first_trailing_zero_ui,                                   \
+      unsigned long: stdc_first_trailing_zero_ul,                              \
       unsigned long long: stdc_first_trailing_zero_ull)(x)
 #endif // __cplusplus
 
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 596b1fb530bf93..8cacc35d8c045b 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -300,9 +300,9 @@ SPECIALIZE_FLO(first_leading_one, unsigned long long, __builtin_clzll)
 #define SPECIALIZE_FTZ(NAME, TYPE, BUILTIN)                                    \
   template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
     static_assert(cpp::is_unsigned_v<TYPE>);                                   \
-    return value == cpp::numeric_limits<TYPE>::max()                                       \
+    return value == cpp::numeric_limits<TYPE>::max()                           \
                ? 0                                                             \
-               : BUILTIN(static_cast<TYPE>(~value)) + 1;                        \
+               : BUILTIN(static_cast<TYPE>(~value)) + 1;                       \
   }
 
 template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp b/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp
index e17df2b40d845f..336e7d6e075ff2 100644
--- a/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ull.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of stdc_first_trailing_zero_ull ---------------------===//
+//===-- Implementation of stdc_first_trailing_zero_ull --------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/stdbit/stdc_first_trailing_zero_ull.h b/libc/src/stdbit/stdc_first_trailing_zero_ull.h
index 03a0f40026a26f..3737fc1be2d435 100644
--- a/libc/src/stdbit/stdc_first_trailing_zero_ull.h
+++ b/libc/src/stdbit/stdc_first_trailing_zero_ull.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for stdc_first_trailing_zero_ull ---*- C++ -*-===//
+//===-- Implementation header for stdc_first_trailing_zero_ull --*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp
index 8b0168c76fdce2..2b17aa6536e669 100644
--- a/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_uc_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_first_trailing_zero_uc --------------------------===//
+//===-- Unittests for stdc_first_trailing_zero_uc -------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp
index c259d5aee966c2..08366142e2a737 100644
--- a/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_ui_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_first_trailing_zero_ui --------------------------===//
+//===-- Unittests for stdc_first_trailing_zero_ui -------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp
index 61881a7c2186a8..0c18cc73ffcc4c 100644
--- a/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_ul_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_first_trailing_zero_ul --------------------------===//
+//===-- Unittests for stdc_first_trailing_zero_ul -------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp
index 96e7209c0e3f66..5dce42987e52fa 100644
--- a/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_ull_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_first_trailing_zero_ull --------------------------===//
+//===-- Unittests for stdc_first_trailing_zero_ull ------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -16,5 +16,6 @@ TEST(LlvmLibcStdcFirstTrailingZeroUllTest, ALL) {
 
 TEST(LlvmLibcStdcFirstTrailingZeroUllTest, ZeroHot) {
   for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ull(~(1ULL << i)), i + 1);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_ull(~(1ULL << i)),
+              i + 1);
 }
diff --git a/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp
index 792dd502bea1e3..e370379300e4a4 100644
--- a/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp
+++ b/libc/test/src/stdbit/stdc_first_trailing_zero_us_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_first_trailing_zero_us --------------------------===//
+//===-- Unittests for stdc_first_trailing_zero_us -------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.



More information about the libc-commits mailing list