Bug 95329 - fmaxf(inf, nan) does not always work
Summary: fmaxf(inf, nan) does not always work
Status: RESOLVED MOVED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 8.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-26 09:40 UTC by Florian Schanda
Modified: 2020-05-26 12:48 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Reduced testcase of the fmaxf issue (204 bytes, text/x-csrc)
2020-05-26 09:40 UTC, Florian Schanda
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Schanda 2020-05-26 09:40:43 UTC
Created attachment 48601 [details]
Reduced testcase of the fmaxf issue

I've been creating some floating point benchmarks for SMT-LIB. I have noticed that gcc (or something else) does not produce the same result in some specific examples. I am on amd64 Debian, but I've also reproduced this on a recent amd64 Ubuntu. Below is a reduced test-case showing this:

#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <string.h>

int main()
{
  uint32_t bv = 0x7FACBA7E;
  // (0 11111111 01011001011101001111110)

  float a, b;
  a = -INFINITY;
  memcpy(&b, &bv, 4);

  printf("max(%f, %f) = %f\n", a, b, fmaxf(a, b));

  b = NAN;
  printf("max(%f, %f) = %f\n", a, b, fmaxf(a, b));
}

When compiling and running this program you get:

max(-inf, nan) = nan
max(-inf, nan) = -inf

I've tried with both:
  gcc bug.c -lm
and
  gcc -std=c99 -frounding-math -fsignaling-nans -ffp-contract=off -mfma -mno-fma4 -pedantic -Wall -W -msse2 -mfpmath=sse -fstrict-aliasing bug.c -lm


However, IEEE-754 says that max(something not NaN, NaN) should get you the
thing that is not NaN. With the built-in constant it works, but with my hand-crafted NaN it does not.

I've also tried replacing fmaxf by __builtin_fmaxf, with no success.


I also realise this likely may not be a gcc bug, but something deeper down in libm or elsewhere. I do note that with clang-9 this works though. However I
suspect you guys will know where exactly this report needs to go to.
Comment 1 Jakub Jelinek 2020-05-26 12:02:49 UTC
I believe this is a glibc bug rather than gcc.  This is without optimizations and the library function in libm is called in both cases and giving different results based on whether it is -inf, nan(0x2cba7e) or -inf, nan(0x400000).
glibc has a different bug tracker though, so you need to file it there (sourceware.org/bugzilla).
If you compile with -O2, you'll get -inf in both cases (in that case no library call is done and it is folded by gcc).
Comment 2 Florian Schanda 2020-05-26 12:37:17 UTC
Cool, thanks for the pointer to their bug tracker. I'll bother them instead ;)
Comment 3 Florian Schanda 2020-05-26 12:43:23 UTC
I have created https://sourceware.org/bugzilla/show_bug.cgi?id=26045
Comment 4 Jakub Jelinek 2020-05-26 12:48:57 UTC
.