Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pedantic: Extend byte_length()/bit_length() to support systems where CHAR_BIT is not 8 #119

Open
saxbophone opened this issue Jun 6, 2022 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@saxbophone
Copy link
Owner

Current code assumes 8-bit (a reasonable assumption):

std::size_t leading_occupancy = PRIVATE::fit(_digits.front(), 256);
bytes_for_digits -= (sizeof(StorageType) - leading_occupancy);
return bytes_for_digits;
}
/**
* @returns size by number of bits needed to store the number's value
* @note this can be less than \f$ bytes \times 8 \f$
*/
constexpr std::size_t bit_length() const {
// this is how many bits are needed to store the digits
std::size_t bits_for_digits = _digits.size() * sizeof(StorageType) * 8;
// reduce size if leading digit is not full occupancy
std::size_t leading_occupancy = PRIVATE::fit(_digits.front(), 2);
bits_for_digits -= (sizeof(StorageType) * 8 - leading_occupancy);

But we can make this portable without compromising the behaviour on the vast majority of systems where CHAR_BIT=8 by using CHAR_BIT in place of 8 and PRIVATE::exp(2, CHAR_BIT) in place of 256

This should support any systems where CHAR_BIT > 8. The rest of the code should be checked over for any other places where byte = 8-bit is assumed.

@saxbophone saxbophone added the enhancement New feature or request label Jun 6, 2022
@saxbophone saxbophone added the good first issue Good for newcomers label Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant