From patchwork Thu Jun 27 15:47:42 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 255100 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 31C0A2C00A1 for ; Fri, 28 Jun 2013 01:48:17 +1000 (EST) Received: from localhost ([::1]:57151 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsEQg-0001N7-Au for incoming@patchwork.ozlabs.org; Thu, 27 Jun 2013 11:48:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsEQK-0001LP-1H for qemu-devel@nongnu.org; Thu, 27 Jun 2013 11:47:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UsEQD-00014C-On for qemu-devel@nongnu.org; Thu, 27 Jun 2013 11:47:51 -0400 Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.1.0.0.b.8.0.1.0.0.2.ip6.arpa ([2001:8b0:1d0::1]:58295 helo=mnementh.archaic.org.uk) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsEQD-000146-Gx for qemu-devel@nongnu.org; Thu, 27 Jun 2013 11:47:45 -0400 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.80) (envelope-from ) id 1UsEQA-0001BJ-Da; Thu, 27 Jun 2013 16:47:42 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Thu, 27 Jun 2013 16:47:42 +0100 Message-Id: <1372348062-4516-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.10.4 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:8b0:1d0::1 Cc: Richard Henderson , patches@linaro.org Subject: [Qemu-devel] [RFC] bitops: Provide sext32() and sext64() for signextending 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 A common operation in instruction decoding is to take a field from an instruction that represents a signed integer in some arbitrary number of bits, and sign extend it into a C signed integer type for manipulation. Provide new functions sext32() and sext64() to abstract away the bit manipulation. Signed-off-by: Peter Maydell --- I think we've vaguely tossed around the idea of a function to abstract away the concept of doing a signextension before, so here's an RFC... Does the API look right? The other approach I thought of would be to have functions sextract32()/sextract64() which work like the existing extract{32,64} but return signed (and sign extended) values, but providing the raw sign-extension separately seemed more flexible. (If we want the sextract ops then we could implement them as sext32(extract32(value, start, length), length).) This implementation continues to rely on the behaviour of right-shift of signed integers (as do most of the places which open-code this operation today; see also HACKING section 6). If we decide in future that we'd rather do this in a strictly-portable way we'll have a single place we need to change. (PS: Not really very tested yet :-)) include/qemu/bitops.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h index affcc96..5c6a756 100644 --- a/include/qemu/bitops.h +++ b/include/qemu/bitops.h @@ -273,4 +273,44 @@ static inline uint64_t deposit64(uint64_t value, int start, int length, return (value & ~mask) | ((fieldval << start) & mask); } +/** + * sext32: + * @value: value to sign-extend + * @length: length of the bitfield in value + * + * Sign-extend the least significant @length bits in @value to + * a 32 bit signed integer. That is to say, bits [0..@length-2] + * are untouched, and bit [@length-1] is duplicated into all + * higher bits of the returned value. + * + * Returns: the sign-extended value of the bitfield. + */ +static inline int32_t sext32(uint32_t value, int length) +{ + /* Note that this implementation relies on right shift of signed + * integers being an arithmetic shift. + */ + return ((int32_t)(value << (32 - length))) >> length; +} + +/** + * sext64: + * @value: value to sign-extend + * @length: length of the bitfield in value + * + * Sign-extend the least significant @length bits in @value to + * a 64 bit signed integer. That is to say, bits [0..@length-2] + * are untouched, and bit [@length-1] is duplicated into all + * higher bits of the returned value. + * + * Returns: the sign-extended value of the bitfield. + */ +static inline int64_t sext64(uint64_t value, int length) +{ + /* Note that this implementation relies on right shift of signed + * integers being an arithmetic shift. + */ + return ((int64_t)(value << (64 - length))) >> length; +} + #endif