|
From: | Paul Eggert |
Subject: | Re: Using the GNU GMP Library for Bignums in Emacs |
Date: | Mon, 23 Apr 2018 07:36:39 -0700 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 |
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. Checking for fixnum overflow (as opposed to machine-word overflow) requires one more conditional branch after some quick bit-twiddling.
[Prev in Thread] | Current Thread | [Next in Thread] |