开发者

How to map 10 bits to 6 bits in C++ (as efficiently as possible)?

开发者 https://www.devze.com 2023-04-08 23:41 出处:网络
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++...

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?


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.

  1. Time-efficient: Look-up table.
  2. Space-efficient: Use a Karnaugh map.


Use a look-up table of size 1024.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消