Move the sign extension function to a common place
This commit is contained in:
parent
1930b9eb59
commit
86fd58c8c6
20
src/array.c
20
src/array.c
@ -384,3 +384,23 @@ dec2bcd (unsigned char value)
|
|||||||
unsigned char lo = value % 10;
|
unsigned char lo = value % 10;
|
||||||
return (hi << 4) | lo;
|
return (hi << 4) | lo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* When turning a two's-complement number with a certain number
|
||||||
|
* of bits into one with more bits, the sign bit must be repeated
|
||||||
|
* in all the extra bits.
|
||||||
|
*/
|
||||||
|
unsigned int
|
||||||
|
signextend (unsigned int value, unsigned int nbits)
|
||||||
|
{
|
||||||
|
if (nbits <= 0 || nbits > 32)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
unsigned int signbit = 1U << (nbits - 1);
|
||||||
|
unsigned int mask = signbit - 1;
|
||||||
|
|
||||||
|
if ((value & signbit) == signbit)
|
||||||
|
return value | ~mask;
|
||||||
|
else
|
||||||
|
return value & mask;
|
||||||
|
}
|
||||||
|
|||||||
@ -123,6 +123,9 @@ bcd2dec (unsigned char value);
|
|||||||
unsigned char
|
unsigned char
|
||||||
dec2bcd (unsigned char value);
|
dec2bcd (unsigned char value);
|
||||||
|
|
||||||
|
unsigned int
|
||||||
|
signextend (unsigned int value, unsigned int nbits);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|||||||
@ -893,25 +893,6 @@ uwatec_galileo_identify (unsigned char value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static unsigned int
|
|
||||||
uwatec_smart_fixsignbit (unsigned int x, unsigned int n)
|
|
||||||
{
|
|
||||||
if (n <= 0 || n > 32)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
unsigned int signbit = (1 << (n - 1));
|
|
||||||
unsigned int mask = (signbit - 1);
|
|
||||||
|
|
||||||
// When turning a two's-complement number with a certain number
|
|
||||||
// of bits into one with more bits, the sign bit must be repeated
|
|
||||||
// in all the extra bits.
|
|
||||||
if ((x & signbit) == signbit)
|
|
||||||
return x | ~mask;
|
|
||||||
else
|
|
||||||
return x & mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static dc_status_t
|
static dc_status_t
|
||||||
uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback, void *userdata)
|
uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback, void *userdata)
|
||||||
{
|
{
|
||||||
@ -1018,7 +999,7 @@ uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fix the sign bit.
|
// Fix the sign bit.
|
||||||
signed int svalue = uwatec_smart_fixsignbit (value, nbits);
|
signed int svalue = signextend (value, nbits);
|
||||||
|
|
||||||
// Parse the value.
|
// Parse the value.
|
||||||
unsigned int idx = 0;
|
unsigned int idx = 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user