[libcxx-commits] [libcxx] [libc++] Implement C++29 Bit Permutations P3104R6 (PR #217820)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 27 09:52:32 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;
+}
----------------
siradam7th wrote:

After some lightweight benchmarking, It looks like this reference implementation is actually comparable to the two other implementations:
1) This reference `bit_repeat`
2) Bit operations + loop
3) Lookup table.

I'll have to investigate more before replacing this implementation, since this one is more simple and clear, in fact that's why I used it since if it's simple enough the compiler might be able to optimize it better.

https://github.com/llvm/llvm-project/pull/217820


More information about the libcxx-commits mailing list