关于bsr和lzcnt的困惑

关于bsr和lzcnt的困惑

本文介绍了关于bsr和lzcnt的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这两个指令有些困惑.首先,当扫描值为0且结果不确定/bsr或位大小/lzcnt时,让我们放弃特殊情况-这种区别很明显,不是我的问题的一部分.

I'm a bit confused about both instructions. First let's discard the special case when the scanned value is 0 and the undefined/bsr or bitsize/lzcnt result - this difference is clear and not part of my question.

让我们取二进制值0001 1111 1111 1111 1111 1111 1111 1111

根据Intel的规范,lzcnt的结果为3

According to Intel's spec the result for lzcnt is 3

根据Intel的规范,bsr的结果为28

According to Intel's spec the result for bsr is 28

lzcnt计数,bsr返回距位0(即lsb)的索引或距离.

lzcnt counts, bsr returns the index or distance from bit 0 (which is the lsb).

在CPU上没有可用的BMI的情况下,两个指令如何相同,如何将lzcnt模仿为bsr?还是bsr msb是位0?英特尔规范中的两个代码操作"也不同,一个从左数起或索引,另一个从右数起.

How can both instructions be the same and how can lzcnt be emulated as bsr in case there's no BMI on the CPU available? Or is bit 0 in case of bsr the msb? Both "code operations" in Intel's spec are different too, one counts or indexes from the left, the other from the right.

也许有人可以对此有所了解,如果没有BMI/lzcnt指令,我没有CPU来测试回退到bsr的结果是否具有相同的结果(因为值0的特殊情况永远不会发生)./p>

Maybe someone can shed some light on this, I have no CPU without BMI/lzcnt instruction to test if the fallback to bsr works with the same result (as the special case of value 0 to scan never happens).

推荐答案

LZCNT给出前导零位的数量. BSR给出最高1位的位索引.因此,对于结果为非零的情况,它们将有效地执行相同的操作,只是对结果的解释不同.因此,您只需从31中减去BSR结果即可获得与LZCNT相同的行为,即LZCNT == (31 - BSR).

LZCNT gives the number of leading zero bits. BSR gives the bit index of the most significant 1 bit. So they do effectively the same thing for the non-zero case, except the result is interpreted differently. Therefore you can just subtract the BSR result from 31 to get the same behaviour as with LZCNT, i.e. LZCNT == (31 - BSR).

这篇关于关于bsr和lzcnt的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 09:20