How to handle edge cases when allocating bits for the bits output tag? #405
-
I apologize if the title was confusing, but I can't think right now of a better title. Anyway, I have a question about the number of "bits the implementation uses to store the indication (flag) if a number in the sieve is a prime number, or not" guidelines for the In PrimeC/solution_2/sieve_1of2.c, we have this snippet of code: // We need to store only odd integers, so only half the number of integers
sieve_state->a=calloc(maxints/2/sizeof(TYPE)+1,sizeof(TYPE)); If I understand this correctly, this line allocates the necessary memory for the array of However, I also noticed that there is one edge case here where it might allocate one more For example, if How would you consider this edge case in classifying the solution in terms of bits per prime candidate? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
As another "edge case", in the solution I am currently writing, I use the same technique as described above to allocate the bit array. This leads to more edge cases:
Note that I'm adding 2 to the number of bits to accommodate the fact that I ignore numbers 1 and 2 when setting bits. The first bit maps to the number 3. I can easily make this approach more accurate to the first one by subtracting 2 before starting the divisions (or subtracting 1 after dividing by 2), but I was just wondering if I could avoid a few extra operations by allocating a little more memory than absolutely necessary. |
Beta Was this translation helpful? Give feedback.
-
⌊(n+7) ÷ 8⌋ will give you the number of bytes needed to store n bits. If you want to allocate your array in larger increments, adapt the calculation accordingly. As for the definitions of the number of bits: assume the sieve size is chosen in a way that fits perfectly with your alignment constraints (i.e. is a multiple of 8 or 64 or whatever), and if you have a few bits of padding at the end that's absolutely fine. What matters is how you address the flags. |
Beta Was this translation helpful? Give feedback.
-
We had a rather extensive discussion with Dave about this, a while back. What it boils down to is that his main requirement for marking an implementation as a 1-bit implementation is that meaningful "bit twiddling" is used. Reserving individual bits for even numbers but never using them is fine, having a small over-allocation of the applicable "bit-hosting type" (integer or otherwise) is fine. Over-allocating by a factor of 8 is a bit excessive, so identifying that one is a great catch indeed. |
Beta Was this translation helpful? Give feedback.
We had a rather extensive discussion with Dave about this, a while back. What it boils down to is that his main requirement for marking an implementation as a 1-bit implementation is that meaningful "bit twiddling" is used. Reserving individual bits for even numbers but never using them is fine, having a small over-allocation of the applicable "bit-hosting type" (integer or otherwise) is fine.
Over-allocating by a factor of 8 is a bit excessive, so identifying that one is a great catch indeed.