From patchwork Fri Mar 3 21:12:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65989 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 C55E9385B511 for ; Fri, 3 Mar 2023 21:12:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C55E9385B511 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677877968; bh=2Va6z0J5/qxI3LuHf0apgC91vaVebsNLUTeZjt2aMCI=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=JtSeSiVvW3aIVt2uVwy6TsT7h12Cagnl4oAJ3edtbFSzL7O9azNZTWC7jBP7dI6zX wCik26DtKn2EgG7Bl3cS9ouJhy81/jd2hNLxhtKVZ7rIXwimuPbBU8quCTSxVxvi3i yrkHHOgN9qZVWGinWLznY7+opxymqKKuyANfhUlg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 290633858D37 for ; Fri, 3 Mar 2023 21:12:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 290633858D37 Received: by mail-il1-x132.google.com with SMTP id g9so2575317ila.8 for ; Fri, 03 Mar 2023 13:12:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677877939; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2Va6z0J5/qxI3LuHf0apgC91vaVebsNLUTeZjt2aMCI=; b=VluA5TZnZ5OKqgpGAK+VvJuS1uoAliydPe6BHzBwhv/TZtLqiWqgTDjGfMs82naDOs l2zf2uP2hGBzIrMWSLikRZNWdiznHE8OF+WDPTFWIkFhkFIzgi5K/Huvoii70qCyxirS fhz8wbNz2g3GKDxBOKD3WzDCb2dMXtCGD3ot4tgVn2M+HfdfdUupB+Oq4t3lXXDTHmgD 5mlCsc78dfjU915rItahTItOB4nytJVFD2A0pFHuedT7Qhzi9mhRvV4xFt8CoFIRlBrX 4hI18iBhgzT8zkknKrT5+SBvhSbwatZxbRNn3rTFO+xOMv4VLBtN0OjCIQ+1DRgg563I Dy5w== X-Gm-Message-State: AO0yUKXuUebO1XlsWbOg4BOd7JgPp9s28s+X7+FjI0OHZ/1FyOi6MRYv TMAVkH3ZPd7nXmPPzhxfO2YD3IKB9/4ktWmVlI0= X-Google-Smtp-Source: AK7set876gBYMVatMCcDci4DVW+sTOURLJHA/NdIKz+H7pWDmAakH3dUtJWNOBiQ+RQVurhHjVb6vQ== X-Received: by 2002:a05:6e02:b4d:b0:314:e6f:f2d1 with SMTP id f13-20020a056e020b4d00b003140e6ff2d1mr2497706ilu.31.1677877939359; Fri, 03 Mar 2023 13:12:19 -0800 (PST) Received: from localhost.localdomain (75-166-130-93.hlrn.qwest.net. [75.166.130.93]) by smtp.gmail.com with ESMTPSA id g8-20020a925208000000b00313b281ecd2sm867988ilb.70.2023.03.03.13.12.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 13:12:19 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 1/8] Add many operators to gdb_mpz Date: Fri, 3 Mar 2023 14:12:00 -0700 Message-Id: <20230303211207.1053037-2-tromey@adacore.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303211207.1053037-1-tromey@adacore.com> References: <20230303211207.1053037-1-tromey@adacore.com> MIME-Version: 1.0 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 many operator overloads and other useful methods to gdb_mpz. This is preparation for using this class for scalar arithmetic in gdb expression evaluation. --- gdb/gmp-utils.h | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 274c28c0ce8..d4e5015e345 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -90,6 +90,12 @@ struct gdb_mpz return *this; } + gdb_mpz &operator= (bool src) + { + mpz_set_ui (m_val, (unsigned long) src); + return *this; + } + /* Initialize this value from a string and a base. Returns true if the string was parsed successfully, false otherwise. */ bool set (const char *str, int base) @@ -105,6 +111,14 @@ struct gdb_mpz return result; } + /* Return a new value that is this value raised to EXP. */ + gdb_mpz pow (unsigned long exp) const + { + gdb_mpz result; + mpz_pow_ui (result.m_val, m_val, exp); + return result; + } + /* Convert VAL to an integer of the given type. The return type can signed or unsigned, with no size restriction. */ @@ -137,35 +151,154 @@ struct gdb_mpz mpz_neg (m_val, m_val); } + /* Take the one's complement in place. */ + void complement () + { mpz_com (m_val, m_val); } + + /* Mask this value to N bits, in place. */ + void mask (unsigned n) + { mpz_tdiv_r_2exp (m_val, m_val, n); } + + /* Return the sign of this value. This returns -1 for a negative + value, 0 if the value is 0, and 1 for a positive value. */ + int sgn () const + { return mpz_sgn (m_val); } + + explicit operator bool () const + { return sgn () != 0; } + gdb_mpz &operator*= (long other) { mpz_mul_si (m_val, m_val, other); return *this; } + gdb_mpz operator* (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_mul (result.m_val, m_val, other.m_val); + return result; + } + + gdb_mpz operator/ (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_tdiv_q (result.m_val, m_val, other.m_val); + return result; + } + + gdb_mpz operator% (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_tdiv_r (result.m_val, m_val, other.m_val); + return result; + } + gdb_mpz &operator+= (unsigned long other) { mpz_add_ui (m_val, m_val, other); return *this; } + gdb_mpz &operator+= (const gdb_mpz &other) + { + mpz_add (m_val, m_val, other.m_val); + return *this; + } + + gdb_mpz operator+ (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_add (result.m_val, m_val, other.m_val); + return result; + } + gdb_mpz &operator-= (unsigned long other) { mpz_sub_ui (m_val, m_val, other); return *this; } + gdb_mpz &operator-= (const gdb_mpz &other) + { + mpz_sub (m_val, m_val, other.m_val); + return *this; + } + + gdb_mpz operator- (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_sub (result.m_val, m_val, other.m_val); + return result; + } + gdb_mpz &operator<<= (unsigned long nbits) { mpz_mul_2exp (m_val, m_val, nbits); return *this; } + gdb_mpz operator<< (unsigned long nbits) const + { + gdb_mpz result; + mpz_mul_2exp (result.m_val, m_val, nbits); + return result; + } + + gdb_mpz operator>> (unsigned long nbits) const + { + gdb_mpz result; + mpz_tdiv_q_2exp (result.m_val, m_val, nbits); + return result; + } + + gdb_mpz &operator>>= (unsigned long nbits) + { + mpz_tdiv_q_2exp (m_val, m_val, nbits); + return *this; + } + + gdb_mpz operator& (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_and (result.m_val, m_val, other.m_val); + return result; + } + + gdb_mpz operator| (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_ior (result.m_val, m_val, other.m_val); + return result; + } + + gdb_mpz operator^ (const gdb_mpz &other) const + { + gdb_mpz result; + mpz_xor (result.m_val, m_val, other.m_val); + return result; + } + bool operator> (const gdb_mpz &other) const { return mpz_cmp (m_val, other.m_val) > 0; } + bool operator>= (const gdb_mpz &other) const + { + return mpz_cmp (m_val, other.m_val) >= 0; + } + + bool operator< (const gdb_mpz &other) const + { + return mpz_cmp (m_val, other.m_val) < 0; + } + + bool operator<= (const gdb_mpz &other) const + { + return mpz_cmp (m_val, other.m_val) <= 0; + } + bool operator< (int other) const { return mpz_cmp_si (m_val, other) < 0; @@ -181,6 +314,11 @@ struct gdb_mpz return mpz_cmp (m_val, other.m_val) == 0; } + bool operator!= (const gdb_mpz &other) const + { + return mpz_cmp (m_val, other.m_val) != 0; + } + private: /* Helper template for constructor and operator=. */