Define a popcount util

add include

Define a popcount util

Update MathUtils.h
This commit is contained in:
Alejandro Alonso
2024-04-26 09:45:30 -07:00
parent 0e673f70a4
commit 852ef0bc01
2 changed files with 15 additions and 1 deletions

View File

@@ -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.
@@ -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