如何在C++中将10位映射为6位(尽可能有效)?
所以我知道功能上我想要发生什么,我只是不知道让计算机做到这一点的最佳方法...在 C++ 中...
我想实现一个 C++ 函数,将 10 位序列映射到6 位序列。
不管这些位现在代表什么...有 2^10 = 1024 个可能的输入。有 2^6 = 64 个不同的输出。大概有很多图案。显然有很多图案。但这很复杂。这是一个已知的映射,只是一个复杂的映射。
输出只是 64 种可能性之一。可能大家都不习惯吧。他们可能不会。但假设他们这样做了。
现在,我正在考虑一个四重嵌套 switch 语句,它只处理 1024 个情况中的每一个,并处理内联业务,为指向我传递给该函数的任何结构的任何指针分配适当的值。这似乎很幼稚而且有点缓慢。并不是说我已经实现了它,但这就是为什么我想先问你。
这个基本函数(映射)必须在每个语句节点上运行,通常不止一次,因为系统希望支持尽可能多的语句。我问你,如何在 C++ 中尽可能有效地将 10 位映射到 6 位?
我知道映射是什么,我知道哪些 10 位输入与哪些 6 位输出对应……我可以完全硬编码……不知何故?多开关太丑了如何将 10 位映射到 6 位?!神经网络?记忆松饼?你会怎么办?
自我提醒:这就是为什么我不喜欢查找表的原因。让我们假设所有输入都是同样可能的(当然它们不是,并且可以更有效地排序,但仍然如此),那么平均需要数组的 512 内存进步来检索输出值......似乎如果你使一个深度为 10 层的(全局的,为什么不)二叉树,您可以覆盖 1024 个输入,并且平均只需 10 步即可检索输出...如果有好的模式,可能会更少...给定一个确定性函数跑得那么频繁,如何最好地恢复已知输入的已知输出?
So I know functionally what I would like to happen, I just don't know the best way to make a computer do it... in C++...
I would like to implement a C++ function that maps a 10 bit sequence to a 6 bit sequence.
Nevermind what the bits stand for right now... There are 2^10 = 1024 possible inputs. There are 2^6 = 64 different outputs. Probably lots of patterns. Obviously lots of patterns. But it's complicated. It's s a known mapping, just a complicated mapping.
The output is just one of 64 possibilities. Maybe they all don't get used. They probably won't. But assume they do.
Right now, I'm thinking a quadruple nested switch statement that just takes care of each of the 1024 cases and takes care of business inline, assigning appropriate values to whatever pointer to whatever structure I passed to this function. This seems to be naive and sort of slow. Not that I've implemented it, but that's why I want to ask you first.
This basic function (mapping) will have to be run at every statement node, often more than once, for as many statements this system wishes to support. I ask you, how do I map 10 bits to 6 bits as efficiently as possible in C++?
I know what the mapping is, I know which inputs of 10 bits go with what output of 6 bits... I could totally hard-code that ... somehow? Multi-switch is so ugly. How can I map my 10 bits to 6 bits?! Neural net? Memory muffin? What would you do?
Note to self: So here is why I am not a fan of the lookup table. Let's assume all inputs are equally likely (of course they are not, and could be ordered more effectively, but still) then it will take on average 512 memory advances of the array to retrieve the output values... It seems that if you make a (global, why not) binary tree 10 levels deep, you cover the 1024 inputs and can retrieve the output in an average of just 10 steps... and maybe less if there are good patterns... given a deterministic function that is run so often, how best to retrieve known outputs from known inputs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用 1024 个元素的查找表。因此,对其进行硬编码,然后通过索引访问它。
这节省了对大量 switch 语句的需要,并且可能会更具可读性。
I would use a lookup table of 1024 elements. So hard-code that and just access it by index.
This saves the need for a massive switch statement and will probably be much more readable.
取决于你对效率的定义。
Depends on your definition of efficiency.
使用大小为 1024 的查找表。
Use a look-up table of size 1024.
如果您需要映射到某些特定 6 位值,请在除以 16 后使用大小 64(不是 1024!)的查找表。这将适合缓存比 16 倍冗余 1024 项表更容易(并且,右移的 2 个额外周期远远超过了可能的缓存未命中的成本)。
否则,如果简单的顺序映射就可以,只需除以 16。1024
/64 = 16,因此除以 16(打开编译器优化的右移)会映射到 6 位(顺序)。没有比这更高效的了。
If you need to map to some particular 6-bit values, use a lookup table of size 64 (not 1024!) after dividing by 16. This will fit into the cache more easily than a 16-times redundant 1024-entry table (and, the 2 extra cycles for a right shift outweight the cost of a possible cache miss by far).
Otherwise, if a simple sequential mapping is fine, just do a divide by 16.
1024/64 = 16, so dividing by 16 (a right shift with compiler optimizations turned on), maps to 6 bits (sequentially). It cannot get more efficient than that.