Skip to content

Commit 852ef0b

Browse files
committed
Define a popcount util
add include Define a popcount util Update MathUtils.h
1 parent 0e673f7 commit 852ef0b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

include/swift/ABI/GenericContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/ABI/MetadataRef.h"
2424
#include "swift/ABI/InvertibleProtocols.h"
2525
#include "swift/ABI/TrailingObjects.h"
26+
#include "swift/Basic/MathUtils.h"
2627
#include "swift/Demangling/Demangle.h"
2728

2829
namespace swift {
@@ -696,7 +697,7 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
696697
if (!asSelf()->hasConditionalInvertedProtocols())
697698
return 0;
698699

699-
return __builtin_popcount(getConditionalInvertedProtocols().rawBits());
700+
return popcount(getConditionalInvertedProtocols().rawBits());
700701
}
701702

702703
size_t numTrailingObjects(

include/swift/Basic/MathUtils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
#include <cstddef>
2121

22+
#if SWIFT_COMPILER_IS_MSVC
23+
#include <intrin.h>
24+
#endif
25+
2226
namespace swift {
2327

2428
/// Round the given value up to the given alignment, as a power of two.
@@ -33,6 +37,15 @@ static inline size_t roundUpToAlignMask(size_t size, size_t alignMask) {
3337
return (size + alignMask) & ~alignMask;
3438
}
3539

40+
static inline unsigned popcount(unsigned value) {
41+
#if SWIFT_COMPILER_IS_MSVC
42+
return __popcnt(value);
43+
#else
44+
// Assume we have a compiler with this intrinsic.
45+
return __builtin_popcount(value);
46+
#endif
47+
}
48+
3649
} // namespace swift
3750

3851
#endif // #ifndef SWIFT_BASIC_MATH_UTILS_H

0 commit comments

Comments
 (0)