[libcxx-commits] [libcxx] [libc++] Implement C++29 Bit Permutations P3104R6 (PR #217820)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Aug 21 04:51:42 PDT 2026
================
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 _LIBCPP___BIT_BIT_REPEAT_H
+#define _LIBCPP___BIT_BIT_REPEAT_H
+
+#include <__config>
+#include <__type_traits/integer_traits.h>
+#include <limits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 29
+
+template <__unsigned_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_repeat(_Tp __t, int __l) {
+ _Tp __res = 0;
+ for (int i = 0; i < numeric_limits<_Tp>::digits; ++i) {
+ __res |= ((__t >> (i % __l)) & 1) << i;
+ }
+ return __res;
+}
----------------
frederick-vs-ja wrote:
Also, I believe we shouldn't use pretty identifier `i` here.
And note that we should add test cases to verify that UB causes constant evaluation failure - these cases should be SFINAE-friendly and live in the `bit_repeat.pass.cpp` file (assuming this name is wanted).
https://github.com/llvm/llvm-project/pull/217820
More information about the libcxx-commits
mailing list