File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change 17
17
#ifndef SWIFT_BASIC_MATH_UTILS_H
18
18
#define SWIFT_BASIC_MATH_UTILS_H
19
19
20
+ #include " Compiler.h"
20
21
#include < cstddef>
22
+ #include < cstdint>
21
23
22
24
#if SWIFT_COMPILER_IS_MSVC
23
25
#include < intrin.h>
@@ -38,11 +40,17 @@ static inline size_t roundUpToAlignMask(size_t size, size_t alignMask) {
38
40
}
39
41
40
42
static inline unsigned popcount (unsigned value) {
41
- #if SWIFT_COMPILER_IS_MSVC
43
+ #if SWIFT_COMPILER_IS_MSVC && (defined(_M_IX86) || defined(_M_X64))
44
+ // The __popcnt intrinsic is only available when targetting x86{_64} with MSVC.
42
45
return __popcnt (value);
43
- #else
44
- // Assume we have a compiler with this intrinsic.
46
+ #elif __has_builtin(__builtin_popcount)
45
47
return __builtin_popcount (value);
48
+ #else
49
+ // From llvm/ADT/bit.h which the runtime doesn't have access to (yet?)
50
+ uint32_t v = value;
51
+ v = v - ((v >> 1 ) & 0x55555555 );
52
+ v = (v & 0x33333333 ) + ((v >> 2 ) & 0x33333333 );
53
+ return int (((v + (v >> 4 ) & 0xF0F0F0F ) * 0x1010101 ) >> 24 );
46
54
#endif
47
55
}
48
56
You can’t perform that action at this time.
0 commit comments