blob: a2ca3bc83cf6840777e6f03e7d434b049fa93aa8 [file] [log] [blame]
Marshall Clowbf32ec92018-08-17 16:07:48 +00001// -*- C++ -*-
2//===------------------------------ bit ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Marshall Clowbf32ec92018-08-17 16:07:48 +00007//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BIT
11#define _LIBCPP_BIT
12
13/*
14 bit synopsis
15
16namespace std {
17
18} // namespace std
19
20*/
21
22#include <__config>
Marshall Clow0a1e7502018-09-12 19:41:40 +000023#include <version>
Marshall Clowbf32ec92018-08-17 16:07:48 +000024
25#if defined(__IBMCPP__)
26#include "support/ibm/support.h"
27#endif
28#if defined(_LIBCPP_COMPILER_MSVC)
29#include <intrin.h>
30#endif
31
32#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33#pragma GCC system_header
34#endif
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
Marshall Clowea687e22018-08-17 17:27:25 +000038#ifndef _LIBCPP_COMPILER_MSVC
39
40inline _LIBCPP_INLINE_VISIBILITY
41int __ctz(unsigned __x) { return __builtin_ctz(__x); }
42
43inline _LIBCPP_INLINE_VISIBILITY
44int __ctz(unsigned long __x) { return __builtin_ctzl(__x); }
45
46inline _LIBCPP_INLINE_VISIBILITY
47int __ctz(unsigned long long __x) { return __builtin_ctzll(__x); }
48
49
50inline _LIBCPP_INLINE_VISIBILITY
51int __clz(unsigned __x) { return __builtin_clz(__x); }
52
53inline _LIBCPP_INLINE_VISIBILITY
54int __clz(unsigned long __x) { return __builtin_clzl(__x); }
55
56inline _LIBCPP_INLINE_VISIBILITY
57int __clz(unsigned long long __x) { return __builtin_clzll(__x); }
58
59
60inline _LIBCPP_INLINE_VISIBILITY
61int __popcount(unsigned __x) { return __builtin_popcount(__x); }
62
63inline _LIBCPP_INLINE_VISIBILITY
64int __popcount(unsigned long __x) { return __builtin_popcountl(__x); }
65
66inline _LIBCPP_INLINE_VISIBILITY
67int __popcount(unsigned long long __x) { return __builtin_popcountll(__x); }
68
69#else // _LIBCPP_COMPILER_MSVC
70
Marshall Clowbf32ec92018-08-17 16:07:48 +000071// Precondition: __x != 0
72inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +000073int __ctz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +000074 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
75 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +000076 unsigned long __where;
77 if (_BitScanForward(&__where, __x))
78 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +000079 return 32;
Marshall Clowbf32ec92018-08-17 16:07:48 +000080}
81
82inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +000083int __ctz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +000084 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
85 return __ctz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +000086}
87
88inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +000089int __ctz(unsigned long long __x) {
90 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +000091#if defined(_LIBCPP_HAS_BITSCAN64)
92 (defined(_M_AMD64) || defined(__x86_64__))
Marshall Clowea687e22018-08-17 17:27:25 +000093 if (_BitScanForward64(&__where, __x))
94 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +000095#else
96 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
Marshall Clowea687e22018-08-17 17:27:25 +000097 if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
98 return static_cast<int>(__where);
99 if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
100 return static_cast<int>(__where + 32);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000101#endif
102 return 64;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000103}
104
105// Precondition: __x != 0
106inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +0000107int __clz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000108 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
109 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000110 unsigned long __where;
111 if (_BitScanReverse(&__where, __x))
112 return static_cast<int>(31 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000113 return 32; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000114}
115
116inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +0000117int __clz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000118 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
119 return __clz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000120}
121
122inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +0000123int __clz(unsigned long long __x) {
124 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000125#if defined(_LIBCPP_HAS_BITSCAN64)
Marshall Clowea687e22018-08-17 17:27:25 +0000126 if (_BitScanReverse64(&__where, __x))
127 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000128#else
Marshall Clowea687e22018-08-17 17:27:25 +0000129 // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
130 if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
131 return static_cast<int>(63 - (__where + 32));
132 if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
133 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000134#endif
135 return 64; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000136}
137
Marshall Clowea687e22018-08-17 17:27:25 +0000138inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000139 static_assert(sizeof(unsigned) == 4, "");
140 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000141}
142
Marshall Clowea687e22018-08-17 17:27:25 +0000143inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000144 static_assert(sizeof(unsigned long) == 4, "");
145 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000146}
147
Marshall Clowea687e22018-08-17 17:27:25 +0000148inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000149 static_assert(sizeof(unsigned long long) == 8, "");
150 return __popcnt64(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000151}
152
Marshall Clowea687e22018-08-17 17:27:25 +0000153#endif // _LIBCPP_COMPILER_MSVC
154
Marshall Clowbf32ec92018-08-17 16:07:48 +0000155_LIBCPP_END_NAMESPACE_STD
156
157#endif // _LIBCPP_BIT