On Fri, May 1, 2020 at 11:22 AM James Almer <jamr...@gmail.com> wrote:

> On 5/1/2020 3:07 PM, Carl Eugen Hoyos wrote:
> > Am Fr., 1. Mai 2020 um 19:24 Uhr schrieb Dale Curtis <
> dalecur...@chromium.org>:
> >>
> >> Signed-off-by: Dale Curtis <dalecur...@chromium.org>
> >> ---
> >>  libavutil/common.h | 10 ++++++++++
> >>  1 file changed, 10 insertions(+)
> >
> > Imo, this is guaranteed to break x86 compilation with some unusual
> > toolchain.
> > I looked carefully at all occurrences of AV_GCC_VERSION_AT_LEAST()
> > and I believe they are in fact different (not for x86 or combined with
> other
> > checks).
>
> Can you elaborate on this? AV_GCC_VERSION_AT_LEAST() is a public macro
> used in public headers to choose one or another path depending on if the
> compiler is a recent enough GCC, or something else.
> What toolchain could this break, and why specifically x86?
> __builtin_*_overflow are arch agnostic GCC functions.
>
>
Yes this is accurate. Actual rebased patch.
https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
https://releases.llvm.org/4.0.0/tools/clang/docs/LanguageExtensions.html#checked-arithmetic-builtins


- dale
From 61af5bd4c9a1f937dd0df5e50127bdd2e41d6965 Mon Sep 17 00:00:00 2001
From: Dale Curtis <dalecurtis@chromium.org>
Date: Fri, 1 May 2020 10:20:43 -0700
Subject: [PATCH 2/2] Use gcc/clang builtins for av_sat_(add|sub)_64_c if
 available.

Signed-off-by: Dale Curtis <dalecurtis@chromium.org>
---
 libavutil/common.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/libavutil/common.h b/libavutil/common.h
index 11907e5ba7..c4f5eea409 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -299,11 +299,16 @@ static av_always_inline int av_sat_dsub32_c(int a, int b)
  * @return sum with signed saturation
  */
 static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {
+#if AV_GCC_VERSION_AT_LEAST(5,0) || defined(__clang__)
+  int64_t tmp;
+  return !__builtin_add_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
+#else
   if (b >= 0 && a >= INT64_MAX - b)
     return INT64_MAX;
   if (b <= 0 && a <= INT64_MIN - b)
     return INT64_MIN;
   return a + b;
+#endif
 }
 
 /**
@@ -314,11 +319,16 @@ static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {
  * @return difference with signed saturation
  */
 static av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {
+#if AV_GCC_VERSION_AT_LEAST(5,0) || defined(__clang__)
+  int64_t tmp;
+  return !__builtin_sub_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
+#else
   if (b <= 0 && a >= INT64_MAX + b) {
     return INT64_MAX;
   if (b >= 0 && a <= INT64_MIN + b) {
     return INT64_MIN;
   return a - b;
+#endif
 }
 
 /**
-- 
2.24.1.windows.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to