Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1inSP7-0005Si-Hz for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jan 2020 19:14:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1inSP6-0007xy-6Q for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jan 2020 19:14:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1inSP5-0007xr-QB for pgsql-hackers@lists.postgresql.org; Fri, 03 Jan 2020 19:14:35 +0000 Received: from courriel.mines-paristech.fr ([77.158.173.149] helo=antispam-1.ensmp.fr) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1inSP2-0008In-4V for pgsql-hackers@lists.postgresql.org; Fri, 03 Jan 2020 19:14:34 +0000 Received: from smtp-4.mines-paristech.fr (smtp-4.ensmp.fr [77.158.173.143]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by antispam-1.ensmp.fr (antispam.mines-paristech.fr) with ESMTPS id D0FD856170; Fri, 3 Jan 2020 20:14:26 +0100 (CET) Received: from pseudo (149.64.203.77.rev.sfr.net [77.203.64.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp-4.mines-paristech.fr (Postfix) with ESMTPSA id D4A622C0319; Fri, 3 Jan 2020 20:14:25 +0100 (CET) Date: Fri, 3 Jan 2020 20:14:13 +0100 (CET) From: Fabien COELHO X-X-Sender: fabien@pseudo To: Vik Fearing cc: Tom Lane , Stephen Frost , Dean Rasheed , Chapman Flack , PostgreSQL Hackers Subject: Re: Greatest Common Divisor In-Reply-To: <6855727c-3836-21cc-18f9-e79227b99079@2ndquadrant.com> Message-ID: References: <5E08D947.9000902@anastigmatix.net> <20200102145712.GN3195@tamriel.snowman.net> <16242.1577977961@sss.pgh.pa.us> <6855727c-3836-21cc-18f9-e79227b99079@2ndquadrant.com> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-CLAMAV-SCAN: ok X-VRSPAM-SCORE: -100 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrgedufedrvdegfedguddvvdcutefuodetggdotefrucfrrhhofhhilhgvmecuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepfffhvffujgfkfhgfgggtsehttdertddtredvnecuhfhrohhmpefhrggsihgvnhcuvefqgffnjffquceotghovghlhhhosegtrhhirdgvnhhsmhhprdhfrheqnecukfhppeejjedrvddtfedrieegrddugeelnecurfgrrhgrmhepmhhouggvpehsmhhtphhouhht X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Bonsoir Vik, +int4gcd_internal(int32 arg1, int32 arg2) +{ + int32 swap; + + /* + * Put the greater value in arg1. + * This would happen automatically in the loop below, but avoids an + * expensive modulo simulation on some architectures. + */ + if (arg1 < arg2) + { + swap = arg1; + arg1 = arg2; + arg2 = swap; + } The point of swapping is to a void possibly expensive modulo, but this should be done on absolute values, otherwise it may not achieve its purpose as stated by the comment? > gcd() is now strictly positive, so INT_MIN is no longer a valid result. Ok. I'm unsure about gcd(INT_MIN, 0) should error. Possibly 0 would be nicer? -- Fabien.