/src/libtorrent/src/ffs.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | |
3 | | Copyright (c) 2016, Alden Torres |
4 | | Copyright (c) 2017-2019, Arvid Norberg |
5 | | Copyright (c) 2018, Pavel Pimenov |
6 | | All rights reserved. |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | |
12 | | * Redistributions of source code must retain the above copyright |
13 | | notice, this list of conditions and the following disclaimer. |
14 | | * Redistributions in binary form must reproduce the above copyright |
15 | | notice, this list of conditions and the following disclaimer in |
16 | | the documentation and/or other materials provided with the distribution. |
17 | | * Neither the name of the author nor the names of its |
18 | | contributors may be used to endorse or promote products derived |
19 | | from this software without specific prior written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
22 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
25 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
26 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 | | POSSIBILITY OF SUCH DAMAGE. |
32 | | |
33 | | */ |
34 | | |
35 | | #include "libtorrent/config.hpp" |
36 | | #include "libtorrent/aux_/ffs.hpp" |
37 | | #include "libtorrent/aux_/byteswap.hpp" |
38 | | |
39 | | #include "libtorrent/aux_/disable_warnings_push.hpp" |
40 | | |
41 | | #if (defined _MSC_VER && _MSC_VER >= 1600 && (defined _M_IX86 || defined _M_X64)) |
42 | | #include <nmmintrin.h> |
43 | | #endif |
44 | | |
45 | | #include "libtorrent/aux_/disable_warnings_pop.hpp" |
46 | | |
47 | | namespace libtorrent { |
48 | | namespace aux { |
49 | | |
50 | | // returns the index of the first set bit. |
51 | | // Use std::log2p1 in C++20 |
52 | | int log2p1(std::uint32_t v) |
53 | 0 | { |
54 | | // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn |
55 | 0 | static const int MultiplyDeBruijnBitPosition[32] = |
56 | 0 | { |
57 | 0 | 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, |
58 | 0 | 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 |
59 | 0 | }; |
60 | |
|
61 | 0 | v |= v >> 1; // first round down to one less than a power of 2 |
62 | 0 | v |= v >> 2; |
63 | 0 | v |= v >> 4; |
64 | 0 | v |= v >> 8; |
65 | 0 | v |= v >> 16; |
66 | |
|
67 | 0 | return MultiplyDeBruijnBitPosition[std::uint32_t(v * 0x07C4ACDDU) >> 27]; |
68 | 0 | } |
69 | | |
70 | | int count_leading_zeros_sw(span<std::uint32_t const> buf) |
71 | 0 | { |
72 | 0 | auto const num = int(buf.size()); |
73 | 0 | std::uint32_t const* ptr = buf.data(); |
74 | |
|
75 | 0 | TORRENT_ASSERT(num >= 0); |
76 | 0 | TORRENT_ASSERT(ptr != nullptr); |
77 | |
|
78 | 0 | for (int i = 0; i < num; i++) |
79 | 0 | { |
80 | 0 | if (ptr[i] == 0) continue; |
81 | 0 | return i * 32 + 31 - log2p1(aux::network_to_host(ptr[i])); |
82 | 0 | } |
83 | | |
84 | 0 | return num * 32; |
85 | 0 | } |
86 | | |
87 | | int count_leading_zeros_hw(span<std::uint32_t const> buf) |
88 | 0 | { |
89 | 0 | auto const num = int(buf.size()); |
90 | 0 | std::uint32_t const* ptr = buf.data(); |
91 | |
|
92 | 0 | TORRENT_ASSERT(num >= 0); |
93 | 0 | TORRENT_ASSERT(ptr != nullptr); |
94 | |
|
95 | 0 | for (int i = 0; i < num; i++) |
96 | 0 | { |
97 | 0 | if (ptr[i] == 0) continue; |
98 | | |
99 | 0 | #if TORRENT_HAS_BUILTIN_CLZ |
100 | 0 | std::uint32_t const v = aux::network_to_host(ptr[i]); |
101 | 0 | return i * 32 + __builtin_clz(v); |
102 | | #elif defined _MSC_VER |
103 | | std::uint32_t const v = aux::network_to_host(ptr[i]); |
104 | | DWORD pos; |
105 | | _BitScanReverse(&pos, v); |
106 | | return i * 32 + 31 - pos; |
107 | | #else |
108 | | TORRENT_ASSERT_FAIL(); |
109 | | return -1; |
110 | | #endif |
111 | 0 | } |
112 | | |
113 | 0 | return num * 32; |
114 | 0 | } |
115 | | |
116 | | int count_leading_zeros(span<std::uint32_t const> buf) |
117 | 0 | { |
118 | 0 | #if TORRENT_HAS_BUILTIN_CLZ || defined _MSC_VER |
119 | 0 | return aux::count_leading_zeros_hw(buf); |
120 | | #else |
121 | | return aux::count_leading_zeros_sw(buf); |
122 | | #endif |
123 | 0 | } |
124 | | |
125 | | int count_trailing_ones_sw(span<std::uint32_t const> buf) |
126 | 0 | { |
127 | 0 | auto const num = int(buf.size()); |
128 | 0 | std::uint32_t const* ptr = buf.data(); |
129 | |
|
130 | 0 | TORRENT_ASSERT(num >= 0); |
131 | 0 | TORRENT_ASSERT(ptr != nullptr); |
132 | |
|
133 | 0 | for (int i = num - 1; i >= 0; i--) |
134 | 0 | { |
135 | 0 | if (ptr[i] == 0xffffffff) continue; |
136 | 0 | std::uint32_t v = ~aux::network_to_host(ptr[i]); |
137 | |
|
138 | 0 | for (int k = 0; k < 32; ++k, v >>= 1) |
139 | 0 | { |
140 | 0 | if ((v & 1) == 0) continue; |
141 | 0 | return (num - i - 1) * 32 + k; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 0 | return num * 32; |
146 | 0 | } |
147 | | |
148 | | int count_trailing_ones_hw(span<std::uint32_t const> buf) |
149 | 0 | { |
150 | 0 | auto const num = int(buf.size()); |
151 | 0 | std::uint32_t const* ptr = buf.data(); |
152 | |
|
153 | 0 | TORRENT_ASSERT(num >= 0); |
154 | 0 | TORRENT_ASSERT(ptr != nullptr); |
155 | |
|
156 | 0 | for (int i = num - 1; i >= 0; i--) |
157 | 0 | { |
158 | 0 | if (ptr[i] == 0xffffffff) continue; |
159 | | |
160 | 0 | #if TORRENT_HAS_BUILTIN_CTZ |
161 | 0 | std::uint32_t const v = ~aux::network_to_host(ptr[i]); |
162 | 0 | return (num - i - 1) * 32 + __builtin_ctz(v); |
163 | | #elif defined _MSC_VER |
164 | | std::uint32_t const v = ~aux::network_to_host(ptr[i]); |
165 | | DWORD pos; |
166 | | _BitScanForward(&pos, v); |
167 | | return (num - i - 1) * 32 + pos; |
168 | | #else |
169 | | TORRENT_ASSERT_FAIL(); |
170 | | return -1; |
171 | | #endif |
172 | 0 | } |
173 | | |
174 | 0 | return num * 32; |
175 | 0 | } |
176 | | |
177 | | int count_trailing_ones(span<std::uint32_t const> buf) |
178 | 0 | { |
179 | 0 | #if TORRENT_HAS_BUILTIN_CTZ || defined _MSC_VER |
180 | 0 | return aux::count_trailing_ones_hw(buf); |
181 | | #else |
182 | | return aux::count_trailing_ones_sw(buf); |
183 | | #endif |
184 | 0 | } |
185 | | }} |