diff options
| author | drowe67 <[email protected]> | 2023-07-20 08:59:48 +0930 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-20 08:59:48 +0930 |
| commit | 06d4c11e699b0351765f10398abb4f663a984f36 (patch) | |
| tree | 33e22af0814c5b6c3d676f096ae8c2ac8a3ed9f0 /src/pack.c | |
| parent | 6588e77f38bdebd7adffe091b22e7760d95d0ccb (diff) | |
| parent | 4d6c143c0abec15e1d6ed1fd95d36f80e6cb7df8 (diff) | |
Merge pull request #3 from drowe67/dr-cleanup21.2.0
Cleanup Part 2
Diffstat (limited to 'src/pack.c')
| -rw-r--r-- | src/pack.c | 104 |
1 files changed, 47 insertions, 57 deletions
@@ -15,19 +15,20 @@ along with this program; if not, see <http://www.gnu.org/licenses/>. */ +#include <stdio.h> + #include "defines.h" #include "quantise.h" -#include <stdio.h> /* Compile-time constants */ /* Size of unsigned char in bits. Assumes 8 bits-per-char. */ -static const unsigned int WordSize = 8; +static const unsigned int WordSize = 8; /* Mask to pick the bit component out of bitIndex. */ -static const unsigned int IndexMask = 0x7; +static const unsigned int IndexMask = 0x7; /* Used to pick the word component out of bitIndex. */ -static const unsigned int ShiftRight = 3; +static const unsigned int ShiftRight = 3; /** Pack a bit field into a bit string, encoding the field in Gray code. * @@ -44,86 +45,76 @@ static const unsigned int ShiftRight = 3; * compatibility with the rest of the code, indices are always expected to * be >= 0. */ -void -pack( - unsigned char * bitArray, /* The output bit string. */ - unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ - int field, /* The bit field to be packed. */ - unsigned int fieldWidth/* Width of the field in BITS, not bytes. */ - ) -{ - pack_natural_or_gray(bitArray, bitIndex, field, fieldWidth, 1); +void pack(unsigned char* bitArray, /* The output bit string. */ + unsigned int* bitIndex, /* Index into the string in BITS, not bytes.*/ + int field, /* The bit field to be packed. */ + unsigned int fieldWidth /* Width of the field in BITS, not bytes. */ +) { + pack_natural_or_gray(bitArray, bitIndex, field, fieldWidth, 1); } -void -pack_natural_or_gray( - unsigned char * bitArray, /* The output bit string. */ - unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ - int field, /* The bit field to be packed. */ - unsigned int fieldWidth,/* Width of the field in BITS, not bytes. */ - unsigned int gray /* non-zero for gray coding */ - ) -{ +void pack_natural_or_gray( + unsigned char* bitArray, /* The output bit string. */ + unsigned int* bitIndex, /* Index into the string in BITS, not bytes.*/ + int field, /* The bit field to be packed. */ + unsigned int fieldWidth, /* Width of the field in BITS, not bytes. */ + unsigned int gray /* non-zero for gray coding */ +) { if (gray) { /* Convert the field to Gray code */ field = (field >> 1) ^ field; } do { - unsigned int bI = *bitIndex; - unsigned int bitsLeft = WordSize - (bI & IndexMask); - unsigned int sliceWidth = - bitsLeft < fieldWidth ? bitsLeft : fieldWidth; - unsigned int wordIndex = bI >> ShiftRight; + unsigned int bI = *bitIndex; + unsigned int bitsLeft = WordSize - (bI & IndexMask); + unsigned int sliceWidth = bitsLeft < fieldWidth ? bitsLeft : fieldWidth; + unsigned int wordIndex = bI >> ShiftRight; - bitArray[wordIndex] |= - ((unsigned char)((field >> (fieldWidth - sliceWidth)) - << (bitsLeft - sliceWidth))); + bitArray[wordIndex] |= ((unsigned char)((field >> (fieldWidth - sliceWidth)) + << (bitsLeft - sliceWidth))); *bitIndex = bI + sliceWidth; fieldWidth -= sliceWidth; - } while ( fieldWidth != 0 ); + } while (fieldWidth != 0); } /** Unpack a field from a bit string, converting from Gray code to binary. * */ -int -unpack( - const unsigned char * bitArray, /* The input bit string. */ - unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ - unsigned int fieldWidth/* Width of the field in BITS, not bytes. */ - ) -{ - return unpack_natural_or_gray(bitArray, bitIndex, fieldWidth, 1); +int unpack( + const unsigned char* bitArray, /* The input bit string. */ + unsigned int* bitIndex, /* Index into the string in BITS, not bytes.*/ + unsigned int fieldWidth /* Width of the field in BITS, not bytes. */ +) { + return unpack_natural_or_gray(bitArray, bitIndex, fieldWidth, 1); } /** Unpack a field from a bit string, to binary, optionally using * natural or Gray code. * */ -int -unpack_natural_or_gray( - const unsigned char * bitArray, /* The input bit string. */ - unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ - unsigned int fieldWidth,/* Width of the field in BITS, not bytes. */ - unsigned int gray /* non-zero for Gray coding */ - ) -{ - unsigned int field = 0; - unsigned int t; +int unpack_natural_or_gray( + const unsigned char* bitArray, /* The input bit string. */ + unsigned int* bitIndex, /* Index into the string in BITS, not bytes.*/ + unsigned int fieldWidth, /* Width of the field in BITS, not bytes. */ + unsigned int gray /* non-zero for Gray coding */ +) { + unsigned int field = 0; + unsigned int t; do { - unsigned int bI = *bitIndex; - unsigned int bitsLeft = WordSize - (bI & IndexMask); - unsigned int sliceWidth = - bitsLeft < fieldWidth ? bitsLeft : fieldWidth; + unsigned int bI = *bitIndex; + unsigned int bitsLeft = WordSize - (bI & IndexMask); + unsigned int sliceWidth = bitsLeft < fieldWidth ? bitsLeft : fieldWidth; - field |= (((bitArray[bI >> ShiftRight] >> (bitsLeft - sliceWidth)) & ((1 << sliceWidth) - 1)) << (fieldWidth - sliceWidth)); + field |= (((bitArray[bI >> ShiftRight] >> (bitsLeft - sliceWidth)) & + ((1 << sliceWidth) - 1)) + << (fieldWidth - sliceWidth)); *bitIndex = bI + sliceWidth; fieldWidth -= sliceWidth; - } while ( fieldWidth != 0 ); + } while (fieldWidth != 0); if (gray) { /* Convert from Gray code to binary. Works for maximum 8-bit fields. */ @@ -131,8 +122,7 @@ unpack_natural_or_gray( t ^= (t >> 4); t ^= (t >> 2); t ^= (t >> 1); - } - else { + } else { t = field; } |
