From patchwork Thu Jun 28 14:35:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 167897 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C20E1B6FFD for ; Fri, 29 Jun 2012 00:57:13 +1000 (EST) Received: from localhost ([::1]:59251 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SkG9f-0000BV-Jg for incoming@patchwork.ozlabs.org; Thu, 28 Jun 2012 10:57:11 -0400 Received: from eggs.gnu.org ([208.118.235.92]:43398) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SkG9G-0008Ui-Ln for qemu-devel@nongnu.org; Thu, 28 Jun 2012 10:56:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SkG99-0008BM-3d for qemu-devel@nongnu.org; Thu, 28 Jun 2012 10:56:46 -0400 Received: from mnementh.archaic.org.uk ([81.2.115.146]:35225) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SkG98-00089v-Sc for qemu-devel@nongnu.org; Thu, 28 Jun 2012 10:56:39 -0400 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1SkFpG-0008MM-Br; Thu, 28 Jun 2012 15:36:06 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Thu, 28 Jun 2012 15:35:56 +0100 Message-Id: <1340894166-32105-4-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1340894166-32105-1-git-send-email-peter.maydell@linaro.org> References: <1340894166-32105-1-git-send-email-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 81.2.115.146 Cc: patches@linaro.org Subject: [Qemu-devel] [PATCH 03/13] bitops.h: Add functions to extract and deposit bitfields X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add functions deposit32(), deposit64(), extract32() and extract64() to extract and deposit bitfields in 32 and 64 bit words. Based on ideas by Jia Liu and Avi Kivity. Suggested-by: Jia Liu Suggested-by: Avi Kivity Signed-off-by: Peter Maydell Reviewed-by: Eric Blake --- bitops.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 66 insertions(+), 0 deletions(-) diff --git a/bitops.h b/bitops.h index 07d1a06..5a44bb1 100644 --- a/bitops.h +++ b/bitops.h @@ -269,4 +269,70 @@ static inline unsigned long hweight_long(unsigned long w) return count; } +/** + * extract32 - return a specified bit field from a uint32_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 uint32_t extract32(uint32_t value, int start, int length) +{ + assert(start >= 0 && length > 0 && length <= 32 - start); + return (value >> start) & (~0U >> (32 - length)); +} + +/** + * extract64 - 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 extract64(uint64_t value, int start, int length) +{ + assert(start >= 0 && length > 0 && length <= 64 - start); + return (value >> start) & (~0ULL >> (64 - length)); +} + +/** + * deposit32 - Insert into a specified bit field in a uint32_t + * @value: Initial value to insert bit field into + * @start: The lowest bit in the bit field (numbered from 0) + * @length: The length of the bit field + * @fieldval: The value to insert into the bit field + * + * Returns the input value with the fieldval inserted + * into it at the specified location. + */ +static inline uint32_t deposit32(uint32_t value, int start, int length, + uint32_t fieldval) +{ + uint32_t mask; + assert(start >= 0 && length > 0 && length <= 32 - start); + mask = (~0U >> (32 - length)) << start; + return (value & ~mask) | ((fieldval << start) & mask); +} + +/** + * deposit64 - Insert into a specified bit field in a uint64_t + * @value: Initial value to insert bit field into + * @start: The lowest bit in the bit field (numbered from 0) + * @length: The length of the bit field + * @fieldval: The value to insert into the bit field + * + * Returns the input value with the fieldval inserted + * into it at the specified location. + */ +static inline uint64_t deposit64(uint64_t value, int start, int length, + uint64_t fieldval) +{ + uint64_t mask; + assert(start >= 0 && length > 0 && length <= 64 - start); + mask = (~0ULL >> (64 - length)) << start; + return (value & ~mask) | ((fieldval << start) & mask); +} + #endif