增加位大小的有效条件
假设我有一个递增的无符号整数序列C[i]
。随着它们的增加,它们可能会占用越来越多的位。我正在寻找一个有效的条件,纯粹基于序列 C[i]
和 C[i+1]
的两个连续元素(过去和未来的不是observable),每次所需位数增加时,都会精确或近似地评估为真。
一个明显(但缓慢)的条件选择是:
if (ceil(log(C[i+1])) > ceil(log(C[i]))) ...
同样,任何使用特殊 cpu 操作码计算前导零位数的东西(好得多,但仍然不是很好)。
我怀疑可能有一个很好的解决方案,涉及仅使用按位或和按位以及值 C[i+1]
和 C[i]
的表达式。有什么想法吗?
Suppose I have an increasing sequence of unsigned integers C[i]
. As they increase, it's likely that they will occupy increasingly many bits. I'm looking for an efficient conditional, based purely on two consecutive elements of the sequence C[i]
and C[i+1]
(past and future ones are not observable), that will evaluate to true either exactly or approximately once for every time the number of bits required increases.
An obvious (but slow) choice of conditional is:
if (ceil(log(C[i+1])) > ceil(log(C[i]))) ...
and likewise anything that computes the number of leading zero bits using special cpu opcodes (much better but still not great).
I suspect there may be a nice solution involving an expression using just bitwise or and bitwise and on the values C[i+1]
and C[i]
. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
假设你的两个数字是 x 和 y。如果它们具有相同的高位,则 x^y 小于 x 和 y。否则,它比两者之一更高。
所以
Suppose your two numbers are x and y. If they have the same high order bit, then x^y is less than both x and y. Otherwise, it is higher than one of the two.
So
我认为你只需要 clz(C[i+1])
clz(C[i+1])
clz(C[i+1])
clz(C[i+1])
是一个返回前导零数量的函数(“计数前导零”)。某些 CPU 系列具有用于此目的的指令(可以作为内在指令使用)。如果没有,那么您必须自己动手(通常只需要一些说明) - 请参阅 黑客的乐趣。clz(C[i])
其中 clzI think you just need
clz(C[i+1]) < clz(C[i])
whereclz
is a function which returns the number of leading zeroes ("count leading zeroes"). Some CPU families have an instruction for this (which may be available as an instrinsic). If not then you have to roll your own (it typically only takes a few instructions) - see Hacker's Delight.鉴于(我相信这来自 Hacker's Delight):
您的条件只是
hibit(C[i] ) != hibit(C[i+1])
。Given (I believe this comes from Hacker's Delight):
Your conditional is simply
hibit(C[i]) != hibit(C[i+1])
.BSR - 位扫描反向 (386+)
您需要其中两个(在 C[i] 和 C[i]+1 上)并进行比较。
BSR - Bit Scan Reverse (386+)
You need two of these (on C[i] and C[i]+1) and a compare.
Keith Randall 的解决方案很好,但是您可以通过使用以下代码来节省一条异或指令,该代码在 O(w + n) 条指令中处理整个序列,其中 w 是字中的位数,n 是元素的数量在序列中。如果序列很长,则大多数迭代将仅涉及一次比较,从而避免一条异或指令。
这是通过跟踪已达到的两个的最高功率来完成的,如下所示:
Keith Randall's solution is good, but you can save one xor instruction by using the following code which processes the entire sequence in O(w + n) instructions, where w is the number of bits in a word, and n is the number of elements in the sequence. If the sequence is long, most iterations will only involve one comparison, avoiding one xor instruction.
This is accomplished by tracking the highest power of two that has been reached as follows:
当值大约溢出 2 的幂时,位数会增加。那么一个简单的测试是,该值是否等于 2 的负 1 次方?这可以通过询问来完成:
The number of bits goes up when the value is about overflow a power of two. A simple test is then, is the value equal to a power of two, minus 1? This can be accomplished by asking:
当值即将溢出 2 的幂时,位数会增加。
一个简单的测试是:
如果你想要更快:
The number of bits goes up when the value is about to overflow a power of two.
A simple test is then:
If you want it even faster: