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: 9686 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 5875C23E4E for ; Thu, 28 Jun 2012 14:56:42 +0000 (UTC) Received: from mail-yx0-f180.google.com (mail-yx0-f180.google.com [209.85.213.180]) by fiordland.canonical.com (Postfix) with ESMTP id EE5CDA18938 for ; Thu, 28 Jun 2012 14:56:41 +0000 (UTC) Received: by yenq6 with SMTP id q6so2087192yen.11 for ; Thu, 28 Jun 2012 07:56:41 -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:in-reply-to:references :x-gm-message-state; bh=M2S/WrkdzhAgWYktjQXLoRzwFJXUX0xGxMf85kqF6N0=; b=fyNw7sfLjdyM3e6YAEgj8iKi+TEJ0MucpgVhcVUk+F3F+EbC24o1YSYO9dlEjJquO+ 2jxCVO49/+062kkdU0Y6iwzGZroN76uu0wLe12fCKQVadNG/MFKKy7s6abDVPui0xWXt 2JJ8wSJK25PnyRYHjCCF+P27Bkl0+f7xNQN/a25PUzwQ7cwlNTps8/L1L/ORzcN6gLQP YApujpAmWHZ7nBp45RX7lCuTED7u0N0ZUGJNSA0yAhRIBXOD+jNhi7JrtyO9rL7fYL1M JxIbPMfwnUoZxYlzGYJ8QkJHmDETadXJQtDtqiR8rMJxPXSCLTPo5PyfBBEIvXJ9kQ6O bDQw== Received: by 10.50.163.99 with SMTP id yh3mr147485igb.53.1340895400978; Thu, 28 Jun 2012 07:56:40 -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 v20csp44000ibb; Thu, 28 Jun 2012 07:56:40 -0700 (PDT) Received: by 10.180.103.4 with SMTP id fs4mr696308wib.16.1340895399873; Thu, 28 Jun 2012 07:56:39 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [81.2.115.146]) by mx.google.com with ESMTPS id 34si27504697wej.56.2012.06.28.07.56.39 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 28 Jun 2012 07:56:39 -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 1SkFpG-0008MM-Br; Thu, 28 Jun 2012 15:36:06 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org Subject: [PATCH 03/13] bitops.h: Add functions to extract and deposit bitfields 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-Gm-Message-State: ALoCoQnko2mU/uFMNXJ1aSTXU/NAXvXAXI4iVaGLDinsBg85YPLluloC/E2Ds77d1X0Y+orWEGvi 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