From: Darrick J. Wong Date: Wed, 3 Jul 2024 21:22:08 +0000 (-0700) Subject: xfs_db: hoist bit scraping function X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=a89a554b447b964a0abc805cfc6e5f84a52d0cfe;p=users%2Fhch%2Fxfsprogs.git xfs_db: hoist bit scraping function Hoist the bit scraping code into a separate helper function so that we can create a _le version later, and also enable some testing functions. Signed-off-by: Darrick J. Wong --- diff --git a/db/bit.c b/db/bit.c index 1b9ca054f..26c3ed2ce 100644 --- a/db/bit.c +++ b/db/bit.c @@ -22,6 +22,48 @@ getbit_l( return (*ptr & mask) >> shift; } +/* + * Extract a big-endian integer value from some bits in the middle of a buffer + * and return it as an int64_t in host-endian format. + * + * @p points to the start of the buffer + * @bit is the bit offset beyond @p + * @nbits is the number of bits to extract + * @signext enables sign extension of the return value + */ +static int64_t +scrape_bits_be( + char *p, + int nbits, + int bit, + bool signext) +{ + int i; + int64_t rval = 0LL; + + for (i = 0; i < nbits; i++) { + if (getbit_l(p, bit + i)) { + /* If the last bit is on and we care about sign bits + * and we don't have a full 64 bit container, turn all + * bits on between the sign bit and the most sig bit. + */ + + /* handle endian swap here */ +#if __BYTE_ORDER == LITTLE_ENDIAN + if (i == 0 && signext && nbits < 64) + rval = (~0ULL) << nbits; + rval |= 1ULL << (nbits - i - 1); +#else + if ((i == (nbits - 1)) && signext && nbits < 64) + rval |= ((~0ULL) << nbits); + rval |= 1ULL << (nbits - i - 1); +#endif + } + } + + return rval; +} + void setbit_l( char *ptr, @@ -51,9 +93,7 @@ getbitval( int flags) { int bit; - int i; char *p; - int64_t rval; int signext; ASSERT(nbits<=64); @@ -83,27 +123,7 @@ getbitval( } scrape_bits: - for (i = 0, rval = 0LL; i < nbits; i++) { - if (getbit_l(p, bit + i)) { - /* If the last bit is on and we care about sign - * bits and we don't have a full 64 bit - * container, turn all bits on between the - * sign bit and the most sig bit. - */ - - /* handle endian swap here */ -#if __BYTE_ORDER == LITTLE_ENDIAN - if (i == 0 && signext && nbits < 64) - rval = (~0ULL) << nbits; - rval |= 1ULL << (nbits - i - 1); -#else - if ((i == (nbits - 1)) && signext && nbits < 64) - rval |= ((~0ULL) << nbits); - rval |= 1ULL << (nbits - i - 1); -#endif - } - } - return rval; + return scrape_bits_be(p, nbits, bit, signext); } /*