Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc][stdbit] implement stdc_trailing_ones (C23) #80459

Merged
merged 3 commits into from
Feb 7, 2024

Conversation

nickdesaulniers
Copy link
Member

@nickdesaulniers nickdesaulniers commented Feb 2, 2024

No description provided.

@llvmbot llvmbot added the libc label Feb 2, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 2, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes
  • [libc][stdbit] implement stdc_trailing_zeros (C23)
  • format
  • refactor cmake; getting tired of writing these already
  • s/LLONG_WIDTH/ULLONG_WIDTH/
  • update stdc.td
  • [libc][stdbit] implement stdc_trailing_ones (C23)

Patch is 40.52 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/80459.diff

35 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+10)
  • (modified) libc/include/llvm-libc-macros/stdbit-macros.h (+44)
  • (modified) libc/spec/stdc.td (+11-1)
  • (modified) libc/src/stdbit/CMakeLists.txt (+20-99)
  • (added) libc/src/stdbit/stdc_trailing_ones_uc.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_ones_uc.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_ones_ui.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_ones_ui.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_ones_ul.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_ones_ul.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_ones_ull.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_ones_ull.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_ones_us.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_ones_us.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_uc.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_uc.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ui.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ui.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ul.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ul.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ull.cpp (+21)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ull.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_us.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_us.h (+18)
  • (modified) libc/test/include/stdbit_test.cpp (+26)
  • (modified) libc/test/src/stdbit/CMakeLists.txt (+21-110)
  • (added) libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp (+21)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9946b93c346ce..82c40a5cbcc06 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -102,6 +102,16 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_leading_ones_ui
     libc.src.stdbit.stdc_leading_ones_ul
     libc.src.stdbit.stdc_leading_ones_ull
+    libc.src.stdbit.stdc_trailing_zeros_uc
+    libc.src.stdbit.stdc_trailing_zeros_us
+    libc.src.stdbit.stdc_trailing_zeros_ui
+    libc.src.stdbit.stdc_trailing_zeros_ul
+    libc.src.stdbit.stdc_trailing_zeros_ull
+    libc.src.stdbit.stdc_trailing_ones_uc
+    libc.src.stdbit.stdc_trailing_ones_us
+    libc.src.stdbit.stdc_trailing_ones_ui
+    libc.src.stdbit.stdc_trailing_ones_ul
+    libc.src.stdbit.stdc_trailing_ones_ull
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index cc964a5268b0d..840246b795635 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -40,6 +40,36 @@ inline unsigned stdc_leading_ones(unsigned long x) {
 inline unsigned stdc_leading_ones(unsigned long long x) {
   return stdc_leading_ones_ull(x);
 }
+inline unsigned stdc_trailing_zeros(unsigned char x) {
+  return stdc_trailing_zeros_uc(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned short x) {
+  return stdc_trailing_zeros_us(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned x) {
+  return stdc_trailing_zeros_ui(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned long x) {
+  return stdc_trailing_zeros_ul(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned long long x) {
+  return stdc_trailing_zeros_ull(x);
+}
+inline unsigned stdc_trailing_ones(unsigned char x) {
+  return stdc_trailing_ones_uc(x);
+}
+inline unsigned stdc_trailing_ones(unsigned short x) {
+  return stdc_trailing_ones_us(x);
+}
+inline unsigned stdc_trailing_ones(unsigned x) {
+  return stdc_trailing_ones_ui(x);
+}
+inline unsigned stdc_trailing_ones(unsigned long x) {
+  return stdc_trailing_ones_ul(x);
+}
+inline unsigned stdc_trailing_ones(unsigned long long x) {
+  return stdc_trailing_ones_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -55,6 +85,20 @@ inline unsigned stdc_leading_ones(unsigned long long x) {
       unsigned: stdc_leading_ones_ui,                                          \
       unsigned long: stdc_leading_ones_ul,                                     \
       unsigned long long: stdc_leading_ones_ull)(x)
+#define stdc_trailing_zeros(x)                                                 \
+  _Generic((x),                                                                \
+      unsigned char: stdc_trailing_zeros_uc,                                   \
+      unsigned short: stdc_trailing_zeros_us,                                  \
+      unsigned: stdc_trailing_zeros_ui,                                        \
+      unsigned long: stdc_trailing_zeros_ul,                                   \
+      unsigned long long: stdc_trailing_zeros_ull)(x)
+#define stdc_trailing_ones(x)                                                 \
+  _Generic((x),                                                                \
+      unsigned char: stdc_trailing_ones_uc,                                   \
+      unsigned short: stdc_trailing_ones_us,                                  \
+      unsigned: stdc_trailing_ones_ui,                                        \
+      unsigned long: stdc_trailing_ones_ul,                                   \
+      unsigned long long: stdc_trailing_ones_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 6ff2c7c613696..b9fd3bac317b6 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -784,7 +784,17 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"stdc_leading_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_trailing_zeros_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_trailing_ones_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_trailing_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_trailing_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_trailing_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index d7daff44dfda6..1904f6f77699f 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -1,99 +1,20 @@
-add_entrypoint_object(
-  stdc_leading_zeros_uc
-  SRCS
-    stdc_leading_zeros_uc.cpp
-  HDRS
-    stdc_leading_zeros_uc.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_zeros_us
-  SRCS
-    stdc_leading_zeros_us.cpp
-  HDRS
-    stdc_leading_zeros_us.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_zeros_ui
-  SRCS
-    stdc_leading_zeros_ui.cpp
-  HDRS
-    stdc_leading_zeros_ui.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_zeros_ul
-  SRCS
-    stdc_leading_zeros_ul.cpp
-  HDRS
-    stdc_leading_zeros_ul.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_zeros_ull
-  SRCS
-    stdc_leading_zeros_ull.cpp
-  HDRS
-    stdc_leading_zeros_ull.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_ones_uc
-  SRCS
-    stdc_leading_ones_uc.cpp
-  HDRS
-    stdc_leading_ones_uc.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_ones_us
-  SRCS
-    stdc_leading_ones_us.cpp
-  HDRS
-    stdc_leading_ones_us.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_ones_ui
-  SRCS
-    stdc_leading_ones_ui.cpp
-  HDRS
-    stdc_leading_ones_ui.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_ones_ul
-  SRCS
-    stdc_leading_ones_ul.cpp
-  HDRS
-    stdc_leading_ones_ul.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
-  stdc_leading_ones_ull
-  SRCS
-    stdc_leading_ones_ull.cpp
-  HDRS
-    stdc_leading_ones_ull.h
-  DEPENDS
-    libc.src.__support.CPP.bit
-)
+list(APPEND prefixes
+  leading_zeros
+  leading_ones
+  trailing_zeros
+  trailing_ones
+)
+list(APPEND suffixes c s i l ll)
+foreach(prefix IN LISTS prefixes)
+  foreach(suffix IN LISTS suffixes)
+    add_entrypoint_object(
+      stdc_${prefix}_u${suffix}
+      SRCS
+        stdc_${prefix}_u${suffix}.cpp
+      HDRS
+        stdc_${prefix}_u${suffix}.h
+      DEPENDS
+        libc.src.__support.CPP.bit
+    )
+  endforeach()
+endforeach()
diff --git a/libc/src/stdbit/stdc_trailing_ones_uc.cpp b/libc/src/stdbit/stdc_trailing_ones_uc.cpp
new file mode 100644
index 0000000000000..eabb21367e285
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_trailing_ones_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_uc, (unsigned char value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_uc.h b/libc/src/stdbit/stdc_trailing_ones_uc.h
new file mode 100644
index 0000000000000..9736e21e1f280
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_TRAILING_ONES_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ui.cpp b/libc/src/stdbit/stdc_trailing_ones_ui.cpp
new file mode 100644
index 0000000000000..87eb54fe5f026
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_trailing_ones_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ui.h b/libc/src/stdbit/stdc_trailing_ones_ui.h
new file mode 100644
index 0000000000000..aad7fcf0daf55
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_TRAILING_ONES_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ul.cpp b/libc/src/stdbit/stdc_trailing_ones_ul.cpp
new file mode 100644
index 0000000000000..6d358a21ac337
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_trailing_ones_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ul, (unsigned long value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ul.h b/libc/src/stdbit/stdc_trailing_ones_ul.h
new file mode 100644
index 0000000000000..80589501b1ed2
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_TRAILING_ONES_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.cpp b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
new file mode 100644
index 0000000000000..883b1f2ff5c07
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_trailing_ones_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ull, (unsigned long long value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.h b/libc/src/stdbit/stdc_trailing_ones_ull.h
new file mode 100644
index 0000000000000..3d3f0b306789a
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_TRAILING_ONES_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_us.cpp b/libc/src/stdbit/stdc_trailing_ones_us.cpp
new file mode 100644
index 0000000000000..ee7ff4f78d489
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_trailing_ones_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_us, (unsigned short value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_us.h b/libc/src/stdbit/stdc_trailing_ones_us.h
new file mode 100644
index 0000000000000..b783cd22ad2de
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_TRAILING_ONES_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
diff --git a/libc/src/stdbit/stdc_trailing_zeros_uc.cpp b/libc/src/stdbit/stdc_trailing_zeros_uc.cpp
new file mode 100644
index 0000000000000..36924c5a053ad
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_uc, (unsigned char value)) {
+  return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_uc.h b/libc/src/stdbit/stdc_trailing_zeros_uc.h
new file mode 100644
index 0000000000000..866201e5acea8
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_zeros_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_TRAILING_ZEROS_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_zeros_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ui.cpp b/libc/src/stdbit/stdc_trailing_zeros_ui.cpp
new file mode 100644
index 0000000000000..a264fd97f251f
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ui.h b/libc/src/stdbit/stdc_trailing_zeros_ui.h
new file mode 100644
index 0000000000000..0642e312f4fe7
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_zeros_ui --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.o...
[truncated]

@nickdesaulniers
Copy link
Member Author

Note to reviewers, this commit is stacked on top of #80344. Only the commit "
[libc][stdbit] implement stdc_trailing_ones (C23)" (and subsequent pushes) should be reviewed.

@nickdesaulniers nickdesaulniers changed the title trailing ones [libc][stdbit] implement stdc_trailing_ones (C23) Feb 2, 2024
Copy link

github-actions bot commented Feb 2, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@nickdesaulniers nickdesaulniers marked this pull request as draft February 2, 2024 17:33
Comment on lines 3 to 11
list(APPEND prefixes
leading_zeros
leading_ones
trailing_zeros
trailing_ones
)
list(APPEND suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
foreach(suffix IN LISTS suffixes)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
list(APPEND prefixes
leading_zeros
leading_ones
trailing_zeros
trailing_ones
)
list(APPEND suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
foreach(suffix IN LISTS suffixes)
foreach(prefix leading_zeros leading_ones trailing_zeros trailing_ones)
foreach(suffix c s i l ll)

This should work unless we're actually depending on this appending to some list defined somewhere else.

Copy link
Member Author

Choose a reason for hiding this comment

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

Consider making this point in #80344 (the child PR this PR is based off of; sorry for stacking commits but this was more obvious in phabricator than github PRs).

The list of "suffixes" is set, but "prefixes" will grow to:

  • leading_zeros
  • leading_ones
  • trailing_zeros
  • trailing_ones
  • first_leading_zero
  • first_leading_one
  • first_trailing_zero
  • first_trailing_one
  • count_zeros
  • count_ones
  • has_single_bit
  • bit_width
  • bit_floor
  • bit_ceil

as I finish implementing stdbit.h. Personal preference, but I prefer to add new individual lines for each of these to minimize the impact on git blame, as opposed to making one line excessively longer through multiple commits (which I believe is what you're suggesting). I don't feel strongly about it, but that's an insight into why I did it that way. WDYT?

Copy link
Member Author

@nickdesaulniers nickdesaulniers Feb 2, 2024

Choose a reason for hiding this comment

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

This should work unless we're actually depending on this appending to some list defined somewhere else.

Ah, and @lntue pointed this out as well in review of the child patch that append is not necessary and to just use set.

@nickdesaulniers nickdesaulniers marked this pull request as ready for review February 6, 2024 14:56
@nickdesaulniers nickdesaulniers merged commit 7760006 into llvm:main Feb 7, 2024
6 checks passed
@nickdesaulniers nickdesaulniers deleted the trailing_ones branch February 7, 2024 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants