From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Helmut Eller Newsgroups: gmane.emacs.devel Subject: Re: Using the GNU GMP Library for Bignums in Emacs Date: Mon, 23 Apr 2018 21:22:26 +0200 Message-ID: References: <29f933ac-a6bf-8742-66a7-0a9d6d3e5a88@disroot.org> <83bmecy6fx.fsf@gnu.org> <0d3175d8-d996-651e-b221-71978bde3a65@cs.ucla.edu> <51e619e0-ee38-eb97-6c1d-0925b675290a@disroot.org> <8e12135a-0fcc-7aa3-d000-731d2f26d918@disroot.org> <87lgde9v0b.fsf@linux-m68k.org> <12e4d927-c3fd-933c-6b7c-5e5086b45622@cs.ucla.edu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1524511250 30529 195.159.176.226 (23 Apr 2018 19:20:50 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 23 Apr 2018 19:20:50 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: Paul Eggert , Andreas Schwab To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Apr 23 21:20:46 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fAh11-0007mm-0K for ged-emacs-devel@m.gmane.org; Mon, 23 Apr 2018 21:20:43 +0200 Original-Received: from localhost ([::1]:55148 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fAh37-0000Ek-Qa for ged-emacs-devel@m.gmane.org; Mon, 23 Apr 2018 15:22:53 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58463) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fAh31-0000C5-Po for emacs-devel@gnu.org; Mon, 23 Apr 2018 15:22:48 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fAh2x-0003C9-SC for emacs-devel@gnu.org; Mon, 23 Apr 2018 15:22:47 -0400 Original-Received: from [195.159.176.226] (port=58791 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fAh2x-0003BZ-LN for emacs-devel@gnu.org; Mon, 23 Apr 2018 15:22:43 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1fAh0o-0007a6-Vb for emacs-devel@gnu.org; Mon, 23 Apr 2018 21:20:30 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 37 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:mUtK14I8iYbAR+jdcVVMx8naKw4= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:224817 Archived-At: Paul Eggert wrote: > Andreas Schwab wrote: >> There is no way to detect overflow after the fact because overflow >> invokes undefined behaviour. You need to either check the range >> beforehand, or use special builtins offered by the compiler >> (eg. __builtin_smul_overflow in GCC). > > All true, and Emacs lib/intprops.h has an INT_MULTIPLY_OVERFLOW macro > that arranges for all that. On the x86-64 with GCC, it costs one > additional instruction (typically a conditional branch that is not > taken) to check for overflow in machine-word integer > multiplication. If I read the code in in data.c correctly, than Emacs uses the INT_MULTIPLY_WRAPV macro which boils down to the __builtin_mul_overflow. Which indeed produces nice machine code. But the original question was about ANSI C, which seems to require a division and 3 conditional jumps for the range check (with gcc 6). > Checking for fixnum overflow (as opposed to machine-word overflow) > requires one more conditional branch after some quick bit-twiddling. Does INT_MULTIPLY_WRAPV macro even perform fixnum overflow tests? Anyway, I find it curios that the following two expression yield different values: (* (* most-positive-fixnum 2) 1.0) => -2.0 (* most-positive-fixnum 2 1.0) => 4.611686018427388e+18 Helmut