From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 1B57939CEBF4 for ; Fri, 7 Jun 2024 09:31:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B57939CEBF4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1B57939CEBF4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717752680; cv=none; b=safE4VN7EvZFwDbT3P+VTbCpDb00lahf8rEU/Aawr5pEzTj3cfk4sQ8xinmfpKHwD1clPtmd3n/oqE2QtiXrwqZzPcd7LgThbqsq2gB0SwBd/y7YdAUKtwUFXBhT9Ox10AzS7Ip5gWjd61UGQAXOQ4nhBy4oVsABqNBCVKgHr1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717752680; c=relaxed/simple; bh=UzdID9RecbvP4e54cSVLDoAA4uZhteXsk1NewCvbFnk=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=V0IDNknid07P/GmUL9P/nWNr99RHEqr+vKFnjU5W3gPcsnCm9sWVTrIkUbaoqhpLoTwBhM9BHhAXBOt7OUlqew5xZipNYKWYOQix3RBGBSkQBIHhFAtB5DoQgYf/nKN/CWaXh2/zABpdyJSofKg/IY/HUjRWH/RMDlSdZ20thqk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717752677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=uoY5wgVk9wtPxawygguSWFRPztV5Wuyq8EYaI6cTDaw=; b=IILLPc6VSkR//phArXUNLbQ+4WoCtFIOJOfi9WjHrjI98p1RVHt8WNG3kKOR4cCaLJPSc3 G/h6KN/+G3F1z6AXBmJaWxYvHgAt+k3KbJKIIQN8+PFAYeh47BTt1i9I/lKc4ZsARxAS2b kVJTI0LJ5IQaO/CUY0/2YR/VjisRH1I= Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-2N4PwIPNOPi2LfX-FH_CJg-1; Fri, 07 Jun 2024 05:31:16 -0400 X-MC-Unique: 2N4PwIPNOPi2LfX-FH_CJg-1 Received: by mail-yb1-f197.google.com with SMTP id 3f1490d57ef6-dfa744fe2f9so3254779276.0 for ; Fri, 07 Jun 2024 02:31:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717752675; x=1718357475; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uoY5wgVk9wtPxawygguSWFRPztV5Wuyq8EYaI6cTDaw=; b=Nwbnp5tX+Y1ErTAtiSRHf1hJSknDlLO6fzc5xkn5Qj6VmfuDWsM8YJT7saN/Kgeb6o sWO5rou0YzSl710l+4MJBscG1e0l9ctkOpBpoUMvriMLkAjFFZzkmbiBfjy20Q40LV5F XX6k13PTEoEaBhruOSi5jxDLDrneUdHvFUEGbi7ImE3Ez2GPS+iWF1dpALQA5XIGBSRZ TzqUR5vMGIAB+GG1THg0Aud5Qcs6oikrZHaKVsWNLFVYkMMQKXIIbuQTkRmBydHljfQ5 6TCfZVx5xmu6V3Wzst8ktP4/+1RQ1zRWHW3vYJqGk2EITsGV4Kd8I4C+Gotlty4+khNH rB7w== X-Forwarded-Encrypted: i=1; AJvYcCVoC6/UKJxeGfnQSwRlpJ5W8joXm6h3o7DXqyUEoFx5hrC16S4HTksGSnA/VEHZF39vEzV4LgX16ZKVvGx/IE9jmLI3Q0c= X-Gm-Message-State: AOJu0YwbFjNLRV0lyhIEoJtZUWsIOvLBGS1YjH1ZxwxuCkUyWA7yIH9l 89nWlE8cW8FL1yYVeOsv6A497nvg2PGDGFmXrRZ+vsCG1wsE01nDAlw4ddIeq1mpsp2DUuhGYsb hV8AxmBIsoJsPxnEy5IP5I7K9Hy7zs8zZVoaH3o21geNNgvXifRUCtVQXOhorHz/7rrIsVYQcb7 9n8vR+xlNCwtafHRulZyK+St0AcUk= X-Received: by 2002:a25:accb:0:b0:df4:dd41:f239 with SMTP id 3f1490d57ef6-dfaf6646edamr1964153276.26.1717752675566; Fri, 07 Jun 2024 02:31:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE1CunjM2PEe8qavSUiTGbmsTWWu/TxspYeb1anq9NK86axQkMN6lLUMZ+4LZMdMkm0H0Fd9xgqX+Mnn0AdCaI= X-Received: by 2002:a25:accb:0:b0:df4:dd41:f239 with SMTP id 3f1490d57ef6-dfaf6646edamr1964140276.26.1717752675107; Fri, 07 Jun 2024 02:31:15 -0700 (PDT) MIME-Version: 1.0 References: <9e5636e8-224f-4dde-9c60-489b4aad6534@gmail.com> <87wmn1m8pu.fsf@gentoo.org> In-Reply-To: <87wmn1m8pu.fsf@gentoo.org> From: Jonathan Wakely Date: Fri, 7 Jun 2024 10:30:59 +0100 Message-ID: Subject: Re: [PATCH] libstdc++: Optimize std::gcd To: Sam James Cc: Stephen Face , libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, 7 Jun 2024 at 09:57, Sam James wrote: > > Stephen Face writes: > > > This patch is to optimize the runtime execution of gcd. Mathematically, > > it computes with the same algorithm as before, but subtractions and > > branches are rearranged to encourage generation of code that can use > > flags from the subtractions for conditional moves. Additionally, most > > pairs of integers are coprime, so this patch also includes a check for > > one of the integers to be equal to 1, and then it will exit the loop > > early in this case. > > Is it worth filing a bug for the missed optimisation? You shouldn't have > to write things in a specific order. Thanks. Yes, I think a bug report would be good. But 20%-60% decreases in run time seems significant enough that we should take the libstdc++ patch now, rather than wait for a possible compiler fix to come later. Stephen, could you please confirm whether you have a copyright assignment in place for GCC, and if not whether you would be will to complete one, or alternatively contribute this under the DCO terms: https://gcc.gnu.org/dco.html Thanks! > > > > > libstdc++-v3/ChangeLog: > > > > * include/std/numeric(__gcd): Optimize. > > --- > > I have tested this on x86_64-linux and aarch64-linux. I have tested the > > timing with random distributions of small inputs and large inputs on a > > couple of machines with -O2 and found decreases in execution time from > > 20% to 60% depending on the machine and distribution of inputs. > > > > libstdc++-v3/include/std/numeric | 21 +++++++++++---------- > > 1 file changed, 11 insertions(+), 10 deletions(-) > > > > diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric > > index c912db4a519..3c9e8387a0e 100644 > > --- a/libstdc++-v3/include/std/numeric > > +++ b/libstdc++-v3/include/std/numeric > > @@ -148,19 +148,20 @@ namespace __detail > > > > while (true) > > { > > - if (__m > __n) > > - { > > - _Tp __tmp = __m; > > - __m = __n; > > - __n = __tmp; > > - } > > + _Tp __m_minus_n = __m - __n; > > + if (__m_minus_n == 0) > > + return __m << __k; > > > > - __n -= __m; > > + _Tp __next_m = __m < __n ? __m : __n; > > > > - if (__n == 0) > > - return __m << __k; > > + if (__next_m == 1) > > + return __next_m << __k; > > + > > + _Tp __n_minus_m = __n - __m; > > + __n = __n < __m ? __m_minus_n : __n_minus_m; > > + __m = __next_m; > > > > - __n >>= std::__countr_zero(__n); > > + __n >>= std::__countr_zero(__m_minus_n); > > } > > } > > } // namespace __detail >