Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------------ bit ----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame^] | 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 |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_BIT |
| 11 | #define _LIBCPP_BIT |
| 12 | |
| 13 | /* |
| 14 | bit synopsis |
| 15 | |
| 16 | namespace std { |
| 17 | |
| 18 | } // namespace std |
| 19 | |
| 20 | */ |
| 21 | |
| 22 | #include <__config> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 23 | #include <version> |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 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 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 38 | #ifndef _LIBCPP_COMPILER_MSVC |
| 39 | |
| 40 | inline _LIBCPP_INLINE_VISIBILITY |
| 41 | int __ctz(unsigned __x) { return __builtin_ctz(__x); } |
| 42 | |
| 43 | inline _LIBCPP_INLINE_VISIBILITY |
| 44 | int __ctz(unsigned long __x) { return __builtin_ctzl(__x); } |
| 45 | |
| 46 | inline _LIBCPP_INLINE_VISIBILITY |
| 47 | int __ctz(unsigned long long __x) { return __builtin_ctzll(__x); } |
| 48 | |
| 49 | |
| 50 | inline _LIBCPP_INLINE_VISIBILITY |
| 51 | int __clz(unsigned __x) { return __builtin_clz(__x); } |
| 52 | |
| 53 | inline _LIBCPP_INLINE_VISIBILITY |
| 54 | int __clz(unsigned long __x) { return __builtin_clzl(__x); } |
| 55 | |
| 56 | inline _LIBCPP_INLINE_VISIBILITY |
| 57 | int __clz(unsigned long long __x) { return __builtin_clzll(__x); } |
| 58 | |
| 59 | |
| 60 | inline _LIBCPP_INLINE_VISIBILITY |
| 61 | int __popcount(unsigned __x) { return __builtin_popcount(__x); } |
| 62 | |
| 63 | inline _LIBCPP_INLINE_VISIBILITY |
| 64 | int __popcount(unsigned long __x) { return __builtin_popcountl(__x); } |
| 65 | |
| 66 | inline _LIBCPP_INLINE_VISIBILITY |
| 67 | int __popcount(unsigned long long __x) { return __builtin_popcountll(__x); } |
| 68 | |
| 69 | #else // _LIBCPP_COMPILER_MSVC |
| 70 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 71 | // Precondition: __x != 0 |
| 72 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 73 | int __ctz(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 74 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 75 | static_assert(sizeof(unsigned long) == 4, ""); |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 76 | unsigned long __where; |
| 77 | if (_BitScanForward(&__where, __x)) |
| 78 | return static_cast<int>(__where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 79 | return 32; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 83 | int __ctz(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 84 | static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); |
| 85 | return __ctz(static_cast<unsigned>(__x)); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 89 | int __ctz(unsigned long long __x) { |
| 90 | unsigned long __where; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 91 | #if defined(_LIBCPP_HAS_BITSCAN64) |
| 92 | (defined(_M_AMD64) || defined(__x86_64__)) |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 93 | if (_BitScanForward64(&__where, __x)) |
| 94 | return static_cast<int>(__where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 95 | #else |
| 96 | // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 97 | 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 Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 101 | #endif |
| 102 | return 64; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Precondition: __x != 0 |
| 106 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 107 | int __clz(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 108 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 109 | static_assert(sizeof(unsigned long) == 4, ""); |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 110 | unsigned long __where; |
| 111 | if (_BitScanReverse(&__where, __x)) |
| 112 | return static_cast<int>(31 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 113 | return 32; // Undefined Behavior. |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 117 | int __clz(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 118 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 119 | return __clz(static_cast<unsigned>(__x)); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 123 | int __clz(unsigned long long __x) { |
| 124 | unsigned long __where; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 125 | #if defined(_LIBCPP_HAS_BITSCAN64) |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 126 | if (_BitScanReverse64(&__where, __x)) |
| 127 | return static_cast<int>(63 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 128 | #else |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 129 | // 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 Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 134 | #endif |
| 135 | return 64; // Undefined Behavior. |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 138 | inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 139 | static_assert(sizeof(unsigned) == 4, ""); |
| 140 | return __popcnt(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 143 | inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 144 | static_assert(sizeof(unsigned long) == 4, ""); |
| 145 | return __popcnt(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 148 | inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 149 | static_assert(sizeof(unsigned long long) == 8, ""); |
| 150 | return __popcnt64(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 153 | #endif // _LIBCPP_COMPILER_MSVC |
| 154 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 155 | _LIBCPP_END_NAMESPACE_STD |
| 156 | |
| 157 | #endif // _LIBCPP_BIT |