Intel® oneAPI Data Parallel C++
Support for Intel® oneAPI DPC++ Compiler, Intel® oneAPI DPC++ Library, Intel ICX Compiler , Intel® DPC++ Compatibility Tool, and GDB*
561 Discussions

std::isfinite(INFINITY) returns true on Linux

AJIOB
Novice
2,047 Views

Hello

 

I'm using Intel oneAPI 2022.1.3 from APT on Ubuntu 20.04 LTS x64 English (Linux-based).

 

I got example code of std::isfinite() from cppreference, compile them with DPC++ 2022.0.0.20211123 and it works incorrectly on Linux: not as C++ standard wants.

 

Linux build & execution log was attached.

 

Linux output:

 

isfinite(NaN) = true
isfinite(Inf) = true
isfinite(0.0) = true
isfinite(exp(800)) = true
isfinite(DBL_MIN/2.0) = true

 

Windows version of DPC++ generates correct output:

 

isfinite(NaN) = false
isfinite(Inf) = false
isfinite(0.0) = true
isfinite(exp(800)) = false
isfinite(DBL_MIN/2.0) = true

 

Example code was attached as a test.cpp file in test.zip too.

 

Code was compiled with command dpcpp test.cpp -o test

 

How can I fix that behavior?

 

With regards,
Alex

Labels (1)
0 Kudos
8 Replies
NoorjahanSk_Intel
Moderator
1,940 Views

Hi,

 

Thanks for reaching out to us.

 

>>How can I fix that behavior?

 

Could you please try with below command as it worked (as expected) from our end.

 

dpcpp test.cpp -fp-model=precise -o test

 

Please find the below screenshot for more details.

NoorjahanSk_Intel_0-1648459214066.png

 

"-fp-model=precise" disables the optimization for floating-point calculations, so that the result won't be changed.

 

Please refer to below link for more details.

https://www.intel.com/content/www/us/en/develop/documentation/oneapi-dpcpp-cpp-compiler-dev-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-model-fp.html

 

 

 

Thanks & regards,

Noorjahan.

 

0 Kudos
AJIOB
Novice
1,920 Views

Hi Noorjahan,

 

Yes, you are right, that flag was helpful for fixing behavior on Linux.

 

But I need to have maximum speed for Linux & Windows with equal & correct behaviour.

 

What about fixing an optimized version?

 

With regards,
Alex

0 Kudos
VinInn
Beginner
1,812 Views

I advice to test this version [1] as the original has the risk that everything is computed at compile time (try in godbolt for instance)

with Ofast (aka finite-math) one DOES expect isfinite(NAN) to be true

 

with latest gcc I get

c++ -O3 isfinite.cpp; ./a.out
isfinite(NaN) = false
isfinite(Inf) = false
isfinite(0.0) = true
isfinite(exp(800)) = false
isfinite(DBL_MIN/2.0) = true
x = 0
isfinite(0.f/x) = false
isfinite(1.f/x) = false
isfinite(x) = true
isfinite(exp(x)) = true
isfinite(DBL_MIN/x) = false

 

c++ -Ofast isfinite.cpp
 ./a.out

isfinite(NaN) = false
isfinite(Inf) = false
isfinite(0.0) = true
isfinite(exp(800)) = true
isfinite(DBL_MIN/2.0) = true
x = 0
isfinite(0.f/x) = true
isfinite(1.f/x) = true
isfinite(x) = true
isfinite(exp(x)) = true
isfinite(DBL_MIN/x) = true

 

[1]

#include <iostream>
#include <cmath>
#include <cfloat>

int main(int argc, char**)
{


float x = 1.f -float(argc);


std::cout << std::boolalpha
<< "isfinite(NaN) = " << std::isfinite(NAN) << '\n'
<< "isfinite(Inf) = " << std::isfinite(INFINITY) << '\n'
<< "isfinite(0.0) = " << std::isfinite(0.0) << '\n'
<< "isfinite(exp(800)) = " << std::isfinite(std::exp(800)) << '\n'
<< "isfinite(DBL_MIN/2.0) = " << std::isfinite(DBL_MIN/2.0) << '\n';


std::cout << std::boolalpha
<< "x = " << x << '\n'
<< "isfinite(0.f/x) = " << std::isfinite(0.f/x) << '\n'
<< "isfinite(1.f/x) = " << std::isfinite(1.f/x) << '\n'
<< "isfinite(x) = " << std::isfinite(0.0) << '\n'
<< "isfinite(exp(x)) = " << std::isfinite(std::exp(x)) << '\n'
<< "isfinite(DBL_MIN/x) = " << std::isfinite(DBL_MIN/x) << '\n';

 

0 Kudos
NoorjahanSk_Intel
Moderator
1,861 Views

Hi,


We are working on your issue. We will get back to you soon.


Thanks & Regards,

Noorjahan


0 Kudos
DMITRY_T_Intel
Employee
1,852 Views

Hi Alex,

I escalated this issue to development. I will update it as soon as I get any information.

Thanks!


0 Kudos
DMITRY_T_Intel
Employee
1,796 Views

Hi Alex,

I got reply from developers:

"Use of dpcpp or icpx uses Intel defaults, which includes -fp-model=fast. This behavior is expected."

Thanks!


0 Kudos
lpsinger
Beginner
1,110 Views

The old compiler, icc, also had -fp-model=fast (or its equivalent) as the default, did it not? And yet isfinite does work on icc even in this mode.

I have a program that needs to have isfinite working. It runs about half as fast when I compile it with icx in precise mode as with icc in fast mode. I am eager to switch from icc to icx, but given this performance hit it is not an option for me right now.

Are there any prospects for fixing isfinite in icx in the fast mode?

0 Kudos
lpsinger
Beginner
1,104 Views

...My apologies. This behavior is consistent with the most recent versions of gcc and clang in fast math mode. The solution is to add the compiler option -fno-finite-math-only, which is supported by gcc, clang, and icx.

0 Kudos
Reply