unsigned int fieldWidth/* Width of the field in BITS, not bytes. */
)
{
- /* Convert the field to Gray code */
- field = (field >> 1) ^ field;
+ pack_natural_or_grey(bitArray, bitIndex, field, fieldWidth, 1);
+}
+
+void
+pack_natural_or_grey(
+ 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. */
+ 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 * bitIndex, /* Index into the string in BITS, not bytes.*/
unsigned int fieldWidth/* Width of the field in BITS, not bytes. */
)
+{
+ 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;
fieldWidth -= sliceWidth;
} while ( fieldWidth != 0 );
- /* Convert from Gray code to binary. Works for maximum 8-bit fields. */
- t = field ^ (field >> 8);
- t ^= (t >> 4);
- t ^= (t >> 2);
- t ^= (t >> 1);
+ if (gray) {
+ /* Convert from Gray code to binary. Works for maximum 8-bit fields. */
+ t = field ^ (field >> 8);
+ t ^= (t >> 4);
+ t ^= (t >> 2);
+ t ^= (t >> 1);
+ }
+ else {
+ t = field;
+ }
+
return t;
}
float decode_energy(int index);
void pack(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits);
+void pack_natural_or_grey(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits, int gray);
int unpack(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits);
+int unpack_natural_or_grey(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits, int gray);
int lsp_bits(int i);
int lspd_bits(int i);