emscripten-core / emscripten-core/emscripten
Missed optimization with NaN values and "-O3 -ffinite-math-only"
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
The following source code could always print `NaN` without `-ffinite-math-only` and `Not NaN` with it. Because `n != n` can only be true for NaN values. Assuming there are non it could be simply replaced with `false`. Sadly emcc doesn't do that and always prints `NaN`. GCC and clang do exactly that.
```cpp
#include
int main() {
volatile float n = 0.0 / 0.0;
if (n != n) {
std::printf("NaN\n");
return 1;
}
std::printf("Not NaN\n");
return 0;
}
```
command line options: `emcc -O3 source.cpp -o emcc.js`
[godbolt link](https://godbolt.org/z/YEno8dKhv)
`emcc -v`
> emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.27 (7e538a419c3649f3a540a57beab347aa8f6c6271)
> clang version 14.0.0 (/srcdest/llvm-project 78e87970af888bbbd5652c31f3a8454e8e9dd5b8)
> Target: wasm32-unknown-emscripten
> Thread model: posix
> InstalledDir: /opt/emscripten-llvm/bin
Contributor guide
Assessment
This issue has not been assessed yet.