ImagingTools Core SDK
imtmdbx.h
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ImtCore-Commercial
2#pragma once
3
4
5// STL includes
6#include <array>
7#include <cstdint>
8
9#if __cplusplus >= 202002L // C++20 or newer
10#include <bit.h>
11#endif
12
13#if defined(_MSC_VER)
14#include <intrin.h>
15#endif
16
17// Qt includes
18#include <QtCore/QtTypes>
19
20// ACF includes
21#include <istd/istd.h>
22
23
24namespace imtmdbx
25{
26
27
28// Maximum value for a 64-bit unsigned integer
29static const quint64 QUINT64_MAX = 0xffffffffffffffff;
30
31
32namespace BitUtils {
33
34
41constexpr std::array<int, 256> MakeBitsSetTable256() {
42 std::array<int, 256> table = {};
43 for (int i = 0; i < 256; ++i) {
44 table[i] = (i & 1) + table[i / 2];
45 }
46
47 return table;
48}
49
50// initialize at compile time
51inline constexpr std::array<int, 256> BitsSetTable256 = MakeBitsSetTable256();
52
60inline int BitCountTable(uint64_t n) {
61 return BitsSetTable256[n & 0xff] +
62 BitsSetTable256[(n >> 8) & 0xff] +
63 BitsSetTable256[(n >> 16) & 0xff] +
64 BitsSetTable256[(n >> 24) & 0xff] +
65 BitsSetTable256[(n >> 32) & 0xff] +
66 BitsSetTable256[(n >> 40) & 0xff] +
67 BitsSetTable256[(n >> 48) & 0xff] +
68 BitsSetTable256[(n >> 56)];
69}
70
81inline int BitCount(uint64_t n) {
82#if __cplusplus >= 202002L
83 return std::popcount(n);
84#elif defined(_MSC_VER)
85 return static_cast<int>(__popcnt64(n));
86#elif defined(__GNUG__) || defined(__clang__)
87 return __builtin_popcountll(n);
88#else
89 return BitCountTable(n); // fallback
90#endif
91}
92
93
94}; // namespace BitUtils
95
96
97} // namespace imtmdbx
98
99
100
101