diff mbox series

[FFmpeg-devel,v3,1/2] libavutil/common: clip nan value to amin

Message ID 20211115062221.1650-1-mindmark@gmail.com
State New
Headers show
Series [FFmpeg-devel,v3,1/2] libavutil/common: clip nan value to amin | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Mark Reid Nov. 15, 2021, 6:22 a.m. UTC
From: Mark Reid <mindmark@gmail.com>

Changes av_clipf to return amin if a is nan.
Before if a is nan av_clipf_c returned nan and
av_clipf_sse would return amax. Now the both
should behave the same.

This works because nan > amin is false.
The max(nan, amin) will be amin.
---
 libavutil/common.h      | 12 ++++++------
 libavutil/x86/intmath.h |  8 ++++----
 2 files changed, 10 insertions(+), 10 deletions(-)

--
2.31.1.windows.1

Comments

Anton Khirnov Nov. 16, 2021, 12:57 p.m. UTC | #1
Quoting mindmark@gmail.com (2021-11-15 07:22:20)
> From: Mark Reid <mindmark@gmail.com>
> 
> Changes av_clipf to return amin if a is nan.
> Before if a is nan av_clipf_c returned nan and
> av_clipf_sse would return amax. Now the both
> should behave the same.
> 
> This works because nan > amin is false.
> The max(nan, amin) will be amin.

This seems like it goes against the point of nan. I would expect any
float operation involving nan to return nan.
Paul B Mahol Nov. 16, 2021, 2:41 p.m. UTC | #2
On Tue, Nov 16, 2021 at 1:57 PM Anton Khirnov <anton@khirnov.net> wrote:

> Quoting mindmark@gmail.com (2021-11-15 07:22:20)
> > From: Mark Reid <mindmark@gmail.com>
> >
> > Changes av_clipf to return amin if a is nan.
> > Before if a is nan av_clipf_c returned nan and
> > av_clipf_sse would return amax. Now the both
> > should behave the same.
> >
> > This works because nan > amin is false.
> > The max(nan, amin) will be amin.
>
> This seems like it goes against the point of nan. I would expect any
> float operation involving nan to return nan.
>

fmaxf,fminf does not return nan, unless both values are nan.
So to me patch is fine.


>
> --
> Anton Khirnov
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
diff mbox series

Patch

diff --git a/libavutil/common.h b/libavutil/common.h
index 3cc1f07566..9338bda7d5 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -379,6 +379,8 @@  static av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {

 /**
  * Clip a float value into the amin-amax range.
+ * If a is nan or -inf amin will be returned.
+ * If a is +inf amax will be returned.
  * @param a value to clip
  * @param amin minimum value of the clip range
  * @param amax maximum value of the clip range
@@ -389,13 +391,13 @@  static av_always_inline av_const float av_clipf_c(float a, float amin, float ama
 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
     if (amin > amax) abort();
 #endif
-    if      (a < amin) return amin;
-    else if (a > amax) return amax;
-    else               return a;
+    return FFMIN(FFMAX(a, amin), amax);
 }

 /**
  * Clip a double value into the amin-amax range.
+ * If a is nan or -inf amin will be returned.
+ * If a is +inf amax will be returned.
  * @param a value to clip
  * @param amin minimum value of the clip range
  * @param amax maximum value of the clip range
@@ -406,9 +408,7 @@  static av_always_inline av_const double av_clipd_c(double a, double amin, double
 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
     if (amin > amax) abort();
 #endif
-    if      (a < amin) return amin;
-    else if (a > amax) return amax;
-    else               return a;
+    return FFMIN(FFMAX(a, amin), amax);
 }

 /** Compute ceil(log2(x)).
diff --git a/libavutil/x86/intmath.h b/libavutil/x86/intmath.h
index 40743fd13e..1520c25ec9 100644
--- a/libavutil/x86/intmath.h
+++ b/libavutil/x86/intmath.h
@@ -110,8 +110,8 @@  static av_always_inline av_const double av_clipd_sse2(double a, double amin, dou
 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
     if (amin > amax) abort();
 #endif
-    __asm__ ("minsd %2, %0 \n\t"
-             "maxsd %1, %0 \n\t"
+    __asm__ ("maxsd %1, %0 \n\t"
+             "minsd %2, %0 \n\t"
              : "+&x"(a) : "xm"(amin), "xm"(amax));
     return a;
 }
@@ -126,8 +126,8 @@  static av_always_inline av_const float av_clipf_sse(float a, float amin, float a
 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
     if (amin > amax) abort();
 #endif
-    __asm__ ("minss %2, %0 \n\t"
-             "maxss %1, %0 \n\t"
+    __asm__ ("maxss %1, %0 \n\t"
+             "minss %2, %0 \n\t"
              : "+&x"(a) : "xm"(amin), "xm"(amax));
     return a;
 }