位调整重新排序
我需要对 7 位值进行任意重新排序(是的,我知道我应该使用表格),并且想知道是否有任何位黑客可以做到这一点。
示例:
// <b0, b1, b2, b3, b4, b5, b6> -> <b3, b2, b4, b1, b5, b0, b6>
// the naive way
out =
(0x020 & In) << 5 |
(0x008 & In) << 2 |
(0x040 & In) |
(0x012 & In) >> 1 |
(0x004 & In) >> 2 |
(0x001 & In) >> 3;
// 6 ANDs, 5 ORs, 5 shifts = 16 ops
编辑: 我正在考虑类似 this
只是为了好玩,因为我是 AFTK,所以我正在尝试强力搜索以下形式的解决方案:
((In * C1) >> C2) & 0x7f
未找到解决方案。
I need to do an arbitrary reorder of a 7 bit value (Yes I know I should be using a table) and am wondering if there are any bit hacks to do this.
Example:
// <b0, b1, b2, b3, b4, b5, b6> -> <b3, b2, b4, b1, b5, b0, b6>
// the naive way
out =
(0x020 & In) << 5 |
(0x008 & In) << 2 |
(0x040 & In) |
(0x012 & In) >> 1 |
(0x004 & In) >> 2 |
(0x001 & In) >> 3;
// 6 ANDs, 5 ORs, 5 shifts = 16 ops
edit:
I was thinking of something along the lines of this
Just for kicks and because I was AFTK I'm trying a brute force search for solutions of the form:
((In * C1) >> C2) & 0x7f
No solutions found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一步似乎是理解数学解决方案并对其进行优化。
请参阅此处的位黑客
The first step seems to be to understand a mathematical solution and optimize that.
see here of bit hacks
看看你的“天真的”代码的编译器输出,它可能会让你感到惊讶。 我曾经做过类似的事情,编译器(VC++2005)完全改变了我所有的 and 和移位的值,使它们更有效,例如我确信它会删除你的“(0x001 & In)> ;>3”。
但是,是的,如果重新洗牌是固定功能,那么表格可能是最好的。
更新
为了一笑,我查看了 VC++ 2005 的编译器输出......
首先,我尝试为“In”使用常量值,但编译器并没有被愚弄,它生成了以下代码:
即。 它完全优化了它。
所以......我尝试了一个正确的输入并得到了这个:
这是四个移位操作,五个AND,四个OR - 对于六个输入来说还不错。 可能比大多数人手工做的要好。
它可能还针对乱序执行进行了优化,因此时钟周期比看起来要少。 :-)
Have a look at the compiler output of your "naive" code, it might surprise you. I once did something like that and the compiler (VC++2005) completely changed the values of all the ands and shifts for me to make them more efficient, eg I'm sure it would remove your "(0x001 & In) >> 3".
But yes, if the reshuffle is a fixed function then a table is probably best.
Update
For a laugh I looked at the compiler output from VC++ 2005....
First I tried a constant value for "In" but the compiler wasn't fooled one bit, it produced this code:
ie. it completely optimized it away.
So ... I tried a proper input and got this:
That's four shift operations, five ANDs, four ORs - not bad for six inputs. Probably better than most people could do by hand.
It's probably also optimized for out-of-order execution so it'll be less clock cycles than it seems. :-)
对于常见操作,有很多位旋转技巧,即反转 32 位字中的位顺序(移位、AND 和 OR 各 10 个,AFAICR)。
在这种情况下,从输入到输出的映射显然是完全任意的,我看不到任何清理它的方法。
使用查找表:)
There are plenty of bit-twiddling hacks for common operations, i.e. to reverse the order of the bits in a 32-bit word (10 each of shift, AND and OR, AFAICR).
In this case, with an apparently completely arbitrary mapping from input to output, I can't see any way of cleaning this up.
Use a lookup table :)
在优化之前,您应该确保您的“天真的”方式正在按照您的意图进行。 如果我将您的代码放入一个函数并运行此循环:
它会产生此输出,这与注释相矛盾。 事实上,它会丢失比特。
为了获得您描述的随机播放,我会像这样编码:
Before you optimize, you should make sure your 'naive' way is doing what you intend. If I make your code into a function and run this loop:
It produces this output, which contradicts the comments. In fact, it loses bits.
In order to get the shuffle you describe, I would code it like this: