xref: /llvm-project-15.0.7/libcxx/include/bit (revision bd7f84a4)
1// -*- C++ -*-
2//===------------------------------ bit ----------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BIT
11#define _LIBCPP_BIT
12
13/*
14    bit synopsis
15
16namespace std {
17
18  template <class T>
19    constexpr bool ispow2(T x) noexcept; // C++20
20  template <class T>
21    constexpr T ceil2(T x);              // C++20
22  template <class T>
23    constexpr T floor2(T x) noexcept;    // C++20
24  template <class T>
25    constexpr T log2p1(T x) noexcept;    // C++20
26
27  // 23.20.2, rotating
28  template<class T>
29    constexpr T rotl(T x, unsigned int s) noexcept; // C++20
30  template<class T>
31    constexpr T rotr(T x, unsigned int s) noexcept; // C++20
32
33  // 23.20.3, counting
34  template<class T>
35    constexpr int countl_zero(T x) noexcept;  // C++20
36  template<class T>
37    constexpr int countl_one(T x) noexcept;   // C++20
38  template<class T>
39    constexpr int countr_zero(T x) noexcept;  // C++20
40  template<class T>
41    constexpr int countr_one(T x) noexcept;   // C++20
42  template<class T>
43    constexpr int popcount(T x) noexcept;     // C++20
44
45} // namespace std
46
47*/
48
49#include <__config>
50#include <limits>
51#include <type_traits>
52#include <version>
53#include <__debug>
54
55#if defined(__IBMCPP__)
56#include "support/ibm/support.h"
57#endif
58#if defined(_LIBCPP_COMPILER_MSVC)
59#include <intrin.h>
60#endif
61
62#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
63#pragma GCC system_header
64#endif
65
66_LIBCPP_PUSH_MACROS
67#include <__undef_macros>
68
69_LIBCPP_BEGIN_NAMESPACE_STD
70
71#ifndef _LIBCPP_COMPILER_MSVC
72
73inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
74int __ctz(unsigned __x)           _NOEXCEPT { return __builtin_ctz(__x); }
75
76inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
77int __ctz(unsigned long __x)      _NOEXCEPT { return __builtin_ctzl(__x); }
78
79inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
80int __ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
81
82
83inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
84int __clz(unsigned __x)           _NOEXCEPT { return __builtin_clz(__x); }
85
86inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
87int __clz(unsigned long __x)      _NOEXCEPT { return __builtin_clzl(__x); }
88
89inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
90int __clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
91
92
93inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
94int __popcount(unsigned __x)           _NOEXCEPT { return __builtin_popcount(__x); }
95
96inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
97int __popcount(unsigned long __x)      _NOEXCEPT { return __builtin_popcountl(__x); }
98
99inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
100int __popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
101
102#else  // _LIBCPP_COMPILER_MSVC
103
104// Precondition:  __x != 0
105inline _LIBCPP_INLINE_VISIBILITY
106int __ctz(unsigned __x) {
107  static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
108  static_assert(sizeof(unsigned long) == 4, "");
109  unsigned long __where;
110  if (_BitScanForward(&__where, __x))
111    return static_cast<int>(__where);
112  return 32;
113}
114
115inline _LIBCPP_INLINE_VISIBILITY
116int __ctz(unsigned long __x) {
117    static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
118    return __ctz(static_cast<unsigned>(__x));
119}
120
121inline _LIBCPP_INLINE_VISIBILITY
122int __ctz(unsigned long long __x) {
123    unsigned long __where;
124#if defined(_LIBCPP_HAS_BITSCAN64)
125    (defined(_M_AMD64) || defined(__x86_64__))
126  if (_BitScanForward64(&__where, __x))
127    return static_cast<int>(__where);
128#else
129  // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
130  if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
131    return static_cast<int>(__where);
132  if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
133    return static_cast<int>(__where + 32);
134#endif
135  return 64;
136}
137
138// Precondition:  __x != 0
139inline _LIBCPP_INLINE_VISIBILITY
140int __clz(unsigned __x) {
141  static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
142  static_assert(sizeof(unsigned long) == 4, "");
143  unsigned long __where;
144  if (_BitScanReverse(&__where, __x))
145    return static_cast<int>(31 - __where);
146  return 32; // Undefined Behavior.
147}
148
149inline _LIBCPP_INLINE_VISIBILITY
150int __clz(unsigned long __x) {
151    static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
152    return __clz(static_cast<unsigned>(__x));
153}
154
155inline _LIBCPP_INLINE_VISIBILITY
156int __clz(unsigned long long __x) {
157  unsigned long __where;
158#if defined(_LIBCPP_HAS_BITSCAN64)
159  if (_BitScanReverse64(&__where, __x))
160    return static_cast<int>(63 - __where);
161#else
162  // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
163  if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
164    return static_cast<int>(63 - (__where + 32));
165  if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
166    return static_cast<int>(63 - __where);
167#endif
168  return 64; // Undefined Behavior.
169}
170
171inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned __x) {
172  static_assert(sizeof(unsigned) == 4, "");
173  return __popcnt(__x);
174}
175
176inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long __x) {
177  static_assert(sizeof(unsigned long) == 4, "");
178  return __popcnt(__x);
179}
180
181inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long long __x) {
182  static_assert(sizeof(unsigned long long) == 8, "");
183  return __popcnt64(__x);
184}
185
186#endif // _LIBCPP_COMPILER_MSVC
187
188#if _LIBCPP_STD_VER > 17
189
190template <class _Tp>
191using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
192         is_integral_v<_Tp> &&
193         is_unsigned_v<_Tp> &&
194        _IsNotSame<remove_cv_t<_Tp>, bool>::value &&
195        _IsNotSame<remove_cv_t<_Tp>, signed char>::value &&
196        _IsNotSame<remove_cv_t<_Tp>, wchar_t>::value &&
197        _IsNotSame<remove_cv_t<_Tp>, char16_t>::value &&
198        _IsNotSame<remove_cv_t<_Tp>, char32_t>::value
199    >;
200
201
202// rotl
203template<class _Tp>
204_LIBCPP_INLINE_VISIBILITY constexpr
205enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
206rotl(_Tp __t, unsigned int __cnt) noexcept
207{
208    const unsigned int __dig = numeric_limits<_Tp>::digits;
209    if ((__cnt % __dig) == 0)
210        return __t;
211    return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
212}
213
214
215// rotr
216template<class _Tp>
217_LIBCPP_INLINE_VISIBILITY constexpr
218enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
219rotr(_Tp __t, unsigned int __cnt) noexcept
220{
221    const unsigned int __dig = numeric_limits<_Tp>::digits;
222    if ((__cnt % __dig) == 0)
223        return __t;
224    return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
225}
226
227
228// countl_zero
229template<class _Tp>
230_LIBCPP_INLINE_VISIBILITY constexpr
231enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
232countl_zero(_Tp __t) noexcept
233{
234    if (__t == 0)
235        return numeric_limits<_Tp>::digits;
236
237    if      constexpr (sizeof(_Tp) <= sizeof(unsigned int))
238        return __clz(static_cast<unsigned int>(__t))
239              - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
240    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
241        return __clz(static_cast<unsigned long>(__t))
242              - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
243    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
244        return __clz(static_cast<unsigned long long>(__t))
245              - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
246    else
247    {
248        int __ret = 0;
249        int __iter = 0;
250        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
251        while (true) {
252            __t = rotr(__t, __ulldigits);
253            if ((__iter = countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
254                break;
255            __ret += __iter;
256            }
257        return __ret + __iter;
258    }
259}
260
261
262// countl_one
263template<class _Tp>
264_LIBCPP_INLINE_VISIBILITY constexpr
265enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
266countl_one(_Tp __t) noexcept
267{
268    return __t != numeric_limits<_Tp>::max()
269        ? countl_zero(static_cast<_Tp>(~__t))
270        : numeric_limits<_Tp>::digits;
271}
272
273
274// countr_zero
275template<class _Tp>
276_LIBCPP_INLINE_VISIBILITY constexpr
277enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
278countr_zero(_Tp __t) noexcept
279{
280    if (__t == 0)
281        return numeric_limits<_Tp>::digits;
282
283    if      constexpr (sizeof(_Tp) <= sizeof(unsigned int))
284        return __ctz(static_cast<unsigned int>(__t));
285    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
286        return __ctz(static_cast<unsigned long>(__t));
287    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
288        return __ctz(static_cast<unsigned long long>(__t));
289    else
290    {
291        int __ret = 0;
292        int __iter = 0;
293        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
294        while ((__iter = countr_zero(static_cast<unsigned long long>(__t))) == __ulldigits)
295        {
296            __ret += __iter;
297            __t >>= __ulldigits;
298        }
299        return __ret + __iter;
300    }
301}
302
303
304// countr_one
305template<class _Tp>
306_LIBCPP_INLINE_VISIBILITY constexpr
307enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
308countr_one(_Tp __t) noexcept
309{
310    return __t != numeric_limits<_Tp>::max()
311        ? countr_zero(static_cast<_Tp>(~__t))
312        : numeric_limits<_Tp>::digits;
313}
314
315
316// popcount
317template<class _Tp>
318_LIBCPP_INLINE_VISIBILITY constexpr
319enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
320popcount(_Tp __t) noexcept
321{
322    if      constexpr (sizeof(_Tp) <= sizeof(unsigned int))
323        return __popcount(static_cast<unsigned int>(__t));
324    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
325        return __popcount(static_cast<unsigned long>(__t));
326    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
327        return __popcount(static_cast<unsigned long long>(__t));
328    else
329    {
330        int __ret = 0;
331        while (__t != 0)
332        {
333            __ret += __popcount(static_cast<unsigned long long>(__t));
334            __t >>= numeric_limits<unsigned long long>::digits;
335        }
336        return __ret;
337    }
338}
339
340
341// integral log base 2
342template<class _Tp>
343_LIBCPP_INLINE_VISIBILITY constexpr
344unsigned __bit_log2(_Tp __t) noexcept
345{ return std::numeric_limits<_Tp>::digits - 1 - countl_zero(__t); }
346
347
348template <class _Tp>
349_LIBCPP_INLINE_VISIBILITY constexpr
350enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
351ispow2(_Tp __t) noexcept { return popcount(__t) == 1; }
352
353template <class _Tp>
354_LIBCPP_INLINE_VISIBILITY constexpr
355enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
356floor2(_Tp __t) noexcept { return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); }
357
358template <class _Tp>
359_LIBCPP_INLINE_VISIBILITY constexpr
360enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
361ceil2(_Tp __t) noexcept
362{
363    if (__t < 2) return 1;
364    const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
365    _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2");
366
367    if constexpr (sizeof(_Tp) >= sizeof(unsigned))
368        return _Tp{1} << __n;
369    else
370    {
371        const unsigned __extra = numeric_limits<unsigned>::digits  - numeric_limits<_Tp>::digits;
372        const unsigned __retVal = 1u << (__n + __extra);
373        return (_Tp) (__retVal >> __extra);
374    }
375}
376
377template <class _Tp>
378_LIBCPP_INLINE_VISIBILITY constexpr
379enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
380log2p1(_Tp __t) noexcept
381{ return __t == 0 ? 0 : __bit_log2(__t) + 1; }
382
383#endif // _LIBCPP_STD_VER > 17
384
385_LIBCPP_END_NAMESPACE_STD
386
387_LIBCPP_POP_MACROS
388
389#endif // _LIBCPP_BIT
390