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: 18165 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f197.google.com (mail-qc0-f197.google.com [209.85.216.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 61AC525E0D for ; Thu, 27 Jun 2013 15:47:46 +0000 (UTC) Received: by mail-qc0-f197.google.com with SMTP id u12sf1331397qcx.4 for ; Thu, 27 Jun 2013 08:47:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-beenthere:x-forwarded-to:x-forwarded-for :delivered-to:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe; bh=rGNspLxqgNpzY/QmqeR69LHmlaG15Nn3e6tuCglAkZk=; b=k6j0LBIujw6E2Js61lUIBEzKAHEYWrto0OKl+lk7RTs/M3+LtjdXUyslXQoLbrok9P dC6T2npww52L+ENOhp6IDpOrFa81mpJ3KnCip6cxal93fJQLDP5g45XFFkdO5UL51JF8 tFxVlasnU/BtVUCVpAaJZ/0dg3eKCN5HeXRlpJyfhskh4MsdOypBPf5Kmnc0HRpjh4OU RwbaU3DBXg7vGXCDOMTxetXn8x8s/+bYV29nlqNv7xBSjF2Y68nMdiQ4D5LKLXPXFSko kRYx7KKAHqid39525eL42vsCD8hPMbxR6KXIv5KWHj7YRkrx/nND0Ak7SWSYUqGXVIru wPpA== X-Received: by 10.236.207.169 with SMTP id n29mr5038795yho.50.1372348065942; Thu, 27 Jun 2013 08:47:45 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.14.131 with SMTP id p3ls918014qec.93.gmail; Thu, 27 Jun 2013 08:47:45 -0700 (PDT) X-Received: by 10.52.121.42 with SMTP id lh10mr3160292vdb.39.1372348065776; Thu, 27 Jun 2013 08:47:45 -0700 (PDT) Received: from mail-ve0-f178.google.com (mail-ve0-f178.google.com [209.85.128.178]) by mx.google.com with ESMTPS id t6si919195vcu.84.2013.06.27.08.47.45 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 27 Jun 2013 08:47:45 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.178; Received: by mail-ve0-f178.google.com with SMTP id pb11so827957veb.37 for ; Thu, 27 Jun 2013 08:47:45 -0700 (PDT) X-Received: by 10.58.29.228 with SMTP id n4mr3794217veh.50.1372348065698; Thu, 27 Jun 2013 08:47:45 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.58.165.8 with SMTP id yu8csp153716veb; Thu, 27 Jun 2013 08:47:45 -0700 (PDT) X-Received: by 10.180.206.70 with SMTP id lm6mr16394590wic.50.1372348064655; Thu, 27 Jun 2013 08:47:44 -0700 (PDT) Received: from mnementh.archaic.org.uk (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]) by mx.google.com with ESMTPS id a6si5133731wik.67.2013.06.27.08.47.44 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 27 Jun 2013 08:47:44 -0700 (PDT) Received-SPF: neutral (google.com: 2001:8b0:1d0::1 is neither permitted nor denied by best guess record for domain of pm215@archaic.org.uk) client-ip=2001:8b0:1d0::1; 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 Cc: patches@linaro.org, Richard Henderson Subject: [RFC] bitops: Provide sext32() and sext64() for signextending bitfields 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-Gm-Message-State: ALoCoQntT8Qdm9XqVgtAn043e781/5DGh7MaPHH/7fVSyabfCjhettU5Ju15pw61DErFt/rPzgDv X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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