[cfe-users] Clang9 UBSan and GMP

Hans Åberg via cfe-users cfe-users at lists.llvm.org
Wed Oct 30 16:25:13 PDT 2019

> On 30 Oct 2019, at 23:50, David Blaikie <dblaikie at gmail.com> wrote:
>> On Wed, Oct 30, 2019 at 2:29 PM Hans Åberg <haberg-1 at telia.com> wrote:
>> Indeed, very hard to figure out. If it is some hidden undefined behavior causing it, the UBSan should have caught it, but it does not.
> Right - but especially with numerics (especially floating point) there's loads of room for valid but different behavior between different compilers - behavior that isn't UB. How much precision a certain mathematical equation maintains is really at the whim of the optimizers in a lot of ways.

I believe that GMP is just using integer types, and then uses that to make multiprecision integers, rational numbers, and floating point numbers. At least MPFR uses only the integer and rational number part of GMP, and builds multiprecision floating point numbers on top of that, which is necessary because of special requirements of the standards they adhere to.

>> The link that Matthew gave says that the GMP developers experienced a number of such issues with Clang. One can though turn off the optimizer, and the tests pass.
> Sure - most of the numeric effects would only appear with optimizations. Without them every numeric operation's just done in registers, then written right back to memory (so no chance of excess precision leaking in by storing the value in an 80bit floating point register between multiple operations, or any risk of fused operations that produces extra precision, etc). 
> The only way to know is to trace down/reduce the point where the values diverge & stare at the code to see who's right.

GMP has been used in three years in a sequenced operation that must be exact and without errors to solve the problem [1], and I would think it used GCC with optimizations. So that puts Clang in a tough spot. :-)

1. https://gmplib.org/list-archives/gmp-discuss/2019-April/006319.html

More information about the cfe-users mailing list