Skip to content

Disable finite-math-only option with ENABLE_FAST_MATH=1 case to handle NaN and Inf checks correctly#23881

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
asmorkalov:as/fast_math_nan
Jul 7, 2023
Merged

Disable finite-math-only option with ENABLE_FAST_MATH=1 case to handle NaN and Inf checks correctly#23881
asmorkalov merged 1 commit intoopencv:4.xfrom
asmorkalov:as/fast_math_nan

Conversation

@asmorkalov
Copy link
Copy Markdown
Contributor

Relates to opencv/opencv_contrib#3150

-fast-math option includes -ffinite-math-only option. Compiler expects that floats/doubles could not be NaN/Inf and does optimization accordingly. In particular __builtin_isnan and related built-in returns invalid result or optimized out by compiler.

Related discussion in GCC issue tracker: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949
Stackoverflow question: https://stackoverflow.com/questions/69463347/why-does-gcc-ffast-math-disable-the-correct-result-of-isnan-and-isinf

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to postpone this change to the next release.

  • -fast-math mode is disabled by default for reasons. Users should know that they do before enabling that.
  • low priority fix

|| defined(__PPC64__) \
)
) \
&& !defined(__FAST_MATH__)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPENCV_USE_FASTMATH_BUILTINS

It doesn't make sense to disable all builtins for fast-math (just slow down anything).

Related problem is related to cvIsNan() only (probably no problem at all with cvIsNaN() as there is still no test).

Also this check doesn't handle other cases like:

  #elif defined __PPC64__ && defined _ARCH_PWR9 && defined(scalar_test_data_class)
    #define CV_INLINE_ISNAN_DBL(value) return scalar_test_data_class(value, 0x40);
    #define CV_INLINE_ISNAN_FLT(value) CV_INLINE_ISNAN_DBL(value)
  #endif

At first we need to start from tests.

 CV_INLINE int cvIsNaN( double value )
 {
-#if defined CV_INLINE_ISNAN_DBL
+#if defined(CV_INLINE_ISNAN_DBL) && !defined(__FAST_MATH__)
     CV_INLINE_ISNAN_DBL(value);
 #else
     Cv64suf ieee754;

 CV_INLINE int cvIsNaN( float value )
 {
-#if defined CV_INLINE_ISNAN_FLT
+#if defined(CV_INLINE_ISNAN_FLT) && !defined(__FAST_MATH__)
     CV_INLINE_ISNAN_FLT(value);
 #else
     Cv32suf ieee754;

Some simple test (to avoid reversing of modified implementations):

TEST(Core_FastMath, cvIsNaN)
{
    float nf = std::numeric_limits<float>::quiet_NaN();
    EXPECT_TRUE(cvIsNaN(nf));
    float nd = std::numeric_limits<double>::quiet_NaN();
    EXPECT_TRUE(cvIsNaN(nd));
}

BTW, existed isinf() test fails too. Perhaps we need to add similar checks here too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway own integer-based check is really slow, we should try to use CPU instructions for that.

Looks like compiler optimize NAN checks to false constant in fast-math mode.

Some investigations including -fno-finite-math-only:

@asmorkalov asmorkalov modified the milestones: 4.8.0, 4.9.0 Jun 28, 2023
@asmorkalov asmorkalov changed the title Use own implementation of isNan and isInf with -fast-math option Disable finite-math-only option with ENABLE_FAST_MATH=1 case to handle NaN and Inf checks correctly Jul 5, 2023
@asmorkalov
Copy link
Copy Markdown
Contributor Author

@opencv-alalek Could you take a look on the last version?

@opencv-alalek
Copy link
Copy Markdown
Contributor

@asmorkalov Please cleanup unrelated commits.

Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@asmorkalov asmorkalov merged commit 601a159 into opencv:4.x Jul 7, 2023
This was referenced Jul 25, 2023
asmorkalov pushed a commit to opencv/opencv_contrib that referenced this pull request Sep 6, 2024
Fix(wechat_code): Modify isnan for compatibility with -ffast_math. #3490

fix #3150
Merge with opencv/opencv#23881

Reference: https://stackoverflow.com/questions/7263404/mingw32-stdisnan-with-ffast-math

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants