diff mbox series

[FFmpeg-devel,1/6] lavu/riscv: implement floating point clips

Message ID 20240725202522.276182-1-remi@remlab.net
State New
Headers show
Series [FFmpeg-devel,1/6] lavu/riscv: implement floating point clips | expand

Checks

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

Commit Message

Rémi Denis-Courmont July 25, 2024, 8:25 p.m. UTC
Unlike x86, fmin/fmax are single instructions, not function calls. They
are much much faster than doing a comparison, then branching based on its
results. With this, audiodsp.vector_clipf gets almost twice as fast, and
a properly unrollled version of it gets 4-5x faster, on SiFive-U74.
This is only the low-hanging fruit: FFMIN and FFMAX are presumably
affected as well.

This likely applies to other instruction sets with native IEEE floats,
especially those lacking a conditional select instruction.
---
 libavutil/riscv/intmath.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Rémi Denis-Courmont July 26, 2024, 6:23 a.m. UTC | #1
Le 25 juillet 2024 23:25:15 GMT+03:00, "Rémi Denis-Courmont" <remi@remlab.net> a écrit :
>Unlike x86, fmin/fmax are single instructions, not function calls. They
>are much much faster than doing a comparison, then branching based on its
>results. With this, audiodsp.vector_clipf gets almost twice as fast, and
>a properly unrollled version of it gets 4-5x faster, on SiFive-U74.
>This is only the low-hanging fruit: FFMIN and FFMAX are presumably
>affected as well.
>
>This likely applies to other instruction sets with native IEEE floats,
>especially those lacking a conditional select instruction.

In fact, the same problem occurs on Armv8, and it gets even worse on Armv7 where FFMIN and FFMAX incur calls to fcmp*(). I am not sure if this really works on anything but x86.

Only way to make FFMIN behave as well as math.h functions seems to be enabling -funsafe-math-optimizations -ffinite-math-only.


>---
> libavutil/riscv/intmath.h | 19 +++++++++++++++++++
> 1 file changed, 19 insertions(+)
>
>diff --git a/libavutil/riscv/intmath.h b/libavutil/riscv/intmath.h
>index 3e7ab864c5..24f165eef1 100644
>--- a/libavutil/riscv/intmath.h
>+++ b/libavutil/riscv/intmath.h
>@@ -22,6 +22,7 @@
> #define AVUTIL_RISCV_INTMATH_H
> 
> #include <stdint.h>
>+#include <math.h>
> 
> #include "config.h"
> #include "libavutil/attributes.h"
>@@ -72,6 +73,24 @@ static av_always_inline av_const int av_clip_intp2_rvi(int a, int p)
>     return b;
> }
> 
>+#if defined (__riscv_f) || defined (__riscv_zfinx)
>+#define av_clipf av_clipf_rvf
>+static av_always_inline av_const float av_clipf_rvf(float a, float min,
>+                                                    float max)
>+{
>+    return fminf(fmaxf(a, min), max);
>+}
>+#endif
>+
>+#if defined (__riscv_d) || defined (__riscv_zdinx)
>+#define av_clipd av_clipd_rvd
>+static av_always_inline av_const float av_clipd_rvd(double a, double min,
>+                                                    double max)
>+{
>+    return fmin(fmax(a, min), max);
>+}
>+#endif
>+
> #if defined (__GNUC__) || defined (__clang__)
> static inline av_const int ff_ctz_rv(int x)
> {
diff mbox series

Patch

diff --git a/libavutil/riscv/intmath.h b/libavutil/riscv/intmath.h
index 3e7ab864c5..24f165eef1 100644
--- a/libavutil/riscv/intmath.h
+++ b/libavutil/riscv/intmath.h
@@ -22,6 +22,7 @@ 
 #define AVUTIL_RISCV_INTMATH_H
 
 #include <stdint.h>
+#include <math.h>
 
 #include "config.h"
 #include "libavutil/attributes.h"
@@ -72,6 +73,24 @@  static av_always_inline av_const int av_clip_intp2_rvi(int a, int p)
     return b;
 }
 
+#if defined (__riscv_f) || defined (__riscv_zfinx)
+#define av_clipf av_clipf_rvf
+static av_always_inline av_const float av_clipf_rvf(float a, float min,
+                                                    float max)
+{
+    return fminf(fmaxf(a, min), max);
+}
+#endif
+
+#if defined (__riscv_d) || defined (__riscv_zdinx)
+#define av_clipd av_clipd_rvd
+static av_always_inline av_const float av_clipd_rvd(double a, double min,
+                                                    double max)
+{
+    return fmin(fmax(a, min), max);
+}
+#endif
+
 #if defined (__GNUC__) || defined (__clang__)
 static inline av_const int ff_ctz_rv(int x)
 {