c++ - Array of pairs of 3 bit elements -
Due to memory restrictions, I have to store some combinations in an array with 6 bit / pair (3 bit / value)
). The problem comes when I want to add this array as normal, depending on the pair's index. The array looks like this
| bybyte 0 | | --bite 1 | -byte 2 | 00000011 | 11112222 | 22333333 ... and so on, the pattern repeats. ------ | ------- | -------- | ------ | Pair 0 Pairs 1 Pair 2 Pair 3 = & gt; 4 pairs / 3 bytes
You can see that sometimes 2 bytes are required to remove values (for divisible by index 1 and 2).
I created a function that returns the first value from an index, pair (3 bit) and the second gives one (3 bits too).
get zero (char * array, int index, int & value1, int and value 2) {int groupIndex = index & gt; & Gt; 2; // To get the index of 3 bytes of group (with 4 pairs) / / We use 16 bit starting with the byte before the group for the divisive index from 0 to 1 and 16 bits from the second part Byte when divided by 2 and 3 small difference values = * (small int *) (array + group index + ((index & amp; 0x02)> gt; & gt; 1) switch (index and 0x03) {// Index% 4 Case 0: {// Remove first value of 3 bits 1 = (value & amp; 0xE000)>> 13; // next 3 bit value 2 = (value and am P; 0x1C00) Remove & gt; 10; breakage;} case1: {value1 = (value & amp; 0x380)> 7> value2 = (value & amp; 0x70)> & gt; Gt; 4; breakage;} case 2: {value1 = (value & amp; 0xE00)> 9> value2 = (value & amp; 0x1c0)> 6; breakage;} case 3 : {Value1 = (value & amp; 0x38) & gt; 2; value2 = value & amp; 0x7; breakdown;}}
Now my question is: Is there a faster way to remove these values?
I did a test and all pairs of bytes / pair (1 byte / value) while using 2 (53 in total) Reaching 100 million bar It takes approximately 6 seconds. When using a compact array, it takes approximately 22 seconds :( (Maybe because it is necessary to calculate all those masks and bit changes).
I clearly tried to explain clearly ... Forgive me or not. int does not overflow.)
zero GetPair (char * array, int index, int, and value 1, int and value 2) ) {Unsigned shift = 10 - index 6% 8; unsigned minor data = (* (unsigned minor *) (array + index * 6/8)> gt; & gt; shift) & amp; 0x3f; value2 = data & Amp; 7; value1 = data & gt; & gt; 3;}
There may be a penalty to shorten a 16 bit limit, however, such issues may return When I was still such a thing Kept track of not. If so, it would support would be better to read the 32-bit value to a 16-bit range and adjust shifts and masks accordingly.
Comments
Post a Comment