File indexing completed on 2024-12-22 04:40:13

0001 /*
0002     SPDX-FileCopyrightText: 2020-2022 Mladen Milinkovic <max@smoothware.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef HELPERS_COMMON_H
0008 #define HELPERS_COMMON_H
0009 
0010 #include <type_traits>
0011 
0012 #define $(str) QStringLiteral(str)
0013 
0014 #define GLUE(x, y) x y // needed to make MSVC happy
0015 
0016 #define RE$1(regExp) QRegularExpression(QStringLiteral(regExp))
0017 #define RE$2(regExp, opts) QRegularExpression(QStringLiteral(regExp), opts)
0018 #define RE$_VA(_1, _2, NAME, ...) NAME
0019 #define RE$(...) GLUE(RE$_VA(__VA_ARGS__, RE$2, RE$1), (__VA_ARGS__))
0020 
0021 #define staticRE$2(sVar, regExp) const static QRegularExpression sVar(QStringLiteral(regExp))
0022 #define staticRE$3(sVar, regExp, opts) const static QRegularExpression sVar(QStringLiteral(regExp), opts)
0023 #define staticRE$_VA(_1, _2, _3, NAME, ...) NAME
0024 #define staticRE$(...) GLUE(staticRE$_VA(__VA_ARGS__, staticRE$3, staticRE$2), (__VA_ARGS__))
0025 
0026 #define REu QRegularExpression::UseUnicodePropertiesOption
0027 #define REm QRegularExpression::MultilineOption
0028 #define REs QRegularExpression::DotMatchesEverythingOption
0029 #define REi QRegularExpression::CaseInsensitiveOption
0030 
0031 /**
0032  * @brief Return number of set bits in value - using parallel bit count
0033  * https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
0034  * @param v unsigned value to count bits
0035  * @return number of set bits
0036  */
0037 template<typename T, class = typename std::enable_if<std::is_unsigned<T>::value>::type>
0038 int bitCount(T v)
0039 {
0040     const constexpr T x55 = ~T(0) / T(3);
0041     const constexpr T x33 = ~T(0) / T(5);
0042     const constexpr T x0f = ~T(0) / T(255) * T(15);
0043     const constexpr T x01 = ~T(0) / T(255);
0044     v = v - ((v >> 1) & x55);
0045     v = (v & x33) + ((v >> 2) & x33);
0046     v = (v + (v >> 4)) & x0f;
0047     return (v * x01) >> (sizeof(T) - 1) * 8;
0048 }
0049 
0050 #endif // HELPERS_COMMON_H