Skip to content

Commit

Permalink
Define a popcount util
Browse files Browse the repository at this point in the history
add include

Define a popcount util

Update MathUtils.h
  • Loading branch information
Azoy committed Apr 30, 2024
1 parent 0e673f7 commit 852ef0b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/swift/ABI/GenericContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/ABI/MetadataRef.h"
#include "swift/ABI/InvertibleProtocols.h"
#include "swift/ABI/TrailingObjects.h"
#include "swift/Basic/MathUtils.h"
#include "swift/Demangling/Demangle.h"

namespace swift {
Expand Down Expand Up @@ -696,7 +697,7 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
if (!asSelf()->hasConditionalInvertedProtocols())
return 0;

return __builtin_popcount(getConditionalInvertedProtocols().rawBits());
return popcount(getConditionalInvertedProtocols().rawBits());
}

size_t numTrailingObjects(
Expand Down
13 changes: 13 additions & 0 deletions include/swift/Basic/MathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

#include <cstddef>

#if SWIFT_COMPILER_IS_MSVC
#include <intrin.h>
#endif

namespace swift {

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

static inline unsigned popcount(unsigned value) {
#if SWIFT_COMPILER_IS_MSVC
return __popcnt(value);
#else
// Assume we have a compiler with this intrinsic.
return __builtin_popcount(value);
#endif
}

} // namespace swift

#endif // #ifndef SWIFT_BASIC_MATH_UTILS_H

0 comments on commit 852ef0b

Please sign in to comment.