From patchwork Thu Jun 28 12:55:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 9673 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 39F3E23E23 for ; Thu, 28 Jun 2012 12:56:04 +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 DEDDFA18119 for ; Thu, 28 Jun 2012 12:56:03 +0000 (UTC) Received: by ghbz12 with SMTP id z12so1957589ghb.11 for ; Thu, 28 Jun 2012 05:56:03 -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=/iWgiRL501tb3X5M5ohxcRuqo8X8+6r8i1x6M7OKvK0=; b=H/0Ak8wvpukV/deNhhme/PgdGrKLCWI4Gkm27m7KhmRhX86aKdry/tf2qE2a/UvmPf ppccqg5lkDRyJ7aSTDpTwoNoxTRGnRIdqBaRRr4tMaGX0VkJ+EKVHb24k79DLBejGykr H/+YiLqO+zkv+jo1V3kSI7Q9C8BqEkXf3/pSxbEjEmvB3827vXj8HrndtTn+yX5ET6gz BISZ52sY8j7VyTKPWypxNuwzdvIvw3WUvF52CRZNVByDQO8UCWXL9CZ2A7zDwIHNQOL9 tkh/f1Q5TpI0eTBAAuvrCRqfGiAnE1ecoQrNnk2dwNQHp7sqxhy+wXr4yKU15w8k3SGG lLdA== Received: by 10.50.40.193 with SMTP id z1mr621090igk.0.1340888163083; Thu, 28 Jun 2012 05:56:03 -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 v20csp41105ibb; Thu, 28 Jun 2012 05:56:01 -0700 (PDT) Received: by 10.180.107.70 with SMTP id ha6mr4525706wib.10.1340888160315; Thu, 28 Jun 2012 05:56:00 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [81.2.115.146]) by mx.google.com with ESMTPS id u15si3089463wec.15.2012.06.28.05.55.59 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 28 Jun 2012 05:56:00 -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 1SkEGJ-0008J0-0D; Thu, 28 Jun 2012 13:55:55 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Jia Liu , Blue Swirl , malc , Eric Blake , Avi Kivity , Jay Foad Subject: [PATCH v3] bitops.h: Add functions to extract and deposit bitfields Date: Thu, 28 Jun 2012 13:55:54 +0100 Message-Id: <1340888154-31903-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 X-Gm-Message-State: ALoCoQlHsIQnB5LPfmDBBnef4+NqutZagOV/mQxsbfGrIoGmJYKQ4LLEdmqiqSw+0G10KM/q09nA 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 Reviewed-by: Andreas Färber --- Changes: v1->v2: added missing brackets v2->v3: renamed field32,field64 to extract32,extract64 added deposit32,deposit64 at Avi's suggestion fixed assertion as per Jay Foad's suggestion bikeshed roof is now a slightly darker shade of grey NB: the deposit functions differ slightly from Avi's proposal: * instead of taking a pointer which we modify, we return the updated value. (This matches the semantics of the TCG deposit op and also seems to me generally nicer) * the implementation permits deposits of the entire word (ie length 32/64) 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