From patchwork Fri Jun 28 11:40:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 18180 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f72.google.com (mail-qa0-f72.google.com [209.85.216.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 045B925E4B for ; Fri, 28 Jun 2013 11:40:39 +0000 (UTC) Received: by mail-qa0-f72.google.com with SMTP id f14sf1186179qak.3 for ; Fri, 28 Jun 2013 04:40:38 -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 :in-reply-to:references: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=SM51FpPt0TRC8tWBVuyq4E3x8LzNuDw3sL6R5AxETkg=; b=Eo4gJTgTdorhbEg3MNR7gTqE8BglXftS3dZU4agb0Df9poaDELQ+R44a2qmk266gsr ILYJdVxEEwoJ5I8CdKVtRfbwTG4kQsOXScyQzirfqgAkqjh9ry6ClMU9N/POSXR0WC5S JRdHwabIKW85D83YCpLFJh11+IeuQqp4pSTz7/AY6ahizZI1dlLN3qdk67Yayg7TUTkt cthvQqqNH67pnJ48iTAR9dQm3ujuNVzVOZI9kmiBTEdnBbdrmBzf+B9VQSdQHJaUHTuZ uESnzeT10Dc4RVaNpFYKbF2c1a/uQxmMSYDiOCa+KcaccnCCa/8CSY4lH7vQ5rVgzD9X bnSw== X-Received: by 10.224.200.202 with SMTP id ex10mr13386175qab.8.1372419638795; Fri, 28 Jun 2013 04:40:38 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.62.42 with SMTP id v10ls1148130qer.27.gmail; Fri, 28 Jun 2013 04:40:38 -0700 (PDT) X-Received: by 10.52.92.171 with SMTP id cn11mr4459917vdb.10.1372419638705; Fri, 28 Jun 2013 04:40:38 -0700 (PDT) Received: from mail-vb0-f42.google.com (mail-vb0-f42.google.com [209.85.212.42]) by mx.google.com with ESMTPS id ur15si1889490veb.108.2013.06.28.04.40.38 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 28 Jun 2013 04:40:38 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.212.42 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.42; Received: by mail-vb0-f42.google.com with SMTP id i3so1630739vbh.29 for ; Fri, 28 Jun 2013 04:40:38 -0700 (PDT) X-Received: by 10.58.119.233 with SMTP id kx9mr5488947veb.3.1372419638629; Fri, 28 Jun 2013 04:40:38 -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 yu8csp186087veb; Fri, 28 Jun 2013 04:40:38 -0700 (PDT) X-Received: by 10.15.73.197 with SMTP id h45mr13147474eey.146.1372419636605; Fri, 28 Jun 2013 04:40:36 -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 y5si3894113eew.83.2013.06.28.04.40.35 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 28 Jun 2013 04:40:36 -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 1UsX2W-0001RZ-8u; Fri, 28 Jun 2013 12:40:32 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Richard Henderson , Markus Armbruster Subject: [PATCH 1/2] bitops: Provide sextract32() and sextract64() Date: Fri, 28 Jun 2013 12:40:31 +0100 Message-Id: <1372419632-5521-2-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1372419632-5521-1-git-send-email-peter.maydell@linaro.org> References: <1372419632-5521-1-git-send-email-peter.maydell@linaro.org> X-Gm-Message-State: ALoCoQnt9ALGZbaV/NybjeIt1Qbl3rj+FK00RRFXWo8PH33nzpI6pTvmv80b9Is/w7FPcMx/WLWA X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.42 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 sextract32() and sextract64() which perform this operation; they are like the existing extract32() and extract64() except that the field is sign-extended into the returned result. Signed-off-by: Peter Maydell --- include/qemu/bitops.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h index affcc96..06e2e6f 100644 --- a/include/qemu/bitops.h +++ b/include/qemu/bitops.h @@ -222,6 +222,56 @@ static inline uint64_t extract64(uint64_t value, int start, int length) } /** + * sextract32: + * @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 + * + * Extract from the 32 bit input @value the bit field specified by the + * @start and @length parameters, and return it, sign extended to + * an int32_t (ie with the most significant bit of the field propagated + * to all the upper bits of the return value). The bit field must lie + * entirely within the 32 bit word. It is valid to request that + * all 32 bits are returned (ie @length 32 and @start 0). + * + * Returns: the sign extended value of the bit field extracted from the + * input value. + */ +static inline int32_t sextract32(uint32_t value, int start, int length) +{ + assert(start >= 0 && length > 0 && length <= 32 - start); + /* Note that this implementation relies on right shift of signed + * integers being an arithmetic shift. + */ + return ((int32_t)(value << (32 - length - start))) >> (32 - length); +} + +/** + * sextract64: + * @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 + * + * Extract from the 64 bit input @value the bit field specified by the + * @start and @length parameters, and return it, sign extended to + * an int64_t (ie with the most significant bit of the field propagated + * to all the upper bits of the return value). The bit field must lie + * entirely within the 64 bit word. It is valid to request that + * all 64 bits are returned (ie @length 64 and @start 0). + * + * Returns: the sign extended value of the bit field extracted from the + * input value. + */ +static inline uint64_t sextract64(uint64_t value, int start, int length) +{ + assert(start >= 0 && length > 0 && length <= 64 - start); + /* Note that this implementation relies on right shift of signed + * integers being an arithmetic shift. + */ + return ((int64_t)(value << (64 - length - start))) >> (64 - length); +} + +/** * deposit32: * @value: initial value to insert bit field into * @start: the lowest bit in the bit field (numbered from 0)