blob: 2d403bf7a0c6ca5e0e524ee7b5541898e167d73b [file] [log] [blame]
Marshall Clowbf32ec92018-08-17 16:07:48 +00001// -*- C++ -*-
2//===------------------------------ bit ----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===---------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_BIT
12#define _LIBCPP_BIT
13
14/*
15 bit synopsis
16
17namespace std {
18
19} // namespace std
20
21*/
22
23#include <__config>
24
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
38// Precondition: __x != 0
39inline _LIBCPP_INLINE_VISIBILITY
40unsigned __ctz(unsigned __x) {
41#ifndef _LIBCPP_COMPILER_MSVC
42 return static_cast<unsigned>(__builtin_ctz(__x));
43#else
44 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
45 static_assert(sizeof(unsigned long) == 4, "");
46 unsigned long where;
47 // Search from LSB to MSB for first set bit.
48 // Returns zero if no set bit is found.
49 if (_BitScanForward(&where, __x))
50 return where;
51 return 32;
52#endif
53}
54
55inline _LIBCPP_INLINE_VISIBILITY
56unsigned long __ctz(unsigned long __x) {
57#ifndef _LIBCPP_COMPILER_MSVC
58 return static_cast<unsigned long>(__builtin_ctzl(__x));
59#else
60 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
61 return __ctz(static_cast<unsigned>(__x));
62#endif
63}
64
65inline _LIBCPP_INLINE_VISIBILITY
66unsigned long long __ctz(unsigned long long __x) {
67#ifndef _LIBCPP_COMPILER_MSVC
68 return static_cast<unsigned long long>(__builtin_ctzll(__x));
69#else
70 unsigned long where;
71// Search from LSB to MSB for first set bit.
72// Returns zero if no set bit is found.
73#if defined(_LIBCPP_HAS_BITSCAN64)
74 (defined(_M_AMD64) || defined(__x86_64__))
75 if (_BitScanForward64(&where, __x))
76 return static_cast<int>(where);
77#else
78 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
79 // Scan the Low Word.
80 if (_BitScanForward(&where, static_cast<unsigned long>(__x)))
81 return where;
82 // Scan the High Word.
83 if (_BitScanForward(&where, static_cast<unsigned long>(__x >> 32)))
84 return where + 32; // Create a bit offset from the LSB.
85#endif
86 return 64;
87#endif // _LIBCPP_COMPILER_MSVC
88}
89
90// Precondition: __x != 0
91inline _LIBCPP_INLINE_VISIBILITY
92unsigned __clz(unsigned __x) {
93#ifndef _LIBCPP_COMPILER_MSVC
94 return static_cast<unsigned>(__builtin_clz(__x));
95#else
96 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
97 static_assert(sizeof(unsigned long) == 4, "");
98 unsigned long where;
99 // Search from LSB to MSB for first set bit.
100 // Returns zero if no set bit is found.
101 if (_BitScanReverse(&where, __x))
102 return 31 - where;
103 return 32; // Undefined Behavior.
104#endif
105}
106
107inline _LIBCPP_INLINE_VISIBILITY
108unsigned long __clz(unsigned long __x) {
109#ifndef _LIBCPP_COMPILER_MSVC
110 return static_cast<unsigned long>(__builtin_clzl (__x));
111#else
112 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
113 return __clz(static_cast<unsigned>(__x));
114#endif
115}
116
117inline _LIBCPP_INLINE_VISIBILITY
118unsigned long long __clz(unsigned long long __x) {
119#ifndef _LIBCPP_COMPILER_MSVC
120 return static_cast<unsigned long long>(__builtin_clzll(__x));
121#else
122 unsigned long where;
123// BitScanReverse scans from MSB to LSB for first set bit.
124// Returns 0 if no set bit is found.
125#if defined(_LIBCPP_HAS_BITSCAN64)
126 if (_BitScanReverse64(&where, __x))
127 return static_cast<int>(63 - where);
128#else
129 // Scan the high 32 bits.
130 if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32)))
131 return 63 - (where + 32); // Create a bit offset from the MSB.
132 // Scan the low 32 bits.
133 if (_BitScanReverse(&where, static_cast<unsigned long>(__x)))
134 return 63 - where;
135#endif
136 return 64; // Undefined Behavior.
137#endif // _LIBCPP_COMPILER_MSVC
138}
139
140inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
141#ifndef _LIBCPP_COMPILER_MSVC
142 return __builtin_popcount (__x);
143#else
144 static_assert(sizeof(unsigned) == 4, "");
145 return __popcnt(__x);
146#endif
147}
148
149inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
150#ifndef _LIBCPP_COMPILER_MSVC
151 return __builtin_popcountl (__x);
152#else
153 static_assert(sizeof(unsigned long) == 4, "");
154 return __popcnt(__x);
155#endif
156}
157
158inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
159#ifndef _LIBCPP_COMPILER_MSVC
160 return __builtin_popcountll(__x);
161#else
162 static_assert(sizeof(unsigned long long) == 8, "");
163 return __popcnt64(__x);
164#endif
165}
166
167_LIBCPP_END_NAMESPACE_STD
168
169#endif // _LIBCPP_BIT