[FFmpeg-devel,1/4] libavutil: Add saturating subtraction functions

Submitted by Andrew D'Addesio on Dec. 2, 2017, 5:36 p.m.

Details

Message ID 20171202173627.5292-1-modchipv12@gmail.com
State New
Headers show

Commit Message

Andrew D'Addesio Dec. 2, 2017, 5:36 p.m.
Add av_sat_sub32 and av_sat_dsub32 as the subtraction analogues to
av_sat_add32/av_sat_dadd32.

Also clarify the formulas for dadd32/dsub32.

Signed-off-by: Andrew D'Addesio <modchipv12@gmail.com>
---
 libavutil/arm/intmath.h | 16 ++++++++++++++++
 libavutil/common.h      | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)

Comments

Rostislav Pehlivanov Dec. 4, 2017, 7:36 a.m.
On 2 December 2017 at 17:36, Andrew D'Addesio <modchipv12@gmail.com> wrote:

> Add av_sat_sub32 and av_sat_dsub32 as the subtraction analogues to
> av_sat_add32/av_sat_dadd32.
>
> Also clarify the formulas for dadd32/dsub32.
>
> Signed-off-by: Andrew D'Addesio <modchipv12@gmail.com>
> ---
>  libavutil/arm/intmath.h | 16 ++++++++++++++++
>  libavutil/common.h      | 32 +++++++++++++++++++++++++++++++-
>  2 files changed, 47 insertions(+), 1 deletion(-)
>
> diff --git a/libavutil/arm/intmath.h b/libavutil/arm/intmath.h
> index 65e42c5..5311a7d 100644
> --- a/libavutil/arm/intmath.h
> +++ b/libavutil/arm/intmath.h
> @@ -94,6 +94,22 @@ static av_always_inline int av_sat_dadd32_arm(int a,
> int b)
>      return r;
>  }
>
> +#define av_sat_sub32 av_sat_sub32_arm
> +static av_always_inline int av_sat_sub32_arm(int a, int b)
> +{
> +    int r;
> +    __asm__ ("qsub %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
> +    return r;
> +}
> +
> +#define av_sat_dsub32 av_sat_dsub32_arm
> +static av_always_inline int av_sat_dsub32_arm(int a, int b)
> +{
> +    int r;
> +    __asm__ ("qdsub %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
> +    return r;
> +}
> +
>  #endif /* HAVE_ARMV6_INLINE */
>
>  #if HAVE_ASM_MOD_Q
> diff --git a/libavutil/common.h b/libavutil/common.h
> index 8142b31..5e03828 100644
> --- a/libavutil/common.h
> +++ b/libavutil/common.h
> @@ -260,13 +260,37 @@ static av_always_inline int av_sat_add32_c(int a,
> int b)
>   *
>   * @param  a first value
>   * @param  b value doubled and added to a
> - * @return sum with signed saturation
> + * @return sum sat(a + sat(2*b)) with signed saturation
>   */
>  static av_always_inline int av_sat_dadd32_c(int a, int b)
>  {
>      return av_sat_add32(a, av_sat_add32(b, b));
>  }
>
> +/**
> + * Subtract two signed 32-bit values with saturation.
> + *
> + * @param  a one value
> + * @param  b another value
> + * @return difference with signed saturation
> + */
> +static av_always_inline int av_sat_sub32_c(int a, int b)
> +{
> +    return av_clipl_int32((int64_t)a - b);
> +}
> +
> +/**
> + * Subtract a doubled value from another value with saturation at both
> stages.
> + *
> + * @param  a first value
> + * @param  b value doubled and subtracted from a
> + * @return difference sat(a - sat(2*b)) with signed saturation
> + */
> +static av_always_inline int av_sat_dsub32_c(int a, int b)
> +{
> +    return av_sat_sub32(a, av_sat_add32(b, b));
> +}
> +
>  /**
>   * Clip a float value into the amin-amax range.
>   * @param a value to clip
> @@ -513,6 +537,12 @@ static av_always_inline av_const int
> av_parity_c(uint32_t v)
>  #ifndef av_sat_dadd32
>  #   define av_sat_dadd32    av_sat_dadd32_c
>  #endif
> +#ifndef av_sat_sub32
> +#   define av_sat_sub32     av_sat_sub32_c
> +#endif
> +#ifndef av_sat_dsub32
> +#   define av_sat_dsub32    av_sat_dsub32_c
> +#endif
>  #ifndef av_clipf
>  #   define av_clipf         av_clipf_c
>  #endif
> --
> 2.15.1.windows.2
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

Pushed with a minor bump, these will be useful for some entropy coding
systems which rely on saturated adds.

Patch hide | download patch | download mbox

diff --git a/libavutil/arm/intmath.h b/libavutil/arm/intmath.h
index 65e42c5..5311a7d 100644
--- a/libavutil/arm/intmath.h
+++ b/libavutil/arm/intmath.h
@@ -94,6 +94,22 @@  static av_always_inline int av_sat_dadd32_arm(int a, int b)
     return r;
 }
 
+#define av_sat_sub32 av_sat_sub32_arm
+static av_always_inline int av_sat_sub32_arm(int a, int b)
+{
+    int r;
+    __asm__ ("qsub %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
+    return r;
+}
+
+#define av_sat_dsub32 av_sat_dsub32_arm
+static av_always_inline int av_sat_dsub32_arm(int a, int b)
+{
+    int r;
+    __asm__ ("qdsub %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
+    return r;
+}
+
 #endif /* HAVE_ARMV6_INLINE */
 
 #if HAVE_ASM_MOD_Q
diff --git a/libavutil/common.h b/libavutil/common.h
index 8142b31..5e03828 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -260,13 +260,37 @@  static av_always_inline int av_sat_add32_c(int a, int b)
  *
  * @param  a first value
  * @param  b value doubled and added to a
- * @return sum with signed saturation
+ * @return sum sat(a + sat(2*b)) with signed saturation
  */
 static av_always_inline int av_sat_dadd32_c(int a, int b)
 {
     return av_sat_add32(a, av_sat_add32(b, b));
 }
 
+/**
+ * Subtract two signed 32-bit values with saturation.
+ *
+ * @param  a one value
+ * @param  b another value
+ * @return difference with signed saturation
+ */
+static av_always_inline int av_sat_sub32_c(int a, int b)
+{
+    return av_clipl_int32((int64_t)a - b);
+}
+
+/**
+ * Subtract a doubled value from another value with saturation at both stages.
+ *
+ * @param  a first value
+ * @param  b value doubled and subtracted from a
+ * @return difference sat(a - sat(2*b)) with signed saturation
+ */
+static av_always_inline int av_sat_dsub32_c(int a, int b)
+{
+    return av_sat_sub32(a, av_sat_add32(b, b));
+}
+
 /**
  * Clip a float value into the amin-amax range.
  * @param a value to clip
@@ -513,6 +537,12 @@  static av_always_inline av_const int av_parity_c(uint32_t v)
 #ifndef av_sat_dadd32
 #   define av_sat_dadd32    av_sat_dadd32_c
 #endif
+#ifndef av_sat_sub32
+#   define av_sat_sub32     av_sat_sub32_c
+#endif
+#ifndef av_sat_dsub32
+#   define av_sat_dsub32    av_sat_dsub32_c
+#endif
 #ifndef av_clipf
 #   define av_clipf         av_clipf_c
 #endif