Issue 165989
Summary `const`/`constexpr` INFINITY can cause clang to make a different result from `inf/inf` or `inf-inf``
Labels clang
Assignees
Reporter wind0204
    _Title_.

The usual result from `inf/inf` or `inf-inf` is a negative NaN, but when I use a `const` infinity clang calculates it in compile-time and saves a positive NaN for the program

`Ubuntu GCC 13.3.0` didn't have the issue but `Ubuntu clang 18` and `Ubuntu clang 20` showed the symptom.

The CPU I use is Haswell.

I think you can reproduce the symptom with the code below ( `clang minimal.c -std=gnu23 -lm` )
```c
#include <stdio.h>
#include <stdint.h>
#include <math.h>

/*
 * \brief for little endian systems
 */
union ieee754_binary32 {
  struct {
    uint32_t fraction: 23;
 uint16_t exponent: 8;
    uint8_t sign: 1;
  };
  float whole;
};

int main(int argc, char * argv[])
{
  float inf = INFINITY;
  float nan = NAN;

#if __clang_major__ <= 18
  const float cinf = INFINITY;
  const float cnan = NAN;
#else
  constexpr float cinf = INFINITY;
  constexpr float cnan = NAN;
#endif

  fprintf( stdout, "inf - inf = %f\n", (inf - inf) );
  fprintf( stdout, "const_inf - const_inf = %f\n", (cinf - cinf) );
  union ieee754_binary32 inspector = {.whole=(cinf - cinf)};
  fprintf( stdout, "inspector = {.whole=(const_inf - const_inf)}; inspector.sign = %u\n", inspector.sign );

  fprintf( stdout, "inf / inf = %f\n", (inf / inf) );
  inspector.whole = (inf / inf);
  fprintf( stdout, "inspector = {.whole=(inf / inf)}; inspector.sign = %u\n", inspector.sign );
  fprintf( stdout, "const_inf / const_inf = %f\n", (cinf / cinf) );

 inspector.whole = (cinf / cinf);
  fprintf( stdout, "inspector = {.whole=(const_inf / const_inf)}; inspector.sign = %u\n", inspector.sign );

  return 0;
}
```

The output from clang-built binaries:
```c
inf - inf = -nan
const_inf - const_inf = nan
inspector = {.whole=(const_inf - const_inf)}; inspector.sign = 0
inf / inf = -nan
inspector = {.whole=(inf / inf)}; inspector.sign = 1
const_inf / const_inf = nan
inspector = {.whole=(const_inf / const_inf)}; inspector.sign = 0
```

The output from a GCC-built binary
```c
inf - inf = -nan
const_inf - const_inf = -nan
inspector = {.whole=(const_inf - const_inf)}; inspector.sign = 1
inf / inf = -nan
inspector = {.whole=(inf / inf)}; inspector.sign = 1
const_inf / const_inf = -nan
inspector = {.whole=(const_inf / const_inf)}; inspector.sign = 1
```

Thank you so much!
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to