[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v2] bitops.h: Add field32() and field64() functi
From: |
Avi Kivity |
Subject: |
Re: [Qemu-devel] [PATCH v2] bitops.h: Add field32() and field64() functions to extract bitfields |
Date: |
Wed, 27 Jun 2012 16:15:02 +0300 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120605 Thunderbird/13.0 |
On 06/27/2012 01:29 PM, Peter Maydell wrote:
> Add field32() and field64() functions which extract a particular
> bit field from a word and return it. Based on an idea by Jia Liu.
>
>
> +/**
> + * field64 - return a specified bit field from a uint64_t value
> + * @value: The value to extract the bit field from
> + * @start: The lowest bit in the bit field (numbered from 0)
> + * @length: The length of the bit field
> + *
> + * Returns the value of the bit field extracted from the input value.
> + */
> +static inline uint64_t field64(uint64_t value, int start, int length)
> +{
> + assert(start >= 0 && start <= 63 && length > 0 && start + length <= 64);
> + return (value >> start) & (~0ULL >> (64 - length));
> +}
> +
Undefined for length == 64, so better add that to the assert.
I suggest adding the analogous functions for writing. I believe the
common naming is extract/deposit.
static inline uint64_t deposit64(uint64_t *pvalue, unsigned start,
unsigned length, uint64_t fieldval)
{
uint64_t mask = (((uint64_t)1 << length) - 1) << start;
*pvalue = (*pvalue & ~mask) | ((fieldval << start) & mask);
}
Useful for setting a bit to a specific value.
--
error compiling committee.c: too many arguments to function