[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using the GNU GMP Library for Bignums in Emacs

From: Helmut Eller
Subject: Re: Using the GNU GMP Library for Bignums in Emacs
Date: Mon, 23 Apr 2018 21:22:26 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]