[FFmpeg-devel] [PATCH 1/2] configure+libm.h: add hypot emulation

Ganesh Ajjanagadde gajjanagadde at gmail.com
Sat Nov 14 18:11:19 CET 2015


It is known that the naive sqrt(x*x + y*y) approach for computing the
hypotenuse suffers from overflow and accuracy issues, see e.g
http://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/.
This adds hypot support to FFmpeg, a C99 function.

On platforms without hypot, this patch for simplicity does the naive
direct computation outlined above. Improvements can be made separately.

Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>
---
 configure        | 2 ++
 libavutil/libm.h | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/configure b/configure
index d518b21..45df724 100755
--- a/configure
+++ b/configure
@@ -1774,6 +1774,7 @@ MATH_FUNCS="
     exp2
     exp2f
     expf
+    hypot
     isinf
     isnan
     ldexpf
@@ -5309,6 +5310,7 @@ disabled crystalhd || check_lib libcrystalhd/libcrystalhd_if.h DtsCrystalHDVersi
 
 atan2f_args=2
 copysign_args=2
+hypot_args=2
 ldexpf_args=2
 powf_args=2
 
diff --git a/libavutil/libm.h b/libavutil/libm.h
index 6c17b28..ab5df1b 100644
--- a/libavutil/libm.h
+++ b/libavutil/libm.h
@@ -82,6 +82,14 @@ static av_always_inline float cbrtf(float x)
 #define exp2f(x) ((float)exp2(x))
 #endif /* HAVE_EXP2F */
 
+#if !HAVE_HYPOT
+#undef hypot
+static av_always_inline av_const double hypot(double x, double y)
+{
+    return sqrt(x*x + y*y);
+}
+#endif /* HAVE_HYPOT */
+
 #if !HAVE_ISINF
 static av_always_inline av_const int isinf(float x)
 {
-- 
2.6.2



More information about the ffmpeg-devel mailing list