From patchwork Tue Mar 28 15:49:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67029 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 69114387090E for ; Tue, 28 Mar 2023 15:50:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69114387090E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680018621; bh=EWba4sS6t80FYz/U+pkAv3MrY5lKS6X7HKnukfdMFhk=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=M9sBzG9CXBb8f5G/ZiwQU/T7xIm40Szanm0KQb8dBlNUynG6REckfbAsviKNDeBBS YV1ukCm89S/9UjRsxD53jGV85hrJsfw9GA2JseazUQSL+BelGvIsPF5LOMzB20WF3s hMZ6l/w/+UJPbzFZ7QlBEDEjyrkAHNzu8vkDHHWg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 98C8F385558F for ; Tue, 28 Mar 2023 15:49:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 98C8F385558F Received: by mail-io1-xd34.google.com with SMTP id e13so5566888ioc.0 for ; Tue, 28 Mar 2023 08:49:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680018582; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EWba4sS6t80FYz/U+pkAv3MrY5lKS6X7HKnukfdMFhk=; b=prI+crJaOEheTNuFlZRoCDNI5sqJ8aknnldxplVVyezJ0dgSj2yBaRLAEioiNYZAWA 17ccfiU02jIPRaIGMO5iq1z18LC16o9tnePzThETOCw/LKrddbsRqhO0WqC0RW3vmZp6 k1iEEtwJSj2s4R1sfCQ0J1IOdH4+/OKB48cyMxZzn/RQIzhdyR/kzMgx0QOOri8tdEuZ SBvEsqvRFwwTM1tkhHmz10QMF+Ph3FpIXkYhG24oZJVOjPwOUB8I5bEh7hn6uA+GTT1L q8hM+P6wP83xP4jcQ3iGTY3lwnqqqJKH2DVGMYsmXdFBIBcYVXuU2dPqK/Q3dkdYqWsd pI6w== X-Gm-Message-State: AO0yUKV6aTpSFm8/1OuKk7Zk1/FMvLEZWAFA9ah+OsiXtMizKl1b0kG7 lFdem6QIn0g2/oAf7lv+4+avZzPzcrevx82/8saZfg== X-Google-Smtp-Source: AK7set/QjpuCXyhHS3cpaqZ5Ut6f8djG6lgdcxgLmgyx2y7121WuuuiLrJ02EZ/36XAR/Cyz4/uSBA== X-Received: by 2002:a6b:ec11:0:b0:74d:771:6ed5 with SMTP id c17-20020a6bec11000000b0074d07716ed5mr12202785ioh.21.1680018581870; Tue, 28 Mar 2023 08:49:41 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id u5-20020a5ec005000000b0075c37601b5csm1684522iol.4.2023.03.28.08.49.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 08:49:41 -0700 (PDT) Date: Tue, 28 Mar 2023 09:49:40 -0600 Subject: [PATCH 4/6] Add overload of fits_in_type MIME-Version: 1.0 Message-Id: <20230328-expr-128-bit-v1-4-f9eeb0143318@adacore.com> References: <20230328-expr-128-bit-v1-0-f9eeb0143318@adacore.com> In-Reply-To: <20230328-expr-128-bit-v1-0-f9eeb0143318@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.1 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This adds an overload of fits_in_type that accepts a gdb_mpz. A subsequent patch will use this. --- gdb/parse.c | 26 ++++++++++++++++++++++++++ gdb/parser-defs.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/gdb/parse.c b/gdb/parse.c index 24db3ca57d9..23545e32160 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -648,6 +648,32 @@ fits_in_type (int n_sign, ULONGEST n, int type_bits, bool type_signed_p) else gdb_assert_not_reached (""); } + +/* Return true if the number N_SIGN * N fits in a type with TYPE_BITS and + TYPE_SIGNED_P. N_SIGNED is either 1 or -1. */ + +bool +fits_in_type (int n_sign, const gdb_mpz &n, int type_bits, bool type_signed_p) +{ + /* N must be nonnegative. */ + gdb_assert (n.sgn () >= 0); + + /* Zero always fits. */ + /* Normalize -0. */ + if (n.sgn () == 0) + return true; + + if (n_sign == -1 && !type_signed_p) + /* Can't fit a negative number in an unsigned type. */ + return false; + + gdb_mpz max = gdb_mpz::pow (2, (type_signed_p + ? type_bits - 1 + : type_bits)); + if (n_sign == -1) + return n <= max; + return n < max; +} /* This function avoids direct calls to fprintf in the parser generated debug code. */ diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h index 1780d856744..62829a86f9a 100644 --- a/gdb/parser-defs.h +++ b/gdb/parser-defs.h @@ -414,6 +414,8 @@ extern bool parse_float (const char *p, int len, const struct type *type, gdb_byte *data); extern bool fits_in_type (int n_sign, ULONGEST n, int type_bits, bool type_signed_p); +extern bool fits_in_type (int n_sign, const gdb_mpz &n, int type_bits, + bool type_signed_p); /* Function used to avoid direct calls to fprintf