From patchwork Mon Jun 25 19:38:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 9614 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 6669623F19 for ; Mon, 25 Jun 2012 19:39:02 +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 2AD22A18D73 for ; Mon, 25 Jun 2012 19:39:02 +0000 (UTC) Received: by yenq6 with SMTP id q6so3459878yen.11 for ; Mon, 25 Jun 2012 12:39:01 -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=NXzFhPBVxF5KrkdhwfZbjrVh8c1Rwlin1aK3fY5Bydc=; b=ipQQyjxokW1uQRcOslPJjmR3A07+FwEO7VhBNgj57y1+wgFGsW0JV7TQC2M3rqykMl dnEvHWhhJ6kreuD2mHr2pY3FlYlfPUJGHUu+Mi2j9XgtyxKLOOIw0aXaZx6PDrvlwPSu DmviqEpNJRjSQIaIvNjxTGS96uO6tgbyzGxZsSU3hSPlAES8HPlQURRFFhFv8mNM6Kyq q9I0H1nC4zWLoKS9irzD+3dO3BGw00weTGfmJAVjUHPsoD4YUBR1rU97tXRbsHaff+pj Y4rEVeh63xJULsPX2DNDZu4Lx/w/vNL0fTsHZ47DFG4cvQJGWH3oIEZ2BsIdfTEE4o+o QS7w== Received: by 10.42.89.72 with SMTP id f8mr6591459icm.33.1340653141411; Mon, 25 Jun 2012 12:39:01 -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 v20csp58037ibb; Mon, 25 Jun 2012 12:39:00 -0700 (PDT) Received: by 10.180.105.234 with SMTP id gp10mr26816375wib.11.1340653140136; Mon, 25 Jun 2012 12:39:00 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [81.2.115.146]) by mx.google.com with ESMTPS id z55si17251775wem.104.2012.06.25.12.38.58 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 25 Jun 2012 12:39: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 1SjF7f-0006xs-Ja; Mon, 25 Jun 2012 20:38:55 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Jia Liu , Blue Swirl Subject: [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields Date: Mon, 25 Jun 2012 20:38:55 +0100 Message-Id: <1340653135-26749-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 X-Gm-Message-State: ALoCoQn3vlJl1hD7GtNI1/BfB1fj0K+bOg7QKaqMdnuPvZL7yZR0+n48KMf1N5ZtpW/NQ3V5GHzy 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 --- Jia Liu had a function like this in the OpenRISC support patchset; this implementation borrows the API but has a different implementation, because I wanted to handle the length == wordsize case. I've also provided a 64 bit version as well as a 32 bit one (alas, gcc is not smart enough to notice that it only needs to do 32 bit arithmetic if you pass in a uint32_t to the 64 bit function). Based on previous experience with a different codebase I think that this will result in much more comprehensible code than manual shift-and-mask which is what we tend to do today. No users yet, but I wanted to throw this out for review anyway. If people really don't want it until it gets a first user I can throw it into my next random patchset that does bit ops... bitops.h | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-) diff --git a/bitops.h b/bitops.h index 07d1a06..36e4c78 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