https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126049
Bug ID: 126049
Summary: RISC-V auto-vectorization misoptimizes fmax()/fmin()
regarding NaN
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: uwu at icenowy dot me
Target Milestone: ---
GCC auto-vectorization will produce bogus infinity value (either negative or
positive for fmax() or fmin() ) when running a loop of fmax() or fmin() over an
array with only NaNs.
A smallest reproducer is here:
```
/* maxx.c */
#include <math.h>
#include <stddef.h>
float maxx(float *arr, size_t sz)
{
float val = NAN;
size_t i;
for (i = 0; i < sz; i++)
val = fmaxf(val, arr[i]);
return val;
}
```
The source file calling it is here:
```
/* maxx-caller.c */
#include <stddef.h>
#include <stdio.h>
#include <math.h>
float maxx(float *arr, size_t sz);
int main()
{
float arr[2] = {NAN, NAN};
printf("%f\n", maxx(arr, 2));
}
```
When compiling these two files individually (with a -march value containing V
and `-O2 -ftree-vectorize`) and link them together (LTO should be disabled to
prevent compile-time knowledge of the constants passed), the running result
will be an unexpected `-inf` . Disabling auto vectorization will lead to the
expected result of `nan` .
Confirmed on GCC 16.1.0 and master ( c1c66ede4052c2697f3dd17b80f8b80ff3fc2368
).