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: 9648 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id C5CD123E37 for ; Wed, 27 Jun 2012 10:29:21 +0000 (UTC) Received: from mail-gh0-f180.google.com (mail-gh0-f180.google.com [209.85.160.180]) by fiordland.canonical.com (Postfix) with ESMTP id 8C2C6A18208 for ; Wed, 27 Jun 2012 10:29:21 +0000 (UTC) Received: by ghbz12 with SMTP id z12so824024ghb.11 for ; Wed, 27 Jun 2012 03:29:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:from:to:cc :subject:date:message-id:x-mailer:x-gm-message-state; bh=603YlNJff4tSSNkITuvdleIcqQy2fjmSxZf9/08GED8=; b=CJwJfGAidhWuJrKlYFqymK6vxdZ1fWu4dnDskr3WHHsDcwdPnitzQtMH4vos9pXRz3 P+diM2OZu22bJb7oD+CzrFWQf+izQZ3dRsz0RzWHH+exwa2bpVP1C7X4CwxPrmymQqPU zt0A98/Y+s0qrDV2gfp6/BsZqE5ZDiZ4oOb2OtiYFRB3XhJNPNoO28C5UYH/14DQvzZp 2yTjpw7lyBnvykH1MbBH0jGKDbXXUgdDO0fcSsEkskPGp3njIltNP69T3dMIlHnWwI9Y /e9pQCcA6XYCcC7F2w+Nx8O4jwSYR7aYQTyQDNNYka02t5BCiqOpFjO8WN14d1VwijaS uq1w== Received: by 10.50.57.167 with SMTP id j7mr934354igq.53.1340792960587; Wed, 27 Jun 2012 03:29:20 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.24.148 with SMTP id v20csp8152ibb; Wed, 27 Jun 2012 03:29:19 -0700 (PDT) Received: by 10.216.198.10 with SMTP id u10mr346246wen.80.1340792958228; Wed, 27 Jun 2012 03:29:18 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [81.2.115.146]) by mx.google.com with ESMTPS id he8si10144010wib.25.2012.06.27.03.29.16 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 27 Jun 2012 03:29:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 81.2.115.146 as permitted sender) client-ip=81.2.115.146; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 81.2.115.146 as permitted sender) smtp.mail=pm215@archaic.org.uk 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 Cc: patches@linaro.org, Jia Liu , Blue Swirl , malc Subject: [PATCH v2] bitops.h: Add field32() and field64() functions to extract bitfields 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-Gm-Message-State: ALoCoQkA/km6p+gJ8BdAYNVb3Mz/9QcBKMnga415Dc7aTIECZKjnrxdl2LhEVIGEh948whjFkXGU 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