mpn/t_get_d test failure with '-O3 -ffast-math'

Vasili Burdo vasili.burdo at gmail.com
Fri Jul 11 12:27:25 CEST 2025


Hi.

Steps to reproduce:

- configure gmplib-6.3.0 with following command line: configure ABI=64
CC=/path/to/clang-20 CFLAGS="-O3 -ffast-math -march=native -m64"
--with-pic --disable-shared
- make
- make check

Observe failure in test t-get_d:

FAIL: t-get_d
=============
mpn_get_d wrong, didn't get infinity
  nsize    1
  exp      999999
  sign     0
  got      =[00 00 00 00 00 00 F0 7F] inf
  got sign 0
 FAIL t-get_d (exit status: 134)

I tried to report it to Clang bugtracker here (
https://github.com/llvm/llvm-project/issues/148060 ) and people
responded this is probably gmplib issue.
I checked mpn_get_d() code and I see it crafts infinities using union
and it could be UB if -ffast-math is used.

Possible bug resolutions:
- disallow gmplib building w/ -ffast-math. Both GCC and Clang define
__FAST_MATH__ and __FINITE_MATH_ONLY__ if aggressive FP optimizations
requested.
- workaround mpn_gmp_d() somehow in case of aggressive FP optimizations.

Thanks.
Vasili


More information about the gmp-bugs mailing list