扫描位流中位模式的最快方法
我需要扫描比特流中的 16 位字。 不保证在字节或字边界上对齐。
实现这一目标最快的方法是什么?有多种暴力破解方法;使用表格和/或移位,但是是否有任何“位旋转快捷方式”可以通过给出是/否/可能包含每个字节或单词到达时的标志结果来减少计算次数?
C 代码、内在函数、x86 机器代码都会很有趣。
I need to scan for a 16 bit word in a bit stream. It is not guaranteed to be aligned on byte or word boundaries.
What is the fastest way of achieving this? There are various brute force methods; using tables and/or shifts but are there any "bit twiddling shortcuts" that can cut down the number of calculations by giving yes/no/maybe contains the flag results for each byte or word as it arrives?
C code, intrinsics, x86 machine code would all be interesting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用简单的蛮力有时是好的。
我认为预先计算该单词的所有移位值并将它们放入 16 个整数中
所以你得到了一个像这样的数组(假设
int
的宽度是short
的两倍),然后对于从流中得到的每个无符号短整型,将其转换为 int和前一个 Short 并将该 unsigned int 与 16 个 unsigned int 进行比较。如果其中任何一个匹配,您就得到了一个。
所以基本上是这样的:
请注意,当在同一位上多次检测到模式时,这可能意味着多次命中:
例如,32 位 0 且您要检测的模式是 16 个 0,那么这意味着该模式是检测到16次!
假设它大约按照编写的方式编译,其时间成本是每个输入字 16 次检查。对于每个输入位,这会执行一个
&
和==
,以及分支或其他条件增量。还有每一位掩码的表查找。不需要查表;通过右移
combined
,我们可以显着提高asm的效率,如图所示在另一个答案中,它还展示了如何在 x86 上使用 SIMD 对其进行矢量化。Using simple brute force is sometimes good.
I think precalc all shifted values of the word and put them in 16 ints
so you got an array like this (assuming
int
is twice as wide asshort
)and then for every unsigned short you get out of the stream, make an int of that short and the previous short and compare that unsigned int to the 16 unsigned int's. If any of them match, you got one.
So basically like this:
Do note that this could potentially mean multiple hits when the patterns is detected more than once on the same bits:
e.g. 32 bits of 0's and the pattern you want to detect is 16 0's, then it would mean the pattern is detected 16 times!
The time cost of this, assuming it compiles approximately as written, is 16 checks per input word. Per input bit, this does one
&
and==
, and branch or other conditional increment. And also a table lookup for the mask for every bit.The table lookup is unnecessary; by instead right-shifting
combined
we get significantly more efficient asm, as shown in another answer which also shows how to vectorize this with SIMD on x86.如果两个字符 {0, 1} 的字母表上的 Knuth-Morris-Pratt 算法和 Reinier 的想法都不够快,那么这里有一个技巧可以将搜索速度加快 32 倍。
您可以首先使用包含 256 个条目的表来检查比特流中的每个字节是否包含在您要查找的 16 位字中。 然后,您可以使用以下命令
找到比特流中匹配的可能位置。256
个表条目中最多有 8 个不为零,平均而言,您只需仔细查看每 32 个位置。仅对于这个字节(与前一个字节和后一个字节相结合),您必须使用位操作或一些屏蔽技术(如reinier建议的那样)来查看是否存在匹配。
该代码假设您使用小端字节顺序。字节中位的顺序也可能是一个问题(已经实现 CRC32 校验和的每个人都知道)。
Here is a trick to speed up the search by a factor of 32, if neither the Knuth-Morris-Pratt algorithm on the alphabet of two characters {0, 1} nor reinier's idea are fast enough.
You can first use a table with 256 entries to check for each byte in your bit stream if it is contained in the 16-bit word you are looking for. The table you get with
You can then find possible positions for matches in the bit stream using
As at most 8 of the 256 table entries are not zero, in average you have to take a closer look only at every 32th position. Only for this byte (combined with the bytes one before and one after) you have then to use bit operations or some masking techniques as suggested by reinier to see if there is a match.
The code assumes that you use little endian byte order. The order of the bits in a byte can also be an issue (known to everyone who already implemented a CRC32 checksum).
我想建议一个使用 3 个大小为 256 的查找表的解决方案。这对于大比特流来说非常有效。该解决方案需要样本中的 3 个字节进行比较。下图显示了 3 个字节中 16 位数据的所有可能排列。每个字节区域以不同的颜色显示。
替代文本 http://img70.imageshack.us/img70/8711/80541519.jpg< /a>
这里将在第一个样本中检查 1 到 8,在下一个样本中检查 9 到 16,依此类推。现在,当我们搜索一个模式时,我们将找到该模式的所有8种可能的排列方式(如下所示),并将存储在3个查找表中(左、中、中)正确的)。
初始化查找表:
让我们以
0111011101110111
为例作为要查找的模式。现在考虑第四种安排。左侧部分为XXX01110
。用00010000
填充左查找表中左部分(XXX01110
)指向的所有原始数据。 1表示输入Pattern排列的起始位置。因此,左查找表的后续 8 个原始数据将由 16 (00010000
) 填充。排列的中间部分是
11101110
。中间查找表中该索引 (238) 的原始指向将由 16 (00010000
) 填充。现在排列的右侧部分将是
111XXXXX
。所有索引为111XXXXX
的原始数据(32 个原始数据)都将被填充为 16 (00010000
)。填充时我们不应该覆盖查找表中的元素。相反,执行按位或运算来更新已填充的原始数据。在上面的示例中,由第三种排列写入的所有原始数据将由第七种排列更新,如下所示。
因此,左查找表中索引为
XX011101
、中查找表中索引为11101110
以及右查找表中索引为111XXXXX
的原始数据将更新为00100010
按第七种安排。搜索模式:
取三个字节的样本。查找Count如下,其中Left是左查找表,Middle是中间查找表,Right是右查找表。
Count 中的 1 表示所取样本中匹配的 Pattern 的数量。
我可以提供一些经过测试的示例代码。
初始化查找表:
搜索模式:
Data是流缓冲区,Left是左查找表,Middle 是中间查找表,Right是右查找表。
限制:
如果将模式放置在流缓冲区的最末尾,则上述循环无法检测到模式。以下代码需要添加 after 循环来克服此限制。
优点:
此算法仅需要 N-1 个逻辑步骤即可在 N 字节数组中查找模式。唯一的开销是最初填充查找表,这在所有情况下都是恒定的。所以这对于搜索巨大的字节流非常有效。
I would like to suggest a solution using 3 lookup tables of size 256. This would be efficient for large bit streams. This solution takes 3 bytes in a sample for comparison. Following figure shows all possible arrangements of a 16 bit data in 3 bytes. Each byte region has shown in different color.
alt text http://img70.imageshack.us/img70/8711/80541519.jpg
Here checking for 1 to 8 will be taken care in first sample and 9 to 16 in next sample and so on. Now when we are searching for a Pattern, we will find all the 8 possible arrangements (as below) of this Pattern and will store in 3 lookup tables (Left, Middle and Right).
Initializing Lookup Tables:
Lets take an example
0111011101110111
as a Pattern to find. Now consider 4th arrangement. Left part would beXXX01110
. Fill all raws of Left lookup table pointing by left part (XXX01110
) with00010000
. 1 indicates starting position of arrangement of input Pattern. Thus following 8 raws of Left look up table would be filled by 16 (00010000
).Middle part of arrangement would be
11101110
. Raw pointing by this index (238) in Middle look up table will be filled by 16 (00010000
).Now Right part of arrangement would be
111XXXXX
. All raws (32 raws) with index111XXXXX
will be filled by 16 (00010000
).We should not overwrite elements in look up table while filling. Instead do a bitwise OR operation to update an already filled raw. In above example, all raws written by 3rd arrangement would be updated by 7th arrangement as follows.
Thus raws with index
XX011101
in Left lookup table and11101110
in Middle lookup table and111XXXXX
in Right lookup table will be updated to00100010
by 7th arrangement.Searching Pattern:
Take a sample of three bytes. Find Count as follows where Left is left lookup table, Middle is middle lookup table and Right is right lookup table.
Number of 1 in Count gives the number of matching Pattern in taken sample.
I can give some sample code which is tested.
Initializing lookup table:
Searching Pattern:
Data is stream buffer, Left is left lookup table, Middle is middle lookup table and Right is right lookup table.
Limitation:
Above loop cannot detect a Pattern if it is placed at the very end of stream buffer. Following code need to add after loop to overcome this limitation.
Advantage:
This algorithm takes only N-1 logical steps to find a Pattern in an array of N bytes. Only overhead is to fill the lookup tables initially which is constant in all the cases. So this will be very effective for searching huge byte streams.
我的钱花在 Knuth-Morris-Pratt 上两个字符的字母表。
My money's on Knuth-Morris-Pratt with an alphabet of two characters.
我将实现一个具有 16 个状态的状态机。
每个状态代表有多少接收到的比特符合该模式。如果下一个接收到的位符合模式的下一个位,则机器进入下一个状态。如果不是这种情况,机器将返回到第一个状态(或者如果模式的开头可以与较少数量的接收比特相匹配,则返回到另一个状态)。
当机器到达最后一个状态时,这表明该模式已在比特流中被识别。
I would implement a state machine with 16 states.
Each state represents how many received bits conform to the pattern. If the next received bit conform to the next bit of the pattern, the machine steps to the next state. If this is not the case, the machine steps back to the first state (or to another state if the beginning of the pattern can be matched with a smaller number of received bits).
When the machine reaches the last state, this indicates that the pattern has been identified in the bit stream.
原子的
看起来不错,直到我考虑了卢克和 MSalter 要求提供更多有关细节的信息。
事实证明,这些细节可能表明一种比 KMP 更快的方法。 KMP 文章链接到
当搜索模式为“AAAAAA”时, '。对于多种模式搜索,
可能最合适。
您可以在此处找到进一步的介绍性讨论。
atomice's
looked good until I considered Luke and MSalter's requests for more information about the particulars.
Turns out the particulars might indicate a quicker approach than KMP. The KMP article links to
for a particular case when the search pattern is 'AAAAAA'. For a multiple pattern search, the
might be most suitable.
You can find further introductory discussion here.
实现 @Toad 检查每个位位置的简单强力算法 是将数据移动到位,而不是移动掩码。不需要任何数组,简单得多,只需在循环内右移组合 >>= 1 并比较低 16 位即可。 (要么使用固定掩码,要么转换为
uint16_t
。)(在多个问题中,我注意到创建掩码往往比仅移出不需要的位效率低。 )
(正确处理
uint16_t
数组的最后一个16位块,或者特别是奇数大小的字节数组的最后一个字节,留给读者作为练习。)这编译显着对于大多数 ISA(例如 x86、AArch64 和 ARM),比使用最近的 gcc 和 clang 从数组加载掩码更有效。
编译器将循环完全展开 16,以便它们可以使用带有立即操作数的位域提取指令(例如 ARM
ubfx
无符号位域提取或 PowerPCrwlinm
向左旋转 + 立即屏蔽位范围)来提取16 位到 32 或 64 位寄存器的底部,它们可以在其中进行常规比较和分支。实际上并不存在右移 1 的依赖链。在 x86 上,CPU 可以执行忽略高位的 16 位比较,例如右移
后的
cmp cx,dx
edx 中的组合一些 ISA 的编译器设法在 @Toad 的版本上做得和这个版本一样好,例如 PowerPC 的 clang 设法使用
rlwinm
使用立即数屏蔽 16 位范围的组合
,并且它将所有 16 个预移位模式值保留在 16 个寄存器中,因此无论哪种方式,它都只是 rlwinm / 比较 / 分支是否rlwinm 是否具有非零旋转计数。但右移版本不需要设置16个tmp寄存器。 https://godbolt.org/z/8mUaDIAVX2 暴力破解
有(至少)2实现方法:
使用 64 位元素移位而不是 32 位元素移位,我们可以检查多个相邻的 16 位窗口,而不是总是忽略高 16 位(其中移入零)。但我们在 SIMD 元素边界处仍然存在中断,其中移入了零,而不是来自更高地址的实际数据。 (未来的解决方案:AVX512VBMI2双班,如
VPSHRDW
,SHRD
的 SIMD 版本。)也许无论如何,这样做都是值得的,然后返回我们在
__m256i 中每个 64 位元素顶部错过的 4x 16 位元素
。也许将多个向量的剩余部分组合起来。这对于通常快速找到命中的搜索很有用,尤其是在少于前 32 字节的数据中。对于大型搜索来说这还不错(但仍然是纯粹的蛮力,一次只检查 1 个单词),并且在 Skylake 上可能不比并行检查多个窗口的 16 个偏移量差。
这是针对 Skylake 进行调整的,在其他 CPU 上,变量移位效率较低,您可能会考虑仅对偏移量 0..7 进行 1 个变量移位,然后通过移位来创建偏移量 8..15。或者完全是别的东西。
编译效果出奇地好与 gcc/clang (在 Godbolt 上),具有直接从内存中广播的内部循环。 (将
memcpy
未对齐加载和set1()
优化为单个vpbroadcastd
)Godbolt 链接中还包含一个测试
main
在一个小数组上运行它。 (自上次调整以来我可能没有进行过测试,但我之前确实测试过它,并且打包 + 位扫描的东西确实有效。)这是 8 uops 的工作 + 3 uops 的循环开销(假设 and/jne 的宏融合,以及 cmp/jb,我们将在 Haswell/Skylake 上获得)。在 AMD 上,256 位指令是多个 uops,它会更多。
或者当然使用简单的右移立即数将所有元素移动 1,并并行检查多个窗口,而不是在同一窗口中进行多个偏移。
如果没有高效的变量移位(尤其是根本没有 AVX2),这对于大型搜索来说会更好,即使它需要更多的工作来整理第一个命中的位置,以防出现 很受欢迎。 (在除了最低元素之外的其他位置找到命中后,您需要检查所有早期窗口的所有剩余偏移量。)
A simpler way to implement @Toad's simple brute-force algorithm that checks every bit-position is to shift the data into place, instead of shifting a mask. There's no need for any arrays, much simpler is just to right shift
combined >>= 1
inside the loop and compare the low 16 bits. (Either use a fixed mask, or cast touint16_t
.)(Across multiple problems, I've noticed that creating a mask tends to be less efficient than just shifting out the bits you don't want.)
(correctly handling the very last 16-bit chunk of an array of
uint16_t
, or especially the last byte of an odd-sized byte array, is left as an exercise for the reader.)This compiles significantly more efficiently than loading a mask from an array with recent gcc and clang for most ISAs, like x86, AArch64, and ARM.
Compilers fully unroll the loop by 16 so they can use bitfield-extract instructions with immediate operands (like ARM
ubfx
unsigned bitfield extract or PowerPCrwlinm
rotate-left + immediate-mask a bit-range) to extract 16 bits to the bottom of a 32 or 64-bit register where they can do a regular compare-and-branch. There isn't actually a dependency chain of right shifts by 1.On x86, the CPU can do a 16-bit compare that ignores high bits, e.g.
cmp cx,dx
after right-shiftingcombined
inedx
Some compilers for some ISAs manage to do as good a job with @Toad's version as with this, e.g. clang for PowerPC manages to optimize away the array of masks, using
rlwinm
to mask a 16-bit range ofcombined
using immediates, and it keeps all 16 pre-shifted pattern values in 16 registers, so either way it's just rlwinm / compare / branch whether the rlwinm has a non-zero rotate count or not. But the right-shift version doesn't need to set up 16 tmp registers. https://godbolt.org/z/8mUaDIAVX2 brute-force
There are (at least) 2 ways to do this:
With 64-bit element shifts instead of 32, we could check multiple adjacent 16-bit windows instead of always ignoring the upper 16 (where zeros are shifted in). But we still have a break at SIMD element boundaries where zeros are shifted in, instead of actual data from a higher address. (Future solution: AVX512VBMI2 double-shifts like
VPSHRDW
, a SIMD version ofSHRD
.)Maybe it's worth doing this anyway, then coming back for the 4x 16-bit elements we missed at the top of each 64-bit element in a
__m256i
. Maybe combining leftovers across multiple vectors.This is good for searches that normally find a hit quickly, especially in less than the first 32 bytes of data. It's not bad for big searches (but is still pure brute force, only checking 1 word at a time), and on Skylake maybe not worse than checking 16 offsets of multiple windows in parallel.
This is tuned for Skylake, on other CPUs, where variable-shifts are less efficient, you might consider just 1 variable shift for offsets 0..7, and then create offsets 8..15 by shifting that. Or something else entirely.
This compiles surprisingly well with gcc/clang (on Godbolt), with an inner loop that broadcasts straight from memory. (Optimizing the
memcpy
unaligned load and theset1()
into a singlevpbroadcastd
)Also included on the Godbolt link is a test
main
that runs it on a small array. (I may not have tested since the last tweak, but I did test it earlier and the packing + bit-scan stuff does work.)That's 8 uops of work + 3 uops of loop overhead (assuming macro-fusion of and/jne, and of cmp/jb, which we'll get on Haswell/Skylake). On AMD where 256-bit instructions are multiple uops, it'll be more.
Or of course using plain right-shift immediate to shift all elements by 1, and check multiple windows in parallel instead of multiple offsets in the same window.
Without efficient variable-shift (especially without AVX2 at all), that would be better for big searches, even if it requires a bit more work to sort out where the first hit is located in case there is a hit. (After finding a hit somewhere other than the lowest element, you need to check all remaining offsets of all earlier windows.)
SIMD 指令似乎很有用。 SSE2 添加了一堆整数指令,用于同时处理多个整数,但我无法想象有多少解决方案不涉及大量位移,因为您的数据不会对齐。这实际上听起来像是 FPGA 应该做的事情。
Seems like a good use for SIMD instructions. SSE2 added a bunch of integer instructions for crunching multiple integers at the same time, but I can't imagine many solutions for this that don't involve a lot of bit shifts since your data isn't going to be aligned. This actually sounds like something an FPGA should be doing.
我要做的是创建 16 个前缀和 16 个后缀。然后对于每个 16 位输入块确定最长的后缀匹配。如果下一个块具有长度为
(16-N)
的前缀匹配,那么您就获得了匹配。后缀匹配实际上并不是 16 次比较。然而,这需要根据模式词进行预先计算。例如,如果模式字是 101010101010101010,您可以首先测试 16 位输入块的最后一位。如果该位为 0,则只需测试 ...10101010 就足够了。如果最后一位是1,则需要测试...1010101是否足够。每个都有 8 个,总共 1+8 次比较。如果模式字是 1111111111110000,您仍然需要测试输入的最后一位后缀匹配。如果该位为 1,则必须执行 12 次后缀匹配(正则表达式:1{1,12}),但如果为 0,则只有 4 个可能的匹配(正则表达式 1111 1111 1111 0{1,4}),同样是平均值共 9 项测试。添加
16-N
前缀匹配,您会发现每个 16 位块只需要 10 次检查。What I would do is create 16 prefixes and 16 suffixes. Then for each 16 bit input chunk determine the longest suffix match. You've got a match if the next chunk has a prefix match of length
(16-N)
A suffix match doesn't actually 16 comparisons. However, this takes pre-calculation based upon the pattern word. For example, if the patternword is 101010101010101010, you can first test the last bit of your 16 bit input chunk. If that bit is 0, you only need to test the ...10101010 suffices. If the last bit is 1, you need to test the ...1010101 suffices. You've got 8 of each, for a total of 1+8 comparisons. If the patternword is 1111111111110000, you'd still test the last bit of your input for a suffix match. If that bit is 1, you have to do 12 suffix matches (regex: 1{1,12}) but if it's 0 you have only 4 possible matches (regex 1111 1111 1111 0{1,4}), again for an average of 9 tests. Add the
16-N
prefix match, and you see that you only need 10 checks per 16 bit chunk.对于通用的非 SIMD 算法,您不可能比这样做得更好:
For a general-purpose, non-SIMD algorithm you are unlikely to be able to do much better than something like this:
您可以对极大的输入(n 值)使用快速傅里叶变换,以在 O(n log n ) 时间内找到任何位模式。计算位掩码与输入的互相关。序列 x 和大小分别为 n 和 n' 的掩码 y 的互相关由与
掩码完全匹配的位模式的出现情况定义,其中 R(m) = Y,其中 Y 是位中一个的总和面具。
因此,如果您尝试匹配位模式
,
则必须使用掩码,
掩码中的 -1 保证这些位置必须为 0。
您可以使用 FFT 在 O(n log n ) 时间内实现互相关。
我认为 KMP 的运行时间为 O(n + k),所以它胜过这个。
You can use the fast fourier transform for extremely large input (value of n) to find any bit pattern in O(n log n ) time. Compute the cross-correlation of a bit mask with the input. Cross -correlation of a sequence x and a mask y with a size n and n' respectively is defined by
then occurences of your bit pattern that match the mask exactly where R(m) = Y where Y is the sum of one's in your bit mask.
So if you are trying to match for the bit pattern
in
then you must use the mask
the -1's in the mask guarantee that those places must be 0.
You can implement cross-correlation, using the FFT in O(n log n ) time.
I think KMP has O(n + k) runtime, so it beats this out.
也许您应该在向量(vec_str)中流式传输您的比特流,在另一个向量(vec_pattern)中流式传输您的模式,然后执行类似于下面的算法的操作
(希望该算法是正确的)
Maybe you should stream in your bit stream in a vector (vec_str), stream in your pattern in another vector (vec_pattern) and then do something like the algorithm below
(hope the algorithm is correct)
在大位串中查找匹配的一种快速方法是计算一个查找表,该表显示给定输入字节与模式匹配的位偏移量。然后将三个连续的偏移匹配组合在一起,您可以获得一个位向量,显示哪些偏移与整个模式匹配。例如,如果字节 x 匹配模式的前 3 位,字节 x+1 匹配位 3..11,字节 x+2 匹配位 11..16,则字节 x + 5 位匹配。
下面是执行此操作的一些示例代码,一次累积两个字节的结果:
该主循环有 18 条指令长,每次迭代处理 2 个字节。如果设置成本不是问题,这应该是尽可能快的。
A fast way to find the matches in big bitstrings would be to calculate a lookup table that shows the bit offsets where a given input byte matches the pattern. Then combining three consecutive offset matches together you can get a bit vector that shows which offsets match the whole pattern. For example if byte x matches first 3 bits of the pattern, byte x+1 matches bits 3..11 and byte x+2 matches bits 11..16, then there is a match at byte x + 5 bits.
Here's some example code that does this, accumulating the results for two bytes at a time:
The main loop of this is 18 instructions long and processes 2 bytes per iteration. If the setup cost isn't an issue, this should be about as fast as it gets.