I need to work with a large array of 26-bit variables in RAM. It is too expensive to use 32-bit ints. Access should be as fast as possible (especially read operation).
I came to the following scheme: each 26-bit value splits to three 8-bit values and one 2-bit value.
#define N 500000000
uint8 arr1[N], arr2[N], arr3[N];
uint8 arr4[N / 4];
int read_value(int index)
{
int a1 = arr1[index]; // bits 0..7
int a2 = arr2[index]; // bits 8..15
int a3 = arr3[index]; // bits 16..23
int a4 = (arr4[index / 4] >> (2 * (index % 4))) & 3; // bits 24..25
return a1 | (a2 << 8) | (a3 << 16) | (a4 << 24);
}
Is there some better technique to do this?
Or maybe there is a nice way to work with 27/28/29/30-bit integers?
When you say it’s “too expensive” to use 32-bit ints, do you mean space-wise?
Assuming that you do, I’m not really sure how to help you there. However, in terms of read speed, an array in C/C++ provides you constant-time access to the elements of the array(this is assuming that the memory is already in the CPU cache; if it isn’t, it’s going to take longer). Therefore, reading element 0 takes the same amount of time as reading element 10,000; the code that you have might make this slower, but I can’t say that for certain.
While it looks like this code should do what you want to do, it would probably make the most sense to simply do an array of ints, even though it would take up more space. If you absolutely have to this, you could try putting
inlinein your method declaration so the compiler can expand it whenever you use it.