[FFmpeg-devel] [PATCH] Optimisations for av_log2 and integer clip functions

Mans Rullgard mans
Wed Jan 13 20:27:59 CET 2010


10% faster flac decoding on x86 and ARM.
---
 libavutil/arm/intmath.h |   54 +++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/common.h      |   14 ++++++++++++
 libavutil/intmath.h     |   48 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 0 deletions(-)
 create mode 100644 libavutil/arm/intmath.h
 create mode 100644 libavutil/intmath.h

diff --git a/libavutil/arm/intmath.h b/libavutil/arm/intmath.h
new file mode 100644
index 0000000..aa63a05
--- /dev/null
+++ b/libavutil/arm/intmath.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2010 Mans Rullgard <mans at mansr.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_ARM_INTMATH_H
+#define AVUTIL_ARM_INTMATH_H
+
+#include <stdint.h>
+
+#if HAVE_ARMV6 && HAVE_INLINE_ASM
+
+#define av_clip_uint8 av_clip_uint8
+static inline av_const uint8_t av_clip_uint8(int a)
+{
+    unsigned x;
+    __asm__ volatile ("usat %0, #8,  %1" : "=r"(x) : "r"(a));
+    return x;
+}
+
+#define av_clip_uint16 av_clip_uint16
+static inline av_const uint16_t av_clip_uint16(int a)
+{
+    unsigned x;
+    __asm__ volatile ("usat %0, #16, %1" : "=r"(x) : "r"(a));
+    return x;
+}
+
+#define av_clip_int16 av_clip_int16
+static inline av_const int16_t av_clip_int16(int a)
+{
+    int x;
+    __asm__ volatile ("ssat %0, #16, %1" : "=r"(x) : "r"(a));
+    return x;
+}
+
+#endif /* HAVE_ARMV6 && HAVE_INLINE_ASM */
+
+#endif /* AVUTIL_ARM_INTMATH_H */
diff --git a/libavutil/common.h b/libavutil/common.h
index a8a9bd3..3e07522 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -117,6 +117,10 @@
 #endif
 #endif
 
+#ifdef HAVE_AV_CONFIG_H
+#   include "intmath.h"
+#endif
+
 //rounded division & shift
 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
 /* assume b>0 */
@@ -138,6 +142,7 @@ extern const uint8_t ff_log2_tab[256];
 
 extern const uint8_t av_reverse[256];
 
+#ifndef av_log2
 static inline av_const int av_log2(unsigned int v)
 {
     int n = 0;
@@ -153,7 +158,9 @@ static inline av_const int av_log2(unsigned int v)
 
     return n;
 }
+#endif
 
+#ifndef av_log2_16bit
 static inline av_const int av_log2_16bit(unsigned int v)
 {
     int n = 0;
@@ -165,6 +172,7 @@ static inline av_const int av_log2_16bit(unsigned int v)
 
     return n;
 }
+#endif
 
 /**
  * Clips a signed integer value into the amin-amax range.
@@ -185,33 +193,39 @@ static inline av_const int av_clip(int a, int amin, int amax)
  * @param a value to clip
  * @return clipped value
  */
+#ifndef av_clip_uint8
 static inline av_const uint8_t av_clip_uint8(int a)
 {
     if (a&(~255)) return (-a)>>31;
     else          return a;
 }
+#endif
 
 /**
  * Clips a signed integer value into the 0-65535 range.
  * @param a value to clip
  * @return clipped value
  */
+#ifndef av_clip_uint16
 static inline av_const uint16_t av_clip_uint16(int a)
 {
     if (a&(~65535)) return (-a)>>31;
     else            return a;
 }
+#endif
 
 /**
  * Clips a signed integer value into the -32768,32767 range.
  * @param a value to clip
  * @return clipped value
  */
+#ifndef av_clip_int16
 static inline av_const int16_t av_clip_int16(int a)
 {
     if ((a+32768) & ~65535) return (a>>31) ^ 32767;
     else                    return a;
 }
+#endif
 
 /**
  * Clips a float value into the amin-amax range.
diff --git a/libavutil/intmath.h b/libavutil/intmath.h
new file mode 100644
index 0000000..be7de74
--- /dev/null
+++ b/libavutil/intmath.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2010 Mans Rullgard <mans at mansr.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_INTMATH_H
+#define AVUTIL_INTMATH_H
+
+#include "common.h"
+
+#if   ARCH_ARM
+#   include "arm/intmath.h"
+#endif
+
+#if AV_GCC_VERSION_AT_LEAST(3,4)
+
+#ifndef av_log2
+
+#define av_log2 av_log2
+static inline av_const int av_log2(unsigned int v)
+{
+    return v? 31 - __builtin_clz(v) : 0;
+}
+
+#ifndef av_log2_16bit
+#define av_log2_16bit av_log2
+#endif
+
+#endif /* av_log2 */
+
+#endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
+
+#endif /* AVUTIL_INTMATH_H */
-- 
1.6.6




More information about the ffmpeg-devel mailing list