From patchwork Wed Jun 27 10:29:13 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 167600 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 491BBB6F9D for ; Wed, 27 Jun 2012 20:29:38 +1000 (EST) Received: from localhost ([::1]:46991 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SjpV9-0001AN-P9 for incoming@patchwork.ozlabs.org; Wed, 27 Jun 2012 06:29:35 -0400 Received: from eggs.gnu.org ([208.118.235.92]:51936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SjpUy-00019o-Ql for qemu-devel@nongnu.org; Wed, 27 Jun 2012 06:29:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SjpUs-0003uY-8U for qemu-devel@nongnu.org; Wed, 27 Jun 2012 06:29:24 -0400 Received: from mnementh.archaic.org.uk ([81.2.115.146]:44688) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SjpUs-0003tg-12 for qemu-devel@nongnu.org; Wed, 27 Jun 2012 06:29:18 -0400 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1SjpUn-0007l9-Br; Wed, 27 Jun 2012 11:29:13 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Wed, 27 Jun 2012 11:29:13 +0100 Message-Id: <1340792953-29804-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 81.2.115.146 Cc: Blue Swirl , Jia Liu , patches@linaro.org Subject: [Qemu-devel] [PATCH v2] bitops.h: Add field32() and field64() functions to extract 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 field32() and field64() functions which extract a particular bit field from a word and return it. Based on an idea by Jia Liu. Suggested-by: Jia Liu Signed-off-by: Peter Maydell Reviewed-by: Andreas Färber --- v1->v2: added missing brackets to field32() to bring it in to line with field64() (Still using 'int' rather than 'unsigned' for bit numbers as per rationale in previous discussion.) bitops.h | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-) diff --git a/bitops.h b/bitops.h index 07d1a06..ffbb387 100644 --- a/bitops.h +++ b/bitops.h @@ -269,4 +269,32 @@ static inline unsigned long hweight_long(unsigned long w) return count; } +/** + * 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)); +} + +/** + * field32 - 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 field32(uint32_t value, int start, int length) +{ + assert(start >= 0 && start <= 31 && length > 0 && start + length <= 32); + return (value >> start) & (~0U >> (32 - length)); +} + #endif